blob: 6472c7f928dc1330a2d7e1d368a012eef78ae48b [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 Malinen36aedc902008-08-25 11:58:58 +0300277
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800278 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700279 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700280
Johannes Berg6c739412011-11-03 09:27:01 +0100281 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200282
283 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
284 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100286 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
287 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200288
289 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
290 .len = IEEE80211_MAX_SSID_LEN },
291 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
292 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200293 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300294 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300295 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300296 [NL80211_ATTR_STA_FLAGS2] = {
297 .len = sizeof(struct nl80211_sta_flag_update),
298 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300299 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300300 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
301 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200302 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
303 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
304 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200305 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100306 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100307 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
308 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100309 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
310 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200311 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200312 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
313 .len = IEEE80211_MAX_DATA_LEN },
314 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200315 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200316 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300317 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200318 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300319 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
320 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200321 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900322 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
323 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100324 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100325 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100326 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200327 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700328 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300329 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200330 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200331 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300332 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300333 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
334 .len = IEEE80211_MAX_DATA_LEN },
335 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
336 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530337 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300338 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530339 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300340 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
341 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
342 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
343 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
344 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100345 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200346 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
347 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700348 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800349 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
351 .len = NL80211_HT_CAPABILITY_LEN
352 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100353 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530354 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530355 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200356 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Johannes Berg55682962007-09-20 13:09:35 -0400357};
358
Johannes Berge31b8212010-10-05 19:39:30 +0200359/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000360static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200361 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200362 [NL80211_KEY_IDX] = { .type = NLA_U8 },
363 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200364 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200365 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
366 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200367 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100368 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
369};
370
371/* policy for the key default flags */
372static const struct nla_policy
373nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
374 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
375 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200376};
377
Johannes Bergff1b6e62011-05-04 15:37:28 +0200378/* policy for WoWLAN attributes */
379static const struct nla_policy
380nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
381 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
382 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
383 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
384 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200385 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
386 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
387 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
388 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200389};
390
Johannes Berge5497d72011-07-05 16:35:40 +0200391/* policy for GTK rekey offload attributes */
392static const struct nla_policy
393nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
394 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
395 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
396 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
397};
398
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300399static const struct nla_policy
400nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200401 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300402 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700403 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300404};
405
Holger Schuriga0438972009-11-11 11:30:02 +0100406/* ifidx get helper */
407static int nl80211_get_ifidx(struct netlink_callback *cb)
408{
409 int res;
410
411 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
412 nl80211_fam.attrbuf, nl80211_fam.maxattr,
413 nl80211_policy);
414 if (res)
415 return res;
416
417 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
418 return -EINVAL;
419
420 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
421 if (!res)
422 return -EINVAL;
423 return res;
424}
425
Johannes Berg67748892010-10-04 21:14:06 +0200426static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
427 struct netlink_callback *cb,
428 struct cfg80211_registered_device **rdev,
429 struct net_device **dev)
430{
431 int ifidx = cb->args[0];
432 int err;
433
434 if (!ifidx)
435 ifidx = nl80211_get_ifidx(cb);
436 if (ifidx < 0)
437 return ifidx;
438
439 cb->args[0] = ifidx;
440
441 rtnl_lock();
442
443 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
444 if (!*dev) {
445 err = -ENODEV;
446 goto out_rtnl;
447 }
448
449 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100450 if (IS_ERR(*rdev)) {
451 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200452 goto out_rtnl;
453 }
454
455 return 0;
456 out_rtnl:
457 rtnl_unlock();
458 return err;
459}
460
461static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
462{
463 cfg80211_unlock_rdev(rdev);
464 rtnl_unlock();
465}
466
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100467/* IE validation */
468static bool is_valid_ie_attr(const struct nlattr *attr)
469{
470 const u8 *pos;
471 int len;
472
473 if (!attr)
474 return true;
475
476 pos = nla_data(attr);
477 len = nla_len(attr);
478
479 while (len) {
480 u8 elemlen;
481
482 if (len < 2)
483 return false;
484 len -= 2;
485
486 elemlen = pos[1];
487 if (elemlen > len)
488 return false;
489
490 len -= elemlen;
491 pos += 2 + elemlen;
492 }
493
494 return true;
495}
496
Johannes Berg55682962007-09-20 13:09:35 -0400497/* message building helper */
498static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
499 int flags, u8 cmd)
500{
501 /* since there is no private header just add the generic one */
502 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
503}
504
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400505static int nl80211_msg_put_channel(struct sk_buff *msg,
506 struct ieee80211_channel *chan)
507{
David S. Miller9360ffd2012-03-29 04:41:26 -0400508 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
509 chan->center_freq))
510 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400511
David S. Miller9360ffd2012-03-29 04:41:26 -0400512 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
513 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
514 goto nla_put_failure;
515 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
516 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
517 goto nla_put_failure;
518 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
519 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
520 goto nla_put_failure;
521 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
522 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
523 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400524
David S. Miller9360ffd2012-03-29 04:41:26 -0400525 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
526 DBM_TO_MBM(chan->max_power)))
527 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400528
529 return 0;
530
531 nla_put_failure:
532 return -ENOBUFS;
533}
534
Johannes Berg55682962007-09-20 13:09:35 -0400535/* netlink command implementations */
536
Johannes Bergb9454e82009-07-08 13:29:08 +0200537struct key_parse {
538 struct key_params p;
539 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200540 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200541 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100542 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200543};
544
545static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
546{
547 struct nlattr *tb[NL80211_KEY_MAX + 1];
548 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
549 nl80211_key_policy);
550 if (err)
551 return err;
552
553 k->def = !!tb[NL80211_KEY_DEFAULT];
554 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
555
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100556 if (k->def) {
557 k->def_uni = true;
558 k->def_multi = true;
559 }
560 if (k->defmgmt)
561 k->def_multi = true;
562
Johannes Bergb9454e82009-07-08 13:29:08 +0200563 if (tb[NL80211_KEY_IDX])
564 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
565
566 if (tb[NL80211_KEY_DATA]) {
567 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
568 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
569 }
570
571 if (tb[NL80211_KEY_SEQ]) {
572 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
573 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
574 }
575
576 if (tb[NL80211_KEY_CIPHER])
577 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
578
Johannes Berge31b8212010-10-05 19:39:30 +0200579 if (tb[NL80211_KEY_TYPE]) {
580 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
581 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
582 return -EINVAL;
583 }
584
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100585 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
586 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100587 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
588 tb[NL80211_KEY_DEFAULT_TYPES],
589 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100590 if (err)
591 return err;
592
593 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
594 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
595 }
596
Johannes Bergb9454e82009-07-08 13:29:08 +0200597 return 0;
598}
599
600static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
601{
602 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
603 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
604 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
605 }
606
607 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
608 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
609 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
610 }
611
612 if (info->attrs[NL80211_ATTR_KEY_IDX])
613 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
614
615 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
616 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
617
618 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
619 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
620
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100621 if (k->def) {
622 k->def_uni = true;
623 k->def_multi = true;
624 }
625 if (k->defmgmt)
626 k->def_multi = true;
627
Johannes Berge31b8212010-10-05 19:39:30 +0200628 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
629 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
630 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
631 return -EINVAL;
632 }
633
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100634 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
635 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
636 int err = nla_parse_nested(
637 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
638 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
639 nl80211_key_default_policy);
640 if (err)
641 return err;
642
643 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
644 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
645 }
646
Johannes Bergb9454e82009-07-08 13:29:08 +0200647 return 0;
648}
649
650static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
651{
652 int err;
653
654 memset(k, 0, sizeof(*k));
655 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200656 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200657
658 if (info->attrs[NL80211_ATTR_KEY])
659 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
660 else
661 err = nl80211_parse_key_old(info, k);
662
663 if (err)
664 return err;
665
666 if (k->def && k->defmgmt)
667 return -EINVAL;
668
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100669 if (k->defmgmt) {
670 if (k->def_uni || !k->def_multi)
671 return -EINVAL;
672 }
673
Johannes Bergb9454e82009-07-08 13:29:08 +0200674 if (k->idx != -1) {
675 if (k->defmgmt) {
676 if (k->idx < 4 || k->idx > 5)
677 return -EINVAL;
678 } else if (k->def) {
679 if (k->idx < 0 || k->idx > 3)
680 return -EINVAL;
681 } else {
682 if (k->idx < 0 || k->idx > 5)
683 return -EINVAL;
684 }
685 }
686
687 return 0;
688}
689
Johannes Bergfffd0932009-07-08 14:22:54 +0200690static struct cfg80211_cached_keys *
691nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
692 struct nlattr *keys)
693{
694 struct key_parse parse;
695 struct nlattr *key;
696 struct cfg80211_cached_keys *result;
697 int rem, err, def = 0;
698
699 result = kzalloc(sizeof(*result), GFP_KERNEL);
700 if (!result)
701 return ERR_PTR(-ENOMEM);
702
703 result->def = -1;
704 result->defmgmt = -1;
705
706 nla_for_each_nested(key, keys, rem) {
707 memset(&parse, 0, sizeof(parse));
708 parse.idx = -1;
709
710 err = nl80211_parse_key_new(key, &parse);
711 if (err)
712 goto error;
713 err = -EINVAL;
714 if (!parse.p.key)
715 goto error;
716 if (parse.idx < 0 || parse.idx > 4)
717 goto error;
718 if (parse.def) {
719 if (def)
720 goto error;
721 def = 1;
722 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100723 if (!parse.def_uni || !parse.def_multi)
724 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200725 } else if (parse.defmgmt)
726 goto error;
727 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200728 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200729 if (err)
730 goto error;
731 result->params[parse.idx].cipher = parse.p.cipher;
732 result->params[parse.idx].key_len = parse.p.key_len;
733 result->params[parse.idx].key = result->data[parse.idx];
734 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
735 }
736
737 return result;
738 error:
739 kfree(result);
740 return ERR_PTR(err);
741}
742
743static int nl80211_key_allowed(struct wireless_dev *wdev)
744{
745 ASSERT_WDEV_LOCK(wdev);
746
Johannes Bergfffd0932009-07-08 14:22:54 +0200747 switch (wdev->iftype) {
748 case NL80211_IFTYPE_AP:
749 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200750 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700751 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200752 break;
753 case NL80211_IFTYPE_ADHOC:
754 if (!wdev->current_bss)
755 return -ENOLINK;
756 break;
757 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200758 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200759 if (wdev->sme_state != CFG80211_SME_CONNECTED)
760 return -ENOLINK;
761 break;
762 default:
763 return -EINVAL;
764 }
765
766 return 0;
767}
768
Johannes Berg7527a782011-05-13 10:58:57 +0200769static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
770{
771 struct nlattr *nl_modes = nla_nest_start(msg, attr);
772 int i;
773
774 if (!nl_modes)
775 goto nla_put_failure;
776
777 i = 0;
778 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400779 if ((ifmodes & 1) && nla_put_flag(msg, i))
780 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200781 ifmodes >>= 1;
782 i++;
783 }
784
785 nla_nest_end(msg, nl_modes);
786 return 0;
787
788nla_put_failure:
789 return -ENOBUFS;
790}
791
792static int nl80211_put_iface_combinations(struct wiphy *wiphy,
793 struct sk_buff *msg)
794{
795 struct nlattr *nl_combis;
796 int i, j;
797
798 nl_combis = nla_nest_start(msg,
799 NL80211_ATTR_INTERFACE_COMBINATIONS);
800 if (!nl_combis)
801 goto nla_put_failure;
802
803 for (i = 0; i < wiphy->n_iface_combinations; i++) {
804 const struct ieee80211_iface_combination *c;
805 struct nlattr *nl_combi, *nl_limits;
806
807 c = &wiphy->iface_combinations[i];
808
809 nl_combi = nla_nest_start(msg, i + 1);
810 if (!nl_combi)
811 goto nla_put_failure;
812
813 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
814 if (!nl_limits)
815 goto nla_put_failure;
816
817 for (j = 0; j < c->n_limits; j++) {
818 struct nlattr *nl_limit;
819
820 nl_limit = nla_nest_start(msg, j + 1);
821 if (!nl_limit)
822 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400823 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
824 c->limits[j].max))
825 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200826 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
827 c->limits[j].types))
828 goto nla_put_failure;
829 nla_nest_end(msg, nl_limit);
830 }
831
832 nla_nest_end(msg, nl_limits);
833
David S. Miller9360ffd2012-03-29 04:41:26 -0400834 if (c->beacon_int_infra_match &&
835 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
836 goto nla_put_failure;
837 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
838 c->num_different_channels) ||
839 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
840 c->max_interfaces))
841 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200842
843 nla_nest_end(msg, nl_combi);
844 }
845
846 nla_nest_end(msg, nl_combis);
847
848 return 0;
849nla_put_failure:
850 return -ENOBUFS;
851}
852
Johannes Berg55682962007-09-20 13:09:35 -0400853static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
854 struct cfg80211_registered_device *dev)
855{
856 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100857 struct nlattr *nl_bands, *nl_band;
858 struct nlattr *nl_freqs, *nl_freq;
859 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100860 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100861 enum ieee80211_band band;
862 struct ieee80211_channel *chan;
863 struct ieee80211_rate *rate;
864 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200865 const struct ieee80211_txrx_stypes *mgmt_stypes =
866 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400867
868 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
869 if (!hdr)
870 return -1;
871
David S. Miller9360ffd2012-03-29 04:41:26 -0400872 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
873 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
874 nla_put_u32(msg, NL80211_ATTR_GENERATION,
875 cfg80211_rdev_list_generation) ||
876 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
877 dev->wiphy.retry_short) ||
878 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
879 dev->wiphy.retry_long) ||
880 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
881 dev->wiphy.frag_threshold) ||
882 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
883 dev->wiphy.rts_threshold) ||
884 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
885 dev->wiphy.coverage_class) ||
886 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
887 dev->wiphy.max_scan_ssids) ||
888 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
889 dev->wiphy.max_sched_scan_ssids) ||
890 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
891 dev->wiphy.max_scan_ie_len) ||
892 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
893 dev->wiphy.max_sched_scan_ie_len) ||
894 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
895 dev->wiphy.max_match_sets))
896 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200897
David S. Miller9360ffd2012-03-29 04:41:26 -0400898 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
899 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
900 goto nla_put_failure;
901 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
902 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
903 goto nla_put_failure;
904 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
905 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
906 goto nla_put_failure;
907 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
908 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
909 goto nla_put_failure;
910 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
911 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
912 goto nla_put_failure;
913 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
914 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
915 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200916
David S. Miller9360ffd2012-03-29 04:41:26 -0400917 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
918 sizeof(u32) * dev->wiphy.n_cipher_suites,
919 dev->wiphy.cipher_suites))
920 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100921
David S. Miller9360ffd2012-03-29 04:41:26 -0400922 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
923 dev->wiphy.max_num_pmkids))
924 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530925
David S. Miller9360ffd2012-03-29 04:41:26 -0400926 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
927 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
928 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200929
David S. Miller9360ffd2012-03-29 04:41:26 -0400930 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
931 dev->wiphy.available_antennas_tx) ||
932 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
933 dev->wiphy.available_antennas_rx))
934 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100935
David S. Miller9360ffd2012-03-29 04:41:26 -0400936 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
937 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
938 dev->wiphy.probe_resp_offload))
939 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200940
Bruno Randolf7f531e02010-12-16 11:30:22 +0900941 if ((dev->wiphy.available_antennas_tx ||
942 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900943 u32 tx_ant = 0, rx_ant = 0;
944 int res;
945 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
946 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400947 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
948 tx_ant) ||
949 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
950 rx_ant))
951 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900952 }
953 }
954
Johannes Berg7527a782011-05-13 10:58:57 +0200955 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
956 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700957 goto nla_put_failure;
958
Johannes Bergee688b002008-01-24 19:38:39 +0100959 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
960 if (!nl_bands)
961 goto nla_put_failure;
962
963 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
964 if (!dev->wiphy.bands[band])
965 continue;
966
967 nl_band = nla_nest_start(msg, band);
968 if (!nl_band)
969 goto nla_put_failure;
970
Johannes Bergd51626d2008-10-09 12:20:13 +0200971 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400972 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
973 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
974 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
975 &dev->wiphy.bands[band]->ht_cap.mcs) ||
976 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
977 dev->wiphy.bands[band]->ht_cap.cap) ||
978 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
979 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
980 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
981 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
982 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200983
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000984 /* add VHT info */
985 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
986 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
987 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
988 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
989 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
990 dev->wiphy.bands[band]->vht_cap.cap)))
991 goto nla_put_failure;
992
Johannes Bergee688b002008-01-24 19:38:39 +0100993 /* add frequencies */
994 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
995 if (!nl_freqs)
996 goto nla_put_failure;
997
998 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
999 nl_freq = nla_nest_start(msg, i);
1000 if (!nl_freq)
1001 goto nla_put_failure;
1002
1003 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001004
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001005 if (nl80211_msg_put_channel(msg, chan))
1006 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001007
Johannes Bergee688b002008-01-24 19:38:39 +01001008 nla_nest_end(msg, nl_freq);
1009 }
1010
1011 nla_nest_end(msg, nl_freqs);
1012
1013 /* add bitrates */
1014 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1015 if (!nl_rates)
1016 goto nla_put_failure;
1017
1018 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1019 nl_rate = nla_nest_start(msg, i);
1020 if (!nl_rate)
1021 goto nla_put_failure;
1022
1023 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001024 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1025 rate->bitrate))
1026 goto nla_put_failure;
1027 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1028 nla_put_flag(msg,
1029 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1030 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001031
1032 nla_nest_end(msg, nl_rate);
1033 }
1034
1035 nla_nest_end(msg, nl_rates);
1036
1037 nla_nest_end(msg, nl_band);
1038 }
1039 nla_nest_end(msg, nl_bands);
1040
Johannes Berg8fdc6212009-03-14 09:34:01 +01001041 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1042 if (!nl_cmds)
1043 goto nla_put_failure;
1044
1045 i = 0;
1046#define CMD(op, n) \
1047 do { \
1048 if (dev->ops->op) { \
1049 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001050 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1051 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001052 } \
1053 } while (0)
1054
1055 CMD(add_virtual_intf, NEW_INTERFACE);
1056 CMD(change_virtual_intf, SET_INTERFACE);
1057 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001058 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001059 CMD(add_station, NEW_STATION);
1060 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001061 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001062 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001063 CMD(auth, AUTHENTICATE);
1064 CMD(assoc, ASSOCIATE);
1065 CMD(deauth, DEAUTHENTICATE);
1066 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001067 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001068 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001069 CMD(set_pmksa, SET_PMKSA);
1070 CMD(del_pmksa, DEL_PMKSA);
1071 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001072 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1073 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001074 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001075 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001076 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001077 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001078 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001079 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1080 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001081 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001082 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001083 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001084 i++;
1085 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1086 goto nla_put_failure;
1087 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001088 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001089 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1090 CMD(tdls_mgmt, TDLS_MGMT);
1091 CMD(tdls_oper, TDLS_OPER);
1092 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001093 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1094 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001095 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001096 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e7602302011-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 Berg5e7602302011-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 Malinen36aedc902008-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 Malinen36aedc902008-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];
Johannes Bergfd014282012-06-18 19:17:03 +02004133 struct wireless_dev *wdev = 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 Bergfd014282012-06-18 19:17:03 +02004293 request->wdev = wdev;
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;
Johannes Bergfd014282012-06-18 19:17:03 +02004297 err = rdev->ops->scan(&rdev->wiphy, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004298
Johannes Berg463d0182009-07-14 00:33:35 +02004299 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004300 nl80211_send_scan_start(rdev, wdev);
4301 if (wdev->netdev)
4302 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004303 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004304 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004305 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004306 kfree(request);
4307 }
Johannes Berg3b858752009-03-12 09:55:09 +01004308
Johannes Berg2a519312009-02-10 21:25:55 +01004309 return err;
4310}
4311
Luciano Coelho807f8a82011-05-11 17:09:35 +03004312static int nl80211_start_sched_scan(struct sk_buff *skb,
4313 struct genl_info *info)
4314{
4315 struct cfg80211_sched_scan_request *request;
4316 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4317 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004318 struct nlattr *attr;
4319 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004320 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004321 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004322 enum ieee80211_band band;
4323 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004324 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004325
4326 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4327 !rdev->ops->sched_scan_start)
4328 return -EOPNOTSUPP;
4329
4330 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4331 return -EINVAL;
4332
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004333 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4334 return -EINVAL;
4335
4336 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4337 if (interval == 0)
4338 return -EINVAL;
4339
Luciano Coelho807f8a82011-05-11 17:09:35 +03004340 wiphy = &rdev->wiphy;
4341
4342 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4343 n_channels = validate_scan_freqs(
4344 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4345 if (!n_channels)
4346 return -EINVAL;
4347 } else {
4348 n_channels = 0;
4349
4350 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4351 if (wiphy->bands[band])
4352 n_channels += wiphy->bands[band]->n_channels;
4353 }
4354
4355 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4356 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4357 tmp)
4358 n_ssids++;
4359
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004360 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004361 return -EINVAL;
4362
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004363 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4364 nla_for_each_nested(attr,
4365 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4366 tmp)
4367 n_match_sets++;
4368
4369 if (n_match_sets > wiphy->max_match_sets)
4370 return -EINVAL;
4371
Luciano Coelho807f8a82011-05-11 17:09:35 +03004372 if (info->attrs[NL80211_ATTR_IE])
4373 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4374 else
4375 ie_len = 0;
4376
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004377 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004378 return -EINVAL;
4379
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004380 mutex_lock(&rdev->sched_scan_mtx);
4381
4382 if (rdev->sched_scan_req) {
4383 err = -EINPROGRESS;
4384 goto out;
4385 }
4386
Luciano Coelho807f8a82011-05-11 17:09:35 +03004387 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004388 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004389 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004390 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004391 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004392 if (!request) {
4393 err = -ENOMEM;
4394 goto out;
4395 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004396
4397 if (n_ssids)
4398 request->ssids = (void *)&request->channels[n_channels];
4399 request->n_ssids = n_ssids;
4400 if (ie_len) {
4401 if (request->ssids)
4402 request->ie = (void *)(request->ssids + n_ssids);
4403 else
4404 request->ie = (void *)(request->channels + n_channels);
4405 }
4406
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004407 if (n_match_sets) {
4408 if (request->ie)
4409 request->match_sets = (void *)(request->ie + ie_len);
4410 else if (request->ssids)
4411 request->match_sets =
4412 (void *)(request->ssids + n_ssids);
4413 else
4414 request->match_sets =
4415 (void *)(request->channels + n_channels);
4416 }
4417 request->n_match_sets = n_match_sets;
4418
Luciano Coelho807f8a82011-05-11 17:09:35 +03004419 i = 0;
4420 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4421 /* user specified, bail out if channel not found */
4422 nla_for_each_nested(attr,
4423 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4424 tmp) {
4425 struct ieee80211_channel *chan;
4426
4427 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4428
4429 if (!chan) {
4430 err = -EINVAL;
4431 goto out_free;
4432 }
4433
4434 /* ignore disabled channels */
4435 if (chan->flags & IEEE80211_CHAN_DISABLED)
4436 continue;
4437
4438 request->channels[i] = chan;
4439 i++;
4440 }
4441 } else {
4442 /* all channels */
4443 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4444 int j;
4445 if (!wiphy->bands[band])
4446 continue;
4447 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4448 struct ieee80211_channel *chan;
4449
4450 chan = &wiphy->bands[band]->channels[j];
4451
4452 if (chan->flags & IEEE80211_CHAN_DISABLED)
4453 continue;
4454
4455 request->channels[i] = chan;
4456 i++;
4457 }
4458 }
4459 }
4460
4461 if (!i) {
4462 err = -EINVAL;
4463 goto out_free;
4464 }
4465
4466 request->n_channels = i;
4467
4468 i = 0;
4469 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4470 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4471 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004472 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004473 err = -EINVAL;
4474 goto out_free;
4475 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004476 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004477 memcpy(request->ssids[i].ssid, nla_data(attr),
4478 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004479 i++;
4480 }
4481 }
4482
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004483 i = 0;
4484 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4485 nla_for_each_nested(attr,
4486 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4487 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004488 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004489
4490 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4491 nla_data(attr), nla_len(attr),
4492 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004493 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004494 if (ssid) {
4495 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4496 err = -EINVAL;
4497 goto out_free;
4498 }
4499 memcpy(request->match_sets[i].ssid.ssid,
4500 nla_data(ssid), nla_len(ssid));
4501 request->match_sets[i].ssid.ssid_len =
4502 nla_len(ssid);
4503 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004504 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4505 if (rssi)
4506 request->rssi_thold = nla_get_u32(rssi);
4507 else
4508 request->rssi_thold =
4509 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004510 i++;
4511 }
4512 }
4513
Luciano Coelho807f8a82011-05-11 17:09:35 +03004514 if (info->attrs[NL80211_ATTR_IE]) {
4515 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4516 memcpy((void *)request->ie,
4517 nla_data(info->attrs[NL80211_ATTR_IE]),
4518 request->ie_len);
4519 }
4520
4521 request->dev = dev;
4522 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004523 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004524
4525 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4526 if (!err) {
4527 rdev->sched_scan_req = request;
4528 nl80211_send_sched_scan(rdev, dev,
4529 NL80211_CMD_START_SCHED_SCAN);
4530 goto out;
4531 }
4532
4533out_free:
4534 kfree(request);
4535out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004536 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004537 return err;
4538}
4539
4540static int nl80211_stop_sched_scan(struct sk_buff *skb,
4541 struct genl_info *info)
4542{
4543 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004544 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004545
4546 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4547 !rdev->ops->sched_scan_stop)
4548 return -EOPNOTSUPP;
4549
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004550 mutex_lock(&rdev->sched_scan_mtx);
4551 err = __cfg80211_stop_sched_scan(rdev, false);
4552 mutex_unlock(&rdev->sched_scan_mtx);
4553
4554 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004555}
4556
Johannes Berg9720bb32011-06-21 09:45:33 +02004557static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4558 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004559 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004560 struct wireless_dev *wdev,
4561 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004562{
Johannes Berg48ab9052009-07-10 18:42:31 +02004563 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004564 void *hdr;
4565 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004566
4567 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004568
Johannes Berg9720bb32011-06-21 09:45:33 +02004569 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004570 NL80211_CMD_NEW_SCAN_RESULTS);
4571 if (!hdr)
4572 return -1;
4573
Johannes Berg9720bb32011-06-21 09:45:33 +02004574 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4575
David S. Miller9360ffd2012-03-29 04:41:26 -04004576 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4577 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4578 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004579
4580 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4581 if (!bss)
4582 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004583 if ((!is_zero_ether_addr(res->bssid) &&
4584 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4585 (res->information_elements && res->len_information_elements &&
4586 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4587 res->len_information_elements,
4588 res->information_elements)) ||
4589 (res->beacon_ies && res->len_beacon_ies &&
4590 res->beacon_ies != res->information_elements &&
4591 nla_put(msg, NL80211_BSS_BEACON_IES,
4592 res->len_beacon_ies, res->beacon_ies)))
4593 goto nla_put_failure;
4594 if (res->tsf &&
4595 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4596 goto nla_put_failure;
4597 if (res->beacon_interval &&
4598 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4599 goto nla_put_failure;
4600 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4601 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4602 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4603 jiffies_to_msecs(jiffies - intbss->ts)))
4604 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004605
Johannes Berg77965c92009-02-18 18:45:06 +01004606 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004607 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004608 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4609 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004610 break;
4611 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004612 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4613 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004614 break;
4615 default:
4616 break;
4617 }
4618
Johannes Berg48ab9052009-07-10 18:42:31 +02004619 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004620 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004621 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004622 if (intbss == wdev->current_bss &&
4623 nla_put_u32(msg, NL80211_BSS_STATUS,
4624 NL80211_BSS_STATUS_ASSOCIATED))
4625 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004626 break;
4627 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004628 if (intbss == wdev->current_bss &&
4629 nla_put_u32(msg, NL80211_BSS_STATUS,
4630 NL80211_BSS_STATUS_IBSS_JOINED))
4631 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004632 break;
4633 default:
4634 break;
4635 }
4636
Johannes Berg2a519312009-02-10 21:25:55 +01004637 nla_nest_end(msg, bss);
4638
4639 return genlmsg_end(msg, hdr);
4640
4641 nla_put_failure:
4642 genlmsg_cancel(msg, hdr);
4643 return -EMSGSIZE;
4644}
4645
4646static int nl80211_dump_scan(struct sk_buff *skb,
4647 struct netlink_callback *cb)
4648{
Johannes Berg48ab9052009-07-10 18:42:31 +02004649 struct cfg80211_registered_device *rdev;
4650 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004651 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004652 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004653 int start = cb->args[1], idx = 0;
4654 int err;
4655
Johannes Berg67748892010-10-04 21:14:06 +02004656 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4657 if (err)
4658 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004659
Johannes Berg48ab9052009-07-10 18:42:31 +02004660 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004661
Johannes Berg48ab9052009-07-10 18:42:31 +02004662 wdev_lock(wdev);
4663 spin_lock_bh(&rdev->bss_lock);
4664 cfg80211_bss_expire(rdev);
4665
Johannes Berg9720bb32011-06-21 09:45:33 +02004666 cb->seq = rdev->bss_generation;
4667
Johannes Berg48ab9052009-07-10 18:42:31 +02004668 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004669 if (++idx <= start)
4670 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004671 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004672 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004673 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004674 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004675 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004676 }
4677 }
4678
Johannes Berg48ab9052009-07-10 18:42:31 +02004679 spin_unlock_bh(&rdev->bss_lock);
4680 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004681
4682 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004683 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004684
Johannes Berg67748892010-10-04 21:14:06 +02004685 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004686}
4687
Holger Schurig61fa7132009-11-11 12:25:40 +01004688static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4689 int flags, struct net_device *dev,
4690 struct survey_info *survey)
4691{
4692 void *hdr;
4693 struct nlattr *infoattr;
4694
Holger Schurig61fa7132009-11-11 12:25:40 +01004695 hdr = nl80211hdr_put(msg, pid, seq, flags,
4696 NL80211_CMD_NEW_SURVEY_RESULTS);
4697 if (!hdr)
4698 return -ENOMEM;
4699
David S. Miller9360ffd2012-03-29 04:41:26 -04004700 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4701 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004702
4703 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4704 if (!infoattr)
4705 goto nla_put_failure;
4706
David S. Miller9360ffd2012-03-29 04:41:26 -04004707 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4708 survey->channel->center_freq))
4709 goto nla_put_failure;
4710
4711 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4712 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4713 goto nla_put_failure;
4714 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4715 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4716 goto nla_put_failure;
4717 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4718 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4719 survey->channel_time))
4720 goto nla_put_failure;
4721 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4722 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4723 survey->channel_time_busy))
4724 goto nla_put_failure;
4725 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4726 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4727 survey->channel_time_ext_busy))
4728 goto nla_put_failure;
4729 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4730 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4731 survey->channel_time_rx))
4732 goto nla_put_failure;
4733 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4734 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4735 survey->channel_time_tx))
4736 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004737
4738 nla_nest_end(msg, infoattr);
4739
4740 return genlmsg_end(msg, hdr);
4741
4742 nla_put_failure:
4743 genlmsg_cancel(msg, hdr);
4744 return -EMSGSIZE;
4745}
4746
4747static int nl80211_dump_survey(struct sk_buff *skb,
4748 struct netlink_callback *cb)
4749{
4750 struct survey_info survey;
4751 struct cfg80211_registered_device *dev;
4752 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004753 int survey_idx = cb->args[1];
4754 int res;
4755
Johannes Berg67748892010-10-04 21:14:06 +02004756 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4757 if (res)
4758 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004759
4760 if (!dev->ops->dump_survey) {
4761 res = -EOPNOTSUPP;
4762 goto out_err;
4763 }
4764
4765 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004766 struct ieee80211_channel *chan;
4767
Holger Schurig61fa7132009-11-11 12:25:40 +01004768 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4769 &survey);
4770 if (res == -ENOENT)
4771 break;
4772 if (res)
4773 goto out_err;
4774
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004775 /* Survey without a channel doesn't make sense */
4776 if (!survey.channel) {
4777 res = -EINVAL;
4778 goto out;
4779 }
4780
4781 chan = ieee80211_get_channel(&dev->wiphy,
4782 survey.channel->center_freq);
4783 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4784 survey_idx++;
4785 continue;
4786 }
4787
Holger Schurig61fa7132009-11-11 12:25:40 +01004788 if (nl80211_send_survey(skb,
4789 NETLINK_CB(cb->skb).pid,
4790 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4791 netdev,
4792 &survey) < 0)
4793 goto out;
4794 survey_idx++;
4795 }
4796
4797 out:
4798 cb->args[1] = survey_idx;
4799 res = skb->len;
4800 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004801 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004802 return res;
4803}
4804
Jouni Malinen255e7372009-03-20 21:21:17 +02004805static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4806{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004807 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004808}
4809
Samuel Ortizb23aa672009-07-01 21:26:54 +02004810static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4811{
4812 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4813 NL80211_WPA_VERSION_2));
4814}
4815
Jouni Malinen636a5d32009-03-19 13:39:22 +02004816static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4817{
Johannes Berg4c476992010-10-04 21:36:35 +02004818 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4819 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004820 struct ieee80211_channel *chan;
4821 const u8 *bssid, *ssid, *ie = NULL;
4822 int err, ssid_len, ie_len = 0;
4823 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004824 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004825 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004826
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004827 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4828 return -EINVAL;
4829
4830 if (!info->attrs[NL80211_ATTR_MAC])
4831 return -EINVAL;
4832
Jouni Malinen17780922009-03-27 20:52:47 +02004833 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4834 return -EINVAL;
4835
Johannes Berg19957bb2009-07-02 17:20:43 +02004836 if (!info->attrs[NL80211_ATTR_SSID])
4837 return -EINVAL;
4838
4839 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4840 return -EINVAL;
4841
Johannes Bergfffd0932009-07-08 14:22:54 +02004842 err = nl80211_parse_key(info, &key);
4843 if (err)
4844 return err;
4845
4846 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004847 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4848 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004849 if (!key.p.key || !key.p.key_len)
4850 return -EINVAL;
4851 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4852 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4853 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4854 key.p.key_len != WLAN_KEY_LEN_WEP104))
4855 return -EINVAL;
4856 if (key.idx > 4)
4857 return -EINVAL;
4858 } else {
4859 key.p.key_len = 0;
4860 key.p.key = NULL;
4861 }
4862
Johannes Bergafea0b72010-08-10 09:46:42 +02004863 if (key.idx >= 0) {
4864 int i;
4865 bool ok = false;
4866 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4867 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4868 ok = true;
4869 break;
4870 }
4871 }
Johannes Berg4c476992010-10-04 21:36:35 +02004872 if (!ok)
4873 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004874 }
4875
Johannes Berg4c476992010-10-04 21:36:35 +02004876 if (!rdev->ops->auth)
4877 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004878
Johannes Berg074ac8d2010-09-16 14:58:22 +02004879 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004880 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4881 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004882
Johannes Berg19957bb2009-07-02 17:20:43 +02004883 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004884 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004885 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004886 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4887 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004888
Johannes Berg19957bb2009-07-02 17:20:43 +02004889 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4890 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4891
4892 if (info->attrs[NL80211_ATTR_IE]) {
4893 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4894 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4895 }
4896
4897 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004898 if (!nl80211_valid_auth_type(auth_type))
4899 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004900
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004901 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4902
Johannes Berg95de8172012-01-20 13:55:25 +01004903 /*
4904 * Since we no longer track auth state, ignore
4905 * requests to only change local state.
4906 */
4907 if (local_state_change)
4908 return 0;
4909
Johannes Berg4c476992010-10-04 21:36:35 +02004910 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4911 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004912 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004913}
4914
Johannes Bergc0692b82010-08-27 14:26:53 +03004915static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4916 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004917 struct cfg80211_crypto_settings *settings,
4918 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004919{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004920 memset(settings, 0, sizeof(*settings));
4921
Samuel Ortizb23aa672009-07-01 21:26:54 +02004922 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4923
Johannes Bergc0692b82010-08-27 14:26:53 +03004924 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4925 u16 proto;
4926 proto = nla_get_u16(
4927 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4928 settings->control_port_ethertype = cpu_to_be16(proto);
4929 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4930 proto != ETH_P_PAE)
4931 return -EINVAL;
4932 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4933 settings->control_port_no_encrypt = true;
4934 } else
4935 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4936
Samuel Ortizb23aa672009-07-01 21:26:54 +02004937 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4938 void *data;
4939 int len, i;
4940
4941 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4942 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4943 settings->n_ciphers_pairwise = len / sizeof(u32);
4944
4945 if (len % sizeof(u32))
4946 return -EINVAL;
4947
Johannes Berg3dc27d22009-07-02 21:36:37 +02004948 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004949 return -EINVAL;
4950
4951 memcpy(settings->ciphers_pairwise, data, len);
4952
4953 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004954 if (!cfg80211_supported_cipher_suite(
4955 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004956 settings->ciphers_pairwise[i]))
4957 return -EINVAL;
4958 }
4959
4960 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4961 settings->cipher_group =
4962 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004963 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4964 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004965 return -EINVAL;
4966 }
4967
4968 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4969 settings->wpa_versions =
4970 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4971 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4972 return -EINVAL;
4973 }
4974
4975 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4976 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004977 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004978
4979 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4980 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4981 settings->n_akm_suites = len / sizeof(u32);
4982
4983 if (len % sizeof(u32))
4984 return -EINVAL;
4985
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004986 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4987 return -EINVAL;
4988
Samuel Ortizb23aa672009-07-01 21:26:54 +02004989 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004990 }
4991
4992 return 0;
4993}
4994
Jouni Malinen636a5d32009-03-19 13:39:22 +02004995static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4996{
Johannes Berg4c476992010-10-04 21:36:35 +02004997 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4998 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004999 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005000 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005001 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005002 int err, ssid_len, ie_len = 0;
5003 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005004 u32 flags = 0;
5005 struct ieee80211_ht_cap *ht_capa = NULL;
5006 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005007
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005008 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5009 return -EINVAL;
5010
5011 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005012 !info->attrs[NL80211_ATTR_SSID] ||
5013 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005014 return -EINVAL;
5015
Johannes Berg4c476992010-10-04 21:36:35 +02005016 if (!rdev->ops->assoc)
5017 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005018
Johannes Berg074ac8d2010-09-16 14:58:22 +02005019 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005020 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5021 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005022
Johannes Berg19957bb2009-07-02 17:20:43 +02005023 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005024
Johannes Berg19957bb2009-07-02 17:20:43 +02005025 chan = ieee80211_get_channel(&rdev->wiphy,
5026 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005027 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5028 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005029
Johannes Berg19957bb2009-07-02 17:20:43 +02005030 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5031 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005032
5033 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005034 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5035 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005036 }
5037
Jouni Malinendc6382c2009-05-06 22:09:37 +03005038 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005039 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005040 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005041 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005042 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005043 else if (mfp != NL80211_MFP_NO)
5044 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005045 }
5046
Johannes Berg3e5d7642009-07-07 14:37:26 +02005047 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5048 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5049
Ben Greear7e7c8922011-11-18 11:31:59 -08005050 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5051 flags |= ASSOC_REQ_DISABLE_HT;
5052
5053 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5054 ht_capa_mask =
5055 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5056
5057 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5058 if (!ht_capa_mask)
5059 return -EINVAL;
5060 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5061 }
5062
Johannes Bergc0692b82010-08-27 14:26:53 +03005063 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005064 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005065 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5066 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005067 &crypto, flags, ht_capa,
5068 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005069
Jouni Malinen636a5d32009-03-19 13:39:22 +02005070 return err;
5071}
5072
5073static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5074{
Johannes Berg4c476992010-10-04 21:36:35 +02005075 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5076 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005077 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005078 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005079 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005080 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005081
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005082 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5083 return -EINVAL;
5084
5085 if (!info->attrs[NL80211_ATTR_MAC])
5086 return -EINVAL;
5087
5088 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5089 return -EINVAL;
5090
Johannes Berg4c476992010-10-04 21:36:35 +02005091 if (!rdev->ops->deauth)
5092 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005093
Johannes Berg074ac8d2010-09-16 14:58:22 +02005094 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005095 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5096 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005097
Johannes Berg19957bb2009-07-02 17:20:43 +02005098 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005099
Johannes Berg19957bb2009-07-02 17:20:43 +02005100 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5101 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005102 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005103 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005104 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005105
5106 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005107 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5108 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005109 }
5110
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005111 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5112
Johannes Berg4c476992010-10-04 21:36:35 +02005113 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5114 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005115}
5116
5117static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5118{
Johannes Berg4c476992010-10-04 21:36:35 +02005119 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5120 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005121 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005122 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005123 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005124 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005125
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005126 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5127 return -EINVAL;
5128
5129 if (!info->attrs[NL80211_ATTR_MAC])
5130 return -EINVAL;
5131
5132 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5133 return -EINVAL;
5134
Johannes Berg4c476992010-10-04 21:36:35 +02005135 if (!rdev->ops->disassoc)
5136 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005137
Johannes Berg074ac8d2010-09-16 14:58:22 +02005138 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005139 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5140 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005141
Johannes Berg19957bb2009-07-02 17:20:43 +02005142 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005143
Johannes Berg19957bb2009-07-02 17:20:43 +02005144 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5145 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005146 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005147 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005148 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005149
5150 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005151 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5152 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005153 }
5154
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005155 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5156
Johannes Berg4c476992010-10-04 21:36:35 +02005157 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5158 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005159}
5160
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005161static bool
5162nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5163 int mcast_rate[IEEE80211_NUM_BANDS],
5164 int rateval)
5165{
5166 struct wiphy *wiphy = &rdev->wiphy;
5167 bool found = false;
5168 int band, i;
5169
5170 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5171 struct ieee80211_supported_band *sband;
5172
5173 sband = wiphy->bands[band];
5174 if (!sband)
5175 continue;
5176
5177 for (i = 0; i < sband->n_bitrates; i++) {
5178 if (sband->bitrates[i].bitrate == rateval) {
5179 mcast_rate[band] = i + 1;
5180 found = true;
5181 break;
5182 }
5183 }
5184 }
5185
5186 return found;
5187}
5188
Johannes Berg04a773a2009-04-19 21:24:32 +02005189static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5190{
Johannes Berg4c476992010-10-04 21:36:35 +02005191 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5192 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005193 struct cfg80211_ibss_params ibss;
5194 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005195 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005196 int err;
5197
Johannes Berg8e30bc52009-04-22 17:45:38 +02005198 memset(&ibss, 0, sizeof(ibss));
5199
Johannes Berg04a773a2009-04-19 21:24:32 +02005200 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5201 return -EINVAL;
5202
5203 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5204 !info->attrs[NL80211_ATTR_SSID] ||
5205 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5206 return -EINVAL;
5207
Johannes Berg8e30bc52009-04-22 17:45:38 +02005208 ibss.beacon_interval = 100;
5209
5210 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5211 ibss.beacon_interval =
5212 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5213 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5214 return -EINVAL;
5215 }
5216
Johannes Berg4c476992010-10-04 21:36:35 +02005217 if (!rdev->ops->join_ibss)
5218 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005219
Johannes Berg4c476992010-10-04 21:36:35 +02005220 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5221 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005222
Johannes Berg79c97e92009-07-07 03:56:12 +02005223 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005224
Johannes Berg39193492011-09-16 13:45:25 +02005225 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005226 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005227
5228 if (!is_valid_ether_addr(ibss.bssid))
5229 return -EINVAL;
5230 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005231 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5232 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5233
5234 if (info->attrs[NL80211_ATTR_IE]) {
5235 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5236 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5237 }
5238
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005239 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5240 enum nl80211_channel_type channel_type;
5241
Johannes Bergcd6c6592012-05-10 21:27:18 +02005242 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005243 return -EINVAL;
5244
5245 if (channel_type != NL80211_CHAN_NO_HT &&
5246 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5247 return -EINVAL;
5248
5249 ibss.channel_type = channel_type;
5250 } else {
5251 ibss.channel_type = NL80211_CHAN_NO_HT;
5252 }
5253
5254 ibss.channel = rdev_freq_to_chan(rdev,
5255 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5256 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005257 if (!ibss.channel ||
5258 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005259 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5260 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005261
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005262 /* Both channels should be able to initiate communication */
5263 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5264 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5265 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5266 ibss.channel_type))
5267 return -EINVAL;
5268
Johannes Berg04a773a2009-04-19 21:24:32 +02005269 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005270 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005271
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005272 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5273 u8 *rates =
5274 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5275 int n_rates =
5276 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5277 struct ieee80211_supported_band *sband =
5278 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005279
Johannes Berg34850ab2011-07-18 18:08:35 +02005280 err = ieee80211_get_ratemask(sband, rates, n_rates,
5281 &ibss.basic_rates);
5282 if (err)
5283 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005284 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005285
5286 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5287 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5288 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5289 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005290
Johannes Berg4c476992010-10-04 21:36:35 +02005291 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5292 connkeys = nl80211_parse_connkeys(rdev,
5293 info->attrs[NL80211_ATTR_KEYS]);
5294 if (IS_ERR(connkeys))
5295 return PTR_ERR(connkeys);
5296 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005297
Antonio Quartulli267335d2012-01-31 20:25:47 +01005298 ibss.control_port =
5299 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5300
Johannes Berg4c476992010-10-04 21:36:35 +02005301 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005302 if (err)
5303 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005304 return err;
5305}
5306
5307static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5308{
Johannes Berg4c476992010-10-04 21:36:35 +02005309 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5310 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005311
Johannes Berg4c476992010-10-04 21:36:35 +02005312 if (!rdev->ops->leave_ibss)
5313 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005314
Johannes Berg4c476992010-10-04 21:36:35 +02005315 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5316 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005317
Johannes Berg4c476992010-10-04 21:36:35 +02005318 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005319}
5320
Johannes Bergaff89a92009-07-01 21:26:51 +02005321#ifdef CONFIG_NL80211_TESTMODE
5322static struct genl_multicast_group nl80211_testmode_mcgrp = {
5323 .name = "testmode",
5324};
5325
5326static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5327{
Johannes Berg4c476992010-10-04 21:36:35 +02005328 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005329 int err;
5330
5331 if (!info->attrs[NL80211_ATTR_TESTDATA])
5332 return -EINVAL;
5333
Johannes Bergaff89a92009-07-01 21:26:51 +02005334 err = -EOPNOTSUPP;
5335 if (rdev->ops->testmode_cmd) {
5336 rdev->testmode_info = info;
5337 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5338 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5339 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5340 rdev->testmode_info = NULL;
5341 }
5342
Johannes Bergaff89a92009-07-01 21:26:51 +02005343 return err;
5344}
5345
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005346static int nl80211_testmode_dump(struct sk_buff *skb,
5347 struct netlink_callback *cb)
5348{
Johannes Berg00918d32011-12-13 17:22:05 +01005349 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005350 int err;
5351 long phy_idx;
5352 void *data = NULL;
5353 int data_len = 0;
5354
5355 if (cb->args[0]) {
5356 /*
5357 * 0 is a valid index, but not valid for args[0],
5358 * so we need to offset by 1.
5359 */
5360 phy_idx = cb->args[0] - 1;
5361 } else {
5362 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5363 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5364 nl80211_policy);
5365 if (err)
5366 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005367
Johannes Berg2bd7e352012-06-15 14:23:16 +02005368 mutex_lock(&cfg80211_mutex);
5369 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5370 nl80211_fam.attrbuf);
5371 if (IS_ERR(rdev)) {
5372 mutex_unlock(&cfg80211_mutex);
5373 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005374 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005375 phy_idx = rdev->wiphy_idx;
5376 rdev = NULL;
5377 mutex_unlock(&cfg80211_mutex);
5378
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005379 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5380 cb->args[1] =
5381 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5382 }
5383
5384 if (cb->args[1]) {
5385 data = nla_data((void *)cb->args[1]);
5386 data_len = nla_len((void *)cb->args[1]);
5387 }
5388
5389 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005390 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5391 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005392 mutex_unlock(&cfg80211_mutex);
5393 return -ENOENT;
5394 }
Johannes Berg00918d32011-12-13 17:22:05 +01005395 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005396 mutex_unlock(&cfg80211_mutex);
5397
Johannes Berg00918d32011-12-13 17:22:05 +01005398 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005399 err = -EOPNOTSUPP;
5400 goto out_err;
5401 }
5402
5403 while (1) {
5404 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5405 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5406 NL80211_CMD_TESTMODE);
5407 struct nlattr *tmdata;
5408
David S. Miller9360ffd2012-03-29 04:41:26 -04005409 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005410 genlmsg_cancel(skb, hdr);
5411 break;
5412 }
5413
5414 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5415 if (!tmdata) {
5416 genlmsg_cancel(skb, hdr);
5417 break;
5418 }
Johannes Berg00918d32011-12-13 17:22:05 +01005419 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5420 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005421 nla_nest_end(skb, tmdata);
5422
5423 if (err == -ENOBUFS || err == -ENOENT) {
5424 genlmsg_cancel(skb, hdr);
5425 break;
5426 } else if (err) {
5427 genlmsg_cancel(skb, hdr);
5428 goto out_err;
5429 }
5430
5431 genlmsg_end(skb, hdr);
5432 }
5433
5434 err = skb->len;
5435 /* see above */
5436 cb->args[0] = phy_idx + 1;
5437 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005438 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005439 return err;
5440}
5441
Johannes Bergaff89a92009-07-01 21:26:51 +02005442static struct sk_buff *
5443__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5444 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5445{
5446 struct sk_buff *skb;
5447 void *hdr;
5448 struct nlattr *data;
5449
5450 skb = nlmsg_new(approxlen + 100, gfp);
5451 if (!skb)
5452 return NULL;
5453
5454 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5455 if (!hdr) {
5456 kfree_skb(skb);
5457 return NULL;
5458 }
5459
David S. Miller9360ffd2012-03-29 04:41:26 -04005460 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5461 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005462 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5463
5464 ((void **)skb->cb)[0] = rdev;
5465 ((void **)skb->cb)[1] = hdr;
5466 ((void **)skb->cb)[2] = data;
5467
5468 return skb;
5469
5470 nla_put_failure:
5471 kfree_skb(skb);
5472 return NULL;
5473}
5474
5475struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5476 int approxlen)
5477{
5478 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5479
5480 if (WARN_ON(!rdev->testmode_info))
5481 return NULL;
5482
5483 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5484 rdev->testmode_info->snd_pid,
5485 rdev->testmode_info->snd_seq,
5486 GFP_KERNEL);
5487}
5488EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5489
5490int cfg80211_testmode_reply(struct sk_buff *skb)
5491{
5492 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5493 void *hdr = ((void **)skb->cb)[1];
5494 struct nlattr *data = ((void **)skb->cb)[2];
5495
5496 if (WARN_ON(!rdev->testmode_info)) {
5497 kfree_skb(skb);
5498 return -EINVAL;
5499 }
5500
5501 nla_nest_end(skb, data);
5502 genlmsg_end(skb, hdr);
5503 return genlmsg_reply(skb, rdev->testmode_info);
5504}
5505EXPORT_SYMBOL(cfg80211_testmode_reply);
5506
5507struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5508 int approxlen, gfp_t gfp)
5509{
5510 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5511
5512 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5513}
5514EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5515
5516void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5517{
5518 void *hdr = ((void **)skb->cb)[1];
5519 struct nlattr *data = ((void **)skb->cb)[2];
5520
5521 nla_nest_end(skb, data);
5522 genlmsg_end(skb, hdr);
5523 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5524}
5525EXPORT_SYMBOL(cfg80211_testmode_event);
5526#endif
5527
Samuel Ortizb23aa672009-07-01 21:26:54 +02005528static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5529{
Johannes Berg4c476992010-10-04 21:36:35 +02005530 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5531 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005532 struct cfg80211_connect_params connect;
5533 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005534 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005535 int err;
5536
5537 memset(&connect, 0, sizeof(connect));
5538
5539 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5540 return -EINVAL;
5541
5542 if (!info->attrs[NL80211_ATTR_SSID] ||
5543 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5544 return -EINVAL;
5545
5546 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5547 connect.auth_type =
5548 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5549 if (!nl80211_valid_auth_type(connect.auth_type))
5550 return -EINVAL;
5551 } else
5552 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5553
5554 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5555
Johannes Bergc0692b82010-08-27 14:26:53 +03005556 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005557 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005558 if (err)
5559 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005560
Johannes Berg074ac8d2010-09-16 14:58:22 +02005561 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005562 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5563 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005564
Johannes Berg79c97e92009-07-07 03:56:12 +02005565 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005566
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305567 connect.bg_scan_period = -1;
5568 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5569 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5570 connect.bg_scan_period =
5571 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5572 }
5573
Samuel Ortizb23aa672009-07-01 21:26:54 +02005574 if (info->attrs[NL80211_ATTR_MAC])
5575 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5576 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5577 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5578
5579 if (info->attrs[NL80211_ATTR_IE]) {
5580 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5581 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5582 }
5583
5584 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5585 connect.channel =
5586 ieee80211_get_channel(wiphy,
5587 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5588 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005589 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5590 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005591 }
5592
Johannes Bergfffd0932009-07-08 14:22:54 +02005593 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5594 connkeys = nl80211_parse_connkeys(rdev,
5595 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005596 if (IS_ERR(connkeys))
5597 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005598 }
5599
Ben Greear7e7c8922011-11-18 11:31:59 -08005600 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5601 connect.flags |= ASSOC_REQ_DISABLE_HT;
5602
5603 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5604 memcpy(&connect.ht_capa_mask,
5605 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5606 sizeof(connect.ht_capa_mask));
5607
5608 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5609 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5610 return -EINVAL;
5611 memcpy(&connect.ht_capa,
5612 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5613 sizeof(connect.ht_capa));
5614 }
5615
Johannes Bergfffd0932009-07-08 14:22:54 +02005616 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005617 if (err)
5618 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005619 return err;
5620}
5621
5622static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5623{
Johannes Berg4c476992010-10-04 21:36:35 +02005624 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5625 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005626 u16 reason;
5627
5628 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5629 reason = WLAN_REASON_DEAUTH_LEAVING;
5630 else
5631 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5632
5633 if (reason == 0)
5634 return -EINVAL;
5635
Johannes Berg074ac8d2010-09-16 14:58:22 +02005636 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005637 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5638 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005639
Johannes Berg4c476992010-10-04 21:36:35 +02005640 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005641}
5642
Johannes Berg463d0182009-07-14 00:33:35 +02005643static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5644{
Johannes Berg4c476992010-10-04 21:36:35 +02005645 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005646 struct net *net;
5647 int err;
5648 u32 pid;
5649
5650 if (!info->attrs[NL80211_ATTR_PID])
5651 return -EINVAL;
5652
5653 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5654
Johannes Berg463d0182009-07-14 00:33:35 +02005655 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005656 if (IS_ERR(net))
5657 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005658
5659 err = 0;
5660
5661 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005662 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5663 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005664
Johannes Berg463d0182009-07-14 00:33:35 +02005665 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005666 return err;
5667}
5668
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005669static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5670{
Johannes Berg4c476992010-10-04 21:36:35 +02005671 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005672 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5673 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005674 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005675 struct cfg80211_pmksa pmksa;
5676
5677 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5678
5679 if (!info->attrs[NL80211_ATTR_MAC])
5680 return -EINVAL;
5681
5682 if (!info->attrs[NL80211_ATTR_PMKID])
5683 return -EINVAL;
5684
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005685 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5686 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5687
Johannes Berg074ac8d2010-09-16 14:58:22 +02005688 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005689 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5690 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005691
5692 switch (info->genlhdr->cmd) {
5693 case NL80211_CMD_SET_PMKSA:
5694 rdev_ops = rdev->ops->set_pmksa;
5695 break;
5696 case NL80211_CMD_DEL_PMKSA:
5697 rdev_ops = rdev->ops->del_pmksa;
5698 break;
5699 default:
5700 WARN_ON(1);
5701 break;
5702 }
5703
Johannes Berg4c476992010-10-04 21:36:35 +02005704 if (!rdev_ops)
5705 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005706
Johannes Berg4c476992010-10-04 21:36:35 +02005707 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005708}
5709
5710static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5711{
Johannes Berg4c476992010-10-04 21:36:35 +02005712 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5713 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005714
Johannes Berg074ac8d2010-09-16 14:58:22 +02005715 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005716 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5717 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005718
Johannes Berg4c476992010-10-04 21:36:35 +02005719 if (!rdev->ops->flush_pmksa)
5720 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005721
Johannes Berg4c476992010-10-04 21:36:35 +02005722 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005723}
5724
Arik Nemtsov109086c2011-09-28 14:12:50 +03005725static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5726{
5727 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5728 struct net_device *dev = info->user_ptr[1];
5729 u8 action_code, dialog_token;
5730 u16 status_code;
5731 u8 *peer;
5732
5733 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5734 !rdev->ops->tdls_mgmt)
5735 return -EOPNOTSUPP;
5736
5737 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5738 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5739 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5740 !info->attrs[NL80211_ATTR_IE] ||
5741 !info->attrs[NL80211_ATTR_MAC])
5742 return -EINVAL;
5743
5744 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5745 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5746 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5747 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5748
5749 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5750 dialog_token, status_code,
5751 nla_data(info->attrs[NL80211_ATTR_IE]),
5752 nla_len(info->attrs[NL80211_ATTR_IE]));
5753}
5754
5755static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5756{
5757 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5758 struct net_device *dev = info->user_ptr[1];
5759 enum nl80211_tdls_operation operation;
5760 u8 *peer;
5761
5762 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5763 !rdev->ops->tdls_oper)
5764 return -EOPNOTSUPP;
5765
5766 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5767 !info->attrs[NL80211_ATTR_MAC])
5768 return -EINVAL;
5769
5770 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5771 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5772
5773 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5774}
5775
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005776static int nl80211_remain_on_channel(struct sk_buff *skb,
5777 struct genl_info *info)
5778{
Johannes Berg4c476992010-10-04 21:36:35 +02005779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005780 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005781 struct ieee80211_channel *chan;
5782 struct sk_buff *msg;
5783 void *hdr;
5784 u64 cookie;
5785 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5786 u32 freq, duration;
5787 int err;
5788
5789 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5790 !info->attrs[NL80211_ATTR_DURATION])
5791 return -EINVAL;
5792
5793 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5794
Johannes Berg7c4ef712011-11-18 15:33:48 +01005795 if (!rdev->ops->remain_on_channel ||
5796 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005797 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005798
Johannes Bergebf348f2012-06-01 12:50:54 +02005799 /*
5800 * We should be on that channel for at least a minimum amount of
5801 * time (10ms) but no longer than the driver supports.
5802 */
5803 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5804 duration > rdev->wiphy.max_remain_on_channel_duration)
5805 return -EINVAL;
5806
Johannes Bergcd6c6592012-05-10 21:27:18 +02005807 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5808 !nl80211_valid_channel_type(info, &channel_type))
5809 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005810
5811 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5812 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005813 if (chan == NULL)
5814 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005815
5816 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005817 if (!msg)
5818 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005819
5820 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5821 NL80211_CMD_REMAIN_ON_CHANNEL);
5822
5823 if (IS_ERR(hdr)) {
5824 err = PTR_ERR(hdr);
5825 goto free_msg;
5826 }
5827
Johannes Berg71bbc992012-06-15 15:30:18 +02005828 err = rdev->ops->remain_on_channel(&rdev->wiphy, wdev, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005829 channel_type, duration, &cookie);
5830
5831 if (err)
5832 goto free_msg;
5833
David S. Miller9360ffd2012-03-29 04:41:26 -04005834 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5835 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005836
5837 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005838
5839 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005840
5841 nla_put_failure:
5842 err = -ENOBUFS;
5843 free_msg:
5844 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005845 return err;
5846}
5847
5848static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5849 struct genl_info *info)
5850{
Johannes Berg4c476992010-10-04 21:36:35 +02005851 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005852 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005853 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005854
5855 if (!info->attrs[NL80211_ATTR_COOKIE])
5856 return -EINVAL;
5857
Johannes Berg4c476992010-10-04 21:36:35 +02005858 if (!rdev->ops->cancel_remain_on_channel)
5859 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005860
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005861 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5862
Johannes Berg71bbc992012-06-15 15:30:18 +02005863 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005864}
5865
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005866static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5867 u8 *rates, u8 rates_len)
5868{
5869 u8 i;
5870 u32 mask = 0;
5871
5872 for (i = 0; i < rates_len; i++) {
5873 int rate = (rates[i] & 0x7f) * 5;
5874 int ridx;
5875 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5876 struct ieee80211_rate *srate =
5877 &sband->bitrates[ridx];
5878 if (rate == srate->bitrate) {
5879 mask |= 1 << ridx;
5880 break;
5881 }
5882 }
5883 if (ridx == sband->n_bitrates)
5884 return 0; /* rate not found */
5885 }
5886
5887 return mask;
5888}
5889
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005890static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5891 u8 *rates, u8 rates_len,
5892 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5893{
5894 u8 i;
5895
5896 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5897
5898 for (i = 0; i < rates_len; i++) {
5899 int ridx, rbit;
5900
5901 ridx = rates[i] / 8;
5902 rbit = BIT(rates[i] % 8);
5903
5904 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005905 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005906 return false;
5907
5908 /* check availability */
5909 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5910 mcs[ridx] |= rbit;
5911 else
5912 return false;
5913 }
5914
5915 return true;
5916}
5917
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005918static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005919 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5920 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005921 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5922 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005923};
5924
5925static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5926 struct genl_info *info)
5927{
5928 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005929 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005930 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005931 int rem, i;
5932 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005933 struct nlattr *tx_rates;
5934 struct ieee80211_supported_band *sband;
5935
5936 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5937 return -EINVAL;
5938
Johannes Berg4c476992010-10-04 21:36:35 +02005939 if (!rdev->ops->set_bitrate_mask)
5940 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005941
5942 memset(&mask, 0, sizeof(mask));
5943 /* Default to all rates enabled */
5944 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5945 sband = rdev->wiphy.bands[i];
5946 mask.control[i].legacy =
5947 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005948 if (sband)
5949 memcpy(mask.control[i].mcs,
5950 sband->ht_cap.mcs.rx_mask,
5951 sizeof(mask.control[i].mcs));
5952 else
5953 memset(mask.control[i].mcs, 0,
5954 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005955 }
5956
5957 /*
5958 * The nested attribute uses enum nl80211_band as the index. This maps
5959 * directly to the enum ieee80211_band values used in cfg80211.
5960 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005961 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005962 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5963 {
5964 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005965 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5966 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005967 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005968 if (sband == NULL)
5969 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005970 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5971 nla_len(tx_rates), nl80211_txattr_policy);
5972 if (tb[NL80211_TXRATE_LEGACY]) {
5973 mask.control[band].legacy = rateset_to_mask(
5974 sband,
5975 nla_data(tb[NL80211_TXRATE_LEGACY]),
5976 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05305977 if ((mask.control[band].legacy == 0) &&
5978 nla_len(tb[NL80211_TXRATE_LEGACY]))
5979 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005980 }
5981 if (tb[NL80211_TXRATE_MCS]) {
5982 if (!ht_rateset_to_mask(
5983 sband,
5984 nla_data(tb[NL80211_TXRATE_MCS]),
5985 nla_len(tb[NL80211_TXRATE_MCS]),
5986 mask.control[band].mcs))
5987 return -EINVAL;
5988 }
5989
5990 if (mask.control[band].legacy == 0) {
5991 /* don't allow empty legacy rates if HT
5992 * is not even supported. */
5993 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5994 return -EINVAL;
5995
5996 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5997 if (mask.control[band].mcs[i])
5998 break;
5999
6000 /* legacy and mcs rates may not be both empty */
6001 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006002 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006003 }
6004 }
6005
Johannes Berg4c476992010-10-04 21:36:35 +02006006 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006007}
6008
Johannes Berg2e161f72010-08-12 15:38:38 +02006009static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006010{
Johannes Berg4c476992010-10-04 21:36:35 +02006011 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006012 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006013 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006014
6015 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6016 return -EINVAL;
6017
Johannes Berg2e161f72010-08-12 15:38:38 +02006018 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6019 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006020
Johannes Berg71bbc992012-06-15 15:30:18 +02006021 switch (wdev->iftype) {
6022 case NL80211_IFTYPE_STATION:
6023 case NL80211_IFTYPE_ADHOC:
6024 case NL80211_IFTYPE_P2P_CLIENT:
6025 case NL80211_IFTYPE_AP:
6026 case NL80211_IFTYPE_AP_VLAN:
6027 case NL80211_IFTYPE_MESH_POINT:
6028 case NL80211_IFTYPE_P2P_GO:
6029 break;
6030 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006031 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006032 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006033
6034 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006035 if (!rdev->ops->mgmt_tx)
6036 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006037
Johannes Berg71bbc992012-06-15 15:30:18 +02006038 return cfg80211_mlme_register_mgmt(wdev, info->snd_pid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006039 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6040 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006041}
6042
Johannes Berg2e161f72010-08-12 15:38:38 +02006043static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006044{
Johannes Berg4c476992010-10-04 21:36:35 +02006045 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006046 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02006047 struct ieee80211_channel *chan;
6048 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02006049 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02006050 u32 freq;
6051 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006052 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006053 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006054 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006055 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006056 bool offchan, no_cck, dont_wait_for_ack;
6057
6058 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006059
6060 if (!info->attrs[NL80211_ATTR_FRAME] ||
6061 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6062 return -EINVAL;
6063
Johannes Berg4c476992010-10-04 21:36:35 +02006064 if (!rdev->ops->mgmt_tx)
6065 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006066
Johannes Berg71bbc992012-06-15 15:30:18 +02006067 switch (wdev->iftype) {
6068 case NL80211_IFTYPE_STATION:
6069 case NL80211_IFTYPE_ADHOC:
6070 case NL80211_IFTYPE_P2P_CLIENT:
6071 case NL80211_IFTYPE_AP:
6072 case NL80211_IFTYPE_AP_VLAN:
6073 case NL80211_IFTYPE_MESH_POINT:
6074 case NL80211_IFTYPE_P2P_GO:
6075 break;
6076 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006077 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006078 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006079
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006080 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006081 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006082 return -EINVAL;
6083 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006084
6085 /*
6086 * We should wait on the channel for at least a minimum amount
6087 * of time (10ms) but no longer than the driver supports.
6088 */
6089 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6090 wait > rdev->wiphy.max_remain_on_channel_duration)
6091 return -EINVAL;
6092
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006093 }
6094
Jouni Malinen026331c2010-02-15 12:53:10 +02006095 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02006096 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02006097 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02006098 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02006099 }
6100
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006101 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6102
Johannes Berg7c4ef712011-11-18 15:33:48 +01006103 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6104 return -EINVAL;
6105
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306106 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6107
Jouni Malinen026331c2010-02-15 12:53:10 +02006108 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6109 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02006110 if (chan == NULL)
6111 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006112
Johannes Berge247bd902011-11-04 11:18:21 +01006113 if (!dont_wait_for_ack) {
6114 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6115 if (!msg)
6116 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006117
Johannes Berge247bd902011-11-04 11:18:21 +01006118 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6119 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006120
Johannes Berge247bd902011-11-04 11:18:21 +01006121 if (IS_ERR(hdr)) {
6122 err = PTR_ERR(hdr);
6123 goto free_msg;
6124 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006125 }
Johannes Berge247bd902011-11-04 11:18:21 +01006126
Johannes Berg71bbc992012-06-15 15:30:18 +02006127 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chan, offchan, channel_type,
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006128 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006129 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6130 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006131 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006132 if (err)
6133 goto free_msg;
6134
Johannes Berge247bd902011-11-04 11:18:21 +01006135 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006136 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6137 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006138
Johannes Berge247bd902011-11-04 11:18:21 +01006139 genlmsg_end(msg, hdr);
6140 return genlmsg_reply(msg, info);
6141 }
6142
6143 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006144
6145 nla_put_failure:
6146 err = -ENOBUFS;
6147 free_msg:
6148 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006149 return err;
6150}
6151
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006152static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6153{
6154 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006155 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006156 u64 cookie;
6157
6158 if (!info->attrs[NL80211_ATTR_COOKIE])
6159 return -EINVAL;
6160
6161 if (!rdev->ops->mgmt_tx_cancel_wait)
6162 return -EOPNOTSUPP;
6163
Johannes Berg71bbc992012-06-15 15:30:18 +02006164 switch (wdev->iftype) {
6165 case NL80211_IFTYPE_STATION:
6166 case NL80211_IFTYPE_ADHOC:
6167 case NL80211_IFTYPE_P2P_CLIENT:
6168 case NL80211_IFTYPE_AP:
6169 case NL80211_IFTYPE_AP_VLAN:
6170 case NL80211_IFTYPE_P2P_GO:
6171 break;
6172 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006173 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006174 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006175
6176 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6177
Johannes Berg71bbc992012-06-15 15:30:18 +02006178 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006179}
6180
Kalle Valoffb9eb32010-02-17 17:58:10 +02006181static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6182{
Johannes Berg4c476992010-10-04 21:36:35 +02006183 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006184 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006185 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006186 u8 ps_state;
6187 bool state;
6188 int err;
6189
Johannes Berg4c476992010-10-04 21:36:35 +02006190 if (!info->attrs[NL80211_ATTR_PS_STATE])
6191 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006192
6193 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6194
Johannes Berg4c476992010-10-04 21:36:35 +02006195 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6196 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006197
6198 wdev = dev->ieee80211_ptr;
6199
Johannes Berg4c476992010-10-04 21:36:35 +02006200 if (!rdev->ops->set_power_mgmt)
6201 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006202
6203 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6204
6205 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006206 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006207
Johannes Berg4c476992010-10-04 21:36:35 +02006208 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
6209 wdev->ps_timeout);
6210 if (!err)
6211 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006212 return err;
6213}
6214
6215static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6216{
Johannes Berg4c476992010-10-04 21:36:35 +02006217 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006218 enum nl80211_ps_state ps_state;
6219 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006220 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006221 struct sk_buff *msg;
6222 void *hdr;
6223 int err;
6224
Kalle Valoffb9eb32010-02-17 17:58:10 +02006225 wdev = dev->ieee80211_ptr;
6226
Johannes Berg4c476992010-10-04 21:36:35 +02006227 if (!rdev->ops->set_power_mgmt)
6228 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006229
6230 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006231 if (!msg)
6232 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006233
6234 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6235 NL80211_CMD_GET_POWER_SAVE);
6236 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006237 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006238 goto free_msg;
6239 }
6240
6241 if (wdev->ps)
6242 ps_state = NL80211_PS_ENABLED;
6243 else
6244 ps_state = NL80211_PS_DISABLED;
6245
David S. Miller9360ffd2012-03-29 04:41:26 -04006246 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6247 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006248
6249 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006250 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006251
Johannes Berg4c476992010-10-04 21:36:35 +02006252 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006253 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006254 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006255 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006256 return err;
6257}
6258
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006259static struct nla_policy
6260nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6261 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6262 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6263 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
6264};
6265
6266static int nl80211_set_cqm_rssi(struct genl_info *info,
6267 s32 threshold, u32 hysteresis)
6268{
Johannes Berg4c476992010-10-04 21:36:35 +02006269 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006270 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006271 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006272
6273 if (threshold > 0)
6274 return -EINVAL;
6275
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006276 wdev = dev->ieee80211_ptr;
6277
Johannes Berg4c476992010-10-04 21:36:35 +02006278 if (!rdev->ops->set_cqm_rssi_config)
6279 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006280
Johannes Berg074ac8d2010-09-16 14:58:22 +02006281 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006282 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6283 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006284
Johannes Berg4c476992010-10-04 21:36:35 +02006285 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6286 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006287}
6288
6289static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6290{
6291 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6292 struct nlattr *cqm;
6293 int err;
6294
6295 cqm = info->attrs[NL80211_ATTR_CQM];
6296 if (!cqm) {
6297 err = -EINVAL;
6298 goto out;
6299 }
6300
6301 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6302 nl80211_attr_cqm_policy);
6303 if (err)
6304 goto out;
6305
6306 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6307 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6308 s32 threshold;
6309 u32 hysteresis;
6310 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6311 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6312 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
6313 } else
6314 err = -EINVAL;
6315
6316out:
6317 return err;
6318}
6319
Johannes Berg29cbe682010-12-03 09:20:44 +01006320static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6321{
6322 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6323 struct net_device *dev = info->user_ptr[1];
6324 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006325 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006326 int err;
6327
6328 /* start with default */
6329 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006330 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006331
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006332 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006333 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006334 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006335 if (err)
6336 return err;
6337 }
6338
6339 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6340 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6341 return -EINVAL;
6342
Javier Cardonac80d5452010-12-16 17:37:49 -08006343 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6344 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6345
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006346 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6347 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6348 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6349 return -EINVAL;
6350
Javier Cardonac80d5452010-12-16 17:37:49 -08006351 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6352 /* parse additional setup parameters if given */
6353 err = nl80211_parse_mesh_setup(info, &setup);
6354 if (err)
6355 return err;
6356 }
6357
Johannes Bergcc1d2802012-05-16 23:50:20 +02006358 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6359 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6360
6361 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6362 !nl80211_valid_channel_type(info, &channel_type))
6363 return -EINVAL;
6364
6365 setup.channel = rdev_freq_to_chan(rdev,
6366 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6367 channel_type);
6368 if (!setup.channel)
6369 return -EINVAL;
6370 setup.channel_type = channel_type;
6371 } else {
6372 /* cfg80211_join_mesh() will sort it out */
6373 setup.channel = NULL;
6374 }
6375
Javier Cardonac80d5452010-12-16 17:37:49 -08006376 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006377}
6378
6379static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6380{
6381 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6382 struct net_device *dev = info->user_ptr[1];
6383
6384 return cfg80211_leave_mesh(rdev, dev);
6385}
6386
Johannes Bergdfb89c52012-06-27 09:23:48 +02006387#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006388static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6389{
6390 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6391 struct sk_buff *msg;
6392 void *hdr;
6393
6394 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6395 return -EOPNOTSUPP;
6396
6397 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6398 if (!msg)
6399 return -ENOMEM;
6400
6401 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6402 NL80211_CMD_GET_WOWLAN);
6403 if (!hdr)
6404 goto nla_put_failure;
6405
6406 if (rdev->wowlan) {
6407 struct nlattr *nl_wowlan;
6408
6409 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6410 if (!nl_wowlan)
6411 goto nla_put_failure;
6412
David S. Miller9360ffd2012-03-29 04:41:26 -04006413 if ((rdev->wowlan->any &&
6414 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6415 (rdev->wowlan->disconnect &&
6416 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6417 (rdev->wowlan->magic_pkt &&
6418 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6419 (rdev->wowlan->gtk_rekey_failure &&
6420 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6421 (rdev->wowlan->eap_identity_req &&
6422 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6423 (rdev->wowlan->four_way_handshake &&
6424 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6425 (rdev->wowlan->rfkill_release &&
6426 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6427 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006428 if (rdev->wowlan->n_patterns) {
6429 struct nlattr *nl_pats, *nl_pat;
6430 int i, pat_len;
6431
6432 nl_pats = nla_nest_start(msg,
6433 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6434 if (!nl_pats)
6435 goto nla_put_failure;
6436
6437 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6438 nl_pat = nla_nest_start(msg, i + 1);
6439 if (!nl_pat)
6440 goto nla_put_failure;
6441 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006442 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6443 DIV_ROUND_UP(pat_len, 8),
6444 rdev->wowlan->patterns[i].mask) ||
6445 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6446 pat_len,
6447 rdev->wowlan->patterns[i].pattern))
6448 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006449 nla_nest_end(msg, nl_pat);
6450 }
6451 nla_nest_end(msg, nl_pats);
6452 }
6453
6454 nla_nest_end(msg, nl_wowlan);
6455 }
6456
6457 genlmsg_end(msg, hdr);
6458 return genlmsg_reply(msg, info);
6459
6460nla_put_failure:
6461 nlmsg_free(msg);
6462 return -ENOBUFS;
6463}
6464
6465static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6466{
6467 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6468 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6469 struct cfg80211_wowlan no_triggers = {};
6470 struct cfg80211_wowlan new_triggers = {};
6471 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6472 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006473 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006474
6475 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6476 return -EOPNOTSUPP;
6477
6478 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6479 goto no_triggers;
6480
6481 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6482 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6483 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6484 nl80211_wowlan_policy);
6485 if (err)
6486 return err;
6487
6488 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6489 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6490 return -EINVAL;
6491 new_triggers.any = true;
6492 }
6493
6494 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6495 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6496 return -EINVAL;
6497 new_triggers.disconnect = true;
6498 }
6499
6500 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6501 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6502 return -EINVAL;
6503 new_triggers.magic_pkt = true;
6504 }
6505
Johannes Berg77dbbb12011-07-13 10:48:55 +02006506 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6507 return -EINVAL;
6508
6509 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6510 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6511 return -EINVAL;
6512 new_triggers.gtk_rekey_failure = true;
6513 }
6514
6515 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6516 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6517 return -EINVAL;
6518 new_triggers.eap_identity_req = true;
6519 }
6520
6521 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6522 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6523 return -EINVAL;
6524 new_triggers.four_way_handshake = true;
6525 }
6526
6527 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6528 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6529 return -EINVAL;
6530 new_triggers.rfkill_release = true;
6531 }
6532
Johannes Bergff1b6e62011-05-04 15:37:28 +02006533 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6534 struct nlattr *pat;
6535 int n_patterns = 0;
6536 int rem, pat_len, mask_len;
6537 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6538
6539 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6540 rem)
6541 n_patterns++;
6542 if (n_patterns > wowlan->n_patterns)
6543 return -EINVAL;
6544
6545 new_triggers.patterns = kcalloc(n_patterns,
6546 sizeof(new_triggers.patterns[0]),
6547 GFP_KERNEL);
6548 if (!new_triggers.patterns)
6549 return -ENOMEM;
6550
6551 new_triggers.n_patterns = n_patterns;
6552 i = 0;
6553
6554 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6555 rem) {
6556 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6557 nla_data(pat), nla_len(pat), NULL);
6558 err = -EINVAL;
6559 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6560 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6561 goto error;
6562 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6563 mask_len = DIV_ROUND_UP(pat_len, 8);
6564 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6565 mask_len)
6566 goto error;
6567 if (pat_len > wowlan->pattern_max_len ||
6568 pat_len < wowlan->pattern_min_len)
6569 goto error;
6570
6571 new_triggers.patterns[i].mask =
6572 kmalloc(mask_len + pat_len, GFP_KERNEL);
6573 if (!new_triggers.patterns[i].mask) {
6574 err = -ENOMEM;
6575 goto error;
6576 }
6577 new_triggers.patterns[i].pattern =
6578 new_triggers.patterns[i].mask + mask_len;
6579 memcpy(new_triggers.patterns[i].mask,
6580 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6581 mask_len);
6582 new_triggers.patterns[i].pattern_len = pat_len;
6583 memcpy(new_triggers.patterns[i].pattern,
6584 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6585 pat_len);
6586 i++;
6587 }
6588 }
6589
6590 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6591 struct cfg80211_wowlan *ntrig;
6592 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6593 GFP_KERNEL);
6594 if (!ntrig) {
6595 err = -ENOMEM;
6596 goto error;
6597 }
6598 cfg80211_rdev_free_wowlan(rdev);
6599 rdev->wowlan = ntrig;
6600 } else {
6601 no_triggers:
6602 cfg80211_rdev_free_wowlan(rdev);
6603 rdev->wowlan = NULL;
6604 }
6605
Johannes Berg6d525632012-04-04 15:05:25 +02006606 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6607 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6608
Johannes Bergff1b6e62011-05-04 15:37:28 +02006609 return 0;
6610 error:
6611 for (i = 0; i < new_triggers.n_patterns; i++)
6612 kfree(new_triggers.patterns[i].mask);
6613 kfree(new_triggers.patterns);
6614 return err;
6615}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006616#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006617
Johannes Berge5497d72011-07-05 16:35:40 +02006618static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6619{
6620 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6621 struct net_device *dev = info->user_ptr[1];
6622 struct wireless_dev *wdev = dev->ieee80211_ptr;
6623 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6624 struct cfg80211_gtk_rekey_data rekey_data;
6625 int err;
6626
6627 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6628 return -EINVAL;
6629
6630 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6631 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6632 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6633 nl80211_rekey_policy);
6634 if (err)
6635 return err;
6636
6637 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6638 return -ERANGE;
6639 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6640 return -ERANGE;
6641 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6642 return -ERANGE;
6643
6644 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6645 NL80211_KEK_LEN);
6646 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6647 NL80211_KCK_LEN);
6648 memcpy(rekey_data.replay_ctr,
6649 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6650 NL80211_REPLAY_CTR_LEN);
6651
6652 wdev_lock(wdev);
6653 if (!wdev->current_bss) {
6654 err = -ENOTCONN;
6655 goto out;
6656 }
6657
6658 if (!rdev->ops->set_rekey_data) {
6659 err = -EOPNOTSUPP;
6660 goto out;
6661 }
6662
6663 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6664 out:
6665 wdev_unlock(wdev);
6666 return err;
6667}
6668
Johannes Berg28946da2011-11-04 11:18:12 +01006669static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6670 struct genl_info *info)
6671{
6672 struct net_device *dev = info->user_ptr[1];
6673 struct wireless_dev *wdev = dev->ieee80211_ptr;
6674
6675 if (wdev->iftype != NL80211_IFTYPE_AP &&
6676 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6677 return -EINVAL;
6678
6679 if (wdev->ap_unexpected_nlpid)
6680 return -EBUSY;
6681
6682 wdev->ap_unexpected_nlpid = info->snd_pid;
6683 return 0;
6684}
6685
Johannes Berg7f6cf312011-11-04 11:18:15 +01006686static int nl80211_probe_client(struct sk_buff *skb,
6687 struct genl_info *info)
6688{
6689 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6690 struct net_device *dev = info->user_ptr[1];
6691 struct wireless_dev *wdev = dev->ieee80211_ptr;
6692 struct sk_buff *msg;
6693 void *hdr;
6694 const u8 *addr;
6695 u64 cookie;
6696 int err;
6697
6698 if (wdev->iftype != NL80211_IFTYPE_AP &&
6699 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6700 return -EOPNOTSUPP;
6701
6702 if (!info->attrs[NL80211_ATTR_MAC])
6703 return -EINVAL;
6704
6705 if (!rdev->ops->probe_client)
6706 return -EOPNOTSUPP;
6707
6708 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6709 if (!msg)
6710 return -ENOMEM;
6711
6712 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6713 NL80211_CMD_PROBE_CLIENT);
6714
6715 if (IS_ERR(hdr)) {
6716 err = PTR_ERR(hdr);
6717 goto free_msg;
6718 }
6719
6720 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6721
6722 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6723 if (err)
6724 goto free_msg;
6725
David S. Miller9360ffd2012-03-29 04:41:26 -04006726 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6727 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006728
6729 genlmsg_end(msg, hdr);
6730
6731 return genlmsg_reply(msg, info);
6732
6733 nla_put_failure:
6734 err = -ENOBUFS;
6735 free_msg:
6736 nlmsg_free(msg);
6737 return err;
6738}
6739
Johannes Berg5e7602302011-11-04 11:18:17 +01006740static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6741{
6742 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6743
6744 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6745 return -EOPNOTSUPP;
6746
6747 if (rdev->ap_beacons_nlpid)
6748 return -EBUSY;
6749
6750 rdev->ap_beacons_nlpid = info->snd_pid;
6751
6752 return 0;
6753}
6754
Johannes Berg4c476992010-10-04 21:36:35 +02006755#define NL80211_FLAG_NEED_WIPHY 0x01
6756#define NL80211_FLAG_NEED_NETDEV 0x02
6757#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006758#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6759#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6760 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02006761#define NL80211_FLAG_NEED_WDEV 0x10
6762/* If a netdev is associated, it must be UP */
6763#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
6764 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006765
6766static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6767 struct genl_info *info)
6768{
6769 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02006770 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006771 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02006772 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6773
6774 if (rtnl)
6775 rtnl_lock();
6776
6777 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02006778 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02006779 if (IS_ERR(rdev)) {
6780 if (rtnl)
6781 rtnl_unlock();
6782 return PTR_ERR(rdev);
6783 }
6784 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02006785 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
6786 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02006787 mutex_lock(&cfg80211_mutex);
6788 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
6789 info->attrs);
6790 if (IS_ERR(wdev)) {
6791 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02006792 if (rtnl)
6793 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02006794 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02006795 }
Johannes Berg89a54e42012-06-15 14:33:17 +02006796
Johannes Berg89a54e42012-06-15 14:33:17 +02006797 dev = wdev->netdev;
6798 rdev = wiphy_to_dev(wdev->wiphy);
6799
Johannes Berg1bf614e2012-06-15 15:23:36 +02006800 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
6801 if (!dev) {
6802 mutex_unlock(&cfg80211_mutex);
6803 if (rtnl)
6804 rtnl_unlock();
6805 return -EINVAL;
6806 }
6807
6808 info->user_ptr[1] = dev;
6809 } else {
6810 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02006811 }
Johannes Berg89a54e42012-06-15 14:33:17 +02006812
Johannes Berg1bf614e2012-06-15 15:23:36 +02006813 if (dev) {
6814 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6815 !netif_running(dev)) {
6816 mutex_unlock(&cfg80211_mutex);
6817 if (rtnl)
6818 rtnl_unlock();
6819 return -ENETDOWN;
6820 }
6821
6822 dev_hold(dev);
6823 }
6824
Johannes Berg89a54e42012-06-15 14:33:17 +02006825 cfg80211_lock_rdev(rdev);
6826
6827 mutex_unlock(&cfg80211_mutex);
6828
Johannes Berg4c476992010-10-04 21:36:35 +02006829 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006830 }
6831
6832 return 0;
6833}
6834
6835static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6836 struct genl_info *info)
6837{
6838 if (info->user_ptr[0])
6839 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02006840 if (info->user_ptr[1]) {
6841 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
6842 struct wireless_dev *wdev = info->user_ptr[1];
6843
6844 if (wdev->netdev)
6845 dev_put(wdev->netdev);
6846 } else {
6847 dev_put(info->user_ptr[1]);
6848 }
6849 }
Johannes Berg4c476992010-10-04 21:36:35 +02006850 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6851 rtnl_unlock();
6852}
6853
Johannes Berg55682962007-09-20 13:09:35 -04006854static struct genl_ops nl80211_ops[] = {
6855 {
6856 .cmd = NL80211_CMD_GET_WIPHY,
6857 .doit = nl80211_get_wiphy,
6858 .dumpit = nl80211_dump_wiphy,
6859 .policy = nl80211_policy,
6860 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006861 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006862 },
6863 {
6864 .cmd = NL80211_CMD_SET_WIPHY,
6865 .doit = nl80211_set_wiphy,
6866 .policy = nl80211_policy,
6867 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006868 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006869 },
6870 {
6871 .cmd = NL80211_CMD_GET_INTERFACE,
6872 .doit = nl80211_get_interface,
6873 .dumpit = nl80211_dump_interface,
6874 .policy = nl80211_policy,
6875 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02006876 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006877 },
6878 {
6879 .cmd = NL80211_CMD_SET_INTERFACE,
6880 .doit = nl80211_set_interface,
6881 .policy = nl80211_policy,
6882 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006883 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6884 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006885 },
6886 {
6887 .cmd = NL80211_CMD_NEW_INTERFACE,
6888 .doit = nl80211_new_interface,
6889 .policy = nl80211_policy,
6890 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006891 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6892 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006893 },
6894 {
6895 .cmd = NL80211_CMD_DEL_INTERFACE,
6896 .doit = nl80211_del_interface,
6897 .policy = nl80211_policy,
6898 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02006899 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02006900 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006901 },
Johannes Berg41ade002007-12-19 02:03:29 +01006902 {
6903 .cmd = NL80211_CMD_GET_KEY,
6904 .doit = nl80211_get_key,
6905 .policy = nl80211_policy,
6906 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006907 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006908 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006909 },
6910 {
6911 .cmd = NL80211_CMD_SET_KEY,
6912 .doit = nl80211_set_key,
6913 .policy = nl80211_policy,
6914 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006915 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006916 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006917 },
6918 {
6919 .cmd = NL80211_CMD_NEW_KEY,
6920 .doit = nl80211_new_key,
6921 .policy = nl80211_policy,
6922 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006923 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006924 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006925 },
6926 {
6927 .cmd = NL80211_CMD_DEL_KEY,
6928 .doit = nl80211_del_key,
6929 .policy = nl80211_policy,
6930 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006931 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006932 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006933 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006934 {
6935 .cmd = NL80211_CMD_SET_BEACON,
6936 .policy = nl80211_policy,
6937 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006938 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006939 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006940 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006941 },
6942 {
Johannes Berg88600202012-02-13 15:17:18 +01006943 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006944 .policy = nl80211_policy,
6945 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006946 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006947 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006948 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006949 },
6950 {
Johannes Berg88600202012-02-13 15:17:18 +01006951 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006952 .policy = nl80211_policy,
6953 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006954 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006955 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006956 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006957 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006958 {
6959 .cmd = NL80211_CMD_GET_STATION,
6960 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006961 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006962 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006963 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6964 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006965 },
6966 {
6967 .cmd = NL80211_CMD_SET_STATION,
6968 .doit = nl80211_set_station,
6969 .policy = nl80211_policy,
6970 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006971 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006972 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006973 },
6974 {
6975 .cmd = NL80211_CMD_NEW_STATION,
6976 .doit = nl80211_new_station,
6977 .policy = nl80211_policy,
6978 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006979 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006980 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006981 },
6982 {
6983 .cmd = NL80211_CMD_DEL_STATION,
6984 .doit = nl80211_del_station,
6985 .policy = nl80211_policy,
6986 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006987 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006988 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006989 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006990 {
6991 .cmd = NL80211_CMD_GET_MPATH,
6992 .doit = nl80211_get_mpath,
6993 .dumpit = nl80211_dump_mpath,
6994 .policy = nl80211_policy,
6995 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006996 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006997 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006998 },
6999 {
7000 .cmd = NL80211_CMD_SET_MPATH,
7001 .doit = nl80211_set_mpath,
7002 .policy = nl80211_policy,
7003 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007004 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007005 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007006 },
7007 {
7008 .cmd = NL80211_CMD_NEW_MPATH,
7009 .doit = nl80211_new_mpath,
7010 .policy = nl80211_policy,
7011 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007012 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007013 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007014 },
7015 {
7016 .cmd = NL80211_CMD_DEL_MPATH,
7017 .doit = nl80211_del_mpath,
7018 .policy = nl80211_policy,
7019 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007020 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007021 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007022 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007023 {
7024 .cmd = NL80211_CMD_SET_BSS,
7025 .doit = nl80211_set_bss,
7026 .policy = nl80211_policy,
7027 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007028 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007029 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007030 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007031 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007032 .cmd = NL80211_CMD_GET_REG,
7033 .doit = nl80211_get_reg,
7034 .policy = nl80211_policy,
7035 /* can be retrieved by unprivileged users */
7036 },
7037 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007038 .cmd = NL80211_CMD_SET_REG,
7039 .doit = nl80211_set_reg,
7040 .policy = nl80211_policy,
7041 .flags = GENL_ADMIN_PERM,
7042 },
7043 {
7044 .cmd = NL80211_CMD_REQ_SET_REG,
7045 .doit = nl80211_req_set_reg,
7046 .policy = nl80211_policy,
7047 .flags = GENL_ADMIN_PERM,
7048 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007049 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007050 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7051 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007052 .policy = nl80211_policy,
7053 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007054 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007055 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007056 },
7057 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007058 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7059 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007060 .policy = nl80211_policy,
7061 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007062 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007063 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007064 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007065 {
Johannes Berg2a519312009-02-10 21:25:55 +01007066 .cmd = NL80211_CMD_TRIGGER_SCAN,
7067 .doit = nl80211_trigger_scan,
7068 .policy = nl80211_policy,
7069 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007070 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007071 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007072 },
7073 {
7074 .cmd = NL80211_CMD_GET_SCAN,
7075 .policy = nl80211_policy,
7076 .dumpit = nl80211_dump_scan,
7077 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007078 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007079 .cmd = NL80211_CMD_START_SCHED_SCAN,
7080 .doit = nl80211_start_sched_scan,
7081 .policy = nl80211_policy,
7082 .flags = GENL_ADMIN_PERM,
7083 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7084 NL80211_FLAG_NEED_RTNL,
7085 },
7086 {
7087 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7088 .doit = nl80211_stop_sched_scan,
7089 .policy = nl80211_policy,
7090 .flags = GENL_ADMIN_PERM,
7091 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7092 NL80211_FLAG_NEED_RTNL,
7093 },
7094 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007095 .cmd = NL80211_CMD_AUTHENTICATE,
7096 .doit = nl80211_authenticate,
7097 .policy = nl80211_policy,
7098 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007099 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007100 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007101 },
7102 {
7103 .cmd = NL80211_CMD_ASSOCIATE,
7104 .doit = nl80211_associate,
7105 .policy = nl80211_policy,
7106 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007107 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007108 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007109 },
7110 {
7111 .cmd = NL80211_CMD_DEAUTHENTICATE,
7112 .doit = nl80211_deauthenticate,
7113 .policy = nl80211_policy,
7114 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007115 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007116 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007117 },
7118 {
7119 .cmd = NL80211_CMD_DISASSOCIATE,
7120 .doit = nl80211_disassociate,
7121 .policy = nl80211_policy,
7122 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007123 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007124 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007125 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007126 {
7127 .cmd = NL80211_CMD_JOIN_IBSS,
7128 .doit = nl80211_join_ibss,
7129 .policy = nl80211_policy,
7130 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007131 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007132 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007133 },
7134 {
7135 .cmd = NL80211_CMD_LEAVE_IBSS,
7136 .doit = nl80211_leave_ibss,
7137 .policy = nl80211_policy,
7138 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007139 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007140 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007141 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007142#ifdef CONFIG_NL80211_TESTMODE
7143 {
7144 .cmd = NL80211_CMD_TESTMODE,
7145 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007146 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007147 .policy = nl80211_policy,
7148 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007149 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7150 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007151 },
7152#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007153 {
7154 .cmd = NL80211_CMD_CONNECT,
7155 .doit = nl80211_connect,
7156 .policy = nl80211_policy,
7157 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007158 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007159 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007160 },
7161 {
7162 .cmd = NL80211_CMD_DISCONNECT,
7163 .doit = nl80211_disconnect,
7164 .policy = nl80211_policy,
7165 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007166 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007167 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007168 },
Johannes Berg463d0182009-07-14 00:33:35 +02007169 {
7170 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7171 .doit = nl80211_wiphy_netns,
7172 .policy = nl80211_policy,
7173 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007174 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7175 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007176 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007177 {
7178 .cmd = NL80211_CMD_GET_SURVEY,
7179 .policy = nl80211_policy,
7180 .dumpit = nl80211_dump_survey,
7181 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007182 {
7183 .cmd = NL80211_CMD_SET_PMKSA,
7184 .doit = nl80211_setdel_pmksa,
7185 .policy = nl80211_policy,
7186 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007187 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007188 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007189 },
7190 {
7191 .cmd = NL80211_CMD_DEL_PMKSA,
7192 .doit = nl80211_setdel_pmksa,
7193 .policy = nl80211_policy,
7194 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007195 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007196 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007197 },
7198 {
7199 .cmd = NL80211_CMD_FLUSH_PMKSA,
7200 .doit = nl80211_flush_pmksa,
7201 .policy = nl80211_policy,
7202 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007203 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007204 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007205 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007206 {
7207 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7208 .doit = nl80211_remain_on_channel,
7209 .policy = nl80211_policy,
7210 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007211 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007212 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007213 },
7214 {
7215 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7216 .doit = nl80211_cancel_remain_on_channel,
7217 .policy = nl80211_policy,
7218 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007219 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007220 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007221 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007222 {
7223 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7224 .doit = nl80211_set_tx_bitrate_mask,
7225 .policy = nl80211_policy,
7226 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007227 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7228 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007229 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007230 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007231 .cmd = NL80211_CMD_REGISTER_FRAME,
7232 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007233 .policy = nl80211_policy,
7234 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007235 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007236 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007237 },
7238 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007239 .cmd = NL80211_CMD_FRAME,
7240 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007241 .policy = nl80211_policy,
7242 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007243 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007244 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007245 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007246 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007247 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7248 .doit = nl80211_tx_mgmt_cancel_wait,
7249 .policy = nl80211_policy,
7250 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007251 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007252 NL80211_FLAG_NEED_RTNL,
7253 },
7254 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007255 .cmd = NL80211_CMD_SET_POWER_SAVE,
7256 .doit = nl80211_set_power_save,
7257 .policy = nl80211_policy,
7258 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007259 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7260 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007261 },
7262 {
7263 .cmd = NL80211_CMD_GET_POWER_SAVE,
7264 .doit = nl80211_get_power_save,
7265 .policy = nl80211_policy,
7266 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007267 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7268 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007269 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007270 {
7271 .cmd = NL80211_CMD_SET_CQM,
7272 .doit = nl80211_set_cqm,
7273 .policy = nl80211_policy,
7274 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007275 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7276 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007277 },
Johannes Bergf444de02010-05-05 15:25:02 +02007278 {
7279 .cmd = NL80211_CMD_SET_CHANNEL,
7280 .doit = nl80211_set_channel,
7281 .policy = nl80211_policy,
7282 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007283 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7284 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007285 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007286 {
7287 .cmd = NL80211_CMD_SET_WDS_PEER,
7288 .doit = nl80211_set_wds_peer,
7289 .policy = nl80211_policy,
7290 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007291 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7292 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007293 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007294 {
7295 .cmd = NL80211_CMD_JOIN_MESH,
7296 .doit = nl80211_join_mesh,
7297 .policy = nl80211_policy,
7298 .flags = GENL_ADMIN_PERM,
7299 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7300 NL80211_FLAG_NEED_RTNL,
7301 },
7302 {
7303 .cmd = NL80211_CMD_LEAVE_MESH,
7304 .doit = nl80211_leave_mesh,
7305 .policy = nl80211_policy,
7306 .flags = GENL_ADMIN_PERM,
7307 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7308 NL80211_FLAG_NEED_RTNL,
7309 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007310#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007311 {
7312 .cmd = NL80211_CMD_GET_WOWLAN,
7313 .doit = nl80211_get_wowlan,
7314 .policy = nl80211_policy,
7315 /* can be retrieved by unprivileged users */
7316 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7317 NL80211_FLAG_NEED_RTNL,
7318 },
7319 {
7320 .cmd = NL80211_CMD_SET_WOWLAN,
7321 .doit = nl80211_set_wowlan,
7322 .policy = nl80211_policy,
7323 .flags = GENL_ADMIN_PERM,
7324 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7325 NL80211_FLAG_NEED_RTNL,
7326 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007327#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007328 {
7329 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7330 .doit = nl80211_set_rekey_data,
7331 .policy = nl80211_policy,
7332 .flags = GENL_ADMIN_PERM,
7333 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7334 NL80211_FLAG_NEED_RTNL,
7335 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007336 {
7337 .cmd = NL80211_CMD_TDLS_MGMT,
7338 .doit = nl80211_tdls_mgmt,
7339 .policy = nl80211_policy,
7340 .flags = GENL_ADMIN_PERM,
7341 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7342 NL80211_FLAG_NEED_RTNL,
7343 },
7344 {
7345 .cmd = NL80211_CMD_TDLS_OPER,
7346 .doit = nl80211_tdls_oper,
7347 .policy = nl80211_policy,
7348 .flags = GENL_ADMIN_PERM,
7349 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7350 NL80211_FLAG_NEED_RTNL,
7351 },
Johannes Berg28946da2011-11-04 11:18:12 +01007352 {
7353 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7354 .doit = nl80211_register_unexpected_frame,
7355 .policy = nl80211_policy,
7356 .flags = GENL_ADMIN_PERM,
7357 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7358 NL80211_FLAG_NEED_RTNL,
7359 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007360 {
7361 .cmd = NL80211_CMD_PROBE_CLIENT,
7362 .doit = nl80211_probe_client,
7363 .policy = nl80211_policy,
7364 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007365 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007366 NL80211_FLAG_NEED_RTNL,
7367 },
Johannes Berg5e7602302011-11-04 11:18:17 +01007368 {
7369 .cmd = NL80211_CMD_REGISTER_BEACONS,
7370 .doit = nl80211_register_beacons,
7371 .policy = nl80211_policy,
7372 .flags = GENL_ADMIN_PERM,
7373 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7374 NL80211_FLAG_NEED_RTNL,
7375 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007376 {
7377 .cmd = NL80211_CMD_SET_NOACK_MAP,
7378 .doit = nl80211_set_noack_map,
7379 .policy = nl80211_policy,
7380 .flags = GENL_ADMIN_PERM,
7381 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7382 NL80211_FLAG_NEED_RTNL,
7383 },
7384
Johannes Berg55682962007-09-20 13:09:35 -04007385};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007386
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007387static struct genl_multicast_group nl80211_mlme_mcgrp = {
7388 .name = "mlme",
7389};
Johannes Berg55682962007-09-20 13:09:35 -04007390
7391/* multicast groups */
7392static struct genl_multicast_group nl80211_config_mcgrp = {
7393 .name = "config",
7394};
Johannes Berg2a519312009-02-10 21:25:55 +01007395static struct genl_multicast_group nl80211_scan_mcgrp = {
7396 .name = "scan",
7397};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007398static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7399 .name = "regulatory",
7400};
Johannes Berg55682962007-09-20 13:09:35 -04007401
7402/* notification functions */
7403
7404void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7405{
7406 struct sk_buff *msg;
7407
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007408 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007409 if (!msg)
7410 return;
7411
7412 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7413 nlmsg_free(msg);
7414 return;
7415 }
7416
Johannes Berg463d0182009-07-14 00:33:35 +02007417 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7418 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007419}
7420
Johannes Berg362a4152009-05-24 16:43:15 +02007421static int nl80211_add_scan_req(struct sk_buff *msg,
7422 struct cfg80211_registered_device *rdev)
7423{
7424 struct cfg80211_scan_request *req = rdev->scan_req;
7425 struct nlattr *nest;
7426 int i;
7427
Johannes Berg667503dd2009-07-07 03:56:11 +02007428 ASSERT_RDEV_LOCK(rdev);
7429
Johannes Berg362a4152009-05-24 16:43:15 +02007430 if (WARN_ON(!req))
7431 return 0;
7432
7433 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7434 if (!nest)
7435 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007436 for (i = 0; i < req->n_ssids; i++) {
7437 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7438 goto nla_put_failure;
7439 }
Johannes Berg362a4152009-05-24 16:43:15 +02007440 nla_nest_end(msg, nest);
7441
7442 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7443 if (!nest)
7444 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007445 for (i = 0; i < req->n_channels; i++) {
7446 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7447 goto nla_put_failure;
7448 }
Johannes Berg362a4152009-05-24 16:43:15 +02007449 nla_nest_end(msg, nest);
7450
David S. Miller9360ffd2012-03-29 04:41:26 -04007451 if (req->ie &&
7452 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7453 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007454
7455 return 0;
7456 nla_put_failure:
7457 return -ENOBUFS;
7458}
7459
Johannes Berga538e2d2009-06-16 19:56:42 +02007460static int nl80211_send_scan_msg(struct sk_buff *msg,
7461 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007462 struct wireless_dev *wdev,
Johannes Berga538e2d2009-06-16 19:56:42 +02007463 u32 pid, u32 seq, int flags,
7464 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007465{
7466 void *hdr;
7467
7468 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7469 if (!hdr)
7470 return -1;
7471
David S. Miller9360ffd2012-03-29 04:41:26 -04007472 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007473 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7474 wdev->netdev->ifindex)) ||
7475 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007476 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007477
Johannes Berg362a4152009-05-24 16:43:15 +02007478 /* ignore errors and send incomplete event anyway */
7479 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007480
7481 return genlmsg_end(msg, hdr);
7482
7483 nla_put_failure:
7484 genlmsg_cancel(msg, hdr);
7485 return -EMSGSIZE;
7486}
7487
Luciano Coelho807f8a82011-05-11 17:09:35 +03007488static int
7489nl80211_send_sched_scan_msg(struct sk_buff *msg,
7490 struct cfg80211_registered_device *rdev,
7491 struct net_device *netdev,
7492 u32 pid, u32 seq, int flags, u32 cmd)
7493{
7494 void *hdr;
7495
7496 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7497 if (!hdr)
7498 return -1;
7499
David S. Miller9360ffd2012-03-29 04:41:26 -04007500 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7501 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7502 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007503
7504 return genlmsg_end(msg, hdr);
7505
7506 nla_put_failure:
7507 genlmsg_cancel(msg, hdr);
7508 return -EMSGSIZE;
7509}
7510
Johannes Berga538e2d2009-06-16 19:56:42 +02007511void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007512 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007513{
7514 struct sk_buff *msg;
7515
7516 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7517 if (!msg)
7518 return;
7519
Johannes Bergfd014282012-06-18 19:17:03 +02007520 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007521 NL80211_CMD_TRIGGER_SCAN) < 0) {
7522 nlmsg_free(msg);
7523 return;
7524 }
7525
Johannes Berg463d0182009-07-14 00:33:35 +02007526 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7527 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007528}
7529
Johannes Berg2a519312009-02-10 21:25:55 +01007530void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007531 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007532{
7533 struct sk_buff *msg;
7534
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007535 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007536 if (!msg)
7537 return;
7538
Johannes Bergfd014282012-06-18 19:17:03 +02007539 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007540 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007541 nlmsg_free(msg);
7542 return;
7543 }
7544
Johannes Berg463d0182009-07-14 00:33:35 +02007545 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7546 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007547}
7548
7549void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007550 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007551{
7552 struct sk_buff *msg;
7553
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007554 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007555 if (!msg)
7556 return;
7557
Johannes Bergfd014282012-06-18 19:17:03 +02007558 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007559 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007560 nlmsg_free(msg);
7561 return;
7562 }
7563
Johannes Berg463d0182009-07-14 00:33:35 +02007564 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7565 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007566}
7567
Luciano Coelho807f8a82011-05-11 17:09:35 +03007568void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7569 struct net_device *netdev)
7570{
7571 struct sk_buff *msg;
7572
7573 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7574 if (!msg)
7575 return;
7576
7577 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7578 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7579 nlmsg_free(msg);
7580 return;
7581 }
7582
7583 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7584 nl80211_scan_mcgrp.id, GFP_KERNEL);
7585}
7586
7587void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7588 struct net_device *netdev, u32 cmd)
7589{
7590 struct sk_buff *msg;
7591
7592 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7593 if (!msg)
7594 return;
7595
7596 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7597 nlmsg_free(msg);
7598 return;
7599 }
7600
7601 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7602 nl80211_scan_mcgrp.id, GFP_KERNEL);
7603}
7604
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007605/*
7606 * This can happen on global regulatory changes or device specific settings
7607 * based on custom world regulatory domains.
7608 */
7609void nl80211_send_reg_change_event(struct regulatory_request *request)
7610{
7611 struct sk_buff *msg;
7612 void *hdr;
7613
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007614 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007615 if (!msg)
7616 return;
7617
7618 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7619 if (!hdr) {
7620 nlmsg_free(msg);
7621 return;
7622 }
7623
7624 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007625 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7626 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007627
David S. Miller9360ffd2012-03-29 04:41:26 -04007628 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7629 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7630 NL80211_REGDOM_TYPE_WORLD))
7631 goto nla_put_failure;
7632 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7633 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7634 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7635 goto nla_put_failure;
7636 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7637 request->intersect) {
7638 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7639 NL80211_REGDOM_TYPE_INTERSECTION))
7640 goto nla_put_failure;
7641 } else {
7642 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7643 NL80211_REGDOM_TYPE_COUNTRY) ||
7644 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7645 request->alpha2))
7646 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007647 }
7648
David S. Miller9360ffd2012-03-29 04:41:26 -04007649 if (wiphy_idx_valid(request->wiphy_idx) &&
7650 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7651 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007652
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007653 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007654
Johannes Bergbc43b282009-07-25 10:54:13 +02007655 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007656 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007657 GFP_ATOMIC);
7658 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007659
7660 return;
7661
7662nla_put_failure:
7663 genlmsg_cancel(msg, hdr);
7664 nlmsg_free(msg);
7665}
7666
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007667static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7668 struct net_device *netdev,
7669 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007670 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007671{
7672 struct sk_buff *msg;
7673 void *hdr;
7674
Johannes Berge6d6e342009-07-01 21:26:47 +02007675 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007676 if (!msg)
7677 return;
7678
7679 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7680 if (!hdr) {
7681 nlmsg_free(msg);
7682 return;
7683 }
7684
David S. Miller9360ffd2012-03-29 04:41:26 -04007685 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7686 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7687 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7688 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007689
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007690 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007691
Johannes Berg463d0182009-07-14 00:33:35 +02007692 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7693 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007694 return;
7695
7696 nla_put_failure:
7697 genlmsg_cancel(msg, hdr);
7698 nlmsg_free(msg);
7699}
7700
7701void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007702 struct net_device *netdev, const u8 *buf,
7703 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007704{
7705 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007706 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007707}
7708
7709void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7710 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007711 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007712{
Johannes Berge6d6e342009-07-01 21:26:47 +02007713 nl80211_send_mlme_event(rdev, netdev, buf, len,
7714 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007715}
7716
Jouni Malinen53b46b82009-03-27 20:53:56 +02007717void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007718 struct net_device *netdev, const u8 *buf,
7719 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007720{
7721 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007722 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007723}
7724
Jouni Malinen53b46b82009-03-27 20:53:56 +02007725void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7726 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007727 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007728{
7729 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007730 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007731}
7732
Jouni Malinencf4e5942010-12-16 00:52:40 +02007733void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7734 struct net_device *netdev, const u8 *buf,
7735 size_t len, gfp_t gfp)
7736{
7737 nl80211_send_mlme_event(rdev, netdev, buf, len,
7738 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7739}
7740
7741void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7742 struct net_device *netdev, const u8 *buf,
7743 size_t len, gfp_t gfp)
7744{
7745 nl80211_send_mlme_event(rdev, netdev, buf, len,
7746 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7747}
7748
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007749static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7750 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007751 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007752{
7753 struct sk_buff *msg;
7754 void *hdr;
7755
Johannes Berge6d6e342009-07-01 21:26:47 +02007756 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007757 if (!msg)
7758 return;
7759
7760 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7761 if (!hdr) {
7762 nlmsg_free(msg);
7763 return;
7764 }
7765
David S. Miller9360ffd2012-03-29 04:41:26 -04007766 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7767 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7768 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7769 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7770 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007771
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007772 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007773
Johannes Berg463d0182009-07-14 00:33:35 +02007774 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7775 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007776 return;
7777
7778 nla_put_failure:
7779 genlmsg_cancel(msg, hdr);
7780 nlmsg_free(msg);
7781}
7782
7783void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007784 struct net_device *netdev, const u8 *addr,
7785 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007786{
7787 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007788 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007789}
7790
7791void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007792 struct net_device *netdev, const u8 *addr,
7793 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007794{
Johannes Berge6d6e342009-07-01 21:26:47 +02007795 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7796 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007797}
7798
Samuel Ortizb23aa672009-07-01 21:26:54 +02007799void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7800 struct net_device *netdev, const u8 *bssid,
7801 const u8 *req_ie, size_t req_ie_len,
7802 const u8 *resp_ie, size_t resp_ie_len,
7803 u16 status, gfp_t gfp)
7804{
7805 struct sk_buff *msg;
7806 void *hdr;
7807
7808 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7809 if (!msg)
7810 return;
7811
7812 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7813 if (!hdr) {
7814 nlmsg_free(msg);
7815 return;
7816 }
7817
David S. Miller9360ffd2012-03-29 04:41:26 -04007818 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7819 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7820 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7821 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7822 (req_ie &&
7823 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7824 (resp_ie &&
7825 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7826 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007827
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007828 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007829
Johannes Berg463d0182009-07-14 00:33:35 +02007830 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7831 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007832 return;
7833
7834 nla_put_failure:
7835 genlmsg_cancel(msg, hdr);
7836 nlmsg_free(msg);
7837
7838}
7839
7840void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7841 struct net_device *netdev, const u8 *bssid,
7842 const u8 *req_ie, size_t req_ie_len,
7843 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7844{
7845 struct sk_buff *msg;
7846 void *hdr;
7847
7848 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7849 if (!msg)
7850 return;
7851
7852 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7853 if (!hdr) {
7854 nlmsg_free(msg);
7855 return;
7856 }
7857
David S. Miller9360ffd2012-03-29 04:41:26 -04007858 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7859 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7860 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7861 (req_ie &&
7862 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7863 (resp_ie &&
7864 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7865 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007866
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007867 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007868
Johannes Berg463d0182009-07-14 00:33:35 +02007869 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7870 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007871 return;
7872
7873 nla_put_failure:
7874 genlmsg_cancel(msg, hdr);
7875 nlmsg_free(msg);
7876
7877}
7878
7879void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7880 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007881 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007882{
7883 struct sk_buff *msg;
7884 void *hdr;
7885
Johannes Berg667503dd2009-07-07 03:56:11 +02007886 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007887 if (!msg)
7888 return;
7889
7890 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7891 if (!hdr) {
7892 nlmsg_free(msg);
7893 return;
7894 }
7895
David S. Miller9360ffd2012-03-29 04:41:26 -04007896 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7897 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7898 (from_ap && reason &&
7899 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7900 (from_ap &&
7901 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7902 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7903 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007904
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007905 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007906
Johannes Berg463d0182009-07-14 00:33:35 +02007907 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7908 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007909 return;
7910
7911 nla_put_failure:
7912 genlmsg_cancel(msg, hdr);
7913 nlmsg_free(msg);
7914
7915}
7916
Johannes Berg04a773a2009-04-19 21:24:32 +02007917void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7918 struct net_device *netdev, const u8 *bssid,
7919 gfp_t gfp)
7920{
7921 struct sk_buff *msg;
7922 void *hdr;
7923
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007924 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007925 if (!msg)
7926 return;
7927
7928 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7929 if (!hdr) {
7930 nlmsg_free(msg);
7931 return;
7932 }
7933
David S. Miller9360ffd2012-03-29 04:41:26 -04007934 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7935 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7936 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7937 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007938
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007939 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007940
Johannes Berg463d0182009-07-14 00:33:35 +02007941 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7942 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007943 return;
7944
7945 nla_put_failure:
7946 genlmsg_cancel(msg, hdr);
7947 nlmsg_free(msg);
7948}
7949
Javier Cardonac93b5e72011-04-07 15:08:34 -07007950void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7951 struct net_device *netdev,
7952 const u8 *macaddr, const u8* ie, u8 ie_len,
7953 gfp_t gfp)
7954{
7955 struct sk_buff *msg;
7956 void *hdr;
7957
7958 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7959 if (!msg)
7960 return;
7961
7962 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7963 if (!hdr) {
7964 nlmsg_free(msg);
7965 return;
7966 }
7967
David S. Miller9360ffd2012-03-29 04:41:26 -04007968 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7969 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7970 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7971 (ie_len && ie &&
7972 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7973 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007974
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007975 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007976
7977 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7978 nl80211_mlme_mcgrp.id, gfp);
7979 return;
7980
7981 nla_put_failure:
7982 genlmsg_cancel(msg, hdr);
7983 nlmsg_free(msg);
7984}
7985
Jouni Malinena3b8b052009-03-27 21:59:49 +02007986void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7987 struct net_device *netdev, const u8 *addr,
7988 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007989 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007990{
7991 struct sk_buff *msg;
7992 void *hdr;
7993
Johannes Berge6d6e342009-07-01 21:26:47 +02007994 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007995 if (!msg)
7996 return;
7997
7998 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7999 if (!hdr) {
8000 nlmsg_free(msg);
8001 return;
8002 }
8003
David S. Miller9360ffd2012-03-29 04:41:26 -04008004 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8005 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8006 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8007 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8008 (key_id != -1 &&
8009 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8010 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8011 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008012
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008013 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008014
Johannes Berg463d0182009-07-14 00:33:35 +02008015 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8016 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008017 return;
8018
8019 nla_put_failure:
8020 genlmsg_cancel(msg, hdr);
8021 nlmsg_free(msg);
8022}
8023
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008024void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8025 struct ieee80211_channel *channel_before,
8026 struct ieee80211_channel *channel_after)
8027{
8028 struct sk_buff *msg;
8029 void *hdr;
8030 struct nlattr *nl_freq;
8031
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008032 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008033 if (!msg)
8034 return;
8035
8036 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8037 if (!hdr) {
8038 nlmsg_free(msg);
8039 return;
8040 }
8041
8042 /*
8043 * Since we are applying the beacon hint to a wiphy we know its
8044 * wiphy_idx is valid
8045 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008046 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8047 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008048
8049 /* Before */
8050 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8051 if (!nl_freq)
8052 goto nla_put_failure;
8053 if (nl80211_msg_put_channel(msg, channel_before))
8054 goto nla_put_failure;
8055 nla_nest_end(msg, nl_freq);
8056
8057 /* After */
8058 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8059 if (!nl_freq)
8060 goto nla_put_failure;
8061 if (nl80211_msg_put_channel(msg, channel_after))
8062 goto nla_put_failure;
8063 nla_nest_end(msg, nl_freq);
8064
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008065 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008066
Johannes Berg463d0182009-07-14 00:33:35 +02008067 rcu_read_lock();
8068 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8069 GFP_ATOMIC);
8070 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008071
8072 return;
8073
8074nla_put_failure:
8075 genlmsg_cancel(msg, hdr);
8076 nlmsg_free(msg);
8077}
8078
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008079static void nl80211_send_remain_on_chan_event(
8080 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008081 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008082 struct ieee80211_channel *chan,
8083 enum nl80211_channel_type channel_type,
8084 unsigned int duration, gfp_t gfp)
8085{
8086 struct sk_buff *msg;
8087 void *hdr;
8088
8089 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8090 if (!msg)
8091 return;
8092
8093 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8094 if (!hdr) {
8095 nlmsg_free(msg);
8096 return;
8097 }
8098
David S. Miller9360ffd2012-03-29 04:41:26 -04008099 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008100 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8101 wdev->netdev->ifindex)) ||
8102 nla_put_u32(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008103 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
8104 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
8105 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8106 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008107
David S. Miller9360ffd2012-03-29 04:41:26 -04008108 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8109 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8110 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008111
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008112 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008113
8114 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8115 nl80211_mlme_mcgrp.id, gfp);
8116 return;
8117
8118 nla_put_failure:
8119 genlmsg_cancel(msg, hdr);
8120 nlmsg_free(msg);
8121}
8122
8123void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008124 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008125 struct ieee80211_channel *chan,
8126 enum nl80211_channel_type channel_type,
8127 unsigned int duration, gfp_t gfp)
8128{
8129 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008130 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008131 channel_type, duration, gfp);
8132}
8133
8134void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008135 struct cfg80211_registered_device *rdev,
8136 struct wireless_dev *wdev,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008137 u64 cookie, struct ieee80211_channel *chan,
8138 enum nl80211_channel_type channel_type, gfp_t gfp)
8139{
8140 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008141 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008142 channel_type, 0, gfp);
8143}
8144
Johannes Berg98b62182009-12-23 13:15:44 +01008145void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8146 struct net_device *dev, const u8 *mac_addr,
8147 struct station_info *sinfo, gfp_t gfp)
8148{
8149 struct sk_buff *msg;
8150
8151 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8152 if (!msg)
8153 return;
8154
John W. Linville66266b32012-03-15 13:25:41 -04008155 if (nl80211_send_station(msg, 0, 0, 0,
8156 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008157 nlmsg_free(msg);
8158 return;
8159 }
8160
8161 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8162 nl80211_mlme_mcgrp.id, gfp);
8163}
8164
Jouni Malinenec15e682011-03-23 15:29:52 +02008165void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8166 struct net_device *dev, const u8 *mac_addr,
8167 gfp_t gfp)
8168{
8169 struct sk_buff *msg;
8170 void *hdr;
8171
8172 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8173 if (!msg)
8174 return;
8175
8176 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8177 if (!hdr) {
8178 nlmsg_free(msg);
8179 return;
8180 }
8181
David S. Miller9360ffd2012-03-29 04:41:26 -04008182 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8183 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8184 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008185
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008186 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008187
8188 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8189 nl80211_mlme_mcgrp.id, gfp);
8190 return;
8191
8192 nla_put_failure:
8193 genlmsg_cancel(msg, hdr);
8194 nlmsg_free(msg);
8195}
8196
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008197static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8198 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008199{
8200 struct wireless_dev *wdev = dev->ieee80211_ptr;
8201 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8202 struct sk_buff *msg;
8203 void *hdr;
8204 int err;
8205 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
8206
8207 if (!nlpid)
8208 return false;
8209
8210 msg = nlmsg_new(100, gfp);
8211 if (!msg)
8212 return true;
8213
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008214 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008215 if (!hdr) {
8216 nlmsg_free(msg);
8217 return true;
8218 }
8219
David S. Miller9360ffd2012-03-29 04:41:26 -04008220 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8221 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8222 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8223 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008224
8225 err = genlmsg_end(msg, hdr);
8226 if (err < 0) {
8227 nlmsg_free(msg);
8228 return true;
8229 }
8230
8231 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8232 return true;
8233
8234 nla_put_failure:
8235 genlmsg_cancel(msg, hdr);
8236 nlmsg_free(msg);
8237 return true;
8238}
8239
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008240bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8241{
8242 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8243 addr, gfp);
8244}
8245
8246bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8247 const u8 *addr, gfp_t gfp)
8248{
8249 return __nl80211_unexpected_frame(dev,
8250 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8251 addr, gfp);
8252}
8253
Johannes Berg2e161f72010-08-12 15:38:38 +02008254int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008255 struct wireless_dev *wdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01008256 int freq, int sig_dbm,
8257 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008258{
Johannes Berg71bbc992012-06-15 15:30:18 +02008259 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008260 struct sk_buff *msg;
8261 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008262
8263 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8264 if (!msg)
8265 return -ENOMEM;
8266
Johannes Berg2e161f72010-08-12 15:38:38 +02008267 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008268 if (!hdr) {
8269 nlmsg_free(msg);
8270 return -ENOMEM;
8271 }
8272
David S. Miller9360ffd2012-03-29 04:41:26 -04008273 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008274 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8275 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008276 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8277 (sig_dbm &&
8278 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8279 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8280 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008281
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008282 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008283
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008284 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008285
8286 nla_put_failure:
8287 genlmsg_cancel(msg, hdr);
8288 nlmsg_free(msg);
8289 return -ENOBUFS;
8290}
8291
Johannes Berg2e161f72010-08-12 15:38:38 +02008292void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008293 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008294 const u8 *buf, size_t len, bool ack,
8295 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008296{
Johannes Berg71bbc992012-06-15 15:30:18 +02008297 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008298 struct sk_buff *msg;
8299 void *hdr;
8300
8301 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8302 if (!msg)
8303 return;
8304
Johannes Berg2e161f72010-08-12 15:38:38 +02008305 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008306 if (!hdr) {
8307 nlmsg_free(msg);
8308 return;
8309 }
8310
David S. Miller9360ffd2012-03-29 04:41:26 -04008311 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008312 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8313 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008314 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8315 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8316 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8317 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008318
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008319 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008320
8321 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8322 return;
8323
8324 nla_put_failure:
8325 genlmsg_cancel(msg, hdr);
8326 nlmsg_free(msg);
8327}
8328
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008329void
8330nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8331 struct net_device *netdev,
8332 enum nl80211_cqm_rssi_threshold_event rssi_event,
8333 gfp_t gfp)
8334{
8335 struct sk_buff *msg;
8336 struct nlattr *pinfoattr;
8337 void *hdr;
8338
8339 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8340 if (!msg)
8341 return;
8342
8343 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8344 if (!hdr) {
8345 nlmsg_free(msg);
8346 return;
8347 }
8348
David S. Miller9360ffd2012-03-29 04:41:26 -04008349 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8350 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8351 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008352
8353 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8354 if (!pinfoattr)
8355 goto nla_put_failure;
8356
David S. Miller9360ffd2012-03-29 04:41:26 -04008357 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8358 rssi_event))
8359 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008360
8361 nla_nest_end(msg, pinfoattr);
8362
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008363 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008364
8365 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8366 nl80211_mlme_mcgrp.id, gfp);
8367 return;
8368
8369 nla_put_failure:
8370 genlmsg_cancel(msg, hdr);
8371 nlmsg_free(msg);
8372}
8373
Johannes Berge5497d72011-07-05 16:35:40 +02008374void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8375 struct net_device *netdev, const u8 *bssid,
8376 const u8 *replay_ctr, gfp_t gfp)
8377{
8378 struct sk_buff *msg;
8379 struct nlattr *rekey_attr;
8380 void *hdr;
8381
8382 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8383 if (!msg)
8384 return;
8385
8386 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8387 if (!hdr) {
8388 nlmsg_free(msg);
8389 return;
8390 }
8391
David S. Miller9360ffd2012-03-29 04:41:26 -04008392 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8393 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8394 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8395 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008396
8397 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8398 if (!rekey_attr)
8399 goto nla_put_failure;
8400
David S. Miller9360ffd2012-03-29 04:41:26 -04008401 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8402 NL80211_REPLAY_CTR_LEN, replay_ctr))
8403 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008404
8405 nla_nest_end(msg, rekey_attr);
8406
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008407 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008408
8409 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8410 nl80211_mlme_mcgrp.id, gfp);
8411 return;
8412
8413 nla_put_failure:
8414 genlmsg_cancel(msg, hdr);
8415 nlmsg_free(msg);
8416}
8417
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008418void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8419 struct net_device *netdev, int index,
8420 const u8 *bssid, bool preauth, gfp_t gfp)
8421{
8422 struct sk_buff *msg;
8423 struct nlattr *attr;
8424 void *hdr;
8425
8426 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8427 if (!msg)
8428 return;
8429
8430 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8431 if (!hdr) {
8432 nlmsg_free(msg);
8433 return;
8434 }
8435
David S. Miller9360ffd2012-03-29 04:41:26 -04008436 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8437 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8438 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008439
8440 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8441 if (!attr)
8442 goto nla_put_failure;
8443
David S. Miller9360ffd2012-03-29 04:41:26 -04008444 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8445 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8446 (preauth &&
8447 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8448 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008449
8450 nla_nest_end(msg, attr);
8451
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008452 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008453
8454 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8455 nl80211_mlme_mcgrp.id, gfp);
8456 return;
8457
8458 nla_put_failure:
8459 genlmsg_cancel(msg, hdr);
8460 nlmsg_free(msg);
8461}
8462
Thomas Pedersen53145262012-04-06 13:35:47 -07008463void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8464 struct net_device *netdev, int freq,
8465 enum nl80211_channel_type type, gfp_t gfp)
8466{
8467 struct sk_buff *msg;
8468 void *hdr;
8469
8470 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8471 if (!msg)
8472 return;
8473
8474 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8475 if (!hdr) {
8476 nlmsg_free(msg);
8477 return;
8478 }
8479
John W. Linville7eab0f62012-04-12 14:25:14 -04008480 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8481 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8482 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8483 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008484
8485 genlmsg_end(msg, hdr);
8486
8487 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8488 nl80211_mlme_mcgrp.id, gfp);
8489 return;
8490
8491 nla_put_failure:
8492 genlmsg_cancel(msg, hdr);
8493 nlmsg_free(msg);
8494}
8495
Johannes Bergc063dbf2010-11-24 08:10:05 +01008496void
8497nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8498 struct net_device *netdev, const u8 *peer,
8499 u32 num_packets, gfp_t gfp)
8500{
8501 struct sk_buff *msg;
8502 struct nlattr *pinfoattr;
8503 void *hdr;
8504
8505 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8506 if (!msg)
8507 return;
8508
8509 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8510 if (!hdr) {
8511 nlmsg_free(msg);
8512 return;
8513 }
8514
David S. Miller9360ffd2012-03-29 04:41:26 -04008515 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8516 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8517 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8518 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008519
8520 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8521 if (!pinfoattr)
8522 goto nla_put_failure;
8523
David S. Miller9360ffd2012-03-29 04:41:26 -04008524 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8525 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008526
8527 nla_nest_end(msg, pinfoattr);
8528
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008529 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008530
8531 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8532 nl80211_mlme_mcgrp.id, gfp);
8533 return;
8534
8535 nla_put_failure:
8536 genlmsg_cancel(msg, hdr);
8537 nlmsg_free(msg);
8538}
8539
Johannes Berg7f6cf312011-11-04 11:18:15 +01008540void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8541 u64 cookie, bool acked, gfp_t gfp)
8542{
8543 struct wireless_dev *wdev = dev->ieee80211_ptr;
8544 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8545 struct sk_buff *msg;
8546 void *hdr;
8547 int err;
8548
8549 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8550 if (!msg)
8551 return;
8552
8553 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8554 if (!hdr) {
8555 nlmsg_free(msg);
8556 return;
8557 }
8558
David S. Miller9360ffd2012-03-29 04:41:26 -04008559 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8560 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8561 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8562 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8563 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8564 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008565
8566 err = genlmsg_end(msg, hdr);
8567 if (err < 0) {
8568 nlmsg_free(msg);
8569 return;
8570 }
8571
8572 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8573 nl80211_mlme_mcgrp.id, gfp);
8574 return;
8575
8576 nla_put_failure:
8577 genlmsg_cancel(msg, hdr);
8578 nlmsg_free(msg);
8579}
8580EXPORT_SYMBOL(cfg80211_probe_status);
8581
Johannes Berg5e7602302011-11-04 11:18:17 +01008582void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8583 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008584 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e7602302011-11-04 11:18:17 +01008585{
8586 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8587 struct sk_buff *msg;
8588 void *hdr;
8589 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8590
8591 if (!nlpid)
8592 return;
8593
8594 msg = nlmsg_new(len + 100, gfp);
8595 if (!msg)
8596 return;
8597
8598 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8599 if (!hdr) {
8600 nlmsg_free(msg);
8601 return;
8602 }
8603
David S. Miller9360ffd2012-03-29 04:41:26 -04008604 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8605 (freq &&
8606 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8607 (sig_dbm &&
8608 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8609 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8610 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01008611
8612 genlmsg_end(msg, hdr);
8613
8614 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8615 return;
8616
8617 nla_put_failure:
8618 genlmsg_cancel(msg, hdr);
8619 nlmsg_free(msg);
8620}
8621EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8622
Jouni Malinen026331c2010-02-15 12:53:10 +02008623static int nl80211_netlink_notify(struct notifier_block * nb,
8624 unsigned long state,
8625 void *_notify)
8626{
8627 struct netlink_notify *notify = _notify;
8628 struct cfg80211_registered_device *rdev;
8629 struct wireless_dev *wdev;
8630
8631 if (state != NETLINK_URELEASE)
8632 return NOTIFY_DONE;
8633
8634 rcu_read_lock();
8635
Johannes Berg5e7602302011-11-04 11:18:17 +01008636 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008637 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008638 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e7602302011-11-04 11:18:17 +01008639 if (rdev->ap_beacons_nlpid == notify->pid)
8640 rdev->ap_beacons_nlpid = 0;
8641 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008642
8643 rcu_read_unlock();
8644
8645 return NOTIFY_DONE;
8646}
8647
8648static struct notifier_block nl80211_netlink_notifier = {
8649 .notifier_call = nl80211_netlink_notify,
8650};
8651
Johannes Berg55682962007-09-20 13:09:35 -04008652/* initialisation/exit functions */
8653
8654int nl80211_init(void)
8655{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008656 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008657
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008658 err = genl_register_family_with_ops(&nl80211_fam,
8659 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008660 if (err)
8661 return err;
8662
Johannes Berg55682962007-09-20 13:09:35 -04008663 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8664 if (err)
8665 goto err_out;
8666
Johannes Berg2a519312009-02-10 21:25:55 +01008667 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8668 if (err)
8669 goto err_out;
8670
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008671 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8672 if (err)
8673 goto err_out;
8674
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008675 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8676 if (err)
8677 goto err_out;
8678
Johannes Bergaff89a92009-07-01 21:26:51 +02008679#ifdef CONFIG_NL80211_TESTMODE
8680 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8681 if (err)
8682 goto err_out;
8683#endif
8684
Jouni Malinen026331c2010-02-15 12:53:10 +02008685 err = netlink_register_notifier(&nl80211_netlink_notifier);
8686 if (err)
8687 goto err_out;
8688
Johannes Berg55682962007-09-20 13:09:35 -04008689 return 0;
8690 err_out:
8691 genl_unregister_family(&nl80211_fam);
8692 return err;
8693}
8694
8695void nl80211_exit(void)
8696{
Jouni Malinen026331c2010-02-15 12:53:10 +02008697 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008698 genl_unregister_family(&nl80211_fam);
8699}