blob: 27cd18e256c5d74922262dffc5f6bc49f693f044 [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
1731
1732static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001733 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001734 struct net_device *dev)
1735{
1736 void *hdr;
Johannes Berg89a54e42012-06-15 14:33:17 +02001737 u64 wdev_id = (u64)dev->ieee80211_ptr->identifier |
1738 ((u64)rdev->wiphy_idx << 32);
Johannes Berg55682962007-09-20 13:09:35 -04001739
1740 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1741 if (!hdr)
1742 return -1;
1743
David S. Miller9360ffd2012-03-29 04:41:26 -04001744 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1745 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1746 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1747 nla_put_u32(msg, NL80211_ATTR_IFTYPE,
1748 dev->ieee80211_ptr->iftype) ||
Johannes Berg89a54e42012-06-15 14:33:17 +02001749 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001750 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1751 rdev->devlist_generation ^
1752 (cfg80211_rdev_list_generation << 2)))
1753 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001754
Michal Kazior2e165b82012-06-29 12:47:06 +02001755 if (rdev->monitor_channel) {
1756 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1757 rdev->monitor_channel->center_freq) ||
1758 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1759 rdev->monitor_channel_type))
John W. Linville59ef43e2012-04-18 14:17:13 -04001760 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001761 }
1762
Johannes Berg55682962007-09-20 13:09:35 -04001763 return genlmsg_end(msg, hdr);
1764
1765 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001766 genlmsg_cancel(msg, hdr);
1767 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001768}
1769
1770static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1771{
1772 int wp_idx = 0;
1773 int if_idx = 0;
1774 int wp_start = cb->args[0];
1775 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001776 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001777 struct wireless_dev *wdev;
1778
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001779 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001780 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1781 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001782 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001783 if (wp_idx < wp_start) {
1784 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001785 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001786 }
Johannes Berg55682962007-09-20 13:09:35 -04001787 if_idx = 0;
1788
Johannes Bergf5ea9122009-08-07 16:17:38 +02001789 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001790 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001791 if (if_idx < if_start) {
1792 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001793 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001794 }
Johannes Berg55682962007-09-20 13:09:35 -04001795 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1796 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001797 rdev, wdev->netdev) < 0) {
1798 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001799 goto out;
1800 }
1801 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001802 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001803 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001804
1805 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001806 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001807 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001808 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001809
1810 cb->args[0] = wp_idx;
1811 cb->args[1] = if_idx;
1812
1813 return skb->len;
1814}
1815
1816static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1817{
1818 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001819 struct cfg80211_registered_device *dev = info->user_ptr[0];
1820 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001821
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001822 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001823 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001824 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001825
Johannes Bergd7264052009-04-19 16:23:20 +02001826 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001827 dev, netdev) < 0) {
1828 nlmsg_free(msg);
1829 return -ENOBUFS;
1830 }
Johannes Berg55682962007-09-20 13:09:35 -04001831
Johannes Berg134e6372009-07-10 09:51:34 +00001832 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001833}
1834
Michael Wu66f7ac52008-01-31 19:48:22 +01001835static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1836 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1837 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1838 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1839 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1840 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1841};
1842
1843static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1844{
1845 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1846 int flag;
1847
1848 *mntrflags = 0;
1849
1850 if (!nla)
1851 return -EINVAL;
1852
1853 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1854 nla, mntr_flags_policy))
1855 return -EINVAL;
1856
1857 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1858 if (flags[flag])
1859 *mntrflags |= (1<<flag);
1860
1861 return 0;
1862}
1863
Johannes Berg9bc383d2009-11-19 11:55:19 +01001864static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001865 struct net_device *netdev, u8 use_4addr,
1866 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001867{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001868 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001869 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001870 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001871 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001872 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001873
1874 switch (iftype) {
1875 case NL80211_IFTYPE_AP_VLAN:
1876 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1877 return 0;
1878 break;
1879 case NL80211_IFTYPE_STATION:
1880 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1881 return 0;
1882 break;
1883 default:
1884 break;
1885 }
1886
1887 return -EOPNOTSUPP;
1888}
1889
Johannes Berg55682962007-09-20 13:09:35 -04001890static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1891{
Johannes Berg4c476992010-10-04 21:36:35 +02001892 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001893 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001894 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001895 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001896 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001897 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001898 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001899
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001900 memset(&params, 0, sizeof(params));
1901
Johannes Berg04a773a2009-04-19 21:24:32 +02001902 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001903
Johannes Berg723b0382008-09-16 20:22:09 +02001904 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001905 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001906 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001907 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001908 if (ntype > NL80211_IFTYPE_MAX)
1909 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001910 }
1911
Johannes Berg92ffe052008-09-16 20:39:36 +02001912 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001913 struct wireless_dev *wdev = dev->ieee80211_ptr;
1914
Johannes Berg4c476992010-10-04 21:36:35 +02001915 if (ntype != NL80211_IFTYPE_MESH_POINT)
1916 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001917 if (netif_running(dev))
1918 return -EBUSY;
1919
1920 wdev_lock(wdev);
1921 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1922 IEEE80211_MAX_MESH_ID_LEN);
1923 wdev->mesh_id_up_len =
1924 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1925 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1926 wdev->mesh_id_up_len);
1927 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001928 }
1929
Felix Fietkau8b787642009-11-10 18:53:10 +01001930 if (info->attrs[NL80211_ATTR_4ADDR]) {
1931 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1932 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001933 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001934 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001935 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001936 } else {
1937 params.use_4addr = -1;
1938 }
1939
Johannes Berg92ffe052008-09-16 20:39:36 +02001940 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001941 if (ntype != NL80211_IFTYPE_MONITOR)
1942 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001943 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1944 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001945 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001946 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001947
1948 flags = &_flags;
1949 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001950 }
Johannes Berg3b858752009-03-12 09:55:09 +01001951
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001952 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001953 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001954 else
1955 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001956
Johannes Berg9bc383d2009-11-19 11:55:19 +01001957 if (!err && params.use_4addr != -1)
1958 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1959
Johannes Berg55682962007-09-20 13:09:35 -04001960 return err;
1961}
1962
1963static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1964{
Johannes Berg4c476992010-10-04 21:36:35 +02001965 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001966 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001967 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001968 int err;
1969 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001970 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001971
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001972 memset(&params, 0, sizeof(params));
1973
Johannes Berg55682962007-09-20 13:09:35 -04001974 if (!info->attrs[NL80211_ATTR_IFNAME])
1975 return -EINVAL;
1976
1977 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1978 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1979 if (type > NL80211_IFTYPE_MAX)
1980 return -EINVAL;
1981 }
1982
Johannes Berg79c97e92009-07-07 03:56:12 +02001983 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001984 !(rdev->wiphy.interface_modes & (1 << type)))
1985 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001986
Johannes Berg9bc383d2009-11-19 11:55:19 +01001987 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001988 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001989 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001990 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001991 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001992 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001993
Michael Wu66f7ac52008-01-31 19:48:22 +01001994 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1995 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1996 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001997 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001998 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001999 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01002000 if (IS_ERR(dev))
2001 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002002
Johannes Berg29cbe682010-12-03 09:20:44 +01002003 if (type == NL80211_IFTYPE_MESH_POINT &&
2004 info->attrs[NL80211_ATTR_MESH_ID]) {
2005 struct wireless_dev *wdev = dev->ieee80211_ptr;
2006
2007 wdev_lock(wdev);
2008 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2009 IEEE80211_MAX_MESH_ID_LEN);
2010 wdev->mesh_id_up_len =
2011 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2012 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2013 wdev->mesh_id_up_len);
2014 wdev_unlock(wdev);
2015 }
2016
Johannes Bergf9e10ce2010-12-03 09:20:42 +01002017 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002018}
2019
2020static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2021{
Johannes Berg4c476992010-10-04 21:36:35 +02002022 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2023 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002024
Johannes Berg4c476992010-10-04 21:36:35 +02002025 if (!rdev->ops->del_virtual_intf)
2026 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002027
Johannes Berg4c476992010-10-04 21:36:35 +02002028 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04002029}
2030
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002031static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2032{
2033 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2034 struct net_device *dev = info->user_ptr[1];
2035 u16 noack_map;
2036
2037 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2038 return -EINVAL;
2039
2040 if (!rdev->ops->set_noack_map)
2041 return -EOPNOTSUPP;
2042
2043 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2044
2045 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
2046}
2047
Johannes Berg41ade002007-12-19 02:03:29 +01002048struct get_key_cookie {
2049 struct sk_buff *msg;
2050 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002051 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002052};
2053
2054static void get_key_callback(void *c, struct key_params *params)
2055{
Johannes Bergb9454e82009-07-08 13:29:08 +02002056 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002057 struct get_key_cookie *cookie = c;
2058
David S. Miller9360ffd2012-03-29 04:41:26 -04002059 if ((params->key &&
2060 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2061 params->key_len, params->key)) ||
2062 (params->seq &&
2063 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2064 params->seq_len, params->seq)) ||
2065 (params->cipher &&
2066 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2067 params->cipher)))
2068 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002069
Johannes Bergb9454e82009-07-08 13:29:08 +02002070 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2071 if (!key)
2072 goto nla_put_failure;
2073
David S. Miller9360ffd2012-03-29 04:41:26 -04002074 if ((params->key &&
2075 nla_put(cookie->msg, NL80211_KEY_DATA,
2076 params->key_len, params->key)) ||
2077 (params->seq &&
2078 nla_put(cookie->msg, NL80211_KEY_SEQ,
2079 params->seq_len, params->seq)) ||
2080 (params->cipher &&
2081 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2082 params->cipher)))
2083 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002084
David S. Miller9360ffd2012-03-29 04:41:26 -04002085 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2086 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002087
2088 nla_nest_end(cookie->msg, key);
2089
Johannes Berg41ade002007-12-19 02:03:29 +01002090 return;
2091 nla_put_failure:
2092 cookie->error = 1;
2093}
2094
2095static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2096{
Johannes Berg4c476992010-10-04 21:36:35 +02002097 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002098 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002099 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002100 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002101 const u8 *mac_addr = NULL;
2102 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002103 struct get_key_cookie cookie = {
2104 .error = 0,
2105 };
2106 void *hdr;
2107 struct sk_buff *msg;
2108
2109 if (info->attrs[NL80211_ATTR_KEY_IDX])
2110 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2111
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002112 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002113 return -EINVAL;
2114
2115 if (info->attrs[NL80211_ATTR_MAC])
2116 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2117
Johannes Berge31b8212010-10-05 19:39:30 +02002118 pairwise = !!mac_addr;
2119 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2120 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2121 if (kt >= NUM_NL80211_KEYTYPES)
2122 return -EINVAL;
2123 if (kt != NL80211_KEYTYPE_GROUP &&
2124 kt != NL80211_KEYTYPE_PAIRWISE)
2125 return -EINVAL;
2126 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2127 }
2128
Johannes Berg4c476992010-10-04 21:36:35 +02002129 if (!rdev->ops->get_key)
2130 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002131
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002132 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002133 if (!msg)
2134 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002135
2136 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2137 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002138 if (IS_ERR(hdr))
2139 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002140
2141 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002142 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002143
David S. Miller9360ffd2012-03-29 04:41:26 -04002144 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2145 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2146 goto nla_put_failure;
2147 if (mac_addr &&
2148 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2149 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002150
Johannes Berge31b8212010-10-05 19:39:30 +02002151 if (pairwise && mac_addr &&
2152 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2153 return -ENOENT;
2154
2155 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
2156 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002157
2158 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002159 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002160
2161 if (cookie.error)
2162 goto nla_put_failure;
2163
2164 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002165 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002166
2167 nla_put_failure:
2168 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002169 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002170 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002171 return err;
2172}
2173
2174static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2175{
Johannes Berg4c476992010-10-04 21:36:35 +02002176 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002177 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002178 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002179 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002180
Johannes Bergb9454e82009-07-08 13:29:08 +02002181 err = nl80211_parse_key(info, &key);
2182 if (err)
2183 return err;
2184
2185 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002186 return -EINVAL;
2187
Johannes Bergb9454e82009-07-08 13:29:08 +02002188 /* only support setting default key */
2189 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002190 return -EINVAL;
2191
Johannes Bergfffd0932009-07-08 14:22:54 +02002192 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002193
2194 if (key.def) {
2195 if (!rdev->ops->set_default_key) {
2196 err = -EOPNOTSUPP;
2197 goto out;
2198 }
2199
2200 err = nl80211_key_allowed(dev->ieee80211_ptr);
2201 if (err)
2202 goto out;
2203
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002204 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2205 key.def_uni, key.def_multi);
2206
2207 if (err)
2208 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002209
Johannes Berg3d23e342009-09-29 23:27:28 +02002210#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002211 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002212#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002213 } else {
2214 if (key.def_uni || !key.def_multi) {
2215 err = -EINVAL;
2216 goto out;
2217 }
2218
2219 if (!rdev->ops->set_default_mgmt_key) {
2220 err = -EOPNOTSUPP;
2221 goto out;
2222 }
2223
2224 err = nl80211_key_allowed(dev->ieee80211_ptr);
2225 if (err)
2226 goto out;
2227
2228 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2229 dev, key.idx);
2230 if (err)
2231 goto out;
2232
2233#ifdef CONFIG_CFG80211_WEXT
2234 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2235#endif
2236 }
2237
2238 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002239 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002240
Johannes Berg41ade002007-12-19 02:03:29 +01002241 return err;
2242}
2243
2244static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2245{
Johannes Berg4c476992010-10-04 21:36:35 +02002246 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002247 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002248 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002249 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002250 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002251
Johannes Bergb9454e82009-07-08 13:29:08 +02002252 err = nl80211_parse_key(info, &key);
2253 if (err)
2254 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002255
Johannes Bergb9454e82009-07-08 13:29:08 +02002256 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002257 return -EINVAL;
2258
Johannes Berg41ade002007-12-19 02:03:29 +01002259 if (info->attrs[NL80211_ATTR_MAC])
2260 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2261
Johannes Berge31b8212010-10-05 19:39:30 +02002262 if (key.type == -1) {
2263 if (mac_addr)
2264 key.type = NL80211_KEYTYPE_PAIRWISE;
2265 else
2266 key.type = NL80211_KEYTYPE_GROUP;
2267 }
2268
2269 /* for now */
2270 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2271 key.type != NL80211_KEYTYPE_GROUP)
2272 return -EINVAL;
2273
Johannes Berg4c476992010-10-04 21:36:35 +02002274 if (!rdev->ops->add_key)
2275 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002276
Johannes Berge31b8212010-10-05 19:39:30 +02002277 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2278 key.type == NL80211_KEYTYPE_PAIRWISE,
2279 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002280 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002281
2282 wdev_lock(dev->ieee80211_ptr);
2283 err = nl80211_key_allowed(dev->ieee80211_ptr);
2284 if (!err)
2285 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002286 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002287 mac_addr, &key.p);
2288 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002289
Johannes Berg41ade002007-12-19 02:03:29 +01002290 return err;
2291}
2292
2293static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2294{
Johannes Berg4c476992010-10-04 21:36:35 +02002295 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002296 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002297 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002298 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002299 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002300
Johannes Bergb9454e82009-07-08 13:29:08 +02002301 err = nl80211_parse_key(info, &key);
2302 if (err)
2303 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002304
2305 if (info->attrs[NL80211_ATTR_MAC])
2306 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2307
Johannes Berge31b8212010-10-05 19:39:30 +02002308 if (key.type == -1) {
2309 if (mac_addr)
2310 key.type = NL80211_KEYTYPE_PAIRWISE;
2311 else
2312 key.type = NL80211_KEYTYPE_GROUP;
2313 }
2314
2315 /* for now */
2316 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2317 key.type != NL80211_KEYTYPE_GROUP)
2318 return -EINVAL;
2319
Johannes Berg4c476992010-10-04 21:36:35 +02002320 if (!rdev->ops->del_key)
2321 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002322
Johannes Bergfffd0932009-07-08 14:22:54 +02002323 wdev_lock(dev->ieee80211_ptr);
2324 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002325
2326 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2327 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2328 err = -ENOENT;
2329
Johannes Bergfffd0932009-07-08 14:22:54 +02002330 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002331 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2332 key.type == NL80211_KEYTYPE_PAIRWISE,
2333 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002334
Johannes Berg3d23e342009-09-29 23:27:28 +02002335#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002336 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002337 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002338 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002339 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002340 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2341 }
2342#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002343 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002344
Johannes Berg41ade002007-12-19 02:03:29 +01002345 return err;
2346}
2347
Johannes Berg88600202012-02-13 15:17:18 +01002348static int nl80211_parse_beacon(struct genl_info *info,
2349 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002350{
Johannes Berg88600202012-02-13 15:17:18 +01002351 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002352
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002353 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2354 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2355 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2356 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002357 return -EINVAL;
2358
Johannes Berg88600202012-02-13 15:17:18 +01002359 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002360
Johannes Berged1b6cc2007-12-19 02:03:32 +01002361 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002362 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2363 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2364 if (!bcn->head_len)
2365 return -EINVAL;
2366 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002367 }
2368
2369 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002370 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2371 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002372 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002373 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002374 }
2375
Johannes Berg4c476992010-10-04 21:36:35 +02002376 if (!haveinfo)
2377 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002378
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002379 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002380 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2381 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002382 }
2383
2384 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002385 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002386 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002387 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002388 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2389 }
2390
2391 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002392 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002393 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002394 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002395 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2396 }
2397
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002398 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002399 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002400 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002401 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002402 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2403 }
2404
Johannes Berg88600202012-02-13 15:17:18 +01002405 return 0;
2406}
2407
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002408static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2409 struct cfg80211_ap_settings *params)
2410{
2411 struct wireless_dev *wdev;
2412 bool ret = false;
2413
2414 mutex_lock(&rdev->devlist_mtx);
2415
Johannes Berg89a54e42012-06-15 14:33:17 +02002416 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002417 if (wdev->iftype != NL80211_IFTYPE_AP &&
2418 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2419 continue;
2420
2421 if (!wdev->preset_chan)
2422 continue;
2423
2424 params->channel = wdev->preset_chan;
2425 params->channel_type = wdev->preset_chantype;
2426 ret = true;
2427 break;
2428 }
2429
2430 mutex_unlock(&rdev->devlist_mtx);
2431
2432 return ret;
2433}
2434
Johannes Berg88600202012-02-13 15:17:18 +01002435static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2436{
2437 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2438 struct net_device *dev = info->user_ptr[1];
2439 struct wireless_dev *wdev = dev->ieee80211_ptr;
2440 struct cfg80211_ap_settings params;
2441 int err;
2442
2443 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2444 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2445 return -EOPNOTSUPP;
2446
2447 if (!rdev->ops->start_ap)
2448 return -EOPNOTSUPP;
2449
2450 if (wdev->beacon_interval)
2451 return -EALREADY;
2452
2453 memset(&params, 0, sizeof(params));
2454
2455 /* these are required for START_AP */
2456 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2457 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2458 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2459 return -EINVAL;
2460
2461 err = nl80211_parse_beacon(info, &params.beacon);
2462 if (err)
2463 return err;
2464
2465 params.beacon_interval =
2466 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2467 params.dtim_period =
2468 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2469
2470 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2471 if (err)
2472 return err;
2473
2474 /*
2475 * In theory, some of these attributes should be required here
2476 * but since they were not used when the command was originally
2477 * added, keep them optional for old user space programs to let
2478 * them continue to work with drivers that do not need the
2479 * additional information -- drivers must check!
2480 */
2481 if (info->attrs[NL80211_ATTR_SSID]) {
2482 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2483 params.ssid_len =
2484 nla_len(info->attrs[NL80211_ATTR_SSID]);
2485 if (params.ssid_len == 0 ||
2486 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2487 return -EINVAL;
2488 }
2489
2490 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2491 params.hidden_ssid = nla_get_u32(
2492 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2493 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2494 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2495 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2496 return -EINVAL;
2497 }
2498
2499 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2500
2501 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2502 params.auth_type = nla_get_u32(
2503 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2504 if (!nl80211_valid_auth_type(params.auth_type))
2505 return -EINVAL;
2506 } else
2507 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2508
2509 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2510 NL80211_MAX_NR_CIPHER_SUITES);
2511 if (err)
2512 return err;
2513
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302514 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2515 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2516 return -EOPNOTSUPP;
2517 params.inactivity_timeout = nla_get_u16(
2518 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2519 }
2520
Johannes Bergaa430da2012-05-16 23:50:18 +02002521 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2522 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2523
2524 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2525 !nl80211_valid_channel_type(info, &channel_type))
2526 return -EINVAL;
2527
2528 params.channel = rdev_freq_to_chan(rdev,
2529 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2530 channel_type);
2531 if (!params.channel)
2532 return -EINVAL;
2533 params.channel_type = channel_type;
2534 } else if (wdev->preset_chan) {
2535 params.channel = wdev->preset_chan;
2536 params.channel_type = wdev->preset_chantype;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002537 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002538 return -EINVAL;
2539
2540 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2541 params.channel_type))
2542 return -EINVAL;
2543
Michal Kaziore4e32452012-06-29 12:47:08 +02002544 mutex_lock(&rdev->devlist_mtx);
2545 err = cfg80211_can_use_chan(rdev, wdev, params.channel,
2546 CHAN_MODE_SHARED);
2547 mutex_unlock(&rdev->devlist_mtx);
2548
2549 if (err)
2550 return err;
2551
Johannes Berg88600202012-02-13 15:17:18 +01002552 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002553 if (!err) {
2554 wdev->preset_chan = params.channel;
2555 wdev->preset_chantype = params.channel_type;
Johannes Berg88600202012-02-13 15:17:18 +01002556 wdev->beacon_interval = params.beacon_interval;
Michal Kaziorf4489eb2012-06-29 12:46:58 +02002557 wdev->channel = params.channel;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002558 }
Johannes Berg56d18932011-05-09 18:41:15 +02002559 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002560}
2561
Johannes Berg88600202012-02-13 15:17:18 +01002562static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2563{
2564 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2565 struct net_device *dev = info->user_ptr[1];
2566 struct wireless_dev *wdev = dev->ieee80211_ptr;
2567 struct cfg80211_beacon_data params;
2568 int err;
2569
2570 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2571 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2572 return -EOPNOTSUPP;
2573
2574 if (!rdev->ops->change_beacon)
2575 return -EOPNOTSUPP;
2576
2577 if (!wdev->beacon_interval)
2578 return -EINVAL;
2579
2580 err = nl80211_parse_beacon(info, &params);
2581 if (err)
2582 return err;
2583
2584 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2585}
2586
2587static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002588{
Johannes Berg4c476992010-10-04 21:36:35 +02002589 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2590 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002591
Michal Kazior60771782012-06-29 12:46:56 +02002592 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002593}
2594
Johannes Berg5727ef12007-12-19 02:03:34 +01002595static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2596 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2597 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2598 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002599 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002600 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002601 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002602};
2603
Johannes Bergeccb8e82009-05-11 21:57:56 +03002604static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002605 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002606 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002607{
2608 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002609 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002610 int flag;
2611
Johannes Bergeccb8e82009-05-11 21:57:56 +03002612 /*
2613 * Try parsing the new attribute first so userspace
2614 * can specify both for older kernels.
2615 */
2616 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2617 if (nla) {
2618 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002619
Johannes Bergeccb8e82009-05-11 21:57:56 +03002620 sta_flags = nla_data(nla);
2621 params->sta_flags_mask = sta_flags->mask;
2622 params->sta_flags_set = sta_flags->set;
2623 if ((params->sta_flags_mask |
2624 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2625 return -EINVAL;
2626 return 0;
2627 }
2628
2629 /* if present, parse the old attribute */
2630
2631 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002632 if (!nla)
2633 return 0;
2634
2635 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2636 nla, sta_flags_policy))
2637 return -EINVAL;
2638
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002639 /*
2640 * Only allow certain flags for interface types so that
2641 * other attributes are silently ignored. Remember that
2642 * this is backward compatibility code with old userspace
2643 * and shouldn't be hit in other cases anyway.
2644 */
2645 switch (iftype) {
2646 case NL80211_IFTYPE_AP:
2647 case NL80211_IFTYPE_AP_VLAN:
2648 case NL80211_IFTYPE_P2P_GO:
2649 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2650 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2651 BIT(NL80211_STA_FLAG_WME) |
2652 BIT(NL80211_STA_FLAG_MFP);
2653 break;
2654 case NL80211_IFTYPE_P2P_CLIENT:
2655 case NL80211_IFTYPE_STATION:
2656 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2657 BIT(NL80211_STA_FLAG_TDLS_PEER);
2658 break;
2659 case NL80211_IFTYPE_MESH_POINT:
2660 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2661 BIT(NL80211_STA_FLAG_MFP) |
2662 BIT(NL80211_STA_FLAG_AUTHORIZED);
2663 default:
2664 return -EINVAL;
2665 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002666
Johannes Berg3383b5a2012-05-10 20:14:43 +02002667 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2668 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002669 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002670
Johannes Berg3383b5a2012-05-10 20:14:43 +02002671 /* no longer support new API additions in old API */
2672 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2673 return -EINVAL;
2674 }
2675 }
2676
Johannes Berg5727ef12007-12-19 02:03:34 +01002677 return 0;
2678}
2679
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002680static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2681 int attr)
2682{
2683 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002684 u32 bitrate;
2685 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002686
2687 rate = nla_nest_start(msg, attr);
2688 if (!rate)
2689 goto nla_put_failure;
2690
2691 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2692 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002693 /* report 16-bit bitrate only if we can */
2694 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002695 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002696 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2697 (bitrate_compat > 0 &&
2698 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002699 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2700 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2701 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2702 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2703 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2704 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2705 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002706
2707 nla_nest_end(msg, rate);
2708 return true;
2709
2710nla_put_failure:
2711 return false;
2712}
2713
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002714static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002715 int flags,
2716 struct cfg80211_registered_device *rdev,
2717 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002718 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002719{
2720 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002721 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002722
2723 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2724 if (!hdr)
2725 return -1;
2726
David S. Miller9360ffd2012-03-29 04:41:26 -04002727 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2728 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2729 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2730 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002731
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002732 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2733 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002734 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002735 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2736 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2737 sinfo->connected_time))
2738 goto nla_put_failure;
2739 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2740 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2741 sinfo->inactive_time))
2742 goto nla_put_failure;
2743 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2744 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2745 sinfo->rx_bytes))
2746 goto nla_put_failure;
2747 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2748 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2749 sinfo->tx_bytes))
2750 goto nla_put_failure;
2751 if ((sinfo->filled & STATION_INFO_LLID) &&
2752 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2753 goto nla_put_failure;
2754 if ((sinfo->filled & STATION_INFO_PLID) &&
2755 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2756 goto nla_put_failure;
2757 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2758 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2759 sinfo->plink_state))
2760 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002761 switch (rdev->wiphy.signal_type) {
2762 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002763 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2764 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2765 sinfo->signal))
2766 goto nla_put_failure;
2767 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2768 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2769 sinfo->signal_avg))
2770 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002771 break;
2772 default:
2773 break;
2774 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002775 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002776 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2777 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002778 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002779 }
2780 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2781 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2782 NL80211_STA_INFO_RX_BITRATE))
2783 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002784 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002785 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2786 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2787 sinfo->rx_packets))
2788 goto nla_put_failure;
2789 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2790 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2791 sinfo->tx_packets))
2792 goto nla_put_failure;
2793 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2794 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2795 sinfo->tx_retries))
2796 goto nla_put_failure;
2797 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2798 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2799 sinfo->tx_failed))
2800 goto nla_put_failure;
2801 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2802 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2803 sinfo->beacon_loss_count))
2804 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002805 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2806 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2807 if (!bss_param)
2808 goto nla_put_failure;
2809
David S. Miller9360ffd2012-03-29 04:41:26 -04002810 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2811 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2812 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2813 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2814 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2815 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2816 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2817 sinfo->bss_param.dtim_period) ||
2818 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2819 sinfo->bss_param.beacon_interval))
2820 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002821
2822 nla_nest_end(msg, bss_param);
2823 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002824 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2825 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2826 sizeof(struct nl80211_sta_flag_update),
2827 &sinfo->sta_flags))
2828 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002829 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2830 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2831 sinfo->t_offset))
2832 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002833 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002834
David S. Miller9360ffd2012-03-29 04:41:26 -04002835 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2836 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2837 sinfo->assoc_req_ies))
2838 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002839
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002840 return genlmsg_end(msg, hdr);
2841
2842 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002843 genlmsg_cancel(msg, hdr);
2844 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002845}
2846
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002847static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002848 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002849{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002850 struct station_info sinfo;
2851 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002852 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002853 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002854 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002855 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002856
Johannes Berg67748892010-10-04 21:14:06 +02002857 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2858 if (err)
2859 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002860
2861 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002862 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002863 goto out_err;
2864 }
2865
Johannes Bergbba95fe2008-07-29 13:22:51 +02002866 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002867 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002868 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2869 mac_addr, &sinfo);
2870 if (err == -ENOENT)
2871 break;
2872 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002873 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002874
2875 if (nl80211_send_station(skb,
2876 NETLINK_CB(cb->skb).pid,
2877 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002878 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002879 &sinfo) < 0)
2880 goto out;
2881
2882 sta_idx++;
2883 }
2884
2885
2886 out:
2887 cb->args[1] = sta_idx;
2888 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002889 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002890 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002891
2892 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002893}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002894
Johannes Berg5727ef12007-12-19 02:03:34 +01002895static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2896{
Johannes Berg4c476992010-10-04 21:36:35 +02002897 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2898 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002899 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002900 struct sk_buff *msg;
2901 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002902 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002903
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002904 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002905
2906 if (!info->attrs[NL80211_ATTR_MAC])
2907 return -EINVAL;
2908
2909 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2910
Johannes Berg4c476992010-10-04 21:36:35 +02002911 if (!rdev->ops->get_station)
2912 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002913
Johannes Berg79c97e92009-07-07 03:56:12 +02002914 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002915 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002916 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002917
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002918 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002919 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002920 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002921
2922 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002923 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002924 nlmsg_free(msg);
2925 return -ENOBUFS;
2926 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002927
Johannes Berg4c476992010-10-04 21:36:35 +02002928 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002929}
2930
2931/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002932 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002933 */
Johannes Berg80b99892011-11-18 16:23:01 +01002934static struct net_device *get_vlan(struct genl_info *info,
2935 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002936{
Johannes Berg463d0182009-07-14 00:33:35 +02002937 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002938 struct net_device *v;
2939 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002940
Johannes Berg80b99892011-11-18 16:23:01 +01002941 if (!vlanattr)
2942 return NULL;
2943
2944 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2945 if (!v)
2946 return ERR_PTR(-ENODEV);
2947
2948 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2949 ret = -EINVAL;
2950 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002951 }
Johannes Berg80b99892011-11-18 16:23:01 +01002952
2953 if (!netif_running(v)) {
2954 ret = -ENETDOWN;
2955 goto error;
2956 }
2957
2958 return v;
2959 error:
2960 dev_put(v);
2961 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002962}
2963
2964static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2965{
Johannes Berg4c476992010-10-04 21:36:35 +02002966 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002967 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002968 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002969 struct station_parameters params;
2970 u8 *mac_addr = NULL;
2971
2972 memset(&params, 0, sizeof(params));
2973
2974 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002975 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002976
2977 if (info->attrs[NL80211_ATTR_STA_AID])
2978 return -EINVAL;
2979
2980 if (!info->attrs[NL80211_ATTR_MAC])
2981 return -EINVAL;
2982
2983 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2984
2985 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2986 params.supported_rates =
2987 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2988 params.supported_rates_len =
2989 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2990 }
2991
2992 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2993 params.listen_interval =
2994 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2995
Jouni Malinen36aedc902008-08-25 11:58:58 +03002996 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2997 params.ht_capa =
2998 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2999
Johannes Bergbdd90d52011-12-14 12:20:27 +01003000 if (!rdev->ops->change_station)
3001 return -EOPNOTSUPP;
3002
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003003 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003004 return -EINVAL;
3005
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003006 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3007 params.plink_action =
3008 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3009
Javier Cardona9c3990a2011-05-03 16:57:11 -07003010 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3011 params.plink_state =
3012 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3013
Johannes Berga97f4422009-06-18 17:23:43 +02003014 switch (dev->ieee80211_ptr->iftype) {
3015 case NL80211_IFTYPE_AP:
3016 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003017 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003018 /* disallow mesh-specific things */
3019 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003020 return -EINVAL;
3021
3022 /* TDLS can't be set, ... */
3023 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3024 return -EINVAL;
3025 /*
3026 * ... but don't bother the driver with it. This works around
3027 * a hostapd/wpa_supplicant issue -- it always includes the
3028 * TLDS_PEER flag in the mask even for AP mode.
3029 */
3030 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3031
3032 /* accept only the listed bits */
3033 if (params.sta_flags_mask &
3034 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3035 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3036 BIT(NL80211_STA_FLAG_WME) |
3037 BIT(NL80211_STA_FLAG_MFP)))
3038 return -EINVAL;
3039
3040 /* must be last in here for error handling */
3041 params.vlan = get_vlan(info, rdev);
3042 if (IS_ERR(params.vlan))
3043 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003044 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003045 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003046 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003047 /*
3048 * Don't allow userspace to change the TDLS_PEER flag,
3049 * but silently ignore attempts to change it since we
3050 * don't have state here to verify that it doesn't try
3051 * to change the flag.
3052 */
3053 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003054 /* fall through */
3055 case NL80211_IFTYPE_ADHOC:
3056 /* disallow things sta doesn't support */
3057 if (params.plink_action)
3058 return -EINVAL;
3059 if (params.ht_capa)
3060 return -EINVAL;
3061 if (params.listen_interval >= 0)
3062 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003063 /* reject any changes other than AUTHORIZED */
3064 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3065 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003066 break;
3067 case NL80211_IFTYPE_MESH_POINT:
3068 /* disallow things mesh doesn't support */
3069 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003070 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003071 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003072 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003073 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003074 return -EINVAL;
3075 /*
3076 * No special handling for TDLS here -- the userspace
3077 * mesh code doesn't have this bug.
3078 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003079 if (params.sta_flags_mask &
3080 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003081 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003082 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003083 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003084 break;
3085 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003086 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003087 }
3088
Johannes Bergbdd90d52011-12-14 12:20:27 +01003089 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003090
Johannes Berg79c97e92009-07-07 03:56:12 +02003091 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003092
Johannes Berg5727ef12007-12-19 02:03:34 +01003093 if (params.vlan)
3094 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003095
Johannes Berg5727ef12007-12-19 02:03:34 +01003096 return err;
3097}
3098
Eliad Pellerc75786c2011-08-23 14:37:46 +03003099static struct nla_policy
3100nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3101 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3102 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3103};
3104
Johannes Berg5727ef12007-12-19 02:03:34 +01003105static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3106{
Johannes Berg4c476992010-10-04 21:36:35 +02003107 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003108 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003109 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003110 struct station_parameters params;
3111 u8 *mac_addr = NULL;
3112
3113 memset(&params, 0, sizeof(params));
3114
3115 if (!info->attrs[NL80211_ATTR_MAC])
3116 return -EINVAL;
3117
Johannes Berg5727ef12007-12-19 02:03:34 +01003118 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3119 return -EINVAL;
3120
3121 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3122 return -EINVAL;
3123
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003124 if (!info->attrs[NL80211_ATTR_STA_AID])
3125 return -EINVAL;
3126
Johannes Berg5727ef12007-12-19 02:03:34 +01003127 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3128 params.supported_rates =
3129 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3130 params.supported_rates_len =
3131 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3132 params.listen_interval =
3133 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003134
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003135 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3136 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3137 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003138
Jouni Malinen36aedc902008-08-25 11:58:58 +03003139 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3140 params.ht_capa =
3141 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003142
Javier Cardona96b78df2011-04-07 15:08:33 -07003143 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3144 params.plink_action =
3145 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3146
Johannes Bergbdd90d52011-12-14 12:20:27 +01003147 if (!rdev->ops->add_station)
3148 return -EOPNOTSUPP;
3149
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003150 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003151 return -EINVAL;
3152
Johannes Bergbdd90d52011-12-14 12:20:27 +01003153 switch (dev->ieee80211_ptr->iftype) {
3154 case NL80211_IFTYPE_AP:
3155 case NL80211_IFTYPE_AP_VLAN:
3156 case NL80211_IFTYPE_P2P_GO:
3157 /* parse WME attributes if sta is WME capable */
3158 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3159 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3160 info->attrs[NL80211_ATTR_STA_WME]) {
3161 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3162 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003163
Johannes Bergbdd90d52011-12-14 12:20:27 +01003164 nla = info->attrs[NL80211_ATTR_STA_WME];
3165 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3166 nl80211_sta_wme_policy);
3167 if (err)
3168 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003169
Johannes Bergbdd90d52011-12-14 12:20:27 +01003170 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3171 params.uapsd_queues =
3172 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3173 if (params.uapsd_queues &
3174 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3175 return -EINVAL;
3176
3177 if (tb[NL80211_STA_WME_MAX_SP])
3178 params.max_sp =
3179 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3180
3181 if (params.max_sp &
3182 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3183 return -EINVAL;
3184
3185 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3186 }
3187 /* TDLS peers cannot be added */
3188 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003189 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003190 /* but don't bother the driver with it */
3191 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003192
Johannes Bergbdd90d52011-12-14 12:20:27 +01003193 /* must be last in here for error handling */
3194 params.vlan = get_vlan(info, rdev);
3195 if (IS_ERR(params.vlan))
3196 return PTR_ERR(params.vlan);
3197 break;
3198 case NL80211_IFTYPE_MESH_POINT:
3199 /* TDLS peers cannot be added */
3200 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003201 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003202 break;
3203 case NL80211_IFTYPE_STATION:
3204 /* Only TDLS peers can be added */
3205 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3206 return -EINVAL;
3207 /* Can only add if TDLS ... */
3208 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3209 return -EOPNOTSUPP;
3210 /* ... with external setup is supported */
3211 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3212 return -EOPNOTSUPP;
3213 break;
3214 default:
3215 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003216 }
3217
Johannes Bergbdd90d52011-12-14 12:20:27 +01003218 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003219
Johannes Berg79c97e92009-07-07 03:56:12 +02003220 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003221
Johannes Berg5727ef12007-12-19 02:03:34 +01003222 if (params.vlan)
3223 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003224 return err;
3225}
3226
3227static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3228{
Johannes Berg4c476992010-10-04 21:36:35 +02003229 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3230 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003231 u8 *mac_addr = NULL;
3232
3233 if (info->attrs[NL80211_ATTR_MAC])
3234 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3235
Johannes Berge80cf852009-05-11 14:43:13 +02003236 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003237 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003238 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003239 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3240 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003241
Johannes Berg4c476992010-10-04 21:36:35 +02003242 if (!rdev->ops->del_station)
3243 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003244
Johannes Berg4c476992010-10-04 21:36:35 +02003245 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003246}
3247
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003248static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3249 int flags, struct net_device *dev,
3250 u8 *dst, u8 *next_hop,
3251 struct mpath_info *pinfo)
3252{
3253 void *hdr;
3254 struct nlattr *pinfoattr;
3255
3256 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3257 if (!hdr)
3258 return -1;
3259
David S. Miller9360ffd2012-03-29 04:41:26 -04003260 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3261 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3262 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3263 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3264 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003265
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003266 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3267 if (!pinfoattr)
3268 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003269 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3270 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3271 pinfo->frame_qlen))
3272 goto nla_put_failure;
3273 if (((pinfo->filled & MPATH_INFO_SN) &&
3274 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3275 ((pinfo->filled & MPATH_INFO_METRIC) &&
3276 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3277 pinfo->metric)) ||
3278 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3279 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3280 pinfo->exptime)) ||
3281 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3282 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3283 pinfo->flags)) ||
3284 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3285 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3286 pinfo->discovery_timeout)) ||
3287 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3288 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3289 pinfo->discovery_retries)))
3290 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003291
3292 nla_nest_end(msg, pinfoattr);
3293
3294 return genlmsg_end(msg, hdr);
3295
3296 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003297 genlmsg_cancel(msg, hdr);
3298 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003299}
3300
3301static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003302 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003303{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003304 struct mpath_info pinfo;
3305 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003306 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003307 u8 dst[ETH_ALEN];
3308 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003309 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003310 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003311
Johannes Berg67748892010-10-04 21:14:06 +02003312 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3313 if (err)
3314 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003315
3316 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003317 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003318 goto out_err;
3319 }
3320
Jouni Malineneec60b02009-03-20 21:21:19 +02003321 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3322 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003323 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003324 }
3325
Johannes Bergbba95fe2008-07-29 13:22:51 +02003326 while (1) {
3327 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3328 dst, next_hop, &pinfo);
3329 if (err == -ENOENT)
3330 break;
3331 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003332 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003333
3334 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3335 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3336 netdev, dst, next_hop,
3337 &pinfo) < 0)
3338 goto out;
3339
3340 path_idx++;
3341 }
3342
3343
3344 out:
3345 cb->args[1] = path_idx;
3346 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003347 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003348 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003349 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003350}
3351
3352static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3353{
Johannes Berg4c476992010-10-04 21:36:35 +02003354 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003355 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003356 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003357 struct mpath_info pinfo;
3358 struct sk_buff *msg;
3359 u8 *dst = NULL;
3360 u8 next_hop[ETH_ALEN];
3361
3362 memset(&pinfo, 0, sizeof(pinfo));
3363
3364 if (!info->attrs[NL80211_ATTR_MAC])
3365 return -EINVAL;
3366
3367 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3368
Johannes Berg4c476992010-10-04 21:36:35 +02003369 if (!rdev->ops->get_mpath)
3370 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003371
Johannes Berg4c476992010-10-04 21:36:35 +02003372 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3373 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003374
Johannes Berg79c97e92009-07-07 03:56:12 +02003375 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003376 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003377 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003378
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003380 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003381 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003382
3383 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003384 dev, dst, next_hop, &pinfo) < 0) {
3385 nlmsg_free(msg);
3386 return -ENOBUFS;
3387 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003388
Johannes Berg4c476992010-10-04 21:36:35 +02003389 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003390}
3391
3392static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3393{
Johannes Berg4c476992010-10-04 21:36:35 +02003394 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3395 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003396 u8 *dst = NULL;
3397 u8 *next_hop = NULL;
3398
3399 if (!info->attrs[NL80211_ATTR_MAC])
3400 return -EINVAL;
3401
3402 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3403 return -EINVAL;
3404
3405 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3406 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3407
Johannes Berg4c476992010-10-04 21:36:35 +02003408 if (!rdev->ops->change_mpath)
3409 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003410
Johannes Berg4c476992010-10-04 21:36:35 +02003411 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3412 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003413
Johannes Berg4c476992010-10-04 21:36:35 +02003414 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003415}
Johannes Berg4c476992010-10-04 21:36:35 +02003416
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003417static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3418{
Johannes Berg4c476992010-10-04 21:36:35 +02003419 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3420 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003421 u8 *dst = NULL;
3422 u8 *next_hop = NULL;
3423
3424 if (!info->attrs[NL80211_ATTR_MAC])
3425 return -EINVAL;
3426
3427 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3428 return -EINVAL;
3429
3430 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3431 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3432
Johannes Berg4c476992010-10-04 21:36:35 +02003433 if (!rdev->ops->add_mpath)
3434 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003435
Johannes Berg4c476992010-10-04 21:36:35 +02003436 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3437 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003438
Johannes Berg4c476992010-10-04 21:36:35 +02003439 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003440}
3441
3442static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3443{
Johannes Berg4c476992010-10-04 21:36:35 +02003444 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3445 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003446 u8 *dst = NULL;
3447
3448 if (info->attrs[NL80211_ATTR_MAC])
3449 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3450
Johannes Berg4c476992010-10-04 21:36:35 +02003451 if (!rdev->ops->del_mpath)
3452 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003453
Johannes Berg4c476992010-10-04 21:36:35 +02003454 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003455}
3456
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003457static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3458{
Johannes Berg4c476992010-10-04 21:36:35 +02003459 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3460 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003461 struct bss_parameters params;
3462
3463 memset(&params, 0, sizeof(params));
3464 /* default to not changing parameters */
3465 params.use_cts_prot = -1;
3466 params.use_short_preamble = -1;
3467 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003468 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003469 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003470
3471 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3472 params.use_cts_prot =
3473 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3474 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3475 params.use_short_preamble =
3476 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3477 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3478 params.use_short_slot_time =
3479 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003480 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3481 params.basic_rates =
3482 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3483 params.basic_rates_len =
3484 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3485 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003486 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3487 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003488 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3489 params.ht_opmode =
3490 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003491
Johannes Berg4c476992010-10-04 21:36:35 +02003492 if (!rdev->ops->change_bss)
3493 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003494
Johannes Berg074ac8d2010-09-16 14:58:22 +02003495 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003496 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3497 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003498
Johannes Berg4c476992010-10-04 21:36:35 +02003499 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003500}
3501
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003502static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003503 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3504 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3505 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3506 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3507 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3508 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3509};
3510
3511static int parse_reg_rule(struct nlattr *tb[],
3512 struct ieee80211_reg_rule *reg_rule)
3513{
3514 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3515 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3516
3517 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3518 return -EINVAL;
3519 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3520 return -EINVAL;
3521 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3522 return -EINVAL;
3523 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3524 return -EINVAL;
3525 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3526 return -EINVAL;
3527
3528 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3529
3530 freq_range->start_freq_khz =
3531 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3532 freq_range->end_freq_khz =
3533 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3534 freq_range->max_bandwidth_khz =
3535 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3536
3537 power_rule->max_eirp =
3538 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3539
3540 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3541 power_rule->max_antenna_gain =
3542 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3543
3544 return 0;
3545}
3546
3547static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3548{
3549 int r;
3550 char *data = NULL;
3551
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003552 /*
3553 * You should only get this when cfg80211 hasn't yet initialized
3554 * completely when built-in to the kernel right between the time
3555 * window between nl80211_init() and regulatory_init(), if that is
3556 * even possible.
3557 */
3558 mutex_lock(&cfg80211_mutex);
3559 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003560 mutex_unlock(&cfg80211_mutex);
3561 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003562 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003563 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003564
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003565 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3566 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003567
3568 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3569
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003570 r = regulatory_hint_user(data);
3571
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003572 return r;
3573}
3574
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003575static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003576 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003577{
Johannes Berg4c476992010-10-04 21:36:35 +02003578 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003579 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003580 struct wireless_dev *wdev = dev->ieee80211_ptr;
3581 struct mesh_config cur_params;
3582 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003583 void *hdr;
3584 struct nlattr *pinfoattr;
3585 struct sk_buff *msg;
3586
Johannes Berg29cbe682010-12-03 09:20:44 +01003587 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3588 return -EOPNOTSUPP;
3589
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003590 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003591 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003592
Johannes Berg29cbe682010-12-03 09:20:44 +01003593 wdev_lock(wdev);
3594 /* If not connected, get default parameters */
3595 if (!wdev->mesh_id_len)
3596 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3597 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003598 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003599 &cur_params);
3600 wdev_unlock(wdev);
3601
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003602 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003603 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003604
3605 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003606 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003607 if (!msg)
3608 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003609 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003610 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003611 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003612 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003613 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003614 if (!pinfoattr)
3615 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003616 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3617 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3618 cur_params.dot11MeshRetryTimeout) ||
3619 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3620 cur_params.dot11MeshConfirmTimeout) ||
3621 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3622 cur_params.dot11MeshHoldingTimeout) ||
3623 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3624 cur_params.dot11MeshMaxPeerLinks) ||
3625 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3626 cur_params.dot11MeshMaxRetries) ||
3627 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3628 cur_params.dot11MeshTTL) ||
3629 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3630 cur_params.element_ttl) ||
3631 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3632 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003633 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3634 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003635 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3636 cur_params.dot11MeshHWMPmaxPREQretries) ||
3637 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3638 cur_params.path_refresh_time) ||
3639 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3640 cur_params.min_discovery_timeout) ||
3641 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3642 cur_params.dot11MeshHWMPactivePathTimeout) ||
3643 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3644 cur_params.dot11MeshHWMPpreqMinInterval) ||
3645 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3646 cur_params.dot11MeshHWMPperrMinInterval) ||
3647 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3648 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3649 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3650 cur_params.dot11MeshHWMPRootMode) ||
3651 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3652 cur_params.dot11MeshHWMPRannInterval) ||
3653 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3654 cur_params.dot11MeshGateAnnouncementProtocol) ||
3655 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3656 cur_params.dot11MeshForwarding) ||
3657 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003658 cur_params.rssi_threshold) ||
3659 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003660 cur_params.ht_opmode) ||
3661 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3662 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3663 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003664 cur_params.dot11MeshHWMProotInterval) ||
3665 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3666 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003667 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003668 nla_nest_end(msg, pinfoattr);
3669 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003670 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003671
Johannes Berg3b858752009-03-12 09:55:09 +01003672 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003673 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003674 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003675 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003676 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003677}
3678
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003679static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003680 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3681 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3682 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3683 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3684 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3685 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003686 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003687 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003688 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003689 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3690 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3691 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3692 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3693 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003694 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003695 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003696 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003697 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003698 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003699 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003700 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3701 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003702 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3703 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003704 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003705};
3706
Javier Cardonac80d5452010-12-16 17:37:49 -08003707static const struct nla_policy
3708 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003709 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003710 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3711 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003712 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003713 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003714 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003715 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003716};
3717
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003718static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003719 struct mesh_config *cfg,
3720 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003721{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003722 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003723 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003724
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003725#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3726do {\
3727 if (table[attr_num]) {\
3728 cfg->param = nla_fn(table[attr_num]); \
3729 mask |= (1 << (attr_num - 1)); \
3730 } \
3731} while (0);\
3732
3733
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003734 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003735 return -EINVAL;
3736 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003737 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003738 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003739 return -EINVAL;
3740
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003741 /* This makes sure that there aren't more than 32 mesh config
3742 * parameters (otherwise our bitfield scheme would not work.) */
3743 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3744
3745 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003746 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003747 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3748 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003749 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003750 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3751 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003752 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003753 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3754 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003755 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003756 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3757 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003758 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003759 mask, NL80211_MESHCONF_MAX_RETRIES,
3760 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003761 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003762 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003763 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003764 mask, NL80211_MESHCONF_ELEMENT_TTL,
3765 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003766 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003767 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3768 nla_get_u8);
3769 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3770 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3771 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003772 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003773 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3774 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003775 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003776 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3777 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003778 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003779 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3780 nla_get_u16);
3781 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3782 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3783 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003784 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003785 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3786 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003787 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003788 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3789 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003790 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003791 dot11MeshHWMPnetDiameterTraversalTime, mask,
3792 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3793 nla_get_u16);
3794 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3795 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3796 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3797 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3798 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003799 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003800 dot11MeshGateAnnouncementProtocol, mask,
3801 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3802 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003803 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003804 mask, NL80211_MESHCONF_FORWARDING,
3805 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003806 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003807 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3808 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003809 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003810 mask, NL80211_MESHCONF_HT_OPMODE,
3811 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003812 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3813 mask,
3814 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3815 nla_get_u32);
3816 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3817 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3818 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003819 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3820 dot11MeshHWMPconfirmationInterval, mask,
3821 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3822 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003823 if (mask_out)
3824 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003825
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003826 return 0;
3827
3828#undef FILL_IN_MESH_PARAM_IF_SET
3829}
3830
Javier Cardonac80d5452010-12-16 17:37:49 -08003831static int nl80211_parse_mesh_setup(struct genl_info *info,
3832 struct mesh_setup *setup)
3833{
3834 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3835
3836 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3837 return -EINVAL;
3838 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3839 info->attrs[NL80211_ATTR_MESH_SETUP],
3840 nl80211_mesh_setup_params_policy))
3841 return -EINVAL;
3842
Javier Cardonad299a1f2012-03-31 11:31:33 -07003843 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3844 setup->sync_method =
3845 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3846 IEEE80211_SYNC_METHOD_VENDOR :
3847 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3848
Javier Cardonac80d5452010-12-16 17:37:49 -08003849 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3850 setup->path_sel_proto =
3851 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3852 IEEE80211_PATH_PROTOCOL_VENDOR :
3853 IEEE80211_PATH_PROTOCOL_HWMP;
3854
3855 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3856 setup->path_metric =
3857 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3858 IEEE80211_PATH_METRIC_VENDOR :
3859 IEEE80211_PATH_METRIC_AIRTIME;
3860
Javier Cardona581a8b02011-04-07 15:08:27 -07003861
3862 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003863 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003864 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003865 if (!is_valid_ie_attr(ieattr))
3866 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003867 setup->ie = nla_data(ieattr);
3868 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003869 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003870 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3871 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003872
3873 return 0;
3874}
3875
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003876static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003877 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003878{
3879 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3880 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003881 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003882 struct mesh_config cfg;
3883 u32 mask;
3884 int err;
3885
Johannes Berg29cbe682010-12-03 09:20:44 +01003886 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3887 return -EOPNOTSUPP;
3888
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003889 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003890 return -EOPNOTSUPP;
3891
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003892 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003893 if (err)
3894 return err;
3895
Johannes Berg29cbe682010-12-03 09:20:44 +01003896 wdev_lock(wdev);
3897 if (!wdev->mesh_id_len)
3898 err = -ENOLINK;
3899
3900 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003901 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003902 mask, &cfg);
3903
3904 wdev_unlock(wdev);
3905
3906 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003907}
3908
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003909static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3910{
3911 struct sk_buff *msg;
3912 void *hdr = NULL;
3913 struct nlattr *nl_reg_rules;
3914 unsigned int i;
3915 int err = -EINVAL;
3916
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003917 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003918
3919 if (!cfg80211_regdomain)
3920 goto out;
3921
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003922 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003923 if (!msg) {
3924 err = -ENOBUFS;
3925 goto out;
3926 }
3927
3928 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3929 NL80211_CMD_GET_REG);
3930 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003931 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003932
David S. Miller9360ffd2012-03-29 04:41:26 -04003933 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3934 cfg80211_regdomain->alpha2) ||
3935 (cfg80211_regdomain->dfs_region &&
3936 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3937 cfg80211_regdomain->dfs_region)))
3938 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003939
3940 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3941 if (!nl_reg_rules)
3942 goto nla_put_failure;
3943
3944 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3945 struct nlattr *nl_reg_rule;
3946 const struct ieee80211_reg_rule *reg_rule;
3947 const struct ieee80211_freq_range *freq_range;
3948 const struct ieee80211_power_rule *power_rule;
3949
3950 reg_rule = &cfg80211_regdomain->reg_rules[i];
3951 freq_range = &reg_rule->freq_range;
3952 power_rule = &reg_rule->power_rule;
3953
3954 nl_reg_rule = nla_nest_start(msg, i);
3955 if (!nl_reg_rule)
3956 goto nla_put_failure;
3957
David S. Miller9360ffd2012-03-29 04:41:26 -04003958 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3959 reg_rule->flags) ||
3960 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3961 freq_range->start_freq_khz) ||
3962 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3963 freq_range->end_freq_khz) ||
3964 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3965 freq_range->max_bandwidth_khz) ||
3966 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3967 power_rule->max_antenna_gain) ||
3968 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3969 power_rule->max_eirp))
3970 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003971
3972 nla_nest_end(msg, nl_reg_rule);
3973 }
3974
3975 nla_nest_end(msg, nl_reg_rules);
3976
3977 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003978 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003979 goto out;
3980
3981nla_put_failure:
3982 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003983put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003984 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003985 err = -EMSGSIZE;
3986out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003987 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003988 return err;
3989}
3990
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003991static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3992{
3993 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3994 struct nlattr *nl_reg_rule;
3995 char *alpha2 = NULL;
3996 int rem_reg_rules = 0, r = 0;
3997 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07003998 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003999 struct ieee80211_regdomain *rd = NULL;
4000
4001 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4002 return -EINVAL;
4003
4004 if (!info->attrs[NL80211_ATTR_REG_RULES])
4005 return -EINVAL;
4006
4007 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4008
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004009 if (info->attrs[NL80211_ATTR_DFS_REGION])
4010 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4011
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004012 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4013 rem_reg_rules) {
4014 num_rules++;
4015 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004016 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004017 }
4018
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004019 mutex_lock(&cfg80211_mutex);
4020
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004021 if (!reg_is_valid_request(alpha2)) {
4022 r = -EINVAL;
4023 goto bad_reg;
4024 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004025
4026 size_of_regd = sizeof(struct ieee80211_regdomain) +
4027 (num_rules * sizeof(struct ieee80211_reg_rule));
4028
4029 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004030 if (!rd) {
4031 r = -ENOMEM;
4032 goto bad_reg;
4033 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004034
4035 rd->n_reg_rules = num_rules;
4036 rd->alpha2[0] = alpha2[0];
4037 rd->alpha2[1] = alpha2[1];
4038
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004039 /*
4040 * Disable DFS master mode if the DFS region was
4041 * not supported or known on this kernel.
4042 */
4043 if (reg_supported_dfs_region(dfs_region))
4044 rd->dfs_region = dfs_region;
4045
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004046 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4047 rem_reg_rules) {
4048 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4049 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4050 reg_rule_policy);
4051 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4052 if (r)
4053 goto bad_reg;
4054
4055 rule_idx++;
4056
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004057 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4058 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004059 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004060 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004061 }
4062
4063 BUG_ON(rule_idx != num_rules);
4064
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004065 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004066
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004067 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004068
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004069 return r;
4070
Johannes Bergd2372b32008-10-24 20:32:20 +02004071 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004072 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004073 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004074 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004075}
4076
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004077static int validate_scan_freqs(struct nlattr *freqs)
4078{
4079 struct nlattr *attr1, *attr2;
4080 int n_channels = 0, tmp1, tmp2;
4081
4082 nla_for_each_nested(attr1, freqs, tmp1) {
4083 n_channels++;
4084 /*
4085 * Some hardware has a limited channel list for
4086 * scanning, and it is pretty much nonsensical
4087 * to scan for a channel twice, so disallow that
4088 * and don't require drivers to check that the
4089 * channel list they get isn't longer than what
4090 * they can scan, as long as they can scan all
4091 * the channels they registered at once.
4092 */
4093 nla_for_each_nested(attr2, freqs, tmp2)
4094 if (attr1 != attr2 &&
4095 nla_get_u32(attr1) == nla_get_u32(attr2))
4096 return 0;
4097 }
4098
4099 return n_channels;
4100}
4101
Johannes Berg2a519312009-02-10 21:25:55 +01004102static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4103{
Johannes Berg4c476992010-10-04 21:36:35 +02004104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4105 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004106 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004107 struct nlattr *attr;
4108 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004109 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004110 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004111
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004112 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4113 return -EINVAL;
4114
Johannes Berg79c97e92009-07-07 03:56:12 +02004115 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004116
Johannes Berg4c476992010-10-04 21:36:35 +02004117 if (!rdev->ops->scan)
4118 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004119
Johannes Berg4c476992010-10-04 21:36:35 +02004120 if (rdev->scan_req)
4121 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004122
4123 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004124 n_channels = validate_scan_freqs(
4125 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004126 if (!n_channels)
4127 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004128 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004129 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004130 n_channels = 0;
4131
Johannes Berg2a519312009-02-10 21:25:55 +01004132 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4133 if (wiphy->bands[band])
4134 n_channels += wiphy->bands[band]->n_channels;
4135 }
4136
4137 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4138 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4139 n_ssids++;
4140
Johannes Berg4c476992010-10-04 21:36:35 +02004141 if (n_ssids > wiphy->max_scan_ssids)
4142 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004143
Jouni Malinen70692ad2009-02-16 19:39:13 +02004144 if (info->attrs[NL80211_ATTR_IE])
4145 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4146 else
4147 ie_len = 0;
4148
Johannes Berg4c476992010-10-04 21:36:35 +02004149 if (ie_len > wiphy->max_scan_ie_len)
4150 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004151
Johannes Berg2a519312009-02-10 21:25:55 +01004152 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004153 + sizeof(*request->ssids) * n_ssids
4154 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004155 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004156 if (!request)
4157 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004158
Johannes Berg2a519312009-02-10 21:25:55 +01004159 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004160 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004161 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004162 if (ie_len) {
4163 if (request->ssids)
4164 request->ie = (void *)(request->ssids + n_ssids);
4165 else
4166 request->ie = (void *)(request->channels + n_channels);
4167 }
Johannes Berg2a519312009-02-10 21:25:55 +01004168
Johannes Berg584991d2009-11-02 13:32:03 +01004169 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004170 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4171 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004172 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004173 struct ieee80211_channel *chan;
4174
4175 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4176
4177 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004178 err = -EINVAL;
4179 goto out_free;
4180 }
Johannes Berg584991d2009-11-02 13:32:03 +01004181
4182 /* ignore disabled channels */
4183 if (chan->flags & IEEE80211_CHAN_DISABLED)
4184 continue;
4185
4186 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004187 i++;
4188 }
4189 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004190 enum ieee80211_band band;
4191
Johannes Berg2a519312009-02-10 21:25:55 +01004192 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004193 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4194 int j;
4195 if (!wiphy->bands[band])
4196 continue;
4197 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004198 struct ieee80211_channel *chan;
4199
4200 chan = &wiphy->bands[band]->channels[j];
4201
4202 if (chan->flags & IEEE80211_CHAN_DISABLED)
4203 continue;
4204
4205 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004206 i++;
4207 }
4208 }
4209 }
4210
Johannes Berg584991d2009-11-02 13:32:03 +01004211 if (!i) {
4212 err = -EINVAL;
4213 goto out_free;
4214 }
4215
4216 request->n_channels = i;
4217
Johannes Berg2a519312009-02-10 21:25:55 +01004218 i = 0;
4219 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4220 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004221 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004222 err = -EINVAL;
4223 goto out_free;
4224 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004225 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004226 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004227 i++;
4228 }
4229 }
4230
Jouni Malinen70692ad2009-02-16 19:39:13 +02004231 if (info->attrs[NL80211_ATTR_IE]) {
4232 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004233 memcpy((void *)request->ie,
4234 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004235 request->ie_len);
4236 }
4237
Johannes Berg34850ab2011-07-18 18:08:35 +02004238 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004239 if (wiphy->bands[i])
4240 request->rates[i] =
4241 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004242
4243 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4244 nla_for_each_nested(attr,
4245 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4246 tmp) {
4247 enum ieee80211_band band = nla_type(attr);
4248
Dan Carpenter84404622011-07-29 11:52:18 +03004249 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004250 err = -EINVAL;
4251 goto out_free;
4252 }
4253 err = ieee80211_get_ratemask(wiphy->bands[band],
4254 nla_data(attr),
4255 nla_len(attr),
4256 &request->rates[band]);
4257 if (err)
4258 goto out_free;
4259 }
4260 }
4261
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304262 request->no_cck =
4263 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4264
Johannes Berg463d0182009-07-14 00:33:35 +02004265 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004266 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004267
Johannes Berg79c97e92009-07-07 03:56:12 +02004268 rdev->scan_req = request;
4269 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004270
Johannes Berg463d0182009-07-14 00:33:35 +02004271 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02004272 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02004273 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02004274 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004275 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004276 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004277 kfree(request);
4278 }
Johannes Berg3b858752009-03-12 09:55:09 +01004279
Johannes Berg2a519312009-02-10 21:25:55 +01004280 return err;
4281}
4282
Luciano Coelho807f8a82011-05-11 17:09:35 +03004283static int nl80211_start_sched_scan(struct sk_buff *skb,
4284 struct genl_info *info)
4285{
4286 struct cfg80211_sched_scan_request *request;
4287 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4288 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004289 struct nlattr *attr;
4290 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004291 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004292 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004293 enum ieee80211_band band;
4294 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004295 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004296
4297 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4298 !rdev->ops->sched_scan_start)
4299 return -EOPNOTSUPP;
4300
4301 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4302 return -EINVAL;
4303
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004304 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4305 return -EINVAL;
4306
4307 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4308 if (interval == 0)
4309 return -EINVAL;
4310
Luciano Coelho807f8a82011-05-11 17:09:35 +03004311 wiphy = &rdev->wiphy;
4312
4313 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4314 n_channels = validate_scan_freqs(
4315 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4316 if (!n_channels)
4317 return -EINVAL;
4318 } else {
4319 n_channels = 0;
4320
4321 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4322 if (wiphy->bands[band])
4323 n_channels += wiphy->bands[band]->n_channels;
4324 }
4325
4326 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4327 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4328 tmp)
4329 n_ssids++;
4330
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004331 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004332 return -EINVAL;
4333
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004334 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4335 nla_for_each_nested(attr,
4336 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4337 tmp)
4338 n_match_sets++;
4339
4340 if (n_match_sets > wiphy->max_match_sets)
4341 return -EINVAL;
4342
Luciano Coelho807f8a82011-05-11 17:09:35 +03004343 if (info->attrs[NL80211_ATTR_IE])
4344 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4345 else
4346 ie_len = 0;
4347
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004348 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004349 return -EINVAL;
4350
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004351 mutex_lock(&rdev->sched_scan_mtx);
4352
4353 if (rdev->sched_scan_req) {
4354 err = -EINPROGRESS;
4355 goto out;
4356 }
4357
Luciano Coelho807f8a82011-05-11 17:09:35 +03004358 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004359 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004360 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004361 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004362 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004363 if (!request) {
4364 err = -ENOMEM;
4365 goto out;
4366 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004367
4368 if (n_ssids)
4369 request->ssids = (void *)&request->channels[n_channels];
4370 request->n_ssids = n_ssids;
4371 if (ie_len) {
4372 if (request->ssids)
4373 request->ie = (void *)(request->ssids + n_ssids);
4374 else
4375 request->ie = (void *)(request->channels + n_channels);
4376 }
4377
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004378 if (n_match_sets) {
4379 if (request->ie)
4380 request->match_sets = (void *)(request->ie + ie_len);
4381 else if (request->ssids)
4382 request->match_sets =
4383 (void *)(request->ssids + n_ssids);
4384 else
4385 request->match_sets =
4386 (void *)(request->channels + n_channels);
4387 }
4388 request->n_match_sets = n_match_sets;
4389
Luciano Coelho807f8a82011-05-11 17:09:35 +03004390 i = 0;
4391 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4392 /* user specified, bail out if channel not found */
4393 nla_for_each_nested(attr,
4394 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4395 tmp) {
4396 struct ieee80211_channel *chan;
4397
4398 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4399
4400 if (!chan) {
4401 err = -EINVAL;
4402 goto out_free;
4403 }
4404
4405 /* ignore disabled channels */
4406 if (chan->flags & IEEE80211_CHAN_DISABLED)
4407 continue;
4408
4409 request->channels[i] = chan;
4410 i++;
4411 }
4412 } else {
4413 /* all channels */
4414 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4415 int j;
4416 if (!wiphy->bands[band])
4417 continue;
4418 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4419 struct ieee80211_channel *chan;
4420
4421 chan = &wiphy->bands[band]->channels[j];
4422
4423 if (chan->flags & IEEE80211_CHAN_DISABLED)
4424 continue;
4425
4426 request->channels[i] = chan;
4427 i++;
4428 }
4429 }
4430 }
4431
4432 if (!i) {
4433 err = -EINVAL;
4434 goto out_free;
4435 }
4436
4437 request->n_channels = i;
4438
4439 i = 0;
4440 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4441 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4442 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004443 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004444 err = -EINVAL;
4445 goto out_free;
4446 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004447 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004448 memcpy(request->ssids[i].ssid, nla_data(attr),
4449 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004450 i++;
4451 }
4452 }
4453
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004454 i = 0;
4455 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4456 nla_for_each_nested(attr,
4457 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4458 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004459 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004460
4461 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4462 nla_data(attr), nla_len(attr),
4463 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004464 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004465 if (ssid) {
4466 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4467 err = -EINVAL;
4468 goto out_free;
4469 }
4470 memcpy(request->match_sets[i].ssid.ssid,
4471 nla_data(ssid), nla_len(ssid));
4472 request->match_sets[i].ssid.ssid_len =
4473 nla_len(ssid);
4474 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004475 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4476 if (rssi)
4477 request->rssi_thold = nla_get_u32(rssi);
4478 else
4479 request->rssi_thold =
4480 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004481 i++;
4482 }
4483 }
4484
Luciano Coelho807f8a82011-05-11 17:09:35 +03004485 if (info->attrs[NL80211_ATTR_IE]) {
4486 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4487 memcpy((void *)request->ie,
4488 nla_data(info->attrs[NL80211_ATTR_IE]),
4489 request->ie_len);
4490 }
4491
4492 request->dev = dev;
4493 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004494 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004495
4496 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4497 if (!err) {
4498 rdev->sched_scan_req = request;
4499 nl80211_send_sched_scan(rdev, dev,
4500 NL80211_CMD_START_SCHED_SCAN);
4501 goto out;
4502 }
4503
4504out_free:
4505 kfree(request);
4506out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004507 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004508 return err;
4509}
4510
4511static int nl80211_stop_sched_scan(struct sk_buff *skb,
4512 struct genl_info *info)
4513{
4514 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004515 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004516
4517 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4518 !rdev->ops->sched_scan_stop)
4519 return -EOPNOTSUPP;
4520
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004521 mutex_lock(&rdev->sched_scan_mtx);
4522 err = __cfg80211_stop_sched_scan(rdev, false);
4523 mutex_unlock(&rdev->sched_scan_mtx);
4524
4525 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004526}
4527
Johannes Berg9720bb32011-06-21 09:45:33 +02004528static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4529 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004530 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004531 struct wireless_dev *wdev,
4532 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004533{
Johannes Berg48ab9052009-07-10 18:42:31 +02004534 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004535 void *hdr;
4536 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004537
4538 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004539
Johannes Berg9720bb32011-06-21 09:45:33 +02004540 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004541 NL80211_CMD_NEW_SCAN_RESULTS);
4542 if (!hdr)
4543 return -1;
4544
Johannes Berg9720bb32011-06-21 09:45:33 +02004545 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4546
David S. Miller9360ffd2012-03-29 04:41:26 -04004547 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4548 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4549 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004550
4551 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4552 if (!bss)
4553 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004554 if ((!is_zero_ether_addr(res->bssid) &&
4555 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4556 (res->information_elements && res->len_information_elements &&
4557 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4558 res->len_information_elements,
4559 res->information_elements)) ||
4560 (res->beacon_ies && res->len_beacon_ies &&
4561 res->beacon_ies != res->information_elements &&
4562 nla_put(msg, NL80211_BSS_BEACON_IES,
4563 res->len_beacon_ies, res->beacon_ies)))
4564 goto nla_put_failure;
4565 if (res->tsf &&
4566 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4567 goto nla_put_failure;
4568 if (res->beacon_interval &&
4569 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4570 goto nla_put_failure;
4571 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4572 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4573 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4574 jiffies_to_msecs(jiffies - intbss->ts)))
4575 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004576
Johannes Berg77965c92009-02-18 18:45:06 +01004577 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004578 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004579 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4580 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004581 break;
4582 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004583 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4584 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004585 break;
4586 default:
4587 break;
4588 }
4589
Johannes Berg48ab9052009-07-10 18:42:31 +02004590 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004591 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004592 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004593 if (intbss == wdev->current_bss &&
4594 nla_put_u32(msg, NL80211_BSS_STATUS,
4595 NL80211_BSS_STATUS_ASSOCIATED))
4596 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004597 break;
4598 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004599 if (intbss == wdev->current_bss &&
4600 nla_put_u32(msg, NL80211_BSS_STATUS,
4601 NL80211_BSS_STATUS_IBSS_JOINED))
4602 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004603 break;
4604 default:
4605 break;
4606 }
4607
Johannes Berg2a519312009-02-10 21:25:55 +01004608 nla_nest_end(msg, bss);
4609
4610 return genlmsg_end(msg, hdr);
4611
4612 nla_put_failure:
4613 genlmsg_cancel(msg, hdr);
4614 return -EMSGSIZE;
4615}
4616
4617static int nl80211_dump_scan(struct sk_buff *skb,
4618 struct netlink_callback *cb)
4619{
Johannes Berg48ab9052009-07-10 18:42:31 +02004620 struct cfg80211_registered_device *rdev;
4621 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004622 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004623 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004624 int start = cb->args[1], idx = 0;
4625 int err;
4626
Johannes Berg67748892010-10-04 21:14:06 +02004627 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4628 if (err)
4629 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004630
Johannes Berg48ab9052009-07-10 18:42:31 +02004631 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004632
Johannes Berg48ab9052009-07-10 18:42:31 +02004633 wdev_lock(wdev);
4634 spin_lock_bh(&rdev->bss_lock);
4635 cfg80211_bss_expire(rdev);
4636
Johannes Berg9720bb32011-06-21 09:45:33 +02004637 cb->seq = rdev->bss_generation;
4638
Johannes Berg48ab9052009-07-10 18:42:31 +02004639 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004640 if (++idx <= start)
4641 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004642 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004643 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004644 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004645 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004646 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004647 }
4648 }
4649
Johannes Berg48ab9052009-07-10 18:42:31 +02004650 spin_unlock_bh(&rdev->bss_lock);
4651 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004652
4653 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004654 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004655
Johannes Berg67748892010-10-04 21:14:06 +02004656 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004657}
4658
Holger Schurig61fa7132009-11-11 12:25:40 +01004659static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4660 int flags, struct net_device *dev,
4661 struct survey_info *survey)
4662{
4663 void *hdr;
4664 struct nlattr *infoattr;
4665
Holger Schurig61fa7132009-11-11 12:25:40 +01004666 hdr = nl80211hdr_put(msg, pid, seq, flags,
4667 NL80211_CMD_NEW_SURVEY_RESULTS);
4668 if (!hdr)
4669 return -ENOMEM;
4670
David S. Miller9360ffd2012-03-29 04:41:26 -04004671 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4672 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004673
4674 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4675 if (!infoattr)
4676 goto nla_put_failure;
4677
David S. Miller9360ffd2012-03-29 04:41:26 -04004678 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4679 survey->channel->center_freq))
4680 goto nla_put_failure;
4681
4682 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4683 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4684 goto nla_put_failure;
4685 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4686 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4687 goto nla_put_failure;
4688 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4689 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4690 survey->channel_time))
4691 goto nla_put_failure;
4692 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4693 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4694 survey->channel_time_busy))
4695 goto nla_put_failure;
4696 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4697 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4698 survey->channel_time_ext_busy))
4699 goto nla_put_failure;
4700 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4701 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4702 survey->channel_time_rx))
4703 goto nla_put_failure;
4704 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4705 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4706 survey->channel_time_tx))
4707 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004708
4709 nla_nest_end(msg, infoattr);
4710
4711 return genlmsg_end(msg, hdr);
4712
4713 nla_put_failure:
4714 genlmsg_cancel(msg, hdr);
4715 return -EMSGSIZE;
4716}
4717
4718static int nl80211_dump_survey(struct sk_buff *skb,
4719 struct netlink_callback *cb)
4720{
4721 struct survey_info survey;
4722 struct cfg80211_registered_device *dev;
4723 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004724 int survey_idx = cb->args[1];
4725 int res;
4726
Johannes Berg67748892010-10-04 21:14:06 +02004727 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4728 if (res)
4729 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004730
4731 if (!dev->ops->dump_survey) {
4732 res = -EOPNOTSUPP;
4733 goto out_err;
4734 }
4735
4736 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004737 struct ieee80211_channel *chan;
4738
Holger Schurig61fa7132009-11-11 12:25:40 +01004739 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4740 &survey);
4741 if (res == -ENOENT)
4742 break;
4743 if (res)
4744 goto out_err;
4745
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004746 /* Survey without a channel doesn't make sense */
4747 if (!survey.channel) {
4748 res = -EINVAL;
4749 goto out;
4750 }
4751
4752 chan = ieee80211_get_channel(&dev->wiphy,
4753 survey.channel->center_freq);
4754 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4755 survey_idx++;
4756 continue;
4757 }
4758
Holger Schurig61fa7132009-11-11 12:25:40 +01004759 if (nl80211_send_survey(skb,
4760 NETLINK_CB(cb->skb).pid,
4761 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4762 netdev,
4763 &survey) < 0)
4764 goto out;
4765 survey_idx++;
4766 }
4767
4768 out:
4769 cb->args[1] = survey_idx;
4770 res = skb->len;
4771 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004772 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004773 return res;
4774}
4775
Jouni Malinen255e7372009-03-20 21:21:17 +02004776static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4777{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004778 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004779}
4780
Samuel Ortizb23aa672009-07-01 21:26:54 +02004781static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4782{
4783 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4784 NL80211_WPA_VERSION_2));
4785}
4786
Jouni Malinen636a5d32009-03-19 13:39:22 +02004787static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4788{
Johannes Berg4c476992010-10-04 21:36:35 +02004789 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4790 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004791 struct ieee80211_channel *chan;
4792 const u8 *bssid, *ssid, *ie = NULL;
4793 int err, ssid_len, ie_len = 0;
4794 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004795 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004796 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004797
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004798 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4799 return -EINVAL;
4800
4801 if (!info->attrs[NL80211_ATTR_MAC])
4802 return -EINVAL;
4803
Jouni Malinen17780922009-03-27 20:52:47 +02004804 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4805 return -EINVAL;
4806
Johannes Berg19957bb2009-07-02 17:20:43 +02004807 if (!info->attrs[NL80211_ATTR_SSID])
4808 return -EINVAL;
4809
4810 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4811 return -EINVAL;
4812
Johannes Bergfffd0932009-07-08 14:22:54 +02004813 err = nl80211_parse_key(info, &key);
4814 if (err)
4815 return err;
4816
4817 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004818 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4819 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004820 if (!key.p.key || !key.p.key_len)
4821 return -EINVAL;
4822 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4823 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4824 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4825 key.p.key_len != WLAN_KEY_LEN_WEP104))
4826 return -EINVAL;
4827 if (key.idx > 4)
4828 return -EINVAL;
4829 } else {
4830 key.p.key_len = 0;
4831 key.p.key = NULL;
4832 }
4833
Johannes Bergafea0b72010-08-10 09:46:42 +02004834 if (key.idx >= 0) {
4835 int i;
4836 bool ok = false;
4837 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4838 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4839 ok = true;
4840 break;
4841 }
4842 }
Johannes Berg4c476992010-10-04 21:36:35 +02004843 if (!ok)
4844 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004845 }
4846
Johannes Berg4c476992010-10-04 21:36:35 +02004847 if (!rdev->ops->auth)
4848 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004849
Johannes Berg074ac8d2010-09-16 14:58:22 +02004850 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004851 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4852 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004853
Johannes Berg19957bb2009-07-02 17:20:43 +02004854 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004855 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004856 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004857 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4858 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004859
Johannes Berg19957bb2009-07-02 17:20:43 +02004860 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4861 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4862
4863 if (info->attrs[NL80211_ATTR_IE]) {
4864 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4865 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4866 }
4867
4868 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004869 if (!nl80211_valid_auth_type(auth_type))
4870 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004871
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004872 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4873
Johannes Berg95de8172012-01-20 13:55:25 +01004874 /*
4875 * Since we no longer track auth state, ignore
4876 * requests to only change local state.
4877 */
4878 if (local_state_change)
4879 return 0;
4880
Johannes Berg4c476992010-10-04 21:36:35 +02004881 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4882 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004883 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004884}
4885
Johannes Bergc0692b82010-08-27 14:26:53 +03004886static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4887 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004888 struct cfg80211_crypto_settings *settings,
4889 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004890{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004891 memset(settings, 0, sizeof(*settings));
4892
Samuel Ortizb23aa672009-07-01 21:26:54 +02004893 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4894
Johannes Bergc0692b82010-08-27 14:26:53 +03004895 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4896 u16 proto;
4897 proto = nla_get_u16(
4898 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4899 settings->control_port_ethertype = cpu_to_be16(proto);
4900 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4901 proto != ETH_P_PAE)
4902 return -EINVAL;
4903 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4904 settings->control_port_no_encrypt = true;
4905 } else
4906 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4907
Samuel Ortizb23aa672009-07-01 21:26:54 +02004908 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4909 void *data;
4910 int len, i;
4911
4912 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4913 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4914 settings->n_ciphers_pairwise = len / sizeof(u32);
4915
4916 if (len % sizeof(u32))
4917 return -EINVAL;
4918
Johannes Berg3dc27d22009-07-02 21:36:37 +02004919 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004920 return -EINVAL;
4921
4922 memcpy(settings->ciphers_pairwise, data, len);
4923
4924 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004925 if (!cfg80211_supported_cipher_suite(
4926 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004927 settings->ciphers_pairwise[i]))
4928 return -EINVAL;
4929 }
4930
4931 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4932 settings->cipher_group =
4933 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004934 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4935 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004936 return -EINVAL;
4937 }
4938
4939 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4940 settings->wpa_versions =
4941 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4942 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4943 return -EINVAL;
4944 }
4945
4946 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4947 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004948 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004949
4950 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4951 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4952 settings->n_akm_suites = len / sizeof(u32);
4953
4954 if (len % sizeof(u32))
4955 return -EINVAL;
4956
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004957 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4958 return -EINVAL;
4959
Samuel Ortizb23aa672009-07-01 21:26:54 +02004960 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004961 }
4962
4963 return 0;
4964}
4965
Jouni Malinen636a5d32009-03-19 13:39:22 +02004966static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4967{
Johannes Berg4c476992010-10-04 21:36:35 +02004968 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4969 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004970 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004971 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004972 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004973 int err, ssid_len, ie_len = 0;
4974 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004975 u32 flags = 0;
4976 struct ieee80211_ht_cap *ht_capa = NULL;
4977 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004978
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004979 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4980 return -EINVAL;
4981
4982 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004983 !info->attrs[NL80211_ATTR_SSID] ||
4984 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004985 return -EINVAL;
4986
Johannes Berg4c476992010-10-04 21:36:35 +02004987 if (!rdev->ops->assoc)
4988 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004989
Johannes Berg074ac8d2010-09-16 14:58:22 +02004990 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004991 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4992 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004993
Johannes Berg19957bb2009-07-02 17:20:43 +02004994 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004995
Johannes Berg19957bb2009-07-02 17:20:43 +02004996 chan = ieee80211_get_channel(&rdev->wiphy,
4997 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004998 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4999 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005000
Johannes Berg19957bb2009-07-02 17:20:43 +02005001 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5002 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005003
5004 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005005 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5006 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005007 }
5008
Jouni Malinendc6382c2009-05-06 22:09:37 +03005009 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005010 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005011 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005012 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005013 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005014 else if (mfp != NL80211_MFP_NO)
5015 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005016 }
5017
Johannes Berg3e5d7642009-07-07 14:37:26 +02005018 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5019 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5020
Ben Greear7e7c8922011-11-18 11:31:59 -08005021 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5022 flags |= ASSOC_REQ_DISABLE_HT;
5023
5024 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5025 ht_capa_mask =
5026 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5027
5028 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5029 if (!ht_capa_mask)
5030 return -EINVAL;
5031 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5032 }
5033
Johannes Bergc0692b82010-08-27 14:26:53 +03005034 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005035 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005036 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5037 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005038 &crypto, flags, ht_capa,
5039 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005040
Jouni Malinen636a5d32009-03-19 13:39:22 +02005041 return err;
5042}
5043
5044static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5045{
Johannes Berg4c476992010-10-04 21:36:35 +02005046 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5047 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005048 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005049 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005050 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005051 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005052
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005053 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5054 return -EINVAL;
5055
5056 if (!info->attrs[NL80211_ATTR_MAC])
5057 return -EINVAL;
5058
5059 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5060 return -EINVAL;
5061
Johannes Berg4c476992010-10-04 21:36:35 +02005062 if (!rdev->ops->deauth)
5063 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005064
Johannes Berg074ac8d2010-09-16 14:58:22 +02005065 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005066 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5067 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005068
Johannes Berg19957bb2009-07-02 17:20:43 +02005069 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005070
Johannes Berg19957bb2009-07-02 17:20:43 +02005071 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5072 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005073 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005074 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005075 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005076
5077 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005078 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5079 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005080 }
5081
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005082 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5083
Johannes Berg4c476992010-10-04 21:36:35 +02005084 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5085 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005086}
5087
5088static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5089{
Johannes Berg4c476992010-10-04 21:36:35 +02005090 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5091 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005092 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005093 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005094 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005095 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005096
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005097 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5098 return -EINVAL;
5099
5100 if (!info->attrs[NL80211_ATTR_MAC])
5101 return -EINVAL;
5102
5103 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5104 return -EINVAL;
5105
Johannes Berg4c476992010-10-04 21:36:35 +02005106 if (!rdev->ops->disassoc)
5107 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005108
Johannes Berg074ac8d2010-09-16 14:58:22 +02005109 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005110 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5111 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005112
Johannes Berg19957bb2009-07-02 17:20:43 +02005113 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005114
Johannes Berg19957bb2009-07-02 17:20:43 +02005115 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5116 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005117 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005118 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005119 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005120
5121 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005122 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5123 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005124 }
5125
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005126 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5127
Johannes Berg4c476992010-10-04 21:36:35 +02005128 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5129 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005130}
5131
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005132static bool
5133nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5134 int mcast_rate[IEEE80211_NUM_BANDS],
5135 int rateval)
5136{
5137 struct wiphy *wiphy = &rdev->wiphy;
5138 bool found = false;
5139 int band, i;
5140
5141 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5142 struct ieee80211_supported_band *sband;
5143
5144 sband = wiphy->bands[band];
5145 if (!sband)
5146 continue;
5147
5148 for (i = 0; i < sband->n_bitrates; i++) {
5149 if (sband->bitrates[i].bitrate == rateval) {
5150 mcast_rate[band] = i + 1;
5151 found = true;
5152 break;
5153 }
5154 }
5155 }
5156
5157 return found;
5158}
5159
Johannes Berg04a773a2009-04-19 21:24:32 +02005160static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5161{
Johannes Berg4c476992010-10-04 21:36:35 +02005162 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5163 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005164 struct cfg80211_ibss_params ibss;
5165 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005166 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005167 int err;
5168
Johannes Berg8e30bc52009-04-22 17:45:38 +02005169 memset(&ibss, 0, sizeof(ibss));
5170
Johannes Berg04a773a2009-04-19 21:24:32 +02005171 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5172 return -EINVAL;
5173
5174 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5175 !info->attrs[NL80211_ATTR_SSID] ||
5176 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5177 return -EINVAL;
5178
Johannes Berg8e30bc52009-04-22 17:45:38 +02005179 ibss.beacon_interval = 100;
5180
5181 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5182 ibss.beacon_interval =
5183 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5184 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5185 return -EINVAL;
5186 }
5187
Johannes Berg4c476992010-10-04 21:36:35 +02005188 if (!rdev->ops->join_ibss)
5189 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005190
Johannes Berg4c476992010-10-04 21:36:35 +02005191 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5192 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005193
Johannes Berg79c97e92009-07-07 03:56:12 +02005194 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005195
Johannes Berg39193492011-09-16 13:45:25 +02005196 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005197 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005198
5199 if (!is_valid_ether_addr(ibss.bssid))
5200 return -EINVAL;
5201 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005202 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5203 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5204
5205 if (info->attrs[NL80211_ATTR_IE]) {
5206 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5207 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5208 }
5209
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005210 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5211 enum nl80211_channel_type channel_type;
5212
Johannes Bergcd6c6592012-05-10 21:27:18 +02005213 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005214 return -EINVAL;
5215
5216 if (channel_type != NL80211_CHAN_NO_HT &&
5217 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5218 return -EINVAL;
5219
5220 ibss.channel_type = channel_type;
5221 } else {
5222 ibss.channel_type = NL80211_CHAN_NO_HT;
5223 }
5224
5225 ibss.channel = rdev_freq_to_chan(rdev,
5226 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5227 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005228 if (!ibss.channel ||
5229 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005230 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5231 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005232
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005233 /* Both channels should be able to initiate communication */
5234 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5235 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5236 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5237 ibss.channel_type))
5238 return -EINVAL;
5239
Johannes Berg04a773a2009-04-19 21:24:32 +02005240 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005241 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005242
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005243 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5244 u8 *rates =
5245 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5246 int n_rates =
5247 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5248 struct ieee80211_supported_band *sband =
5249 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005250
Johannes Berg34850ab2011-07-18 18:08:35 +02005251 err = ieee80211_get_ratemask(sband, rates, n_rates,
5252 &ibss.basic_rates);
5253 if (err)
5254 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005255 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005256
5257 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5258 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5259 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5260 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005261
Johannes Berg4c476992010-10-04 21:36:35 +02005262 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5263 connkeys = nl80211_parse_connkeys(rdev,
5264 info->attrs[NL80211_ATTR_KEYS]);
5265 if (IS_ERR(connkeys))
5266 return PTR_ERR(connkeys);
5267 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005268
Antonio Quartulli267335d2012-01-31 20:25:47 +01005269 ibss.control_port =
5270 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5271
Johannes Berg4c476992010-10-04 21:36:35 +02005272 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005273 if (err)
5274 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005275 return err;
5276}
5277
5278static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5279{
Johannes Berg4c476992010-10-04 21:36:35 +02005280 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5281 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005282
Johannes Berg4c476992010-10-04 21:36:35 +02005283 if (!rdev->ops->leave_ibss)
5284 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005285
Johannes Berg4c476992010-10-04 21:36:35 +02005286 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5287 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005288
Johannes Berg4c476992010-10-04 21:36:35 +02005289 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005290}
5291
Johannes Bergaff89a92009-07-01 21:26:51 +02005292#ifdef CONFIG_NL80211_TESTMODE
5293static struct genl_multicast_group nl80211_testmode_mcgrp = {
5294 .name = "testmode",
5295};
5296
5297static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5298{
Johannes Berg4c476992010-10-04 21:36:35 +02005299 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005300 int err;
5301
5302 if (!info->attrs[NL80211_ATTR_TESTDATA])
5303 return -EINVAL;
5304
Johannes Bergaff89a92009-07-01 21:26:51 +02005305 err = -EOPNOTSUPP;
5306 if (rdev->ops->testmode_cmd) {
5307 rdev->testmode_info = info;
5308 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5309 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5310 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5311 rdev->testmode_info = NULL;
5312 }
5313
Johannes Bergaff89a92009-07-01 21:26:51 +02005314 return err;
5315}
5316
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005317static int nl80211_testmode_dump(struct sk_buff *skb,
5318 struct netlink_callback *cb)
5319{
Johannes Berg00918d32011-12-13 17:22:05 +01005320 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005321 int err;
5322 long phy_idx;
5323 void *data = NULL;
5324 int data_len = 0;
5325
5326 if (cb->args[0]) {
5327 /*
5328 * 0 is a valid index, but not valid for args[0],
5329 * so we need to offset by 1.
5330 */
5331 phy_idx = cb->args[0] - 1;
5332 } else {
5333 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5334 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5335 nl80211_policy);
5336 if (err)
5337 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005338
Johannes Berg2bd7e352012-06-15 14:23:16 +02005339 mutex_lock(&cfg80211_mutex);
5340 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5341 nl80211_fam.attrbuf);
5342 if (IS_ERR(rdev)) {
5343 mutex_unlock(&cfg80211_mutex);
5344 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005345 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005346 phy_idx = rdev->wiphy_idx;
5347 rdev = NULL;
5348 mutex_unlock(&cfg80211_mutex);
5349
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005350 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5351 cb->args[1] =
5352 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5353 }
5354
5355 if (cb->args[1]) {
5356 data = nla_data((void *)cb->args[1]);
5357 data_len = nla_len((void *)cb->args[1]);
5358 }
5359
5360 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005361 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5362 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005363 mutex_unlock(&cfg80211_mutex);
5364 return -ENOENT;
5365 }
Johannes Berg00918d32011-12-13 17:22:05 +01005366 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005367 mutex_unlock(&cfg80211_mutex);
5368
Johannes Berg00918d32011-12-13 17:22:05 +01005369 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005370 err = -EOPNOTSUPP;
5371 goto out_err;
5372 }
5373
5374 while (1) {
5375 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5376 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5377 NL80211_CMD_TESTMODE);
5378 struct nlattr *tmdata;
5379
David S. Miller9360ffd2012-03-29 04:41:26 -04005380 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005381 genlmsg_cancel(skb, hdr);
5382 break;
5383 }
5384
5385 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5386 if (!tmdata) {
5387 genlmsg_cancel(skb, hdr);
5388 break;
5389 }
Johannes Berg00918d32011-12-13 17:22:05 +01005390 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5391 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005392 nla_nest_end(skb, tmdata);
5393
5394 if (err == -ENOBUFS || err == -ENOENT) {
5395 genlmsg_cancel(skb, hdr);
5396 break;
5397 } else if (err) {
5398 genlmsg_cancel(skb, hdr);
5399 goto out_err;
5400 }
5401
5402 genlmsg_end(skb, hdr);
5403 }
5404
5405 err = skb->len;
5406 /* see above */
5407 cb->args[0] = phy_idx + 1;
5408 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005409 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005410 return err;
5411}
5412
Johannes Bergaff89a92009-07-01 21:26:51 +02005413static struct sk_buff *
5414__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5415 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5416{
5417 struct sk_buff *skb;
5418 void *hdr;
5419 struct nlattr *data;
5420
5421 skb = nlmsg_new(approxlen + 100, gfp);
5422 if (!skb)
5423 return NULL;
5424
5425 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5426 if (!hdr) {
5427 kfree_skb(skb);
5428 return NULL;
5429 }
5430
David S. Miller9360ffd2012-03-29 04:41:26 -04005431 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5432 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005433 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5434
5435 ((void **)skb->cb)[0] = rdev;
5436 ((void **)skb->cb)[1] = hdr;
5437 ((void **)skb->cb)[2] = data;
5438
5439 return skb;
5440
5441 nla_put_failure:
5442 kfree_skb(skb);
5443 return NULL;
5444}
5445
5446struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5447 int approxlen)
5448{
5449 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5450
5451 if (WARN_ON(!rdev->testmode_info))
5452 return NULL;
5453
5454 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5455 rdev->testmode_info->snd_pid,
5456 rdev->testmode_info->snd_seq,
5457 GFP_KERNEL);
5458}
5459EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5460
5461int cfg80211_testmode_reply(struct sk_buff *skb)
5462{
5463 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5464 void *hdr = ((void **)skb->cb)[1];
5465 struct nlattr *data = ((void **)skb->cb)[2];
5466
5467 if (WARN_ON(!rdev->testmode_info)) {
5468 kfree_skb(skb);
5469 return -EINVAL;
5470 }
5471
5472 nla_nest_end(skb, data);
5473 genlmsg_end(skb, hdr);
5474 return genlmsg_reply(skb, rdev->testmode_info);
5475}
5476EXPORT_SYMBOL(cfg80211_testmode_reply);
5477
5478struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5479 int approxlen, gfp_t gfp)
5480{
5481 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5482
5483 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5484}
5485EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5486
5487void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5488{
5489 void *hdr = ((void **)skb->cb)[1];
5490 struct nlattr *data = ((void **)skb->cb)[2];
5491
5492 nla_nest_end(skb, data);
5493 genlmsg_end(skb, hdr);
5494 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5495}
5496EXPORT_SYMBOL(cfg80211_testmode_event);
5497#endif
5498
Samuel Ortizb23aa672009-07-01 21:26:54 +02005499static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5500{
Johannes Berg4c476992010-10-04 21:36:35 +02005501 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5502 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005503 struct cfg80211_connect_params connect;
5504 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005505 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005506 int err;
5507
5508 memset(&connect, 0, sizeof(connect));
5509
5510 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5511 return -EINVAL;
5512
5513 if (!info->attrs[NL80211_ATTR_SSID] ||
5514 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5515 return -EINVAL;
5516
5517 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5518 connect.auth_type =
5519 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5520 if (!nl80211_valid_auth_type(connect.auth_type))
5521 return -EINVAL;
5522 } else
5523 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5524
5525 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5526
Johannes Bergc0692b82010-08-27 14:26:53 +03005527 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005528 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005529 if (err)
5530 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005531
Johannes Berg074ac8d2010-09-16 14:58:22 +02005532 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005533 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5534 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005535
Johannes Berg79c97e92009-07-07 03:56:12 +02005536 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005537
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305538 connect.bg_scan_period = -1;
5539 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5540 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5541 connect.bg_scan_period =
5542 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5543 }
5544
Samuel Ortizb23aa672009-07-01 21:26:54 +02005545 if (info->attrs[NL80211_ATTR_MAC])
5546 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5547 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5548 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5549
5550 if (info->attrs[NL80211_ATTR_IE]) {
5551 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5552 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5553 }
5554
5555 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5556 connect.channel =
5557 ieee80211_get_channel(wiphy,
5558 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5559 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005560 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5561 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005562 }
5563
Johannes Bergfffd0932009-07-08 14:22:54 +02005564 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5565 connkeys = nl80211_parse_connkeys(rdev,
5566 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005567 if (IS_ERR(connkeys))
5568 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005569 }
5570
Ben Greear7e7c8922011-11-18 11:31:59 -08005571 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5572 connect.flags |= ASSOC_REQ_DISABLE_HT;
5573
5574 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5575 memcpy(&connect.ht_capa_mask,
5576 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5577 sizeof(connect.ht_capa_mask));
5578
5579 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5580 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5581 return -EINVAL;
5582 memcpy(&connect.ht_capa,
5583 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5584 sizeof(connect.ht_capa));
5585 }
5586
Johannes Bergfffd0932009-07-08 14:22:54 +02005587 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005588 if (err)
5589 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005590 return err;
5591}
5592
5593static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5594{
Johannes Berg4c476992010-10-04 21:36:35 +02005595 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5596 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005597 u16 reason;
5598
5599 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5600 reason = WLAN_REASON_DEAUTH_LEAVING;
5601 else
5602 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5603
5604 if (reason == 0)
5605 return -EINVAL;
5606
Johannes Berg074ac8d2010-09-16 14:58:22 +02005607 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005608 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5609 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005610
Johannes Berg4c476992010-10-04 21:36:35 +02005611 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005612}
5613
Johannes Berg463d0182009-07-14 00:33:35 +02005614static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5615{
Johannes Berg4c476992010-10-04 21:36:35 +02005616 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005617 struct net *net;
5618 int err;
5619 u32 pid;
5620
5621 if (!info->attrs[NL80211_ATTR_PID])
5622 return -EINVAL;
5623
5624 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5625
Johannes Berg463d0182009-07-14 00:33:35 +02005626 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005627 if (IS_ERR(net))
5628 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005629
5630 err = 0;
5631
5632 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005633 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5634 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005635
Johannes Berg463d0182009-07-14 00:33:35 +02005636 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005637 return err;
5638}
5639
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005640static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5641{
Johannes Berg4c476992010-10-04 21:36:35 +02005642 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005643 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5644 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005645 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005646 struct cfg80211_pmksa pmksa;
5647
5648 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5649
5650 if (!info->attrs[NL80211_ATTR_MAC])
5651 return -EINVAL;
5652
5653 if (!info->attrs[NL80211_ATTR_PMKID])
5654 return -EINVAL;
5655
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005656 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5657 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5658
Johannes Berg074ac8d2010-09-16 14:58:22 +02005659 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005660 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5661 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005662
5663 switch (info->genlhdr->cmd) {
5664 case NL80211_CMD_SET_PMKSA:
5665 rdev_ops = rdev->ops->set_pmksa;
5666 break;
5667 case NL80211_CMD_DEL_PMKSA:
5668 rdev_ops = rdev->ops->del_pmksa;
5669 break;
5670 default:
5671 WARN_ON(1);
5672 break;
5673 }
5674
Johannes Berg4c476992010-10-04 21:36:35 +02005675 if (!rdev_ops)
5676 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005677
Johannes Berg4c476992010-10-04 21:36:35 +02005678 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005679}
5680
5681static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5682{
Johannes Berg4c476992010-10-04 21:36:35 +02005683 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5684 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005685
Johannes Berg074ac8d2010-09-16 14:58:22 +02005686 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005687 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5688 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005689
Johannes Berg4c476992010-10-04 21:36:35 +02005690 if (!rdev->ops->flush_pmksa)
5691 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005692
Johannes Berg4c476992010-10-04 21:36:35 +02005693 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005694}
5695
Arik Nemtsov109086c2011-09-28 14:12:50 +03005696static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5697{
5698 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5699 struct net_device *dev = info->user_ptr[1];
5700 u8 action_code, dialog_token;
5701 u16 status_code;
5702 u8 *peer;
5703
5704 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5705 !rdev->ops->tdls_mgmt)
5706 return -EOPNOTSUPP;
5707
5708 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5709 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5710 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5711 !info->attrs[NL80211_ATTR_IE] ||
5712 !info->attrs[NL80211_ATTR_MAC])
5713 return -EINVAL;
5714
5715 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5716 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5717 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5718 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5719
5720 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5721 dialog_token, status_code,
5722 nla_data(info->attrs[NL80211_ATTR_IE]),
5723 nla_len(info->attrs[NL80211_ATTR_IE]));
5724}
5725
5726static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5727{
5728 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5729 struct net_device *dev = info->user_ptr[1];
5730 enum nl80211_tdls_operation operation;
5731 u8 *peer;
5732
5733 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5734 !rdev->ops->tdls_oper)
5735 return -EOPNOTSUPP;
5736
5737 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5738 !info->attrs[NL80211_ATTR_MAC])
5739 return -EINVAL;
5740
5741 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5742 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5743
5744 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5745}
5746
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005747static int nl80211_remain_on_channel(struct sk_buff *skb,
5748 struct genl_info *info)
5749{
Johannes Berg4c476992010-10-04 21:36:35 +02005750 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5751 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005752 struct ieee80211_channel *chan;
5753 struct sk_buff *msg;
5754 void *hdr;
5755 u64 cookie;
5756 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5757 u32 freq, duration;
5758 int err;
5759
5760 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5761 !info->attrs[NL80211_ATTR_DURATION])
5762 return -EINVAL;
5763
5764 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5765
Johannes Berg7c4ef712011-11-18 15:33:48 +01005766 if (!rdev->ops->remain_on_channel ||
5767 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005768 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005769
Johannes Bergebf348f2012-06-01 12:50:54 +02005770 /*
5771 * We should be on that channel for at least a minimum amount of
5772 * time (10ms) but no longer than the driver supports.
5773 */
5774 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5775 duration > rdev->wiphy.max_remain_on_channel_duration)
5776 return -EINVAL;
5777
Johannes Bergcd6c6592012-05-10 21:27:18 +02005778 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5779 !nl80211_valid_channel_type(info, &channel_type))
5780 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005781
5782 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5783 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005784 if (chan == NULL)
5785 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005786
5787 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005788 if (!msg)
5789 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005790
5791 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5792 NL80211_CMD_REMAIN_ON_CHANNEL);
5793
5794 if (IS_ERR(hdr)) {
5795 err = PTR_ERR(hdr);
5796 goto free_msg;
5797 }
5798
5799 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
5800 channel_type, duration, &cookie);
5801
5802 if (err)
5803 goto free_msg;
5804
David S. Miller9360ffd2012-03-29 04:41:26 -04005805 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5806 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005807
5808 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005809
5810 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005811
5812 nla_put_failure:
5813 err = -ENOBUFS;
5814 free_msg:
5815 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005816 return err;
5817}
5818
5819static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5820 struct genl_info *info)
5821{
Johannes Berg4c476992010-10-04 21:36:35 +02005822 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5823 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005824 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005825
5826 if (!info->attrs[NL80211_ATTR_COOKIE])
5827 return -EINVAL;
5828
Johannes Berg4c476992010-10-04 21:36:35 +02005829 if (!rdev->ops->cancel_remain_on_channel)
5830 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005831
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005832 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5833
Johannes Berg4c476992010-10-04 21:36:35 +02005834 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005835}
5836
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005837static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5838 u8 *rates, u8 rates_len)
5839{
5840 u8 i;
5841 u32 mask = 0;
5842
5843 for (i = 0; i < rates_len; i++) {
5844 int rate = (rates[i] & 0x7f) * 5;
5845 int ridx;
5846 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5847 struct ieee80211_rate *srate =
5848 &sband->bitrates[ridx];
5849 if (rate == srate->bitrate) {
5850 mask |= 1 << ridx;
5851 break;
5852 }
5853 }
5854 if (ridx == sband->n_bitrates)
5855 return 0; /* rate not found */
5856 }
5857
5858 return mask;
5859}
5860
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005861static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5862 u8 *rates, u8 rates_len,
5863 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5864{
5865 u8 i;
5866
5867 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5868
5869 for (i = 0; i < rates_len; i++) {
5870 int ridx, rbit;
5871
5872 ridx = rates[i] / 8;
5873 rbit = BIT(rates[i] % 8);
5874
5875 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005876 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005877 return false;
5878
5879 /* check availability */
5880 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5881 mcs[ridx] |= rbit;
5882 else
5883 return false;
5884 }
5885
5886 return true;
5887}
5888
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005889static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005890 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5891 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005892 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5893 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005894};
5895
5896static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5897 struct genl_info *info)
5898{
5899 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005900 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005901 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005902 int rem, i;
5903 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005904 struct nlattr *tx_rates;
5905 struct ieee80211_supported_band *sband;
5906
5907 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5908 return -EINVAL;
5909
Johannes Berg4c476992010-10-04 21:36:35 +02005910 if (!rdev->ops->set_bitrate_mask)
5911 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005912
5913 memset(&mask, 0, sizeof(mask));
5914 /* Default to all rates enabled */
5915 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5916 sband = rdev->wiphy.bands[i];
5917 mask.control[i].legacy =
5918 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005919 if (sband)
5920 memcpy(mask.control[i].mcs,
5921 sband->ht_cap.mcs.rx_mask,
5922 sizeof(mask.control[i].mcs));
5923 else
5924 memset(mask.control[i].mcs, 0,
5925 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005926 }
5927
5928 /*
5929 * The nested attribute uses enum nl80211_band as the index. This maps
5930 * directly to the enum ieee80211_band values used in cfg80211.
5931 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005932 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005933 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5934 {
5935 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005936 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5937 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005938 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005939 if (sband == NULL)
5940 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005941 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5942 nla_len(tx_rates), nl80211_txattr_policy);
5943 if (tb[NL80211_TXRATE_LEGACY]) {
5944 mask.control[band].legacy = rateset_to_mask(
5945 sband,
5946 nla_data(tb[NL80211_TXRATE_LEGACY]),
5947 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05305948 if ((mask.control[band].legacy == 0) &&
5949 nla_len(tb[NL80211_TXRATE_LEGACY]))
5950 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005951 }
5952 if (tb[NL80211_TXRATE_MCS]) {
5953 if (!ht_rateset_to_mask(
5954 sband,
5955 nla_data(tb[NL80211_TXRATE_MCS]),
5956 nla_len(tb[NL80211_TXRATE_MCS]),
5957 mask.control[band].mcs))
5958 return -EINVAL;
5959 }
5960
5961 if (mask.control[band].legacy == 0) {
5962 /* don't allow empty legacy rates if HT
5963 * is not even supported. */
5964 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5965 return -EINVAL;
5966
5967 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5968 if (mask.control[band].mcs[i])
5969 break;
5970
5971 /* legacy and mcs rates may not be both empty */
5972 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005973 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005974 }
5975 }
5976
Johannes Berg4c476992010-10-04 21:36:35 +02005977 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005978}
5979
Johannes Berg2e161f72010-08-12 15:38:38 +02005980static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005981{
Johannes Berg4c476992010-10-04 21:36:35 +02005982 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5983 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005984 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005985
5986 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5987 return -EINVAL;
5988
Johannes Berg2e161f72010-08-12 15:38:38 +02005989 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5990 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005991
Johannes Berg9d38d852010-06-09 17:20:33 +02005992 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005993 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005994 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5995 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5996 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005997 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005998 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5999 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006000
6001 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006002 if (!rdev->ops->mgmt_tx)
6003 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006004
Johannes Berg4c476992010-10-04 21:36:35 +02006005 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02006006 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006007 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6008 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006009}
6010
Johannes Berg2e161f72010-08-12 15:38:38 +02006011static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006012{
Johannes Berg4c476992010-10-04 21:36:35 +02006013 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6014 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02006015 struct ieee80211_channel *chan;
6016 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02006017 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02006018 u32 freq;
6019 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006020 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006021 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006022 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006023 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006024 bool offchan, no_cck, dont_wait_for_ack;
6025
6026 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006027
6028 if (!info->attrs[NL80211_ATTR_FRAME] ||
6029 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6030 return -EINVAL;
6031
Johannes Berg4c476992010-10-04 21:36:35 +02006032 if (!rdev->ops->mgmt_tx)
6033 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006034
Johannes Berg9d38d852010-06-09 17:20:33 +02006035 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02006036 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02006037 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
6038 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6039 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08006040 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02006041 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6042 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006043
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006044 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006045 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006046 return -EINVAL;
6047 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006048
6049 /*
6050 * We should wait on the channel for at least a minimum amount
6051 * of time (10ms) but no longer than the driver supports.
6052 */
6053 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6054 wait > rdev->wiphy.max_remain_on_channel_duration)
6055 return -EINVAL;
6056
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006057 }
6058
Jouni Malinen026331c2010-02-15 12:53:10 +02006059 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02006060 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02006061 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02006062 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02006063 }
6064
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006065 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6066
Johannes Berg7c4ef712011-11-18 15:33:48 +01006067 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6068 return -EINVAL;
6069
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306070 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6071
Jouni Malinen026331c2010-02-15 12:53:10 +02006072 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6073 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02006074 if (chan == NULL)
6075 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006076
Johannes Berge247bd902011-11-04 11:18:21 +01006077 if (!dont_wait_for_ack) {
6078 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6079 if (!msg)
6080 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006081
Johannes Berge247bd902011-11-04 11:18:21 +01006082 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6083 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006084
Johannes Berge247bd902011-11-04 11:18:21 +01006085 if (IS_ERR(hdr)) {
6086 err = PTR_ERR(hdr);
6087 goto free_msg;
6088 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006089 }
Johannes Berge247bd902011-11-04 11:18:21 +01006090
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006091 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
6092 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006093 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6094 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006095 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006096 if (err)
6097 goto free_msg;
6098
Johannes Berge247bd902011-11-04 11:18:21 +01006099 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006100 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6101 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006102
Johannes Berge247bd902011-11-04 11:18:21 +01006103 genlmsg_end(msg, hdr);
6104 return genlmsg_reply(msg, info);
6105 }
6106
6107 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006108
6109 nla_put_failure:
6110 err = -ENOBUFS;
6111 free_msg:
6112 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006113 return err;
6114}
6115
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006116static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6117{
6118 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6119 struct net_device *dev = info->user_ptr[1];
6120 u64 cookie;
6121
6122 if (!info->attrs[NL80211_ATTR_COOKIE])
6123 return -EINVAL;
6124
6125 if (!rdev->ops->mgmt_tx_cancel_wait)
6126 return -EOPNOTSUPP;
6127
6128 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
6129 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6130 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
6131 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
6132 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
6133 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
6134 return -EOPNOTSUPP;
6135
6136 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6137
6138 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
6139}
6140
Kalle Valoffb9eb32010-02-17 17:58:10 +02006141static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6142{
Johannes Berg4c476992010-10-04 21:36:35 +02006143 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006144 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006145 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006146 u8 ps_state;
6147 bool state;
6148 int err;
6149
Johannes Berg4c476992010-10-04 21:36:35 +02006150 if (!info->attrs[NL80211_ATTR_PS_STATE])
6151 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006152
6153 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6154
Johannes Berg4c476992010-10-04 21:36:35 +02006155 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6156 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006157
6158 wdev = dev->ieee80211_ptr;
6159
Johannes Berg4c476992010-10-04 21:36:35 +02006160 if (!rdev->ops->set_power_mgmt)
6161 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006162
6163 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6164
6165 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006166 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006167
Johannes Berg4c476992010-10-04 21:36:35 +02006168 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
6169 wdev->ps_timeout);
6170 if (!err)
6171 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006172 return err;
6173}
6174
6175static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6176{
Johannes Berg4c476992010-10-04 21:36:35 +02006177 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006178 enum nl80211_ps_state ps_state;
6179 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006180 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006181 struct sk_buff *msg;
6182 void *hdr;
6183 int err;
6184
Kalle Valoffb9eb32010-02-17 17:58:10 +02006185 wdev = dev->ieee80211_ptr;
6186
Johannes Berg4c476992010-10-04 21:36:35 +02006187 if (!rdev->ops->set_power_mgmt)
6188 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006189
6190 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006191 if (!msg)
6192 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006193
6194 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6195 NL80211_CMD_GET_POWER_SAVE);
6196 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006197 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006198 goto free_msg;
6199 }
6200
6201 if (wdev->ps)
6202 ps_state = NL80211_PS_ENABLED;
6203 else
6204 ps_state = NL80211_PS_DISABLED;
6205
David S. Miller9360ffd2012-03-29 04:41:26 -04006206 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6207 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006208
6209 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006210 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006211
Johannes Berg4c476992010-10-04 21:36:35 +02006212 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006213 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006214 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006215 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006216 return err;
6217}
6218
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006219static struct nla_policy
6220nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6221 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6222 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6223 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
6224};
6225
6226static int nl80211_set_cqm_rssi(struct genl_info *info,
6227 s32 threshold, u32 hysteresis)
6228{
Johannes Berg4c476992010-10-04 21:36:35 +02006229 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006230 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006231 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006232
6233 if (threshold > 0)
6234 return -EINVAL;
6235
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006236 wdev = dev->ieee80211_ptr;
6237
Johannes Berg4c476992010-10-04 21:36:35 +02006238 if (!rdev->ops->set_cqm_rssi_config)
6239 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006240
Johannes Berg074ac8d2010-09-16 14:58:22 +02006241 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006242 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6243 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006244
Johannes Berg4c476992010-10-04 21:36:35 +02006245 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6246 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006247}
6248
6249static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6250{
6251 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6252 struct nlattr *cqm;
6253 int err;
6254
6255 cqm = info->attrs[NL80211_ATTR_CQM];
6256 if (!cqm) {
6257 err = -EINVAL;
6258 goto out;
6259 }
6260
6261 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6262 nl80211_attr_cqm_policy);
6263 if (err)
6264 goto out;
6265
6266 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6267 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6268 s32 threshold;
6269 u32 hysteresis;
6270 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6271 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6272 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
6273 } else
6274 err = -EINVAL;
6275
6276out:
6277 return err;
6278}
6279
Johannes Berg29cbe682010-12-03 09:20:44 +01006280static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6281{
6282 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6283 struct net_device *dev = info->user_ptr[1];
6284 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006285 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006286 int err;
6287
6288 /* start with default */
6289 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006290 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006291
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006292 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006293 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006294 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006295 if (err)
6296 return err;
6297 }
6298
6299 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6300 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6301 return -EINVAL;
6302
Javier Cardonac80d5452010-12-16 17:37:49 -08006303 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6304 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6305
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006306 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6307 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6308 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6309 return -EINVAL;
6310
Javier Cardonac80d5452010-12-16 17:37:49 -08006311 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6312 /* parse additional setup parameters if given */
6313 err = nl80211_parse_mesh_setup(info, &setup);
6314 if (err)
6315 return err;
6316 }
6317
Johannes Bergcc1d2802012-05-16 23:50:20 +02006318 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6319 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6320
6321 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6322 !nl80211_valid_channel_type(info, &channel_type))
6323 return -EINVAL;
6324
6325 setup.channel = rdev_freq_to_chan(rdev,
6326 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6327 channel_type);
6328 if (!setup.channel)
6329 return -EINVAL;
6330 setup.channel_type = channel_type;
6331 } else {
6332 /* cfg80211_join_mesh() will sort it out */
6333 setup.channel = NULL;
6334 }
6335
Javier Cardonac80d5452010-12-16 17:37:49 -08006336 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006337}
6338
6339static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6340{
6341 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6342 struct net_device *dev = info->user_ptr[1];
6343
6344 return cfg80211_leave_mesh(rdev, dev);
6345}
6346
Johannes Bergdfb89c52012-06-27 09:23:48 +02006347#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006348static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6349{
6350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6351 struct sk_buff *msg;
6352 void *hdr;
6353
6354 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6355 return -EOPNOTSUPP;
6356
6357 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6358 if (!msg)
6359 return -ENOMEM;
6360
6361 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6362 NL80211_CMD_GET_WOWLAN);
6363 if (!hdr)
6364 goto nla_put_failure;
6365
6366 if (rdev->wowlan) {
6367 struct nlattr *nl_wowlan;
6368
6369 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6370 if (!nl_wowlan)
6371 goto nla_put_failure;
6372
David S. Miller9360ffd2012-03-29 04:41:26 -04006373 if ((rdev->wowlan->any &&
6374 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6375 (rdev->wowlan->disconnect &&
6376 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6377 (rdev->wowlan->magic_pkt &&
6378 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6379 (rdev->wowlan->gtk_rekey_failure &&
6380 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6381 (rdev->wowlan->eap_identity_req &&
6382 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6383 (rdev->wowlan->four_way_handshake &&
6384 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6385 (rdev->wowlan->rfkill_release &&
6386 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6387 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006388 if (rdev->wowlan->n_patterns) {
6389 struct nlattr *nl_pats, *nl_pat;
6390 int i, pat_len;
6391
6392 nl_pats = nla_nest_start(msg,
6393 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6394 if (!nl_pats)
6395 goto nla_put_failure;
6396
6397 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6398 nl_pat = nla_nest_start(msg, i + 1);
6399 if (!nl_pat)
6400 goto nla_put_failure;
6401 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006402 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6403 DIV_ROUND_UP(pat_len, 8),
6404 rdev->wowlan->patterns[i].mask) ||
6405 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6406 pat_len,
6407 rdev->wowlan->patterns[i].pattern))
6408 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006409 nla_nest_end(msg, nl_pat);
6410 }
6411 nla_nest_end(msg, nl_pats);
6412 }
6413
6414 nla_nest_end(msg, nl_wowlan);
6415 }
6416
6417 genlmsg_end(msg, hdr);
6418 return genlmsg_reply(msg, info);
6419
6420nla_put_failure:
6421 nlmsg_free(msg);
6422 return -ENOBUFS;
6423}
6424
6425static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6426{
6427 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6428 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6429 struct cfg80211_wowlan no_triggers = {};
6430 struct cfg80211_wowlan new_triggers = {};
6431 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6432 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006433 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006434
6435 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6436 return -EOPNOTSUPP;
6437
6438 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6439 goto no_triggers;
6440
6441 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6442 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6443 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6444 nl80211_wowlan_policy);
6445 if (err)
6446 return err;
6447
6448 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6449 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6450 return -EINVAL;
6451 new_triggers.any = true;
6452 }
6453
6454 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6455 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6456 return -EINVAL;
6457 new_triggers.disconnect = true;
6458 }
6459
6460 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6461 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6462 return -EINVAL;
6463 new_triggers.magic_pkt = true;
6464 }
6465
Johannes Berg77dbbb12011-07-13 10:48:55 +02006466 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6467 return -EINVAL;
6468
6469 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6470 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6471 return -EINVAL;
6472 new_triggers.gtk_rekey_failure = true;
6473 }
6474
6475 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6476 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6477 return -EINVAL;
6478 new_triggers.eap_identity_req = true;
6479 }
6480
6481 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6482 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6483 return -EINVAL;
6484 new_triggers.four_way_handshake = true;
6485 }
6486
6487 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6488 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6489 return -EINVAL;
6490 new_triggers.rfkill_release = true;
6491 }
6492
Johannes Bergff1b6e62011-05-04 15:37:28 +02006493 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6494 struct nlattr *pat;
6495 int n_patterns = 0;
6496 int rem, pat_len, mask_len;
6497 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6498
6499 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6500 rem)
6501 n_patterns++;
6502 if (n_patterns > wowlan->n_patterns)
6503 return -EINVAL;
6504
6505 new_triggers.patterns = kcalloc(n_patterns,
6506 sizeof(new_triggers.patterns[0]),
6507 GFP_KERNEL);
6508 if (!new_triggers.patterns)
6509 return -ENOMEM;
6510
6511 new_triggers.n_patterns = n_patterns;
6512 i = 0;
6513
6514 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6515 rem) {
6516 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6517 nla_data(pat), nla_len(pat), NULL);
6518 err = -EINVAL;
6519 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6520 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6521 goto error;
6522 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6523 mask_len = DIV_ROUND_UP(pat_len, 8);
6524 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6525 mask_len)
6526 goto error;
6527 if (pat_len > wowlan->pattern_max_len ||
6528 pat_len < wowlan->pattern_min_len)
6529 goto error;
6530
6531 new_triggers.patterns[i].mask =
6532 kmalloc(mask_len + pat_len, GFP_KERNEL);
6533 if (!new_triggers.patterns[i].mask) {
6534 err = -ENOMEM;
6535 goto error;
6536 }
6537 new_triggers.patterns[i].pattern =
6538 new_triggers.patterns[i].mask + mask_len;
6539 memcpy(new_triggers.patterns[i].mask,
6540 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6541 mask_len);
6542 new_triggers.patterns[i].pattern_len = pat_len;
6543 memcpy(new_triggers.patterns[i].pattern,
6544 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6545 pat_len);
6546 i++;
6547 }
6548 }
6549
6550 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6551 struct cfg80211_wowlan *ntrig;
6552 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6553 GFP_KERNEL);
6554 if (!ntrig) {
6555 err = -ENOMEM;
6556 goto error;
6557 }
6558 cfg80211_rdev_free_wowlan(rdev);
6559 rdev->wowlan = ntrig;
6560 } else {
6561 no_triggers:
6562 cfg80211_rdev_free_wowlan(rdev);
6563 rdev->wowlan = NULL;
6564 }
6565
Johannes Berg6d525632012-04-04 15:05:25 +02006566 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6567 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6568
Johannes Bergff1b6e62011-05-04 15:37:28 +02006569 return 0;
6570 error:
6571 for (i = 0; i < new_triggers.n_patterns; i++)
6572 kfree(new_triggers.patterns[i].mask);
6573 kfree(new_triggers.patterns);
6574 return err;
6575}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006576#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006577
Johannes Berge5497d72011-07-05 16:35:40 +02006578static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6579{
6580 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6581 struct net_device *dev = info->user_ptr[1];
6582 struct wireless_dev *wdev = dev->ieee80211_ptr;
6583 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6584 struct cfg80211_gtk_rekey_data rekey_data;
6585 int err;
6586
6587 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6588 return -EINVAL;
6589
6590 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6591 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6592 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6593 nl80211_rekey_policy);
6594 if (err)
6595 return err;
6596
6597 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6598 return -ERANGE;
6599 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6600 return -ERANGE;
6601 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6602 return -ERANGE;
6603
6604 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6605 NL80211_KEK_LEN);
6606 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6607 NL80211_KCK_LEN);
6608 memcpy(rekey_data.replay_ctr,
6609 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6610 NL80211_REPLAY_CTR_LEN);
6611
6612 wdev_lock(wdev);
6613 if (!wdev->current_bss) {
6614 err = -ENOTCONN;
6615 goto out;
6616 }
6617
6618 if (!rdev->ops->set_rekey_data) {
6619 err = -EOPNOTSUPP;
6620 goto out;
6621 }
6622
6623 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6624 out:
6625 wdev_unlock(wdev);
6626 return err;
6627}
6628
Johannes Berg28946da2011-11-04 11:18:12 +01006629static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6630 struct genl_info *info)
6631{
6632 struct net_device *dev = info->user_ptr[1];
6633 struct wireless_dev *wdev = dev->ieee80211_ptr;
6634
6635 if (wdev->iftype != NL80211_IFTYPE_AP &&
6636 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6637 return -EINVAL;
6638
6639 if (wdev->ap_unexpected_nlpid)
6640 return -EBUSY;
6641
6642 wdev->ap_unexpected_nlpid = info->snd_pid;
6643 return 0;
6644}
6645
Johannes Berg7f6cf312011-11-04 11:18:15 +01006646static int nl80211_probe_client(struct sk_buff *skb,
6647 struct genl_info *info)
6648{
6649 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6650 struct net_device *dev = info->user_ptr[1];
6651 struct wireless_dev *wdev = dev->ieee80211_ptr;
6652 struct sk_buff *msg;
6653 void *hdr;
6654 const u8 *addr;
6655 u64 cookie;
6656 int err;
6657
6658 if (wdev->iftype != NL80211_IFTYPE_AP &&
6659 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6660 return -EOPNOTSUPP;
6661
6662 if (!info->attrs[NL80211_ATTR_MAC])
6663 return -EINVAL;
6664
6665 if (!rdev->ops->probe_client)
6666 return -EOPNOTSUPP;
6667
6668 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6669 if (!msg)
6670 return -ENOMEM;
6671
6672 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6673 NL80211_CMD_PROBE_CLIENT);
6674
6675 if (IS_ERR(hdr)) {
6676 err = PTR_ERR(hdr);
6677 goto free_msg;
6678 }
6679
6680 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6681
6682 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6683 if (err)
6684 goto free_msg;
6685
David S. Miller9360ffd2012-03-29 04:41:26 -04006686 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6687 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006688
6689 genlmsg_end(msg, hdr);
6690
6691 return genlmsg_reply(msg, info);
6692
6693 nla_put_failure:
6694 err = -ENOBUFS;
6695 free_msg:
6696 nlmsg_free(msg);
6697 return err;
6698}
6699
Johannes Berg5e7602302011-11-04 11:18:17 +01006700static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6701{
6702 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6703
6704 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6705 return -EOPNOTSUPP;
6706
6707 if (rdev->ap_beacons_nlpid)
6708 return -EBUSY;
6709
6710 rdev->ap_beacons_nlpid = info->snd_pid;
6711
6712 return 0;
6713}
6714
Johannes Berg4c476992010-10-04 21:36:35 +02006715#define NL80211_FLAG_NEED_WIPHY 0x01
6716#define NL80211_FLAG_NEED_NETDEV 0x02
6717#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006718#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6719#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6720 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02006721#define NL80211_FLAG_NEED_WDEV 0x10
6722/* If a netdev is associated, it must be UP */
6723#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
6724 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006725
6726static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6727 struct genl_info *info)
6728{
6729 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02006730 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006731 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02006732 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6733
6734 if (rtnl)
6735 rtnl_lock();
6736
6737 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02006738 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02006739 if (IS_ERR(rdev)) {
6740 if (rtnl)
6741 rtnl_unlock();
6742 return PTR_ERR(rdev);
6743 }
6744 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02006745 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
6746 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02006747 mutex_lock(&cfg80211_mutex);
6748 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
6749 info->attrs);
6750 if (IS_ERR(wdev)) {
6751 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02006752 if (rtnl)
6753 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02006754 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02006755 }
Johannes Berg89a54e42012-06-15 14:33:17 +02006756
Johannes Berg89a54e42012-06-15 14:33:17 +02006757 dev = wdev->netdev;
6758 rdev = wiphy_to_dev(wdev->wiphy);
6759
Johannes Berg1bf614e2012-06-15 15:23:36 +02006760 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
6761 if (!dev) {
6762 mutex_unlock(&cfg80211_mutex);
6763 if (rtnl)
6764 rtnl_unlock();
6765 return -EINVAL;
6766 }
6767
6768 info->user_ptr[1] = dev;
6769 } else {
6770 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02006771 }
Johannes Berg89a54e42012-06-15 14:33:17 +02006772
Johannes Berg1bf614e2012-06-15 15:23:36 +02006773 if (dev) {
6774 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6775 !netif_running(dev)) {
6776 mutex_unlock(&cfg80211_mutex);
6777 if (rtnl)
6778 rtnl_unlock();
6779 return -ENETDOWN;
6780 }
6781
6782 dev_hold(dev);
6783 }
6784
Johannes Berg89a54e42012-06-15 14:33:17 +02006785 cfg80211_lock_rdev(rdev);
6786
6787 mutex_unlock(&cfg80211_mutex);
6788
Johannes Berg4c476992010-10-04 21:36:35 +02006789 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006790 }
6791
6792 return 0;
6793}
6794
6795static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6796 struct genl_info *info)
6797{
6798 if (info->user_ptr[0])
6799 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02006800 if (info->user_ptr[1]) {
6801 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
6802 struct wireless_dev *wdev = info->user_ptr[1];
6803
6804 if (wdev->netdev)
6805 dev_put(wdev->netdev);
6806 } else {
6807 dev_put(info->user_ptr[1]);
6808 }
6809 }
Johannes Berg4c476992010-10-04 21:36:35 +02006810 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6811 rtnl_unlock();
6812}
6813
Johannes Berg55682962007-09-20 13:09:35 -04006814static struct genl_ops nl80211_ops[] = {
6815 {
6816 .cmd = NL80211_CMD_GET_WIPHY,
6817 .doit = nl80211_get_wiphy,
6818 .dumpit = nl80211_dump_wiphy,
6819 .policy = nl80211_policy,
6820 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006821 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006822 },
6823 {
6824 .cmd = NL80211_CMD_SET_WIPHY,
6825 .doit = nl80211_set_wiphy,
6826 .policy = nl80211_policy,
6827 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006828 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006829 },
6830 {
6831 .cmd = NL80211_CMD_GET_INTERFACE,
6832 .doit = nl80211_get_interface,
6833 .dumpit = nl80211_dump_interface,
6834 .policy = nl80211_policy,
6835 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006836 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006837 },
6838 {
6839 .cmd = NL80211_CMD_SET_INTERFACE,
6840 .doit = nl80211_set_interface,
6841 .policy = nl80211_policy,
6842 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006843 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6844 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006845 },
6846 {
6847 .cmd = NL80211_CMD_NEW_INTERFACE,
6848 .doit = nl80211_new_interface,
6849 .policy = nl80211_policy,
6850 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006851 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6852 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006853 },
6854 {
6855 .cmd = NL80211_CMD_DEL_INTERFACE,
6856 .doit = nl80211_del_interface,
6857 .policy = nl80211_policy,
6858 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006859 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6860 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006861 },
Johannes Berg41ade002007-12-19 02:03:29 +01006862 {
6863 .cmd = NL80211_CMD_GET_KEY,
6864 .doit = nl80211_get_key,
6865 .policy = nl80211_policy,
6866 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006867 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006868 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006869 },
6870 {
6871 .cmd = NL80211_CMD_SET_KEY,
6872 .doit = nl80211_set_key,
6873 .policy = nl80211_policy,
6874 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006875 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006876 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006877 },
6878 {
6879 .cmd = NL80211_CMD_NEW_KEY,
6880 .doit = nl80211_new_key,
6881 .policy = nl80211_policy,
6882 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006883 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006884 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006885 },
6886 {
6887 .cmd = NL80211_CMD_DEL_KEY,
6888 .doit = nl80211_del_key,
6889 .policy = nl80211_policy,
6890 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006891 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006892 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006893 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006894 {
6895 .cmd = NL80211_CMD_SET_BEACON,
6896 .policy = nl80211_policy,
6897 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006898 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006899 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006900 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006901 },
6902 {
Johannes Berg88600202012-02-13 15:17:18 +01006903 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006904 .policy = nl80211_policy,
6905 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006906 .doit = nl80211_start_ap,
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 Berged1b6cc2007-12-19 02:03:32 +01006909 },
6910 {
Johannes Berg88600202012-02-13 15:17:18 +01006911 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006912 .policy = nl80211_policy,
6913 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006914 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006915 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006916 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006917 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006918 {
6919 .cmd = NL80211_CMD_GET_STATION,
6920 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006921 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006922 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006923 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6924 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006925 },
6926 {
6927 .cmd = NL80211_CMD_SET_STATION,
6928 .doit = nl80211_set_station,
6929 .policy = nl80211_policy,
6930 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006931 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006932 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006933 },
6934 {
6935 .cmd = NL80211_CMD_NEW_STATION,
6936 .doit = nl80211_new_station,
6937 .policy = nl80211_policy,
6938 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006939 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006940 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006941 },
6942 {
6943 .cmd = NL80211_CMD_DEL_STATION,
6944 .doit = nl80211_del_station,
6945 .policy = nl80211_policy,
6946 .flags = GENL_ADMIN_PERM,
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 Berg5727ef12007-12-19 02:03:34 +01006949 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006950 {
6951 .cmd = NL80211_CMD_GET_MPATH,
6952 .doit = nl80211_get_mpath,
6953 .dumpit = nl80211_dump_mpath,
6954 .policy = nl80211_policy,
6955 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006956 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006957 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006958 },
6959 {
6960 .cmd = NL80211_CMD_SET_MPATH,
6961 .doit = nl80211_set_mpath,
6962 .policy = nl80211_policy,
6963 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006964 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006965 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006966 },
6967 {
6968 .cmd = NL80211_CMD_NEW_MPATH,
6969 .doit = nl80211_new_mpath,
6970 .policy = nl80211_policy,
6971 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006972 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006973 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006974 },
6975 {
6976 .cmd = NL80211_CMD_DEL_MPATH,
6977 .doit = nl80211_del_mpath,
6978 .policy = nl80211_policy,
6979 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006980 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006981 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006982 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006983 {
6984 .cmd = NL80211_CMD_SET_BSS,
6985 .doit = nl80211_set_bss,
6986 .policy = nl80211_policy,
6987 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006988 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006989 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03006990 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006991 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08006992 .cmd = NL80211_CMD_GET_REG,
6993 .doit = nl80211_get_reg,
6994 .policy = nl80211_policy,
6995 /* can be retrieved by unprivileged users */
6996 },
6997 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07006998 .cmd = NL80211_CMD_SET_REG,
6999 .doit = nl80211_set_reg,
7000 .policy = nl80211_policy,
7001 .flags = GENL_ADMIN_PERM,
7002 },
7003 {
7004 .cmd = NL80211_CMD_REQ_SET_REG,
7005 .doit = nl80211_req_set_reg,
7006 .policy = nl80211_policy,
7007 .flags = GENL_ADMIN_PERM,
7008 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007009 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007010 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7011 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007012 .policy = nl80211_policy,
7013 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007014 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007015 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007016 },
7017 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007018 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7019 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007020 .policy = nl80211_policy,
7021 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007022 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007023 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007024 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007025 {
Johannes Berg2a519312009-02-10 21:25:55 +01007026 .cmd = NL80211_CMD_TRIGGER_SCAN,
7027 .doit = nl80211_trigger_scan,
7028 .policy = nl80211_policy,
7029 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007030 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007031 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007032 },
7033 {
7034 .cmd = NL80211_CMD_GET_SCAN,
7035 .policy = nl80211_policy,
7036 .dumpit = nl80211_dump_scan,
7037 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007038 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007039 .cmd = NL80211_CMD_START_SCHED_SCAN,
7040 .doit = nl80211_start_sched_scan,
7041 .policy = nl80211_policy,
7042 .flags = GENL_ADMIN_PERM,
7043 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7044 NL80211_FLAG_NEED_RTNL,
7045 },
7046 {
7047 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7048 .doit = nl80211_stop_sched_scan,
7049 .policy = nl80211_policy,
7050 .flags = GENL_ADMIN_PERM,
7051 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7052 NL80211_FLAG_NEED_RTNL,
7053 },
7054 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007055 .cmd = NL80211_CMD_AUTHENTICATE,
7056 .doit = nl80211_authenticate,
7057 .policy = nl80211_policy,
7058 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007059 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007060 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007061 },
7062 {
7063 .cmd = NL80211_CMD_ASSOCIATE,
7064 .doit = nl80211_associate,
7065 .policy = nl80211_policy,
7066 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007067 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007068 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007069 },
7070 {
7071 .cmd = NL80211_CMD_DEAUTHENTICATE,
7072 .doit = nl80211_deauthenticate,
7073 .policy = nl80211_policy,
7074 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007075 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007076 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007077 },
7078 {
7079 .cmd = NL80211_CMD_DISASSOCIATE,
7080 .doit = nl80211_disassociate,
7081 .policy = nl80211_policy,
7082 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007083 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007084 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007085 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007086 {
7087 .cmd = NL80211_CMD_JOIN_IBSS,
7088 .doit = nl80211_join_ibss,
7089 .policy = nl80211_policy,
7090 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007091 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007092 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007093 },
7094 {
7095 .cmd = NL80211_CMD_LEAVE_IBSS,
7096 .doit = nl80211_leave_ibss,
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,
Johannes Berg04a773a2009-04-19 21:24:32 +02007101 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007102#ifdef CONFIG_NL80211_TESTMODE
7103 {
7104 .cmd = NL80211_CMD_TESTMODE,
7105 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007106 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007107 .policy = nl80211_policy,
7108 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007109 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7110 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007111 },
7112#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007113 {
7114 .cmd = NL80211_CMD_CONNECT,
7115 .doit = nl80211_connect,
7116 .policy = nl80211_policy,
7117 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007118 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007119 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007120 },
7121 {
7122 .cmd = NL80211_CMD_DISCONNECT,
7123 .doit = nl80211_disconnect,
7124 .policy = nl80211_policy,
7125 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007126 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007127 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007128 },
Johannes Berg463d0182009-07-14 00:33:35 +02007129 {
7130 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7131 .doit = nl80211_wiphy_netns,
7132 .policy = nl80211_policy,
7133 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007134 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7135 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007136 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007137 {
7138 .cmd = NL80211_CMD_GET_SURVEY,
7139 .policy = nl80211_policy,
7140 .dumpit = nl80211_dump_survey,
7141 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007142 {
7143 .cmd = NL80211_CMD_SET_PMKSA,
7144 .doit = nl80211_setdel_pmksa,
7145 .policy = nl80211_policy,
7146 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007147 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007148 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007149 },
7150 {
7151 .cmd = NL80211_CMD_DEL_PMKSA,
7152 .doit = nl80211_setdel_pmksa,
7153 .policy = nl80211_policy,
7154 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007155 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007156 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007157 },
7158 {
7159 .cmd = NL80211_CMD_FLUSH_PMKSA,
7160 .doit = nl80211_flush_pmksa,
7161 .policy = nl80211_policy,
7162 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007163 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007164 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007165 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007166 {
7167 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7168 .doit = nl80211_remain_on_channel,
7169 .policy = nl80211_policy,
7170 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007171 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007172 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007173 },
7174 {
7175 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7176 .doit = nl80211_cancel_remain_on_channel,
7177 .policy = nl80211_policy,
7178 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007179 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007180 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007181 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007182 {
7183 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7184 .doit = nl80211_set_tx_bitrate_mask,
7185 .policy = nl80211_policy,
7186 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007187 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7188 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007189 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007190 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007191 .cmd = NL80211_CMD_REGISTER_FRAME,
7192 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007193 .policy = nl80211_policy,
7194 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007195 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7196 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007197 },
7198 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007199 .cmd = NL80211_CMD_FRAME,
7200 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007201 .policy = nl80211_policy,
7202 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007203 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007204 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007205 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007206 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007207 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7208 .doit = nl80211_tx_mgmt_cancel_wait,
7209 .policy = nl80211_policy,
7210 .flags = GENL_ADMIN_PERM,
7211 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7212 NL80211_FLAG_NEED_RTNL,
7213 },
7214 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007215 .cmd = NL80211_CMD_SET_POWER_SAVE,
7216 .doit = nl80211_set_power_save,
7217 .policy = nl80211_policy,
7218 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007219 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7220 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007221 },
7222 {
7223 .cmd = NL80211_CMD_GET_POWER_SAVE,
7224 .doit = nl80211_get_power_save,
7225 .policy = nl80211_policy,
7226 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007227 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7228 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007229 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007230 {
7231 .cmd = NL80211_CMD_SET_CQM,
7232 .doit = nl80211_set_cqm,
7233 .policy = nl80211_policy,
7234 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007235 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7236 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007237 },
Johannes Bergf444de02010-05-05 15:25:02 +02007238 {
7239 .cmd = NL80211_CMD_SET_CHANNEL,
7240 .doit = nl80211_set_channel,
7241 .policy = nl80211_policy,
7242 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007243 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7244 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007245 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007246 {
7247 .cmd = NL80211_CMD_SET_WDS_PEER,
7248 .doit = nl80211_set_wds_peer,
7249 .policy = nl80211_policy,
7250 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007251 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7252 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007253 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007254 {
7255 .cmd = NL80211_CMD_JOIN_MESH,
7256 .doit = nl80211_join_mesh,
7257 .policy = nl80211_policy,
7258 .flags = GENL_ADMIN_PERM,
7259 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7260 NL80211_FLAG_NEED_RTNL,
7261 },
7262 {
7263 .cmd = NL80211_CMD_LEAVE_MESH,
7264 .doit = nl80211_leave_mesh,
7265 .policy = nl80211_policy,
7266 .flags = GENL_ADMIN_PERM,
7267 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7268 NL80211_FLAG_NEED_RTNL,
7269 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007270#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007271 {
7272 .cmd = NL80211_CMD_GET_WOWLAN,
7273 .doit = nl80211_get_wowlan,
7274 .policy = nl80211_policy,
7275 /* can be retrieved by unprivileged users */
7276 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7277 NL80211_FLAG_NEED_RTNL,
7278 },
7279 {
7280 .cmd = NL80211_CMD_SET_WOWLAN,
7281 .doit = nl80211_set_wowlan,
7282 .policy = nl80211_policy,
7283 .flags = GENL_ADMIN_PERM,
7284 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7285 NL80211_FLAG_NEED_RTNL,
7286 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007287#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007288 {
7289 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7290 .doit = nl80211_set_rekey_data,
7291 .policy = nl80211_policy,
7292 .flags = GENL_ADMIN_PERM,
7293 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7294 NL80211_FLAG_NEED_RTNL,
7295 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007296 {
7297 .cmd = NL80211_CMD_TDLS_MGMT,
7298 .doit = nl80211_tdls_mgmt,
7299 .policy = nl80211_policy,
7300 .flags = GENL_ADMIN_PERM,
7301 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7302 NL80211_FLAG_NEED_RTNL,
7303 },
7304 {
7305 .cmd = NL80211_CMD_TDLS_OPER,
7306 .doit = nl80211_tdls_oper,
7307 .policy = nl80211_policy,
7308 .flags = GENL_ADMIN_PERM,
7309 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7310 NL80211_FLAG_NEED_RTNL,
7311 },
Johannes Berg28946da2011-11-04 11:18:12 +01007312 {
7313 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7314 .doit = nl80211_register_unexpected_frame,
7315 .policy = nl80211_policy,
7316 .flags = GENL_ADMIN_PERM,
7317 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7318 NL80211_FLAG_NEED_RTNL,
7319 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007320 {
7321 .cmd = NL80211_CMD_PROBE_CLIENT,
7322 .doit = nl80211_probe_client,
7323 .policy = nl80211_policy,
7324 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007325 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007326 NL80211_FLAG_NEED_RTNL,
7327 },
Johannes Berg5e7602302011-11-04 11:18:17 +01007328 {
7329 .cmd = NL80211_CMD_REGISTER_BEACONS,
7330 .doit = nl80211_register_beacons,
7331 .policy = nl80211_policy,
7332 .flags = GENL_ADMIN_PERM,
7333 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7334 NL80211_FLAG_NEED_RTNL,
7335 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007336 {
7337 .cmd = NL80211_CMD_SET_NOACK_MAP,
7338 .doit = nl80211_set_noack_map,
7339 .policy = nl80211_policy,
7340 .flags = GENL_ADMIN_PERM,
7341 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7342 NL80211_FLAG_NEED_RTNL,
7343 },
7344
Johannes Berg55682962007-09-20 13:09:35 -04007345};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007346
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007347static struct genl_multicast_group nl80211_mlme_mcgrp = {
7348 .name = "mlme",
7349};
Johannes Berg55682962007-09-20 13:09:35 -04007350
7351/* multicast groups */
7352static struct genl_multicast_group nl80211_config_mcgrp = {
7353 .name = "config",
7354};
Johannes Berg2a519312009-02-10 21:25:55 +01007355static struct genl_multicast_group nl80211_scan_mcgrp = {
7356 .name = "scan",
7357};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007358static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7359 .name = "regulatory",
7360};
Johannes Berg55682962007-09-20 13:09:35 -04007361
7362/* notification functions */
7363
7364void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7365{
7366 struct sk_buff *msg;
7367
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007368 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007369 if (!msg)
7370 return;
7371
7372 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7373 nlmsg_free(msg);
7374 return;
7375 }
7376
Johannes Berg463d0182009-07-14 00:33:35 +02007377 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7378 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007379}
7380
Johannes Berg362a4152009-05-24 16:43:15 +02007381static int nl80211_add_scan_req(struct sk_buff *msg,
7382 struct cfg80211_registered_device *rdev)
7383{
7384 struct cfg80211_scan_request *req = rdev->scan_req;
7385 struct nlattr *nest;
7386 int i;
7387
Johannes Berg667503dd2009-07-07 03:56:11 +02007388 ASSERT_RDEV_LOCK(rdev);
7389
Johannes Berg362a4152009-05-24 16:43:15 +02007390 if (WARN_ON(!req))
7391 return 0;
7392
7393 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7394 if (!nest)
7395 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007396 for (i = 0; i < req->n_ssids; i++) {
7397 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7398 goto nla_put_failure;
7399 }
Johannes Berg362a4152009-05-24 16:43:15 +02007400 nla_nest_end(msg, nest);
7401
7402 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7403 if (!nest)
7404 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007405 for (i = 0; i < req->n_channels; i++) {
7406 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7407 goto nla_put_failure;
7408 }
Johannes Berg362a4152009-05-24 16:43:15 +02007409 nla_nest_end(msg, nest);
7410
David S. Miller9360ffd2012-03-29 04:41:26 -04007411 if (req->ie &&
7412 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7413 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007414
7415 return 0;
7416 nla_put_failure:
7417 return -ENOBUFS;
7418}
7419
Johannes Berga538e2d2009-06-16 19:56:42 +02007420static int nl80211_send_scan_msg(struct sk_buff *msg,
7421 struct cfg80211_registered_device *rdev,
7422 struct net_device *netdev,
7423 u32 pid, u32 seq, int flags,
7424 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007425{
7426 void *hdr;
7427
7428 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7429 if (!hdr)
7430 return -1;
7431
David S. Miller9360ffd2012-03-29 04:41:26 -04007432 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7433 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7434 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007435
Johannes Berg362a4152009-05-24 16:43:15 +02007436 /* ignore errors and send incomplete event anyway */
7437 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007438
7439 return genlmsg_end(msg, hdr);
7440
7441 nla_put_failure:
7442 genlmsg_cancel(msg, hdr);
7443 return -EMSGSIZE;
7444}
7445
Luciano Coelho807f8a82011-05-11 17:09:35 +03007446static int
7447nl80211_send_sched_scan_msg(struct sk_buff *msg,
7448 struct cfg80211_registered_device *rdev,
7449 struct net_device *netdev,
7450 u32 pid, u32 seq, int flags, u32 cmd)
7451{
7452 void *hdr;
7453
7454 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7455 if (!hdr)
7456 return -1;
7457
David S. Miller9360ffd2012-03-29 04:41:26 -04007458 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7459 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7460 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007461
7462 return genlmsg_end(msg, hdr);
7463
7464 nla_put_failure:
7465 genlmsg_cancel(msg, hdr);
7466 return -EMSGSIZE;
7467}
7468
Johannes Berga538e2d2009-06-16 19:56:42 +02007469void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7470 struct net_device *netdev)
7471{
7472 struct sk_buff *msg;
7473
7474 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7475 if (!msg)
7476 return;
7477
7478 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7479 NL80211_CMD_TRIGGER_SCAN) < 0) {
7480 nlmsg_free(msg);
7481 return;
7482 }
7483
Johannes Berg463d0182009-07-14 00:33:35 +02007484 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7485 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007486}
7487
Johannes Berg2a519312009-02-10 21:25:55 +01007488void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7489 struct net_device *netdev)
7490{
7491 struct sk_buff *msg;
7492
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007493 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007494 if (!msg)
7495 return;
7496
Johannes Berga538e2d2009-06-16 19:56:42 +02007497 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7498 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007499 nlmsg_free(msg);
7500 return;
7501 }
7502
Johannes Berg463d0182009-07-14 00:33:35 +02007503 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7504 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007505}
7506
7507void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7508 struct net_device *netdev)
7509{
7510 struct sk_buff *msg;
7511
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007512 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007513 if (!msg)
7514 return;
7515
Johannes Berga538e2d2009-06-16 19:56:42 +02007516 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7517 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007518 nlmsg_free(msg);
7519 return;
7520 }
7521
Johannes Berg463d0182009-07-14 00:33:35 +02007522 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7523 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007524}
7525
Luciano Coelho807f8a82011-05-11 17:09:35 +03007526void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7527 struct net_device *netdev)
7528{
7529 struct sk_buff *msg;
7530
7531 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7532 if (!msg)
7533 return;
7534
7535 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7536 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7537 nlmsg_free(msg);
7538 return;
7539 }
7540
7541 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7542 nl80211_scan_mcgrp.id, GFP_KERNEL);
7543}
7544
7545void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7546 struct net_device *netdev, u32 cmd)
7547{
7548 struct sk_buff *msg;
7549
7550 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7551 if (!msg)
7552 return;
7553
7554 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7555 nlmsg_free(msg);
7556 return;
7557 }
7558
7559 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7560 nl80211_scan_mcgrp.id, GFP_KERNEL);
7561}
7562
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007563/*
7564 * This can happen on global regulatory changes or device specific settings
7565 * based on custom world regulatory domains.
7566 */
7567void nl80211_send_reg_change_event(struct regulatory_request *request)
7568{
7569 struct sk_buff *msg;
7570 void *hdr;
7571
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007572 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007573 if (!msg)
7574 return;
7575
7576 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7577 if (!hdr) {
7578 nlmsg_free(msg);
7579 return;
7580 }
7581
7582 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007583 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7584 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007585
David S. Miller9360ffd2012-03-29 04:41:26 -04007586 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7587 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7588 NL80211_REGDOM_TYPE_WORLD))
7589 goto nla_put_failure;
7590 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7591 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7592 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7593 goto nla_put_failure;
7594 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7595 request->intersect) {
7596 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7597 NL80211_REGDOM_TYPE_INTERSECTION))
7598 goto nla_put_failure;
7599 } else {
7600 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7601 NL80211_REGDOM_TYPE_COUNTRY) ||
7602 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7603 request->alpha2))
7604 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007605 }
7606
David S. Miller9360ffd2012-03-29 04:41:26 -04007607 if (wiphy_idx_valid(request->wiphy_idx) &&
7608 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7609 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007610
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007611 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007612
Johannes Bergbc43b282009-07-25 10:54:13 +02007613 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007614 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007615 GFP_ATOMIC);
7616 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007617
7618 return;
7619
7620nla_put_failure:
7621 genlmsg_cancel(msg, hdr);
7622 nlmsg_free(msg);
7623}
7624
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007625static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7626 struct net_device *netdev,
7627 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007628 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007629{
7630 struct sk_buff *msg;
7631 void *hdr;
7632
Johannes Berge6d6e342009-07-01 21:26:47 +02007633 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007634 if (!msg)
7635 return;
7636
7637 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7638 if (!hdr) {
7639 nlmsg_free(msg);
7640 return;
7641 }
7642
David S. Miller9360ffd2012-03-29 04:41:26 -04007643 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7644 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7645 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7646 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007647
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007648 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007649
Johannes Berg463d0182009-07-14 00:33:35 +02007650 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7651 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007652 return;
7653
7654 nla_put_failure:
7655 genlmsg_cancel(msg, hdr);
7656 nlmsg_free(msg);
7657}
7658
7659void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007660 struct net_device *netdev, const u8 *buf,
7661 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007662{
7663 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007664 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007665}
7666
7667void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7668 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007669 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007670{
Johannes Berge6d6e342009-07-01 21:26:47 +02007671 nl80211_send_mlme_event(rdev, netdev, buf, len,
7672 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007673}
7674
Jouni Malinen53b46b82009-03-27 20:53:56 +02007675void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007676 struct net_device *netdev, const u8 *buf,
7677 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007678{
7679 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007680 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007681}
7682
Jouni Malinen53b46b82009-03-27 20:53:56 +02007683void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7684 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007685 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007686{
7687 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007688 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007689}
7690
Jouni Malinencf4e5942010-12-16 00:52:40 +02007691void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7692 struct net_device *netdev, const u8 *buf,
7693 size_t len, gfp_t gfp)
7694{
7695 nl80211_send_mlme_event(rdev, netdev, buf, len,
7696 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7697}
7698
7699void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7700 struct net_device *netdev, const u8 *buf,
7701 size_t len, gfp_t gfp)
7702{
7703 nl80211_send_mlme_event(rdev, netdev, buf, len,
7704 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7705}
7706
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007707static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7708 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007709 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007710{
7711 struct sk_buff *msg;
7712 void *hdr;
7713
Johannes Berge6d6e342009-07-01 21:26:47 +02007714 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007715 if (!msg)
7716 return;
7717
7718 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7719 if (!hdr) {
7720 nlmsg_free(msg);
7721 return;
7722 }
7723
David S. Miller9360ffd2012-03-29 04:41:26 -04007724 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7725 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7726 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7727 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7728 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007729
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007730 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007731
Johannes Berg463d0182009-07-14 00:33:35 +02007732 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7733 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007734 return;
7735
7736 nla_put_failure:
7737 genlmsg_cancel(msg, hdr);
7738 nlmsg_free(msg);
7739}
7740
7741void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007742 struct net_device *netdev, const u8 *addr,
7743 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007744{
7745 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007746 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007747}
7748
7749void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007750 struct net_device *netdev, const u8 *addr,
7751 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007752{
Johannes Berge6d6e342009-07-01 21:26:47 +02007753 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7754 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007755}
7756
Samuel Ortizb23aa672009-07-01 21:26:54 +02007757void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7758 struct net_device *netdev, const u8 *bssid,
7759 const u8 *req_ie, size_t req_ie_len,
7760 const u8 *resp_ie, size_t resp_ie_len,
7761 u16 status, gfp_t gfp)
7762{
7763 struct sk_buff *msg;
7764 void *hdr;
7765
7766 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7767 if (!msg)
7768 return;
7769
7770 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7771 if (!hdr) {
7772 nlmsg_free(msg);
7773 return;
7774 }
7775
David S. Miller9360ffd2012-03-29 04:41:26 -04007776 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7777 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7778 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7779 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7780 (req_ie &&
7781 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7782 (resp_ie &&
7783 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7784 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007785
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007786 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007787
Johannes Berg463d0182009-07-14 00:33:35 +02007788 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7789 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007790 return;
7791
7792 nla_put_failure:
7793 genlmsg_cancel(msg, hdr);
7794 nlmsg_free(msg);
7795
7796}
7797
7798void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7799 struct net_device *netdev, const u8 *bssid,
7800 const u8 *req_ie, size_t req_ie_len,
7801 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7802{
7803 struct sk_buff *msg;
7804 void *hdr;
7805
7806 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7807 if (!msg)
7808 return;
7809
7810 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7811 if (!hdr) {
7812 nlmsg_free(msg);
7813 return;
7814 }
7815
David S. Miller9360ffd2012-03-29 04:41:26 -04007816 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7817 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7818 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7819 (req_ie &&
7820 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7821 (resp_ie &&
7822 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7823 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007824
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007825 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007826
Johannes Berg463d0182009-07-14 00:33:35 +02007827 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7828 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007829 return;
7830
7831 nla_put_failure:
7832 genlmsg_cancel(msg, hdr);
7833 nlmsg_free(msg);
7834
7835}
7836
7837void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7838 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007839 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007840{
7841 struct sk_buff *msg;
7842 void *hdr;
7843
Johannes Berg667503dd2009-07-07 03:56:11 +02007844 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007845 if (!msg)
7846 return;
7847
7848 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7849 if (!hdr) {
7850 nlmsg_free(msg);
7851 return;
7852 }
7853
David S. Miller9360ffd2012-03-29 04:41:26 -04007854 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7855 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7856 (from_ap && reason &&
7857 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7858 (from_ap &&
7859 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7860 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7861 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007862
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007863 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007864
Johannes Berg463d0182009-07-14 00:33:35 +02007865 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7866 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007867 return;
7868
7869 nla_put_failure:
7870 genlmsg_cancel(msg, hdr);
7871 nlmsg_free(msg);
7872
7873}
7874
Johannes Berg04a773a2009-04-19 21:24:32 +02007875void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7876 struct net_device *netdev, const u8 *bssid,
7877 gfp_t gfp)
7878{
7879 struct sk_buff *msg;
7880 void *hdr;
7881
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007882 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007883 if (!msg)
7884 return;
7885
7886 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7887 if (!hdr) {
7888 nlmsg_free(msg);
7889 return;
7890 }
7891
David S. Miller9360ffd2012-03-29 04:41:26 -04007892 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7893 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7894 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7895 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007896
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007897 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007898
Johannes Berg463d0182009-07-14 00:33:35 +02007899 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7900 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007901 return;
7902
7903 nla_put_failure:
7904 genlmsg_cancel(msg, hdr);
7905 nlmsg_free(msg);
7906}
7907
Javier Cardonac93b5e72011-04-07 15:08:34 -07007908void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7909 struct net_device *netdev,
7910 const u8 *macaddr, const u8* ie, u8 ie_len,
7911 gfp_t gfp)
7912{
7913 struct sk_buff *msg;
7914 void *hdr;
7915
7916 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7917 if (!msg)
7918 return;
7919
7920 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7921 if (!hdr) {
7922 nlmsg_free(msg);
7923 return;
7924 }
7925
David S. Miller9360ffd2012-03-29 04:41:26 -04007926 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7927 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7928 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7929 (ie_len && ie &&
7930 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7931 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007932
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007933 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007934
7935 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7936 nl80211_mlme_mcgrp.id, gfp);
7937 return;
7938
7939 nla_put_failure:
7940 genlmsg_cancel(msg, hdr);
7941 nlmsg_free(msg);
7942}
7943
Jouni Malinena3b8b052009-03-27 21:59:49 +02007944void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7945 struct net_device *netdev, const u8 *addr,
7946 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007947 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007948{
7949 struct sk_buff *msg;
7950 void *hdr;
7951
Johannes Berge6d6e342009-07-01 21:26:47 +02007952 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007953 if (!msg)
7954 return;
7955
7956 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7957 if (!hdr) {
7958 nlmsg_free(msg);
7959 return;
7960 }
7961
David S. Miller9360ffd2012-03-29 04:41:26 -04007962 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7963 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7964 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
7965 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
7966 (key_id != -1 &&
7967 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
7968 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
7969 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02007970
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007971 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007972
Johannes Berg463d0182009-07-14 00:33:35 +02007973 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7974 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007975 return;
7976
7977 nla_put_failure:
7978 genlmsg_cancel(msg, hdr);
7979 nlmsg_free(msg);
7980}
7981
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007982void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
7983 struct ieee80211_channel *channel_before,
7984 struct ieee80211_channel *channel_after)
7985{
7986 struct sk_buff *msg;
7987 void *hdr;
7988 struct nlattr *nl_freq;
7989
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007990 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04007991 if (!msg)
7992 return;
7993
7994 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
7995 if (!hdr) {
7996 nlmsg_free(msg);
7997 return;
7998 }
7999
8000 /*
8001 * Since we are applying the beacon hint to a wiphy we know its
8002 * wiphy_idx is valid
8003 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008004 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8005 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008006
8007 /* Before */
8008 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8009 if (!nl_freq)
8010 goto nla_put_failure;
8011 if (nl80211_msg_put_channel(msg, channel_before))
8012 goto nla_put_failure;
8013 nla_nest_end(msg, nl_freq);
8014
8015 /* After */
8016 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8017 if (!nl_freq)
8018 goto nla_put_failure;
8019 if (nl80211_msg_put_channel(msg, channel_after))
8020 goto nla_put_failure;
8021 nla_nest_end(msg, nl_freq);
8022
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008023 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008024
Johannes Berg463d0182009-07-14 00:33:35 +02008025 rcu_read_lock();
8026 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8027 GFP_ATOMIC);
8028 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008029
8030 return;
8031
8032nla_put_failure:
8033 genlmsg_cancel(msg, hdr);
8034 nlmsg_free(msg);
8035}
8036
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008037static void nl80211_send_remain_on_chan_event(
8038 int cmd, struct cfg80211_registered_device *rdev,
8039 struct net_device *netdev, u64 cookie,
8040 struct ieee80211_channel *chan,
8041 enum nl80211_channel_type channel_type,
8042 unsigned int duration, gfp_t gfp)
8043{
8044 struct sk_buff *msg;
8045 void *hdr;
8046
8047 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8048 if (!msg)
8049 return;
8050
8051 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8052 if (!hdr) {
8053 nlmsg_free(msg);
8054 return;
8055 }
8056
David S. Miller9360ffd2012-03-29 04:41:26 -04008057 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8058 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8059 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
8060 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
8061 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8062 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008063
David S. Miller9360ffd2012-03-29 04:41:26 -04008064 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8065 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8066 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008067
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008068 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008069
8070 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8071 nl80211_mlme_mcgrp.id, gfp);
8072 return;
8073
8074 nla_put_failure:
8075 genlmsg_cancel(msg, hdr);
8076 nlmsg_free(msg);
8077}
8078
8079void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
8080 struct net_device *netdev, u64 cookie,
8081 struct ieee80211_channel *chan,
8082 enum nl80211_channel_type channel_type,
8083 unsigned int duration, gfp_t gfp)
8084{
8085 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
8086 rdev, netdev, cookie, chan,
8087 channel_type, duration, gfp);
8088}
8089
8090void nl80211_send_remain_on_channel_cancel(
8091 struct cfg80211_registered_device *rdev, struct net_device *netdev,
8092 u64 cookie, struct ieee80211_channel *chan,
8093 enum nl80211_channel_type channel_type, gfp_t gfp)
8094{
8095 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8096 rdev, netdev, cookie, chan,
8097 channel_type, 0, gfp);
8098}
8099
Johannes Berg98b62182009-12-23 13:15:44 +01008100void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8101 struct net_device *dev, const u8 *mac_addr,
8102 struct station_info *sinfo, gfp_t gfp)
8103{
8104 struct sk_buff *msg;
8105
8106 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8107 if (!msg)
8108 return;
8109
John W. Linville66266b32012-03-15 13:25:41 -04008110 if (nl80211_send_station(msg, 0, 0, 0,
8111 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008112 nlmsg_free(msg);
8113 return;
8114 }
8115
8116 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8117 nl80211_mlme_mcgrp.id, gfp);
8118}
8119
Jouni Malinenec15e682011-03-23 15:29:52 +02008120void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8121 struct net_device *dev, const u8 *mac_addr,
8122 gfp_t gfp)
8123{
8124 struct sk_buff *msg;
8125 void *hdr;
8126
8127 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8128 if (!msg)
8129 return;
8130
8131 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8132 if (!hdr) {
8133 nlmsg_free(msg);
8134 return;
8135 }
8136
David S. Miller9360ffd2012-03-29 04:41:26 -04008137 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8138 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8139 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008140
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008141 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008142
8143 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8144 nl80211_mlme_mcgrp.id, gfp);
8145 return;
8146
8147 nla_put_failure:
8148 genlmsg_cancel(msg, hdr);
8149 nlmsg_free(msg);
8150}
8151
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008152static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8153 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008154{
8155 struct wireless_dev *wdev = dev->ieee80211_ptr;
8156 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8157 struct sk_buff *msg;
8158 void *hdr;
8159 int err;
8160 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
8161
8162 if (!nlpid)
8163 return false;
8164
8165 msg = nlmsg_new(100, gfp);
8166 if (!msg)
8167 return true;
8168
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008169 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008170 if (!hdr) {
8171 nlmsg_free(msg);
8172 return true;
8173 }
8174
David S. Miller9360ffd2012-03-29 04:41:26 -04008175 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8176 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8177 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8178 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008179
8180 err = genlmsg_end(msg, hdr);
8181 if (err < 0) {
8182 nlmsg_free(msg);
8183 return true;
8184 }
8185
8186 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8187 return true;
8188
8189 nla_put_failure:
8190 genlmsg_cancel(msg, hdr);
8191 nlmsg_free(msg);
8192 return true;
8193}
8194
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008195bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8196{
8197 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8198 addr, gfp);
8199}
8200
8201bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8202 const u8 *addr, gfp_t gfp)
8203{
8204 return __nl80211_unexpected_frame(dev,
8205 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8206 addr, gfp);
8207}
8208
Johannes Berg2e161f72010-08-12 15:38:38 +02008209int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
8210 struct net_device *netdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01008211 int freq, int sig_dbm,
8212 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008213{
8214 struct sk_buff *msg;
8215 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008216
8217 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8218 if (!msg)
8219 return -ENOMEM;
8220
Johannes Berg2e161f72010-08-12 15:38:38 +02008221 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008222 if (!hdr) {
8223 nlmsg_free(msg);
8224 return -ENOMEM;
8225 }
8226
David S. Miller9360ffd2012-03-29 04:41:26 -04008227 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8228 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8229 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8230 (sig_dbm &&
8231 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8232 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8233 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008234
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008235 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008236
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008237 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008238
8239 nla_put_failure:
8240 genlmsg_cancel(msg, hdr);
8241 nlmsg_free(msg);
8242 return -ENOBUFS;
8243}
8244
Johannes Berg2e161f72010-08-12 15:38:38 +02008245void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
8246 struct net_device *netdev, u64 cookie,
8247 const u8 *buf, size_t len, bool ack,
8248 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008249{
8250 struct sk_buff *msg;
8251 void *hdr;
8252
8253 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8254 if (!msg)
8255 return;
8256
Johannes Berg2e161f72010-08-12 15:38:38 +02008257 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008258 if (!hdr) {
8259 nlmsg_free(msg);
8260 return;
8261 }
8262
David S. Miller9360ffd2012-03-29 04:41:26 -04008263 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8264 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8265 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8266 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8267 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8268 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008269
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008270 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008271
8272 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8273 return;
8274
8275 nla_put_failure:
8276 genlmsg_cancel(msg, hdr);
8277 nlmsg_free(msg);
8278}
8279
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008280void
8281nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8282 struct net_device *netdev,
8283 enum nl80211_cqm_rssi_threshold_event rssi_event,
8284 gfp_t gfp)
8285{
8286 struct sk_buff *msg;
8287 struct nlattr *pinfoattr;
8288 void *hdr;
8289
8290 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8291 if (!msg)
8292 return;
8293
8294 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8295 if (!hdr) {
8296 nlmsg_free(msg);
8297 return;
8298 }
8299
David S. Miller9360ffd2012-03-29 04:41:26 -04008300 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8301 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8302 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008303
8304 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8305 if (!pinfoattr)
8306 goto nla_put_failure;
8307
David S. Miller9360ffd2012-03-29 04:41:26 -04008308 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8309 rssi_event))
8310 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008311
8312 nla_nest_end(msg, pinfoattr);
8313
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008314 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008315
8316 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8317 nl80211_mlme_mcgrp.id, gfp);
8318 return;
8319
8320 nla_put_failure:
8321 genlmsg_cancel(msg, hdr);
8322 nlmsg_free(msg);
8323}
8324
Johannes Berge5497d72011-07-05 16:35:40 +02008325void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8326 struct net_device *netdev, const u8 *bssid,
8327 const u8 *replay_ctr, gfp_t gfp)
8328{
8329 struct sk_buff *msg;
8330 struct nlattr *rekey_attr;
8331 void *hdr;
8332
8333 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8334 if (!msg)
8335 return;
8336
8337 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8338 if (!hdr) {
8339 nlmsg_free(msg);
8340 return;
8341 }
8342
David S. Miller9360ffd2012-03-29 04:41:26 -04008343 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8344 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8345 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8346 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008347
8348 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8349 if (!rekey_attr)
8350 goto nla_put_failure;
8351
David S. Miller9360ffd2012-03-29 04:41:26 -04008352 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8353 NL80211_REPLAY_CTR_LEN, replay_ctr))
8354 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008355
8356 nla_nest_end(msg, rekey_attr);
8357
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008358 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008359
8360 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8361 nl80211_mlme_mcgrp.id, gfp);
8362 return;
8363
8364 nla_put_failure:
8365 genlmsg_cancel(msg, hdr);
8366 nlmsg_free(msg);
8367}
8368
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008369void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8370 struct net_device *netdev, int index,
8371 const u8 *bssid, bool preauth, gfp_t gfp)
8372{
8373 struct sk_buff *msg;
8374 struct nlattr *attr;
8375 void *hdr;
8376
8377 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8378 if (!msg)
8379 return;
8380
8381 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8382 if (!hdr) {
8383 nlmsg_free(msg);
8384 return;
8385 }
8386
David S. Miller9360ffd2012-03-29 04:41:26 -04008387 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8388 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8389 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008390
8391 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8392 if (!attr)
8393 goto nla_put_failure;
8394
David S. Miller9360ffd2012-03-29 04:41:26 -04008395 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8396 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8397 (preauth &&
8398 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8399 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008400
8401 nla_nest_end(msg, attr);
8402
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008403 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008404
8405 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8406 nl80211_mlme_mcgrp.id, gfp);
8407 return;
8408
8409 nla_put_failure:
8410 genlmsg_cancel(msg, hdr);
8411 nlmsg_free(msg);
8412}
8413
Thomas Pedersen53145262012-04-06 13:35:47 -07008414void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8415 struct net_device *netdev, int freq,
8416 enum nl80211_channel_type type, gfp_t gfp)
8417{
8418 struct sk_buff *msg;
8419 void *hdr;
8420
8421 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8422 if (!msg)
8423 return;
8424
8425 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8426 if (!hdr) {
8427 nlmsg_free(msg);
8428 return;
8429 }
8430
John W. Linville7eab0f62012-04-12 14:25:14 -04008431 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8432 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8433 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8434 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008435
8436 genlmsg_end(msg, hdr);
8437
8438 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8439 nl80211_mlme_mcgrp.id, gfp);
8440 return;
8441
8442 nla_put_failure:
8443 genlmsg_cancel(msg, hdr);
8444 nlmsg_free(msg);
8445}
8446
Johannes Bergc063dbf2010-11-24 08:10:05 +01008447void
8448nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8449 struct net_device *netdev, const u8 *peer,
8450 u32 num_packets, gfp_t gfp)
8451{
8452 struct sk_buff *msg;
8453 struct nlattr *pinfoattr;
8454 void *hdr;
8455
8456 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8457 if (!msg)
8458 return;
8459
8460 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8461 if (!hdr) {
8462 nlmsg_free(msg);
8463 return;
8464 }
8465
David S. Miller9360ffd2012-03-29 04:41:26 -04008466 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8467 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8468 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8469 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008470
8471 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8472 if (!pinfoattr)
8473 goto nla_put_failure;
8474
David S. Miller9360ffd2012-03-29 04:41:26 -04008475 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8476 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008477
8478 nla_nest_end(msg, pinfoattr);
8479
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008480 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008481
8482 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8483 nl80211_mlme_mcgrp.id, gfp);
8484 return;
8485
8486 nla_put_failure:
8487 genlmsg_cancel(msg, hdr);
8488 nlmsg_free(msg);
8489}
8490
Johannes Berg7f6cf312011-11-04 11:18:15 +01008491void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8492 u64 cookie, bool acked, gfp_t gfp)
8493{
8494 struct wireless_dev *wdev = dev->ieee80211_ptr;
8495 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8496 struct sk_buff *msg;
8497 void *hdr;
8498 int err;
8499
8500 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8501 if (!msg)
8502 return;
8503
8504 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8505 if (!hdr) {
8506 nlmsg_free(msg);
8507 return;
8508 }
8509
David S. Miller9360ffd2012-03-29 04:41:26 -04008510 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8511 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8512 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8513 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8514 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8515 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008516
8517 err = genlmsg_end(msg, hdr);
8518 if (err < 0) {
8519 nlmsg_free(msg);
8520 return;
8521 }
8522
8523 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8524 nl80211_mlme_mcgrp.id, gfp);
8525 return;
8526
8527 nla_put_failure:
8528 genlmsg_cancel(msg, hdr);
8529 nlmsg_free(msg);
8530}
8531EXPORT_SYMBOL(cfg80211_probe_status);
8532
Johannes Berg5e7602302011-11-04 11:18:17 +01008533void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8534 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008535 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e7602302011-11-04 11:18:17 +01008536{
8537 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8538 struct sk_buff *msg;
8539 void *hdr;
8540 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8541
8542 if (!nlpid)
8543 return;
8544
8545 msg = nlmsg_new(len + 100, gfp);
8546 if (!msg)
8547 return;
8548
8549 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8550 if (!hdr) {
8551 nlmsg_free(msg);
8552 return;
8553 }
8554
David S. Miller9360ffd2012-03-29 04:41:26 -04008555 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8556 (freq &&
8557 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8558 (sig_dbm &&
8559 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8560 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8561 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01008562
8563 genlmsg_end(msg, hdr);
8564
8565 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8566 return;
8567
8568 nla_put_failure:
8569 genlmsg_cancel(msg, hdr);
8570 nlmsg_free(msg);
8571}
8572EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8573
Jouni Malinen026331c2010-02-15 12:53:10 +02008574static int nl80211_netlink_notify(struct notifier_block * nb,
8575 unsigned long state,
8576 void *_notify)
8577{
8578 struct netlink_notify *notify = _notify;
8579 struct cfg80211_registered_device *rdev;
8580 struct wireless_dev *wdev;
8581
8582 if (state != NETLINK_URELEASE)
8583 return NOTIFY_DONE;
8584
8585 rcu_read_lock();
8586
Johannes Berg5e7602302011-11-04 11:18:17 +01008587 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008588 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008589 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e7602302011-11-04 11:18:17 +01008590 if (rdev->ap_beacons_nlpid == notify->pid)
8591 rdev->ap_beacons_nlpid = 0;
8592 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008593
8594 rcu_read_unlock();
8595
8596 return NOTIFY_DONE;
8597}
8598
8599static struct notifier_block nl80211_netlink_notifier = {
8600 .notifier_call = nl80211_netlink_notify,
8601};
8602
Johannes Berg55682962007-09-20 13:09:35 -04008603/* initialisation/exit functions */
8604
8605int nl80211_init(void)
8606{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008607 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008608
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008609 err = genl_register_family_with_ops(&nl80211_fam,
8610 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008611 if (err)
8612 return err;
8613
Johannes Berg55682962007-09-20 13:09:35 -04008614 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8615 if (err)
8616 goto err_out;
8617
Johannes Berg2a519312009-02-10 21:25:55 +01008618 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8619 if (err)
8620 goto err_out;
8621
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008622 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8623 if (err)
8624 goto err_out;
8625
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008626 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8627 if (err)
8628 goto err_out;
8629
Johannes Bergaff89a92009-07-01 21:26:51 +02008630#ifdef CONFIG_NL80211_TESTMODE
8631 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8632 if (err)
8633 goto err_out;
8634#endif
8635
Jouni Malinen026331c2010-02-15 12:53:10 +02008636 err = netlink_register_notifier(&nl80211_netlink_notifier);
8637 if (err)
8638 goto err_out;
8639
Johannes Berg55682962007-09-20 13:09:35 -04008640 return 0;
8641 err_out:
8642 genl_unregister_family(&nl80211_fam);
8643 return err;
8644}
8645
8646void nl80211_exit(void)
8647{
Jouni Malinen026331c2010-02-15 12:53:10 +02008648 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008649 genl_unregister_family(&nl80211_fam);
8650}