blob: e521ca09d6a455d7cc96ce500df845aa57548bc7 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030025#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040026
Jouni Malinen5fb628e2011-08-10 23:54:35 +030027static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg89a54e42012-06-15 14:33:17 +020049/* returns ERR_PTR values */
50static struct wireless_dev *
51__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040052{
Johannes Berg89a54e42012-06-15 14:33:17 +020053 struct cfg80211_registered_device *rdev;
54 struct wireless_dev *result = NULL;
55 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
56 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
57 u64 wdev_id;
58 int wiphy_idx = -1;
59 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040060
Johannes Berg89a54e42012-06-15 14:33:17 +020061 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040062
Johannes Berg89a54e42012-06-15 14:33:17 +020063 if (!have_ifidx && !have_wdev_id)
64 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040065
Johannes Berg89a54e42012-06-15 14:33:17 +020066 if (have_ifidx)
67 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
68 if (have_wdev_id) {
69 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
70 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040071 }
72
Johannes Berg89a54e42012-06-15 14:33:17 +020073 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
74 struct wireless_dev *wdev;
75
76 if (wiphy_net(&rdev->wiphy) != netns)
77 continue;
78
79 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
80 continue;
81
82 mutex_lock(&rdev->devlist_mtx);
83 list_for_each_entry(wdev, &rdev->wdev_list, list) {
84 if (have_ifidx && wdev->netdev &&
85 wdev->netdev->ifindex == ifidx) {
86 result = wdev;
87 break;
88 }
89 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
90 result = wdev;
91 break;
92 }
93 }
94 mutex_unlock(&rdev->devlist_mtx);
95
96 if (result)
97 break;
98 }
99
100 if (result)
101 return result;
102 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400103}
104
Johannes Berga9455402012-06-15 13:32:49 +0200105static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200106__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200107{
Johannes Berg7fee47782012-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 Berg7fee47782012-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 Berg7fee47782012-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 Berg7fee47782012-06-15 14:09:58 +0200151 if (netdev) {
152 if (netdev->ieee80211_ptr)
153 tmp = wiphy_to_dev(
154 netdev->ieee80211_ptr->wiphy);
155 else
156 tmp = NULL;
157
158 dev_put(netdev);
159
160 /* not wireless device -- return error */
161 if (!tmp)
162 return ERR_PTR(-EINVAL);
163
164 /* mismatch -- return error */
165 if (rdev && tmp != rdev)
166 return ERR_PTR(-EINVAL);
167
168 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200169 }
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
171
Johannes Berg4f7eff12012-06-15 14:14:22 +0200172 if (!rdev)
173 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200174
Johannes Berg4f7eff12012-06-15 14:14:22 +0200175 if (netns != wiphy_net(&rdev->wiphy))
176 return ERR_PTR(-ENODEV);
177
178 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200179}
180
181/*
182 * This function returns a pointer to the driver
183 * that the genl_info item that is passed refers to.
184 * If successful, it returns non-NULL and also locks
185 * the driver's mutex!
186 *
187 * This means that you need to call cfg80211_unlock_rdev()
188 * before being allowed to acquire &cfg80211_mutex!
189 *
190 * This is necessary because we need to lock the global
191 * mutex to get an item off the list safely, and then
192 * we lock the rdev mutex so it doesn't go away under us.
193 *
194 * We don't want to keep cfg80211_mutex locked
195 * for all the time in order to allow requests on
196 * other interfaces to go through at the same time.
197 *
198 * The result of this can be a PTR_ERR and hence must
199 * be checked with IS_ERR() for errors.
200 */
201static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200202cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200203{
204 struct cfg80211_registered_device *rdev;
205
206 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200207 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208
209 /* if it is not an error we grab the lock on
210 * it to assure it won't be going away while
211 * we operate on it */
212 if (!IS_ERR(rdev))
213 mutex_lock(&rdev->mtx);
214
215 mutex_unlock(&cfg80211_mutex);
216
217 return rdev;
218}
219
Johannes Berg55682962007-09-20 13:09:35 -0400220/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000221static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400222 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
223 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700224 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200225 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200226 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530227 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200228 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
229 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
230 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
231 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100232 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400233
234 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
235 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
236 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100237
Eliad Pellere007b852011-11-24 18:13:56 +0200238 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
239 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100240
Johannes Bergb9454e82009-07-08 13:29:08 +0200241 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100242 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
243 .len = WLAN_MAX_KEY_LEN },
244 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
245 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
246 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200247 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200248 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100249
250 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
251 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
252 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
253 .len = IEEE80211_MAX_DATA_LEN },
254 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
255 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100256 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
257 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
258 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
259 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
260 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200263 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100264 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800265 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100266 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300267
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700268 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
269 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
270
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300271 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
272 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
273 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200274 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
275 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100276 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300277
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800278 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700279 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700280
Johannes Berg6c739412011-11-03 09:27:01 +0100281 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200282
283 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
284 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100286 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
287 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200288
289 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
290 .len = IEEE80211_MAX_SSID_LEN },
291 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
292 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200293 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300294 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300295 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300296 [NL80211_ATTR_STA_FLAGS2] = {
297 .len = sizeof(struct nl80211_sta_flag_update),
298 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300299 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300300 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
301 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200302 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
303 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
304 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200305 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100306 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100307 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
308 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100309 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
310 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200311 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200312 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
313 .len = IEEE80211_MAX_DATA_LEN },
314 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200315 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200316 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300317 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200318 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300319 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
320 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f782010-08-12 15:38:38 +0200321 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900322 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
323 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100324 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100325 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100326 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200327 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700328 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300329 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200330 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200331 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300332 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300333 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
334 .len = IEEE80211_MAX_DATA_LEN },
335 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
336 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530337 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300338 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530339 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300340 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
341 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
342 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
343 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
344 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100345 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200346 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
347 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700348 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800349 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
351 .len = NL80211_HT_CAPABILITY_LEN
352 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100353 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530354 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530355 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200356 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700357 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300358 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000359 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700360 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400361};
362
Johannes Berge31b8212010-10-05 19:39:30 +0200363/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000364static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200365 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200366 [NL80211_KEY_IDX] = { .type = NLA_U8 },
367 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200368 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200369 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
370 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200371 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100372 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
373};
374
375/* policy for the key default flags */
376static const struct nla_policy
377nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
378 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
379 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200380};
381
Johannes Bergff1b6e62011-05-04 15:37:28 +0200382/* policy for WoWLAN attributes */
383static const struct nla_policy
384nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
385 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
386 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
387 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
388 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200389 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
390 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
391 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
392 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200393};
394
Johannes Berge5497d72011-07-05 16:35:40 +0200395/* policy for GTK rekey offload attributes */
396static const struct nla_policy
397nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
398 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
399 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
400 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
401};
402
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300403static const struct nla_policy
404nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200405 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300406 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700407 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300408};
409
Holger Schuriga0438972009-11-11 11:30:02 +0100410/* ifidx get helper */
411static int nl80211_get_ifidx(struct netlink_callback *cb)
412{
413 int res;
414
415 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
416 nl80211_fam.attrbuf, nl80211_fam.maxattr,
417 nl80211_policy);
418 if (res)
419 return res;
420
421 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
422 return -EINVAL;
423
424 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
425 if (!res)
426 return -EINVAL;
427 return res;
428}
429
Johannes Berg67748892010-10-04 21:14:06 +0200430static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
431 struct netlink_callback *cb,
432 struct cfg80211_registered_device **rdev,
433 struct net_device **dev)
434{
435 int ifidx = cb->args[0];
436 int err;
437
438 if (!ifidx)
439 ifidx = nl80211_get_ifidx(cb);
440 if (ifidx < 0)
441 return ifidx;
442
443 cb->args[0] = ifidx;
444
445 rtnl_lock();
446
447 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
448 if (!*dev) {
449 err = -ENODEV;
450 goto out_rtnl;
451 }
452
453 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100454 if (IS_ERR(*rdev)) {
455 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200456 goto out_rtnl;
457 }
458
459 return 0;
460 out_rtnl:
461 rtnl_unlock();
462 return err;
463}
464
465static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
466{
467 cfg80211_unlock_rdev(rdev);
468 rtnl_unlock();
469}
470
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100471/* IE validation */
472static bool is_valid_ie_attr(const struct nlattr *attr)
473{
474 const u8 *pos;
475 int len;
476
477 if (!attr)
478 return true;
479
480 pos = nla_data(attr);
481 len = nla_len(attr);
482
483 while (len) {
484 u8 elemlen;
485
486 if (len < 2)
487 return false;
488 len -= 2;
489
490 elemlen = pos[1];
491 if (elemlen > len)
492 return false;
493
494 len -= elemlen;
495 pos += 2 + elemlen;
496 }
497
498 return true;
499}
500
Johannes Berg55682962007-09-20 13:09:35 -0400501/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000502static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400503 int flags, u8 cmd)
504{
505 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000506 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400507}
508
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400509static int nl80211_msg_put_channel(struct sk_buff *msg,
510 struct ieee80211_channel *chan)
511{
David S. Miller9360ffd2012-03-29 04:41:26 -0400512 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
513 chan->center_freq))
514 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400515
David S. Miller9360ffd2012-03-29 04:41:26 -0400516 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
517 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
518 goto nla_put_failure;
519 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
520 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
521 goto nla_put_failure;
522 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
523 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
524 goto nla_put_failure;
525 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
526 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
527 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400528
David S. Miller9360ffd2012-03-29 04:41:26 -0400529 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
530 DBM_TO_MBM(chan->max_power)))
531 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400532
533 return 0;
534
535 nla_put_failure:
536 return -ENOBUFS;
537}
538
Johannes Berg55682962007-09-20 13:09:35 -0400539/* netlink command implementations */
540
Johannes Bergb9454e82009-07-08 13:29:08 +0200541struct key_parse {
542 struct key_params p;
543 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200544 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200545 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100546 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200547};
548
549static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
550{
551 struct nlattr *tb[NL80211_KEY_MAX + 1];
552 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
553 nl80211_key_policy);
554 if (err)
555 return err;
556
557 k->def = !!tb[NL80211_KEY_DEFAULT];
558 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
559
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100560 if (k->def) {
561 k->def_uni = true;
562 k->def_multi = true;
563 }
564 if (k->defmgmt)
565 k->def_multi = true;
566
Johannes Bergb9454e82009-07-08 13:29:08 +0200567 if (tb[NL80211_KEY_IDX])
568 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
569
570 if (tb[NL80211_KEY_DATA]) {
571 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
572 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
573 }
574
575 if (tb[NL80211_KEY_SEQ]) {
576 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
577 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
578 }
579
580 if (tb[NL80211_KEY_CIPHER])
581 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
582
Johannes Berge31b8212010-10-05 19:39:30 +0200583 if (tb[NL80211_KEY_TYPE]) {
584 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
585 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
586 return -EINVAL;
587 }
588
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100589 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
590 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100591 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
592 tb[NL80211_KEY_DEFAULT_TYPES],
593 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100594 if (err)
595 return err;
596
597 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
598 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
599 }
600
Johannes Bergb9454e82009-07-08 13:29:08 +0200601 return 0;
602}
603
604static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
605{
606 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
607 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
608 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
609 }
610
611 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
612 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
613 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
614 }
615
616 if (info->attrs[NL80211_ATTR_KEY_IDX])
617 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
618
619 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
620 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
621
622 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
623 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
624
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100625 if (k->def) {
626 k->def_uni = true;
627 k->def_multi = true;
628 }
629 if (k->defmgmt)
630 k->def_multi = true;
631
Johannes Berge31b8212010-10-05 19:39:30 +0200632 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
633 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
634 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
635 return -EINVAL;
636 }
637
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100638 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
639 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
640 int err = nla_parse_nested(
641 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
642 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
643 nl80211_key_default_policy);
644 if (err)
645 return err;
646
647 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
648 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
649 }
650
Johannes Bergb9454e82009-07-08 13:29:08 +0200651 return 0;
652}
653
654static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
655{
656 int err;
657
658 memset(k, 0, sizeof(*k));
659 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200660 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200661
662 if (info->attrs[NL80211_ATTR_KEY])
663 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
664 else
665 err = nl80211_parse_key_old(info, k);
666
667 if (err)
668 return err;
669
670 if (k->def && k->defmgmt)
671 return -EINVAL;
672
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100673 if (k->defmgmt) {
674 if (k->def_uni || !k->def_multi)
675 return -EINVAL;
676 }
677
Johannes Bergb9454e82009-07-08 13:29:08 +0200678 if (k->idx != -1) {
679 if (k->defmgmt) {
680 if (k->idx < 4 || k->idx > 5)
681 return -EINVAL;
682 } else if (k->def) {
683 if (k->idx < 0 || k->idx > 3)
684 return -EINVAL;
685 } else {
686 if (k->idx < 0 || k->idx > 5)
687 return -EINVAL;
688 }
689 }
690
691 return 0;
692}
693
Johannes Bergfffd0932009-07-08 14:22:54 +0200694static struct cfg80211_cached_keys *
695nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530696 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200697{
698 struct key_parse parse;
699 struct nlattr *key;
700 struct cfg80211_cached_keys *result;
701 int rem, err, def = 0;
702
703 result = kzalloc(sizeof(*result), GFP_KERNEL);
704 if (!result)
705 return ERR_PTR(-ENOMEM);
706
707 result->def = -1;
708 result->defmgmt = -1;
709
710 nla_for_each_nested(key, keys, rem) {
711 memset(&parse, 0, sizeof(parse));
712 parse.idx = -1;
713
714 err = nl80211_parse_key_new(key, &parse);
715 if (err)
716 goto error;
717 err = -EINVAL;
718 if (!parse.p.key)
719 goto error;
720 if (parse.idx < 0 || parse.idx > 4)
721 goto error;
722 if (parse.def) {
723 if (def)
724 goto error;
725 def = 1;
726 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100727 if (!parse.def_uni || !parse.def_multi)
728 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200729 } else if (parse.defmgmt)
730 goto error;
731 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200732 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200733 if (err)
734 goto error;
735 result->params[parse.idx].cipher = parse.p.cipher;
736 result->params[parse.idx].key_len = parse.p.key_len;
737 result->params[parse.idx].key = result->data[parse.idx];
738 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530739
740 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
741 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
742 if (no_ht)
743 *no_ht = true;
744 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200745 }
746
747 return result;
748 error:
749 kfree(result);
750 return ERR_PTR(err);
751}
752
753static int nl80211_key_allowed(struct wireless_dev *wdev)
754{
755 ASSERT_WDEV_LOCK(wdev);
756
Johannes Bergfffd0932009-07-08 14:22:54 +0200757 switch (wdev->iftype) {
758 case NL80211_IFTYPE_AP:
759 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200760 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700761 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200762 break;
763 case NL80211_IFTYPE_ADHOC:
764 if (!wdev->current_bss)
765 return -ENOLINK;
766 break;
767 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200768 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200769 if (wdev->sme_state != CFG80211_SME_CONNECTED)
770 return -ENOLINK;
771 break;
772 default:
773 return -EINVAL;
774 }
775
776 return 0;
777}
778
Johannes Berg7527a782011-05-13 10:58:57 +0200779static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
780{
781 struct nlattr *nl_modes = nla_nest_start(msg, attr);
782 int i;
783
784 if (!nl_modes)
785 goto nla_put_failure;
786
787 i = 0;
788 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400789 if ((ifmodes & 1) && nla_put_flag(msg, i))
790 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200791 ifmodes >>= 1;
792 i++;
793 }
794
795 nla_nest_end(msg, nl_modes);
796 return 0;
797
798nla_put_failure:
799 return -ENOBUFS;
800}
801
802static int nl80211_put_iface_combinations(struct wiphy *wiphy,
803 struct sk_buff *msg)
804{
805 struct nlattr *nl_combis;
806 int i, j;
807
808 nl_combis = nla_nest_start(msg,
809 NL80211_ATTR_INTERFACE_COMBINATIONS);
810 if (!nl_combis)
811 goto nla_put_failure;
812
813 for (i = 0; i < wiphy->n_iface_combinations; i++) {
814 const struct ieee80211_iface_combination *c;
815 struct nlattr *nl_combi, *nl_limits;
816
817 c = &wiphy->iface_combinations[i];
818
819 nl_combi = nla_nest_start(msg, i + 1);
820 if (!nl_combi)
821 goto nla_put_failure;
822
823 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
824 if (!nl_limits)
825 goto nla_put_failure;
826
827 for (j = 0; j < c->n_limits; j++) {
828 struct nlattr *nl_limit;
829
830 nl_limit = nla_nest_start(msg, j + 1);
831 if (!nl_limit)
832 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400833 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
834 c->limits[j].max))
835 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200836 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
837 c->limits[j].types))
838 goto nla_put_failure;
839 nla_nest_end(msg, nl_limit);
840 }
841
842 nla_nest_end(msg, nl_limits);
843
David S. Miller9360ffd2012-03-29 04:41:26 -0400844 if (c->beacon_int_infra_match &&
845 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
846 goto nla_put_failure;
847 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
848 c->num_different_channels) ||
849 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
850 c->max_interfaces))
851 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200852
853 nla_nest_end(msg, nl_combi);
854 }
855
856 nla_nest_end(msg, nl_combis);
857
858 return 0;
859nla_put_failure:
860 return -ENOBUFS;
861}
862
Eric W. Biederman15e47302012-09-07 20:12:54 +0000863static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400864 struct cfg80211_registered_device *dev)
865{
866 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100867 struct nlattr *nl_bands, *nl_band;
868 struct nlattr *nl_freqs, *nl_freq;
869 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100870 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100871 enum ieee80211_band band;
872 struct ieee80211_channel *chan;
873 struct ieee80211_rate *rate;
874 int i;
Johannes Berg2e161f782010-08-12 15:38:38 +0200875 const struct ieee80211_txrx_stypes *mgmt_stypes =
876 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400877
Eric W. Biederman15e47302012-09-07 20:12:54 +0000878 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400879 if (!hdr)
880 return -1;
881
David S. Miller9360ffd2012-03-29 04:41:26 -0400882 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
883 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
884 nla_put_u32(msg, NL80211_ATTR_GENERATION,
885 cfg80211_rdev_list_generation) ||
886 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
887 dev->wiphy.retry_short) ||
888 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
889 dev->wiphy.retry_long) ||
890 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
891 dev->wiphy.frag_threshold) ||
892 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
893 dev->wiphy.rts_threshold) ||
894 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
895 dev->wiphy.coverage_class) ||
896 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
897 dev->wiphy.max_scan_ssids) ||
898 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
899 dev->wiphy.max_sched_scan_ssids) ||
900 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
901 dev->wiphy.max_scan_ie_len) ||
902 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
903 dev->wiphy.max_sched_scan_ie_len) ||
904 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
905 dev->wiphy.max_match_sets))
906 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200907
David S. Miller9360ffd2012-03-29 04:41:26 -0400908 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
909 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
910 goto nla_put_failure;
911 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
912 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
913 goto nla_put_failure;
914 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
915 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
916 goto nla_put_failure;
917 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
918 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
919 goto nla_put_failure;
920 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
921 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
922 goto nla_put_failure;
923 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
924 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
925 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200926
David S. Miller9360ffd2012-03-29 04:41:26 -0400927 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
928 sizeof(u32) * dev->wiphy.n_cipher_suites,
929 dev->wiphy.cipher_suites))
930 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100931
David S. Miller9360ffd2012-03-29 04:41:26 -0400932 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
933 dev->wiphy.max_num_pmkids))
934 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530935
David S. Miller9360ffd2012-03-29 04:41:26 -0400936 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
937 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
938 goto nla_put_failure;
Johannes Berg25e47c182009-04-02 20:14:06 +0200939
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
941 dev->wiphy.available_antennas_tx) ||
942 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
943 dev->wiphy.available_antennas_rx))
944 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100945
David S. Miller9360ffd2012-03-29 04:41:26 -0400946 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
947 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
948 dev->wiphy.probe_resp_offload))
949 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200950
Bruno Randolf7f531e02010-12-16 11:30:22 +0900951 if ((dev->wiphy.available_antennas_tx ||
952 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900953 u32 tx_ant = 0, rx_ant = 0;
954 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300955 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900956 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400957 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
958 tx_ant) ||
959 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
960 rx_ant))
961 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900962 }
963 }
964
Johannes Berg7527a782011-05-13 10:58:57 +0200965 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
966 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700967 goto nla_put_failure;
968
Johannes Bergee688b002008-01-24 19:38:39 +0100969 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
970 if (!nl_bands)
971 goto nla_put_failure;
972
973 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
974 if (!dev->wiphy.bands[band])
975 continue;
976
977 nl_band = nla_nest_start(msg, band);
978 if (!nl_band)
979 goto nla_put_failure;
980
Johannes Bergd51626d2008-10-09 12:20:13 +0200981 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400982 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
983 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
984 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
985 &dev->wiphy.bands[band]->ht_cap.mcs) ||
986 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
987 dev->wiphy.bands[band]->ht_cap.cap) ||
988 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
989 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
990 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
991 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
992 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200993
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000994 /* add VHT info */
995 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
996 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
997 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
998 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
999 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1000 dev->wiphy.bands[band]->vht_cap.cap)))
1001 goto nla_put_failure;
1002
Johannes Bergee688b002008-01-24 19:38:39 +01001003 /* add frequencies */
1004 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1005 if (!nl_freqs)
1006 goto nla_put_failure;
1007
1008 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1009 nl_freq = nla_nest_start(msg, i);
1010 if (!nl_freq)
1011 goto nla_put_failure;
1012
1013 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001014
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001015 if (nl80211_msg_put_channel(msg, chan))
1016 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001017
Johannes Bergee688b002008-01-24 19:38:39 +01001018 nla_nest_end(msg, nl_freq);
1019 }
1020
1021 nla_nest_end(msg, nl_freqs);
1022
1023 /* add bitrates */
1024 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1025 if (!nl_rates)
1026 goto nla_put_failure;
1027
1028 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1029 nl_rate = nla_nest_start(msg, i);
1030 if (!nl_rate)
1031 goto nla_put_failure;
1032
1033 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001034 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1035 rate->bitrate))
1036 goto nla_put_failure;
1037 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1038 nla_put_flag(msg,
1039 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1040 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001041
1042 nla_nest_end(msg, nl_rate);
1043 }
1044
1045 nla_nest_end(msg, nl_rates);
1046
1047 nla_nest_end(msg, nl_band);
1048 }
1049 nla_nest_end(msg, nl_bands);
1050
Johannes Berg8fdc6212009-03-14 09:34:01 +01001051 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1052 if (!nl_cmds)
1053 goto nla_put_failure;
1054
1055 i = 0;
1056#define CMD(op, n) \
1057 do { \
1058 if (dev->ops->op) { \
1059 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001060 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1061 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001062 } \
1063 } while (0)
1064
1065 CMD(add_virtual_intf, NEW_INTERFACE);
1066 CMD(change_virtual_intf, SET_INTERFACE);
1067 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001068 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001069 CMD(add_station, NEW_STATION);
1070 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001071 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001072 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001073 CMD(auth, AUTHENTICATE);
1074 CMD(assoc, ASSOCIATE);
1075 CMD(deauth, DEAUTHENTICATE);
1076 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001077 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001078 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001079 CMD(set_pmksa, SET_PMKSA);
1080 CMD(del_pmksa, DEL_PMKSA);
1081 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001082 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1083 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001084 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f782010-08-12 15:38:38 +02001085 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001086 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001087 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001088 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001089 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1090 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001091 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001092 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001093 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001094 i++;
1095 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1096 goto nla_put_failure;
1097 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001098 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001099 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1100 CMD(tdls_mgmt, TDLS_MGMT);
1101 CMD(tdls_oper, TDLS_OPER);
1102 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001103 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1104 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001105 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001106 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001107 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1108 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001109 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1110 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001111 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001112 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001113 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001114
Kalle Valo4745fc02011-11-17 19:06:10 +02001115#ifdef CONFIG_NL80211_TESTMODE
1116 CMD(testmode_cmd, TESTMODE);
1117#endif
1118
Johannes Berg8fdc6212009-03-14 09:34:01 +01001119#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001120
Johannes Berg6829c872009-07-02 09:13:27 +02001121 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001122 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001123 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1124 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001125 }
1126
Johannes Berg6829c872009-07-02 09:13:27 +02001127 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001128 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001129 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1130 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001131 }
1132
Johannes Berg8fdc6212009-03-14 09:34:01 +01001133 nla_nest_end(msg, nl_cmds);
1134
Johannes Berg7c4ef712011-11-18 15:33:48 +01001135 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001136 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1137 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1138 dev->wiphy.max_remain_on_channel_duration))
1139 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001140
David S. Miller9360ffd2012-03-29 04:41:26 -04001141 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1142 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1143 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001144
Johannes Berg2e161f782010-08-12 15:38:38 +02001145 if (mgmt_stypes) {
1146 u16 stypes;
1147 struct nlattr *nl_ftypes, *nl_ifs;
1148 enum nl80211_iftype ift;
1149
1150 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1151 if (!nl_ifs)
1152 goto nla_put_failure;
1153
1154 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1155 nl_ftypes = nla_nest_start(msg, ift);
1156 if (!nl_ftypes)
1157 goto nla_put_failure;
1158 i = 0;
1159 stypes = mgmt_stypes[ift].tx;
1160 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001161 if ((stypes & 1) &&
1162 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1163 (i << 4) | IEEE80211_FTYPE_MGMT))
1164 goto nla_put_failure;
Johannes Berg2e161f782010-08-12 15:38:38 +02001165 stypes >>= 1;
1166 i++;
1167 }
1168 nla_nest_end(msg, nl_ftypes);
1169 }
1170
Johannes Berg74b70a42010-08-24 12:15:53 +02001171 nla_nest_end(msg, nl_ifs);
1172
Johannes Berg2e161f782010-08-12 15:38:38 +02001173 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1174 if (!nl_ifs)
1175 goto nla_put_failure;
1176
1177 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1178 nl_ftypes = nla_nest_start(msg, ift);
1179 if (!nl_ftypes)
1180 goto nla_put_failure;
1181 i = 0;
1182 stypes = mgmt_stypes[ift].rx;
1183 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001184 if ((stypes & 1) &&
1185 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1186 (i << 4) | IEEE80211_FTYPE_MGMT))
1187 goto nla_put_failure;
Johannes Berg2e161f782010-08-12 15:38:38 +02001188 stypes >>= 1;
1189 i++;
1190 }
1191 nla_nest_end(msg, nl_ftypes);
1192 }
1193 nla_nest_end(msg, nl_ifs);
1194 }
1195
Johannes Bergdfb89c52012-06-27 09:23:48 +02001196#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001197 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1198 struct nlattr *nl_wowlan;
1199
1200 nl_wowlan = nla_nest_start(msg,
1201 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1202 if (!nl_wowlan)
1203 goto nla_put_failure;
1204
David S. Miller9360ffd2012-03-29 04:41:26 -04001205 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1206 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1207 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1208 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1209 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1210 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1211 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1212 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1213 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1214 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1215 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1216 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1217 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1218 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1219 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1220 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1221 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001222 if (dev->wiphy.wowlan.n_patterns) {
1223 struct nl80211_wowlan_pattern_support pat = {
1224 .max_patterns = dev->wiphy.wowlan.n_patterns,
1225 .min_pattern_len =
1226 dev->wiphy.wowlan.pattern_min_len,
1227 .max_pattern_len =
1228 dev->wiphy.wowlan.pattern_max_len,
1229 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001230 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1231 sizeof(pat), &pat))
1232 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001233 }
1234
1235 nla_nest_end(msg, nl_wowlan);
1236 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001237#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001238
Johannes Berg7527a782011-05-13 10:58:57 +02001239 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1240 dev->wiphy.software_iftypes))
1241 goto nla_put_failure;
1242
1243 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1244 goto nla_put_failure;
1245
David S. Miller9360ffd2012-03-29 04:41:26 -04001246 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1247 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1248 dev->wiphy.ap_sme_capa))
1249 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001250
David S. Miller9360ffd2012-03-29 04:41:26 -04001251 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1252 dev->wiphy.features))
1253 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001254
David S. Miller9360ffd2012-03-29 04:41:26 -04001255 if (dev->wiphy.ht_capa_mod_mask &&
1256 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1257 sizeof(*dev->wiphy.ht_capa_mod_mask),
1258 dev->wiphy.ht_capa_mod_mask))
1259 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001260
Johannes Berg55682962007-09-20 13:09:35 -04001261 return genlmsg_end(msg, hdr);
1262
1263 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001264 genlmsg_cancel(msg, hdr);
1265 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001266}
1267
1268static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1269{
1270 int idx = 0;
1271 int start = cb->args[0];
1272 struct cfg80211_registered_device *dev;
1273
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001274 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001275 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001276 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1277 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001278 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001279 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001280 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001281 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001282 dev) < 0) {
1283 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001284 break;
Julius Volzb4637272008-07-08 14:02:19 +02001285 }
Johannes Berg55682962007-09-20 13:09:35 -04001286 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001287 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001288
1289 cb->args[0] = idx;
1290
1291 return skb->len;
1292}
1293
1294static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1295{
1296 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001297 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001298
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001299 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001300 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001301 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001302
Eric W. Biederman15e47302012-09-07 20:12:54 +00001303 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001304 nlmsg_free(msg);
1305 return -ENOBUFS;
1306 }
Johannes Berg55682962007-09-20 13:09:35 -04001307
Johannes Berg134e6372009-07-10 09:51:34 +00001308 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001309}
1310
Jouni Malinen31888482008-10-30 16:59:24 +02001311static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1312 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1313 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1314 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1315 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1316 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1317};
1318
1319static int parse_txq_params(struct nlattr *tb[],
1320 struct ieee80211_txq_params *txq_params)
1321{
Johannes Berga3304b02012-03-28 11:04:24 +02001322 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001323 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1324 !tb[NL80211_TXQ_ATTR_AIFS])
1325 return -EINVAL;
1326
Johannes Berga3304b02012-03-28 11:04:24 +02001327 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001328 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1329 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1330 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1331 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1332
Johannes Berga3304b02012-03-28 11:04:24 +02001333 if (txq_params->ac >= NL80211_NUM_ACS)
1334 return -EINVAL;
1335
Jouni Malinen31888482008-10-30 16:59:24 +02001336 return 0;
1337}
1338
Johannes Bergf444de02010-05-05 15:25:02 +02001339static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1340{
1341 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001342 * You can only set the channel explicitly for WDS interfaces,
1343 * all others have their channel managed via their respective
1344 * "establish a connection" command (connect, join, ...)
1345 *
1346 * For AP/GO and mesh mode, the channel can be set with the
1347 * channel userspace API, but is only stored and passed to the
1348 * low-level driver when the AP starts or the mesh is joined.
1349 * This is for backward compatibility, userspace can also give
1350 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001351 *
1352 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001353 * whatever else is going on, so they have their own special
1354 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001355 */
1356 return !wdev ||
1357 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001358 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001359 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1360 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001361}
1362
Johannes Bergcd6c65982012-05-10 21:27:18 +02001363static bool nl80211_valid_channel_type(struct genl_info *info,
1364 enum nl80211_channel_type *channel_type)
1365{
1366 enum nl80211_channel_type tmp;
1367
1368 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1369 return false;
1370
1371 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1372 if (tmp != NL80211_CHAN_NO_HT &&
1373 tmp != NL80211_CHAN_HT20 &&
1374 tmp != NL80211_CHAN_HT40PLUS &&
1375 tmp != NL80211_CHAN_HT40MINUS)
1376 return false;
1377
1378 if (channel_type)
1379 *channel_type = tmp;
1380
1381 return true;
1382}
1383
Johannes Bergf444de02010-05-05 15:25:02 +02001384static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1385 struct wireless_dev *wdev,
1386 struct genl_info *info)
1387{
Johannes Bergaa430da2012-05-16 23:50:18 +02001388 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001389 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1390 u32 freq;
1391 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001392 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1393
1394 if (wdev)
1395 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001396
1397 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1398 return -EINVAL;
1399
1400 if (!nl80211_can_set_dev_channel(wdev))
1401 return -EOPNOTSUPP;
1402
Johannes Bergcd6c65982012-05-10 21:27:18 +02001403 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1404 !nl80211_valid_channel_type(info, &channel_type))
1405 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001406
1407 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1408
1409 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001410 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001411 case NL80211_IFTYPE_AP:
1412 case NL80211_IFTYPE_P2P_GO:
1413 if (wdev->beacon_interval) {
1414 result = -EBUSY;
1415 break;
1416 }
1417 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1418 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1419 channel,
1420 channel_type)) {
1421 result = -EINVAL;
1422 break;
1423 }
1424 wdev->preset_chan = channel;
1425 wdev->preset_chantype = channel_type;
1426 result = 0;
1427 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001428 case NL80211_IFTYPE_MESH_POINT:
1429 result = cfg80211_set_mesh_freq(rdev, wdev, freq, channel_type);
1430 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001431 case NL80211_IFTYPE_MONITOR:
1432 result = cfg80211_set_monitor_channel(rdev, freq, channel_type);
1433 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001434 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001435 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001436 }
1437 mutex_unlock(&rdev->devlist_mtx);
1438
1439 return result;
1440}
1441
1442static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1443{
Johannes Berg4c476992010-10-04 21:36:35 +02001444 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1445 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001446
Johannes Berg4c476992010-10-04 21:36:35 +02001447 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001448}
1449
Bill Jordane8347eb2010-10-01 13:54:28 -04001450static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1451{
Johannes Berg43b19952010-10-07 13:10:30 +02001452 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1453 struct net_device *dev = info->user_ptr[1];
1454 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001455 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001456
1457 if (!info->attrs[NL80211_ATTR_MAC])
1458 return -EINVAL;
1459
Johannes Berg43b19952010-10-07 13:10:30 +02001460 if (netif_running(dev))
1461 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001462
Johannes Berg43b19952010-10-07 13:10:30 +02001463 if (!rdev->ops->set_wds_peer)
1464 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001465
Johannes Berg43b19952010-10-07 13:10:30 +02001466 if (wdev->iftype != NL80211_IFTYPE_WDS)
1467 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001468
1469 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001470 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001471}
1472
1473
Johannes Berg55682962007-09-20 13:09:35 -04001474static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1475{
1476 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001477 struct net_device *netdev = NULL;
1478 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001479 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001480 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001481 u32 changed;
1482 u8 retry_short = 0, retry_long = 0;
1483 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001484 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001485
Johannes Bergf444de02010-05-05 15:25:02 +02001486 /*
1487 * Try to find the wiphy and netdev. Normally this
1488 * function shouldn't need the netdev, but this is
1489 * done for backward compatibility -- previously
1490 * setting the channel was done per wiphy, but now
1491 * it is per netdev. Previous userland like hostapd
1492 * also passed a netdev to set_wiphy, so that it is
1493 * possible to let that go to the right netdev!
1494 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001495 mutex_lock(&cfg80211_mutex);
1496
Johannes Bergf444de02010-05-05 15:25:02 +02001497 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1498 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1499
1500 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1501 if (netdev && netdev->ieee80211_ptr) {
1502 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1503 mutex_lock(&rdev->mtx);
1504 } else
1505 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001506 }
1507
Johannes Bergf444de02010-05-05 15:25:02 +02001508 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001509 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1510 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001511 if (IS_ERR(rdev)) {
1512 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001513 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001514 }
1515 wdev = NULL;
1516 netdev = NULL;
1517 result = 0;
1518
1519 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001520 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001521 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001522
1523 /*
1524 * end workaround code, by now the rdev is available
1525 * and locked, and wdev may or may not be NULL.
1526 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001527
1528 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001529 result = cfg80211_dev_rename(
1530 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001531
1532 mutex_unlock(&cfg80211_mutex);
1533
1534 if (result)
1535 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001536
Jouni Malinen31888482008-10-30 16:59:24 +02001537 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1538 struct ieee80211_txq_params txq_params;
1539 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1540
1541 if (!rdev->ops->set_txq_params) {
1542 result = -EOPNOTSUPP;
1543 goto bad_res;
1544 }
1545
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001546 if (!netdev) {
1547 result = -EINVAL;
1548 goto bad_res;
1549 }
1550
Johannes Berg133a3ff2011-11-03 14:50:13 +01001551 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1552 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1553 result = -EINVAL;
1554 goto bad_res;
1555 }
1556
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001557 if (!netif_running(netdev)) {
1558 result = -ENETDOWN;
1559 goto bad_res;
1560 }
1561
Jouni Malinen31888482008-10-30 16:59:24 +02001562 nla_for_each_nested(nl_txq_params,
1563 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1564 rem_txq_params) {
1565 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1566 nla_data(nl_txq_params),
1567 nla_len(nl_txq_params),
1568 txq_params_policy);
1569 result = parse_txq_params(tb, &txq_params);
1570 if (result)
1571 goto bad_res;
1572
Hila Gonene35e4d22012-06-27 17:19:42 +03001573 result = rdev_set_txq_params(rdev, netdev,
1574 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001575 if (result)
1576 goto bad_res;
1577 }
1578 }
1579
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001580 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001581 result = __nl80211_set_channel(rdev,
1582 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1583 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001584 if (result)
1585 goto bad_res;
1586 }
1587
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001588 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001589 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001590 enum nl80211_tx_power_setting type;
1591 int idx, mbm = 0;
1592
Johannes Bergc8442112012-10-24 10:17:18 +02001593 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1594 txp_wdev = NULL;
1595
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001596 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001597 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001598 goto bad_res;
1599 }
1600
1601 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1602 type = nla_get_u32(info->attrs[idx]);
1603
1604 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1605 (type != NL80211_TX_POWER_AUTOMATIC)) {
1606 result = -EINVAL;
1607 goto bad_res;
1608 }
1609
1610 if (type != NL80211_TX_POWER_AUTOMATIC) {
1611 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1612 mbm = nla_get_u32(info->attrs[idx]);
1613 }
1614
Johannes Bergc8442112012-10-24 10:17:18 +02001615 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001616 if (result)
1617 goto bad_res;
1618 }
1619
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001620 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1621 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1622 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001623 if ((!rdev->wiphy.available_antennas_tx &&
1624 !rdev->wiphy.available_antennas_rx) ||
1625 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001626 result = -EOPNOTSUPP;
1627 goto bad_res;
1628 }
1629
1630 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1631 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1632
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001633 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001634 * available antenna masks, except for the "all" mask */
1635 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1636 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001637 result = -EINVAL;
1638 goto bad_res;
1639 }
1640
Bruno Randolf7f531e02010-12-16 11:30:22 +09001641 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1642 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001643
Hila Gonene35e4d22012-06-27 17:19:42 +03001644 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001645 if (result)
1646 goto bad_res;
1647 }
1648
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001649 changed = 0;
1650
1651 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1652 retry_short = nla_get_u8(
1653 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1654 if (retry_short == 0) {
1655 result = -EINVAL;
1656 goto bad_res;
1657 }
1658 changed |= WIPHY_PARAM_RETRY_SHORT;
1659 }
1660
1661 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1662 retry_long = nla_get_u8(
1663 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1664 if (retry_long == 0) {
1665 result = -EINVAL;
1666 goto bad_res;
1667 }
1668 changed |= WIPHY_PARAM_RETRY_LONG;
1669 }
1670
1671 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1672 frag_threshold = nla_get_u32(
1673 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1674 if (frag_threshold < 256) {
1675 result = -EINVAL;
1676 goto bad_res;
1677 }
1678 if (frag_threshold != (u32) -1) {
1679 /*
1680 * Fragments (apart from the last one) are required to
1681 * have even length. Make the fragmentation code
1682 * simpler by stripping LSB should someone try to use
1683 * odd threshold value.
1684 */
1685 frag_threshold &= ~0x1;
1686 }
1687 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1688 }
1689
1690 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1691 rts_threshold = nla_get_u32(
1692 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1693 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1694 }
1695
Lukáš Turek81077e82009-12-21 22:50:47 +01001696 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1697 coverage_class = nla_get_u8(
1698 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1699 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1700 }
1701
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001702 if (changed) {
1703 u8 old_retry_short, old_retry_long;
1704 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001705 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001706
1707 if (!rdev->ops->set_wiphy_params) {
1708 result = -EOPNOTSUPP;
1709 goto bad_res;
1710 }
1711
1712 old_retry_short = rdev->wiphy.retry_short;
1713 old_retry_long = rdev->wiphy.retry_long;
1714 old_frag_threshold = rdev->wiphy.frag_threshold;
1715 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001716 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001717
1718 if (changed & WIPHY_PARAM_RETRY_SHORT)
1719 rdev->wiphy.retry_short = retry_short;
1720 if (changed & WIPHY_PARAM_RETRY_LONG)
1721 rdev->wiphy.retry_long = retry_long;
1722 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1723 rdev->wiphy.frag_threshold = frag_threshold;
1724 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1725 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001726 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1727 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001728
Hila Gonene35e4d22012-06-27 17:19:42 +03001729 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001730 if (result) {
1731 rdev->wiphy.retry_short = old_retry_short;
1732 rdev->wiphy.retry_long = old_retry_long;
1733 rdev->wiphy.frag_threshold = old_frag_threshold;
1734 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001735 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001736 }
1737 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001738
Johannes Berg306d6112008-12-08 12:39:04 +01001739 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001740 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001741 if (netdev)
1742 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001743 return result;
1744}
1745
Johannes Berg71bbc992012-06-15 15:30:18 +02001746static inline u64 wdev_id(struct wireless_dev *wdev)
1747{
1748 return (u64)wdev->identifier |
1749 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1750}
Johannes Berg55682962007-09-20 13:09:35 -04001751
Eric W. Biederman15e47302012-09-07 20:12:54 +00001752static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001753 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001754 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001755{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001756 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001757 void *hdr;
1758
Eric W. Biederman15e47302012-09-07 20:12:54 +00001759 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001760 if (!hdr)
1761 return -1;
1762
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001763 if (dev &&
1764 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001765 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001766 goto nla_put_failure;
1767
1768 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1769 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001770 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001771 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001772 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1773 rdev->devlist_generation ^
1774 (cfg80211_rdev_list_generation << 2)))
1775 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001776
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001777 if (rdev->ops->get_channel) {
1778 struct ieee80211_channel *chan;
1779 enum nl80211_channel_type channel_type;
1780
Hila Gonene35e4d22012-06-27 17:19:42 +03001781 chan = rdev_get_channel(rdev, wdev, &channel_type);
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001782 if (chan &&
1783 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1784 chan->center_freq) ||
1785 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1786 channel_type)))
John W. Linville59ef43e2012-04-18 14:17:13 -04001787 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001788 }
1789
Johannes Berg55682962007-09-20 13:09:35 -04001790 return genlmsg_end(msg, hdr);
1791
1792 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001793 genlmsg_cancel(msg, hdr);
1794 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001795}
1796
1797static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1798{
1799 int wp_idx = 0;
1800 int if_idx = 0;
1801 int wp_start = cb->args[0];
1802 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001803 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001804 struct wireless_dev *wdev;
1805
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001806 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001807 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1808 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001809 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001810 if (wp_idx < wp_start) {
1811 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001812 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001813 }
Johannes Berg55682962007-09-20 13:09:35 -04001814 if_idx = 0;
1815
Johannes Bergf5ea9122009-08-07 16:17:38 +02001816 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001817 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001818 if (if_idx < if_start) {
1819 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001820 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001821 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001822 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001823 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001824 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001825 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001826 goto out;
1827 }
1828 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001829 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001830 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001831
1832 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001833 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001834 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001835 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001836
1837 cb->args[0] = wp_idx;
1838 cb->args[1] = if_idx;
1839
1840 return skb->len;
1841}
1842
1843static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1844{
1845 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001846 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001847 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001848
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001849 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001850 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001851 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001852
Eric W. Biederman15e47302012-09-07 20:12:54 +00001853 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001854 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001855 nlmsg_free(msg);
1856 return -ENOBUFS;
1857 }
Johannes Berg55682962007-09-20 13:09:35 -04001858
Johannes Berg134e6372009-07-10 09:51:34 +00001859 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001860}
1861
Michael Wu66f7ac52008-01-31 19:48:22 +01001862static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1863 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1864 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1865 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1866 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1867 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1868};
1869
1870static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1871{
1872 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1873 int flag;
1874
1875 *mntrflags = 0;
1876
1877 if (!nla)
1878 return -EINVAL;
1879
1880 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1881 nla, mntr_flags_policy))
1882 return -EINVAL;
1883
1884 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1885 if (flags[flag])
1886 *mntrflags |= (1<<flag);
1887
1888 return 0;
1889}
1890
Johannes Berg9bc383d2009-11-19 11:55:19 +01001891static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001892 struct net_device *netdev, u8 use_4addr,
1893 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001894{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001895 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001896 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001897 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001898 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001899 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001900
1901 switch (iftype) {
1902 case NL80211_IFTYPE_AP_VLAN:
1903 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1904 return 0;
1905 break;
1906 case NL80211_IFTYPE_STATION:
1907 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1908 return 0;
1909 break;
1910 default:
1911 break;
1912 }
1913
1914 return -EOPNOTSUPP;
1915}
1916
Johannes Berg55682962007-09-20 13:09:35 -04001917static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1918{
Johannes Berg4c476992010-10-04 21:36:35 +02001919 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001920 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001921 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001922 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001923 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001924 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001925 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001926
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001927 memset(&params, 0, sizeof(params));
1928
Johannes Berg04a773a2009-04-19 21:24:32 +02001929 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001930
Johannes Berg723b0382008-09-16 20:22:09 +02001931 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001932 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001933 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001934 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001935 if (ntype > NL80211_IFTYPE_MAX)
1936 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001937 }
1938
Johannes Berg92ffe052008-09-16 20:39:36 +02001939 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001940 struct wireless_dev *wdev = dev->ieee80211_ptr;
1941
Johannes Berg4c476992010-10-04 21:36:35 +02001942 if (ntype != NL80211_IFTYPE_MESH_POINT)
1943 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001944 if (netif_running(dev))
1945 return -EBUSY;
1946
1947 wdev_lock(wdev);
1948 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1949 IEEE80211_MAX_MESH_ID_LEN);
1950 wdev->mesh_id_up_len =
1951 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1952 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1953 wdev->mesh_id_up_len);
1954 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001955 }
1956
Felix Fietkau8b787642009-11-10 18:53:10 +01001957 if (info->attrs[NL80211_ATTR_4ADDR]) {
1958 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1959 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001960 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001961 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001962 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001963 } else {
1964 params.use_4addr = -1;
1965 }
1966
Johannes Berg92ffe052008-09-16 20:39:36 +02001967 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001968 if (ntype != NL80211_IFTYPE_MONITOR)
1969 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001970 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1971 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001972 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001973 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001974
1975 flags = &_flags;
1976 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001977 }
Johannes Berg3b858752009-03-12 09:55:09 +01001978
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001979 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001980 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001981 else
1982 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001983
Johannes Berg9bc383d2009-11-19 11:55:19 +01001984 if (!err && params.use_4addr != -1)
1985 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1986
Johannes Berg55682962007-09-20 13:09:35 -04001987 return err;
1988}
1989
1990static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1991{
Johannes Berg4c476992010-10-04 21:36:35 +02001992 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001993 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02001994 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02001995 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04001996 int err;
1997 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001998 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001999
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002000 memset(&params, 0, sizeof(params));
2001
Johannes Berg55682962007-09-20 13:09:35 -04002002 if (!info->attrs[NL80211_ATTR_IFNAME])
2003 return -EINVAL;
2004
2005 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2006 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2007 if (type > NL80211_IFTYPE_MAX)
2008 return -EINVAL;
2009 }
2010
Johannes Berg79c97e92009-07-07 03:56:12 +02002011 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002012 !(rdev->wiphy.interface_modes & (1 << type)))
2013 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002014
Johannes Berg9bc383d2009-11-19 11:55:19 +01002015 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002016 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002017 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002018 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002019 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002020 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002021
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002022 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2023 if (!msg)
2024 return -ENOMEM;
2025
Michael Wu66f7ac52008-01-31 19:48:22 +01002026 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2027 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2028 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002029 wdev = rdev_add_virtual_intf(rdev,
2030 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2031 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002032 if (IS_ERR(wdev)) {
2033 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002034 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002035 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002036
Johannes Berg98104fde2012-06-16 00:19:54 +02002037 switch (type) {
2038 case NL80211_IFTYPE_MESH_POINT:
2039 if (!info->attrs[NL80211_ATTR_MESH_ID])
2040 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002041 wdev_lock(wdev);
2042 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2043 IEEE80211_MAX_MESH_ID_LEN);
2044 wdev->mesh_id_up_len =
2045 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2046 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2047 wdev->mesh_id_up_len);
2048 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002049 break;
2050 case NL80211_IFTYPE_P2P_DEVICE:
2051 /*
2052 * P2P Device doesn't have a netdev, so doesn't go
2053 * through the netdev notifier and must be added here
2054 */
2055 mutex_init(&wdev->mtx);
2056 INIT_LIST_HEAD(&wdev->event_list);
2057 spin_lock_init(&wdev->event_lock);
2058 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2059 spin_lock_init(&wdev->mgmt_registrations_lock);
2060
2061 mutex_lock(&rdev->devlist_mtx);
2062 wdev->identifier = ++rdev->wdev_id;
2063 list_add_rcu(&wdev->list, &rdev->wdev_list);
2064 rdev->devlist_generation++;
2065 mutex_unlock(&rdev->devlist_mtx);
2066 break;
2067 default:
2068 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002069 }
2070
Eric W. Biederman15e47302012-09-07 20:12:54 +00002071 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002072 rdev, wdev) < 0) {
2073 nlmsg_free(msg);
2074 return -ENOBUFS;
2075 }
2076
2077 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002078}
2079
2080static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2081{
Johannes Berg4c476992010-10-04 21:36:35 +02002082 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002083 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002084
Johannes Berg4c476992010-10-04 21:36:35 +02002085 if (!rdev->ops->del_virtual_intf)
2086 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002087
Johannes Berg84efbb82012-06-16 00:00:26 +02002088 /*
2089 * If we remove a wireless device without a netdev then clear
2090 * user_ptr[1] so that nl80211_post_doit won't dereference it
2091 * to check if it needs to do dev_put(). Otherwise it crashes
2092 * since the wdev has been freed, unlike with a netdev where
2093 * we need the dev_put() for the netdev to really be freed.
2094 */
2095 if (!wdev->netdev)
2096 info->user_ptr[1] = NULL;
2097
Hila Gonene35e4d22012-06-27 17:19:42 +03002098 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002099}
2100
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002101static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2102{
2103 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2104 struct net_device *dev = info->user_ptr[1];
2105 u16 noack_map;
2106
2107 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2108 return -EINVAL;
2109
2110 if (!rdev->ops->set_noack_map)
2111 return -EOPNOTSUPP;
2112
2113 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2114
Hila Gonene35e4d22012-06-27 17:19:42 +03002115 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002116}
2117
Johannes Berg41ade002007-12-19 02:03:29 +01002118struct get_key_cookie {
2119 struct sk_buff *msg;
2120 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002121 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002122};
2123
2124static void get_key_callback(void *c, struct key_params *params)
2125{
Johannes Bergb9454e82009-07-08 13:29:08 +02002126 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002127 struct get_key_cookie *cookie = c;
2128
David S. Miller9360ffd2012-03-29 04:41:26 -04002129 if ((params->key &&
2130 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2131 params->key_len, params->key)) ||
2132 (params->seq &&
2133 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2134 params->seq_len, params->seq)) ||
2135 (params->cipher &&
2136 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2137 params->cipher)))
2138 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002139
Johannes Bergb9454e82009-07-08 13:29:08 +02002140 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2141 if (!key)
2142 goto nla_put_failure;
2143
David S. Miller9360ffd2012-03-29 04:41:26 -04002144 if ((params->key &&
2145 nla_put(cookie->msg, NL80211_KEY_DATA,
2146 params->key_len, params->key)) ||
2147 (params->seq &&
2148 nla_put(cookie->msg, NL80211_KEY_SEQ,
2149 params->seq_len, params->seq)) ||
2150 (params->cipher &&
2151 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2152 params->cipher)))
2153 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002154
David S. Miller9360ffd2012-03-29 04:41:26 -04002155 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2156 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002157
2158 nla_nest_end(cookie->msg, key);
2159
Johannes Berg41ade002007-12-19 02:03:29 +01002160 return;
2161 nla_put_failure:
2162 cookie->error = 1;
2163}
2164
2165static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2166{
Johannes Berg4c476992010-10-04 21:36:35 +02002167 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002168 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002169 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002170 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002171 const u8 *mac_addr = NULL;
2172 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002173 struct get_key_cookie cookie = {
2174 .error = 0,
2175 };
2176 void *hdr;
2177 struct sk_buff *msg;
2178
2179 if (info->attrs[NL80211_ATTR_KEY_IDX])
2180 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2181
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002182 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002183 return -EINVAL;
2184
2185 if (info->attrs[NL80211_ATTR_MAC])
2186 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2187
Johannes Berge31b8212010-10-05 19:39:30 +02002188 pairwise = !!mac_addr;
2189 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2190 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2191 if (kt >= NUM_NL80211_KEYTYPES)
2192 return -EINVAL;
2193 if (kt != NL80211_KEYTYPE_GROUP &&
2194 kt != NL80211_KEYTYPE_PAIRWISE)
2195 return -EINVAL;
2196 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2197 }
2198
Johannes Berg4c476992010-10-04 21:36:35 +02002199 if (!rdev->ops->get_key)
2200 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002201
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002202 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002203 if (!msg)
2204 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002205
Eric W. Biederman15e47302012-09-07 20:12:54 +00002206 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002207 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002208 if (IS_ERR(hdr))
2209 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002210
2211 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002212 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002213
David S. Miller9360ffd2012-03-29 04:41:26 -04002214 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2215 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2216 goto nla_put_failure;
2217 if (mac_addr &&
2218 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2219 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002220
Johannes Berge31b8212010-10-05 19:39:30 +02002221 if (pairwise && mac_addr &&
2222 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2223 return -ENOENT;
2224
Hila Gonene35e4d22012-06-27 17:19:42 +03002225 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2226 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002227
2228 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002229 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002230
2231 if (cookie.error)
2232 goto nla_put_failure;
2233
2234 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002235 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002236
2237 nla_put_failure:
2238 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002239 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002240 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002241 return err;
2242}
2243
2244static int nl80211_set_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 Bergb9454e82009-07-08 13:29:08 +02002247 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002248 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002249 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002250
Johannes Bergb9454e82009-07-08 13:29:08 +02002251 err = nl80211_parse_key(info, &key);
2252 if (err)
2253 return err;
2254
2255 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002256 return -EINVAL;
2257
Johannes Bergb9454e82009-07-08 13:29:08 +02002258 /* only support setting default key */
2259 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002260 return -EINVAL;
2261
Johannes Bergfffd0932009-07-08 14:22:54 +02002262 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002263
2264 if (key.def) {
2265 if (!rdev->ops->set_default_key) {
2266 err = -EOPNOTSUPP;
2267 goto out;
2268 }
2269
2270 err = nl80211_key_allowed(dev->ieee80211_ptr);
2271 if (err)
2272 goto out;
2273
Hila Gonene35e4d22012-06-27 17:19:42 +03002274 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002275 key.def_uni, key.def_multi);
2276
2277 if (err)
2278 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002279
Johannes Berg3d23e342009-09-29 23:27:28 +02002280#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002281 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002282#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002283 } else {
2284 if (key.def_uni || !key.def_multi) {
2285 err = -EINVAL;
2286 goto out;
2287 }
2288
2289 if (!rdev->ops->set_default_mgmt_key) {
2290 err = -EOPNOTSUPP;
2291 goto out;
2292 }
2293
2294 err = nl80211_key_allowed(dev->ieee80211_ptr);
2295 if (err)
2296 goto out;
2297
Hila Gonene35e4d22012-06-27 17:19:42 +03002298 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002299 if (err)
2300 goto out;
2301
2302#ifdef CONFIG_CFG80211_WEXT
2303 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2304#endif
2305 }
2306
2307 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002308 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002309
Johannes Berg41ade002007-12-19 02:03:29 +01002310 return err;
2311}
2312
2313static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2314{
Johannes Berg4c476992010-10-04 21:36:35 +02002315 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002316 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002317 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002318 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002319 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002320
Johannes Bergb9454e82009-07-08 13:29:08 +02002321 err = nl80211_parse_key(info, &key);
2322 if (err)
2323 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002324
Johannes Bergb9454e82009-07-08 13:29:08 +02002325 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002326 return -EINVAL;
2327
Johannes Berg41ade002007-12-19 02:03:29 +01002328 if (info->attrs[NL80211_ATTR_MAC])
2329 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2330
Johannes Berge31b8212010-10-05 19:39:30 +02002331 if (key.type == -1) {
2332 if (mac_addr)
2333 key.type = NL80211_KEYTYPE_PAIRWISE;
2334 else
2335 key.type = NL80211_KEYTYPE_GROUP;
2336 }
2337
2338 /* for now */
2339 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2340 key.type != NL80211_KEYTYPE_GROUP)
2341 return -EINVAL;
2342
Johannes Berg4c476992010-10-04 21:36:35 +02002343 if (!rdev->ops->add_key)
2344 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002345
Johannes Berge31b8212010-10-05 19:39:30 +02002346 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2347 key.type == NL80211_KEYTYPE_PAIRWISE,
2348 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002349 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002350
2351 wdev_lock(dev->ieee80211_ptr);
2352 err = nl80211_key_allowed(dev->ieee80211_ptr);
2353 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002354 err = rdev_add_key(rdev, dev, key.idx,
2355 key.type == NL80211_KEYTYPE_PAIRWISE,
2356 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002357 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002358
Johannes Berg41ade002007-12-19 02:03:29 +01002359 return err;
2360}
2361
2362static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2363{
Johannes Berg4c476992010-10-04 21:36:35 +02002364 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002365 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002366 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002367 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002368 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002369
Johannes Bergb9454e82009-07-08 13:29:08 +02002370 err = nl80211_parse_key(info, &key);
2371 if (err)
2372 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002373
2374 if (info->attrs[NL80211_ATTR_MAC])
2375 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2376
Johannes Berge31b8212010-10-05 19:39:30 +02002377 if (key.type == -1) {
2378 if (mac_addr)
2379 key.type = NL80211_KEYTYPE_PAIRWISE;
2380 else
2381 key.type = NL80211_KEYTYPE_GROUP;
2382 }
2383
2384 /* for now */
2385 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2386 key.type != NL80211_KEYTYPE_GROUP)
2387 return -EINVAL;
2388
Johannes Berg4c476992010-10-04 21:36:35 +02002389 if (!rdev->ops->del_key)
2390 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002391
Johannes Bergfffd0932009-07-08 14:22:54 +02002392 wdev_lock(dev->ieee80211_ptr);
2393 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002394
2395 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2396 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2397 err = -ENOENT;
2398
Johannes Bergfffd0932009-07-08 14:22:54 +02002399 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002400 err = rdev_del_key(rdev, dev, key.idx,
2401 key.type == NL80211_KEYTYPE_PAIRWISE,
2402 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002403
Johannes Berg3d23e342009-09-29 23:27:28 +02002404#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002405 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002406 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002407 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002408 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002409 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2410 }
2411#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002412 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002413
Johannes Berg41ade002007-12-19 02:03:29 +01002414 return err;
2415}
2416
Johannes Berg88600202012-02-13 15:17:18 +01002417static int nl80211_parse_beacon(struct genl_info *info,
2418 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002419{
Johannes Berg88600202012-02-13 15:17:18 +01002420 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002421
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002422 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2423 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2424 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2425 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002426 return -EINVAL;
2427
Johannes Berg88600202012-02-13 15:17:18 +01002428 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002429
Johannes Berged1b6cc2007-12-19 02:03:32 +01002430 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002431 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2432 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2433 if (!bcn->head_len)
2434 return -EINVAL;
2435 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002436 }
2437
2438 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002439 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2440 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002441 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002442 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002443 }
2444
Johannes Berg4c476992010-10-04 21:36:35 +02002445 if (!haveinfo)
2446 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002447
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002448 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002449 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2450 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002451 }
2452
2453 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002454 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002455 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002456 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002457 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2458 }
2459
2460 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002461 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002462 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002463 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002464 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2465 }
2466
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002467 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002468 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002469 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002470 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002471 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2472 }
2473
Johannes Berg88600202012-02-13 15:17:18 +01002474 return 0;
2475}
2476
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002477static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2478 struct cfg80211_ap_settings *params)
2479{
2480 struct wireless_dev *wdev;
2481 bool ret = false;
2482
2483 mutex_lock(&rdev->devlist_mtx);
2484
Johannes Berg89a54e42012-06-15 14:33:17 +02002485 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002486 if (wdev->iftype != NL80211_IFTYPE_AP &&
2487 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2488 continue;
2489
2490 if (!wdev->preset_chan)
2491 continue;
2492
2493 params->channel = wdev->preset_chan;
2494 params->channel_type = wdev->preset_chantype;
2495 ret = true;
2496 break;
2497 }
2498
2499 mutex_unlock(&rdev->devlist_mtx);
2500
2501 return ret;
2502}
2503
Jouni Malinene39e5b52012-09-30 19:29:39 +03002504static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2505 enum nl80211_auth_type auth_type,
2506 enum nl80211_commands cmd)
2507{
2508 if (auth_type > NL80211_AUTHTYPE_MAX)
2509 return false;
2510
2511 switch (cmd) {
2512 case NL80211_CMD_AUTHENTICATE:
2513 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2514 auth_type == NL80211_AUTHTYPE_SAE)
2515 return false;
2516 return true;
2517 case NL80211_CMD_CONNECT:
2518 case NL80211_CMD_START_AP:
2519 /* SAE not supported yet */
2520 if (auth_type == NL80211_AUTHTYPE_SAE)
2521 return false;
2522 return true;
2523 default:
2524 return false;
2525 }
2526}
2527
Johannes Berg88600202012-02-13 15:17:18 +01002528static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2529{
2530 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2531 struct net_device *dev = info->user_ptr[1];
2532 struct wireless_dev *wdev = dev->ieee80211_ptr;
2533 struct cfg80211_ap_settings params;
2534 int err;
2535
2536 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2537 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2538 return -EOPNOTSUPP;
2539
2540 if (!rdev->ops->start_ap)
2541 return -EOPNOTSUPP;
2542
2543 if (wdev->beacon_interval)
2544 return -EALREADY;
2545
2546 memset(&params, 0, sizeof(params));
2547
2548 /* these are required for START_AP */
2549 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2550 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2551 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2552 return -EINVAL;
2553
2554 err = nl80211_parse_beacon(info, &params.beacon);
2555 if (err)
2556 return err;
2557
2558 params.beacon_interval =
2559 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2560 params.dtim_period =
2561 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2562
2563 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2564 if (err)
2565 return err;
2566
2567 /*
2568 * In theory, some of these attributes should be required here
2569 * but since they were not used when the command was originally
2570 * added, keep them optional for old user space programs to let
2571 * them continue to work with drivers that do not need the
2572 * additional information -- drivers must check!
2573 */
2574 if (info->attrs[NL80211_ATTR_SSID]) {
2575 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2576 params.ssid_len =
2577 nla_len(info->attrs[NL80211_ATTR_SSID]);
2578 if (params.ssid_len == 0 ||
2579 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2580 return -EINVAL;
2581 }
2582
2583 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2584 params.hidden_ssid = nla_get_u32(
2585 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2586 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2587 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2588 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2589 return -EINVAL;
2590 }
2591
2592 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2593
2594 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2595 params.auth_type = nla_get_u32(
2596 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002597 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2598 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002599 return -EINVAL;
2600 } else
2601 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2602
2603 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2604 NL80211_MAX_NR_CIPHER_SUITES);
2605 if (err)
2606 return err;
2607
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302608 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2609 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2610 return -EOPNOTSUPP;
2611 params.inactivity_timeout = nla_get_u16(
2612 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2613 }
2614
Johannes Bergaa430da2012-05-16 23:50:18 +02002615 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2616 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2617
2618 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2619 !nl80211_valid_channel_type(info, &channel_type))
2620 return -EINVAL;
2621
2622 params.channel = rdev_freq_to_chan(rdev,
2623 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2624 channel_type);
2625 if (!params.channel)
2626 return -EINVAL;
2627 params.channel_type = channel_type;
2628 } else if (wdev->preset_chan) {
2629 params.channel = wdev->preset_chan;
2630 params.channel_type = wdev->preset_chantype;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002631 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002632 return -EINVAL;
2633
2634 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2635 params.channel_type))
2636 return -EINVAL;
2637
Michal Kaziore4e32452012-06-29 12:47:08 +02002638 mutex_lock(&rdev->devlist_mtx);
2639 err = cfg80211_can_use_chan(rdev, wdev, params.channel,
2640 CHAN_MODE_SHARED);
2641 mutex_unlock(&rdev->devlist_mtx);
2642
2643 if (err)
2644 return err;
2645
Hila Gonene35e4d22012-06-27 17:19:42 +03002646 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002647 if (!err) {
2648 wdev->preset_chan = params.channel;
2649 wdev->preset_chantype = params.channel_type;
Johannes Berg88600202012-02-13 15:17:18 +01002650 wdev->beacon_interval = params.beacon_interval;
Michal Kaziorf4489eb2012-06-29 12:46:58 +02002651 wdev->channel = params.channel;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002652 wdev->ssid_len = params.ssid_len;
2653 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002654 }
Johannes Berg56d18932011-05-09 18:41:15 +02002655 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002656}
2657
Johannes Berg88600202012-02-13 15:17:18 +01002658static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2659{
2660 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2661 struct net_device *dev = info->user_ptr[1];
2662 struct wireless_dev *wdev = dev->ieee80211_ptr;
2663 struct cfg80211_beacon_data params;
2664 int err;
2665
2666 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2667 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2668 return -EOPNOTSUPP;
2669
2670 if (!rdev->ops->change_beacon)
2671 return -EOPNOTSUPP;
2672
2673 if (!wdev->beacon_interval)
2674 return -EINVAL;
2675
2676 err = nl80211_parse_beacon(info, &params);
2677 if (err)
2678 return err;
2679
Hila Gonene35e4d22012-06-27 17:19:42 +03002680 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002681}
2682
2683static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002684{
Johannes Berg4c476992010-10-04 21:36:35 +02002685 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2686 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002687
Michal Kazior60771782012-06-29 12:46:56 +02002688 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002689}
2690
Johannes Berg5727ef12007-12-19 02:03:34 +01002691static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2692 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2693 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2694 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002695 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002696 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002697 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002698};
2699
Johannes Bergeccb8e82009-05-11 21:57:56 +03002700static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002701 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002702 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002703{
2704 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002705 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002706 int flag;
2707
Johannes Bergeccb8e82009-05-11 21:57:56 +03002708 /*
2709 * Try parsing the new attribute first so userspace
2710 * can specify both for older kernels.
2711 */
2712 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2713 if (nla) {
2714 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002715
Johannes Bergeccb8e82009-05-11 21:57:56 +03002716 sta_flags = nla_data(nla);
2717 params->sta_flags_mask = sta_flags->mask;
2718 params->sta_flags_set = sta_flags->set;
2719 if ((params->sta_flags_mask |
2720 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2721 return -EINVAL;
2722 return 0;
2723 }
2724
2725 /* if present, parse the old attribute */
2726
2727 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002728 if (!nla)
2729 return 0;
2730
2731 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2732 nla, sta_flags_policy))
2733 return -EINVAL;
2734
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002735 /*
2736 * Only allow certain flags for interface types so that
2737 * other attributes are silently ignored. Remember that
2738 * this is backward compatibility code with old userspace
2739 * and shouldn't be hit in other cases anyway.
2740 */
2741 switch (iftype) {
2742 case NL80211_IFTYPE_AP:
2743 case NL80211_IFTYPE_AP_VLAN:
2744 case NL80211_IFTYPE_P2P_GO:
2745 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2746 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2747 BIT(NL80211_STA_FLAG_WME) |
2748 BIT(NL80211_STA_FLAG_MFP);
2749 break;
2750 case NL80211_IFTYPE_P2P_CLIENT:
2751 case NL80211_IFTYPE_STATION:
2752 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2753 BIT(NL80211_STA_FLAG_TDLS_PEER);
2754 break;
2755 case NL80211_IFTYPE_MESH_POINT:
2756 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2757 BIT(NL80211_STA_FLAG_MFP) |
2758 BIT(NL80211_STA_FLAG_AUTHORIZED);
2759 default:
2760 return -EINVAL;
2761 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002762
Johannes Berg3383b5a2012-05-10 20:14:43 +02002763 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2764 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002765 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002766
Johannes Berg3383b5a2012-05-10 20:14:43 +02002767 /* no longer support new API additions in old API */
2768 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2769 return -EINVAL;
2770 }
2771 }
2772
Johannes Berg5727ef12007-12-19 02:03:34 +01002773 return 0;
2774}
2775
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002776static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2777 int attr)
2778{
2779 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002780 u32 bitrate;
2781 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002782
2783 rate = nla_nest_start(msg, attr);
2784 if (!rate)
2785 goto nla_put_failure;
2786
2787 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2788 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002789 /* report 16-bit bitrate only if we can */
2790 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002791 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002792 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2793 (bitrate_compat > 0 &&
2794 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002795 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2796 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2797 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2798 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2799 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2800 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2801 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002802
2803 nla_nest_end(msg, rate);
2804 return true;
2805
2806nla_put_failure:
2807 return false;
2808}
2809
Eric W. Biederman15e47302012-09-07 20:12:54 +00002810static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002811 int flags,
2812 struct cfg80211_registered_device *rdev,
2813 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002814 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002815{
2816 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002817 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002818
Eric W. Biederman15e47302012-09-07 20:12:54 +00002819 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002820 if (!hdr)
2821 return -1;
2822
David S. Miller9360ffd2012-03-29 04:41:26 -04002823 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2824 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2825 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2826 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002827
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002828 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2829 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002830 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002831 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2832 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2833 sinfo->connected_time))
2834 goto nla_put_failure;
2835 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2836 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2837 sinfo->inactive_time))
2838 goto nla_put_failure;
2839 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2840 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2841 sinfo->rx_bytes))
2842 goto nla_put_failure;
2843 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2844 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2845 sinfo->tx_bytes))
2846 goto nla_put_failure;
2847 if ((sinfo->filled & STATION_INFO_LLID) &&
2848 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2849 goto nla_put_failure;
2850 if ((sinfo->filled & STATION_INFO_PLID) &&
2851 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2852 goto nla_put_failure;
2853 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2854 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2855 sinfo->plink_state))
2856 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002857 switch (rdev->wiphy.signal_type) {
2858 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002859 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2860 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2861 sinfo->signal))
2862 goto nla_put_failure;
2863 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2864 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2865 sinfo->signal_avg))
2866 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002867 break;
2868 default:
2869 break;
2870 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002871 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002872 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2873 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002874 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002875 }
2876 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2877 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2878 NL80211_STA_INFO_RX_BITRATE))
2879 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002880 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002881 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2882 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2883 sinfo->rx_packets))
2884 goto nla_put_failure;
2885 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2886 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2887 sinfo->tx_packets))
2888 goto nla_put_failure;
2889 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2890 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2891 sinfo->tx_retries))
2892 goto nla_put_failure;
2893 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2894 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2895 sinfo->tx_failed))
2896 goto nla_put_failure;
2897 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2898 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2899 sinfo->beacon_loss_count))
2900 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002901 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2902 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2903 if (!bss_param)
2904 goto nla_put_failure;
2905
David S. Miller9360ffd2012-03-29 04:41:26 -04002906 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2907 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2908 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2909 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2910 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2911 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2912 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2913 sinfo->bss_param.dtim_period) ||
2914 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2915 sinfo->bss_param.beacon_interval))
2916 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002917
2918 nla_nest_end(msg, bss_param);
2919 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002920 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2921 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2922 sizeof(struct nl80211_sta_flag_update),
2923 &sinfo->sta_flags))
2924 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002925 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2926 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2927 sinfo->t_offset))
2928 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002929 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002930
David S. Miller9360ffd2012-03-29 04:41:26 -04002931 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2932 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2933 sinfo->assoc_req_ies))
2934 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002935
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002936 return genlmsg_end(msg, hdr);
2937
2938 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002939 genlmsg_cancel(msg, hdr);
2940 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002941}
2942
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002943static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002944 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002945{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002946 struct station_info sinfo;
2947 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002948 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002949 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002950 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002951 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002952
Johannes Berg67748892010-10-04 21:14:06 +02002953 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2954 if (err)
2955 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002956
2957 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002958 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002959 goto out_err;
2960 }
2961
Johannes Bergbba95fe2008-07-29 13:22:51 +02002962 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002963 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03002964 err = rdev_dump_station(dev, netdev, sta_idx,
2965 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002966 if (err == -ENOENT)
2967 break;
2968 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002969 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002970
2971 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002972 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002973 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002974 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002975 &sinfo) < 0)
2976 goto out;
2977
2978 sta_idx++;
2979 }
2980
2981
2982 out:
2983 cb->args[1] = sta_idx;
2984 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002985 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002986 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002987
2988 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002989}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002990
Johannes Berg5727ef12007-12-19 02:03:34 +01002991static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2992{
Johannes Berg4c476992010-10-04 21:36:35 +02002993 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2994 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002995 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002996 struct sk_buff *msg;
2997 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002998 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002999
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003000 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003001
3002 if (!info->attrs[NL80211_ATTR_MAC])
3003 return -EINVAL;
3004
3005 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3006
Johannes Berg4c476992010-10-04 21:36:35 +02003007 if (!rdev->ops->get_station)
3008 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003009
Hila Gonene35e4d22012-06-27 17:19:42 +03003010 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003011 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003012 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003013
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003014 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003015 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003016 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003017
Eric W. Biederman15e47302012-09-07 20:12:54 +00003018 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003019 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003020 nlmsg_free(msg);
3021 return -ENOBUFS;
3022 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003023
Johannes Berg4c476992010-10-04 21:36:35 +02003024 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003025}
3026
3027/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003028 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003029 */
Johannes Berg80b99892011-11-18 16:23:01 +01003030static struct net_device *get_vlan(struct genl_info *info,
3031 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003032{
Johannes Berg463d0182009-07-14 00:33:35 +02003033 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003034 struct net_device *v;
3035 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003036
Johannes Berg80b99892011-11-18 16:23:01 +01003037 if (!vlanattr)
3038 return NULL;
3039
3040 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3041 if (!v)
3042 return ERR_PTR(-ENODEV);
3043
3044 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3045 ret = -EINVAL;
3046 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003047 }
Johannes Berg80b99892011-11-18 16:23:01 +01003048
3049 if (!netif_running(v)) {
3050 ret = -ENETDOWN;
3051 goto error;
3052 }
3053
3054 return v;
3055 error:
3056 dev_put(v);
3057 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003058}
3059
3060static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3061{
Johannes Berg4c476992010-10-04 21:36:35 +02003062 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003063 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003064 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003065 struct station_parameters params;
3066 u8 *mac_addr = NULL;
3067
3068 memset(&params, 0, sizeof(params));
3069
3070 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003071 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003072
3073 if (info->attrs[NL80211_ATTR_STA_AID])
3074 return -EINVAL;
3075
3076 if (!info->attrs[NL80211_ATTR_MAC])
3077 return -EINVAL;
3078
3079 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3080
3081 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3082 params.supported_rates =
3083 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3084 params.supported_rates_len =
3085 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3086 }
3087
3088 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3089 params.listen_interval =
3090 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3091
Jouni Malinen36aedc92008-08-25 11:58:58 +03003092 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3093 params.ht_capa =
3094 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3095
Johannes Bergbdd90d52011-12-14 12:20:27 +01003096 if (!rdev->ops->change_station)
3097 return -EOPNOTSUPP;
3098
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003099 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003100 return -EINVAL;
3101
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003102 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3103 params.plink_action =
3104 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3105
Javier Cardona9c3990a2011-05-03 16:57:11 -07003106 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3107 params.plink_state =
3108 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3109
Johannes Berga97f4422009-06-18 17:23:43 +02003110 switch (dev->ieee80211_ptr->iftype) {
3111 case NL80211_IFTYPE_AP:
3112 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003113 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003114 /* disallow mesh-specific things */
3115 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003116 return -EINVAL;
3117
3118 /* TDLS can't be set, ... */
3119 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3120 return -EINVAL;
3121 /*
3122 * ... but don't bother the driver with it. This works around
3123 * a hostapd/wpa_supplicant issue -- it always includes the
3124 * TLDS_PEER flag in the mask even for AP mode.
3125 */
3126 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3127
3128 /* accept only the listed bits */
3129 if (params.sta_flags_mask &
3130 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3131 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3132 BIT(NL80211_STA_FLAG_WME) |
3133 BIT(NL80211_STA_FLAG_MFP)))
3134 return -EINVAL;
3135
3136 /* must be last in here for error handling */
3137 params.vlan = get_vlan(info, rdev);
3138 if (IS_ERR(params.vlan))
3139 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003140 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003141 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003142 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003143 /*
3144 * Don't allow userspace to change the TDLS_PEER flag,
3145 * but silently ignore attempts to change it since we
3146 * don't have state here to verify that it doesn't try
3147 * to change the flag.
3148 */
3149 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003150 /* fall through */
3151 case NL80211_IFTYPE_ADHOC:
3152 /* disallow things sta doesn't support */
3153 if (params.plink_action)
3154 return -EINVAL;
3155 if (params.ht_capa)
3156 return -EINVAL;
3157 if (params.listen_interval >= 0)
3158 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003159 /* reject any changes other than AUTHORIZED */
3160 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3161 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003162 break;
3163 case NL80211_IFTYPE_MESH_POINT:
3164 /* disallow things mesh doesn't support */
3165 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003166 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003167 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003168 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003169 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003170 return -EINVAL;
3171 /*
3172 * No special handling for TDLS here -- the userspace
3173 * mesh code doesn't have this bug.
3174 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003175 if (params.sta_flags_mask &
3176 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003177 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003178 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003179 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003180 break;
3181 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003182 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003183 }
3184
Johannes Bergbdd90d52011-12-14 12:20:27 +01003185 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003186
Hila Gonene35e4d22012-06-27 17:19:42 +03003187 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003188
Johannes Berg5727ef12007-12-19 02:03:34 +01003189 if (params.vlan)
3190 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003191
Johannes Berg5727ef12007-12-19 02:03:34 +01003192 return err;
3193}
3194
Eliad Pellerc75786c2011-08-23 14:37:46 +03003195static struct nla_policy
3196nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3197 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3198 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3199};
3200
Johannes Berg5727ef12007-12-19 02:03:34 +01003201static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3202{
Johannes Berg4c476992010-10-04 21:36:35 +02003203 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003204 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003205 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003206 struct station_parameters params;
3207 u8 *mac_addr = NULL;
3208
3209 memset(&params, 0, sizeof(params));
3210
3211 if (!info->attrs[NL80211_ATTR_MAC])
3212 return -EINVAL;
3213
Johannes Berg5727ef12007-12-19 02:03:34 +01003214 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3215 return -EINVAL;
3216
3217 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3218 return -EINVAL;
3219
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003220 if (!info->attrs[NL80211_ATTR_STA_AID])
3221 return -EINVAL;
3222
Johannes Berg5727ef12007-12-19 02:03:34 +01003223 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3224 params.supported_rates =
3225 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3226 params.supported_rates_len =
3227 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3228 params.listen_interval =
3229 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003230
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003231 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3232 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3233 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003234
Jouni Malinen36aedc92008-08-25 11:58:58 +03003235 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3236 params.ht_capa =
3237 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003238
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003239 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3240 params.vht_capa =
3241 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3242
Javier Cardona96b78df2011-04-07 15:08:33 -07003243 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3244 params.plink_action =
3245 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3246
Johannes Bergbdd90d52011-12-14 12:20:27 +01003247 if (!rdev->ops->add_station)
3248 return -EOPNOTSUPP;
3249
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003250 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003251 return -EINVAL;
3252
Johannes Bergbdd90d52011-12-14 12:20:27 +01003253 switch (dev->ieee80211_ptr->iftype) {
3254 case NL80211_IFTYPE_AP:
3255 case NL80211_IFTYPE_AP_VLAN:
3256 case NL80211_IFTYPE_P2P_GO:
3257 /* parse WME attributes if sta is WME capable */
3258 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3259 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3260 info->attrs[NL80211_ATTR_STA_WME]) {
3261 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3262 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003263
Johannes Bergbdd90d52011-12-14 12:20:27 +01003264 nla = info->attrs[NL80211_ATTR_STA_WME];
3265 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3266 nl80211_sta_wme_policy);
3267 if (err)
3268 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003269
Johannes Bergbdd90d52011-12-14 12:20:27 +01003270 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3271 params.uapsd_queues =
3272 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3273 if (params.uapsd_queues &
3274 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3275 return -EINVAL;
3276
3277 if (tb[NL80211_STA_WME_MAX_SP])
3278 params.max_sp =
3279 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3280
3281 if (params.max_sp &
3282 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3283 return -EINVAL;
3284
3285 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3286 }
3287 /* TDLS peers cannot be added */
3288 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003289 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003290 /* but don't bother the driver with it */
3291 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003292
Johannes Bergbdd90d52011-12-14 12:20:27 +01003293 /* must be last in here for error handling */
3294 params.vlan = get_vlan(info, rdev);
3295 if (IS_ERR(params.vlan))
3296 return PTR_ERR(params.vlan);
3297 break;
3298 case NL80211_IFTYPE_MESH_POINT:
3299 /* TDLS peers cannot be added */
3300 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003301 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003302 break;
3303 case NL80211_IFTYPE_STATION:
3304 /* Only TDLS peers can be added */
3305 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3306 return -EINVAL;
3307 /* Can only add if TDLS ... */
3308 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3309 return -EOPNOTSUPP;
3310 /* ... with external setup is supported */
3311 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3312 return -EOPNOTSUPP;
3313 break;
3314 default:
3315 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003316 }
3317
Johannes Bergbdd90d52011-12-14 12:20:27 +01003318 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003319
Hila Gonene35e4d22012-06-27 17:19:42 +03003320 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003321
Johannes Berg5727ef12007-12-19 02:03:34 +01003322 if (params.vlan)
3323 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003324 return err;
3325}
3326
3327static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3328{
Johannes Berg4c476992010-10-04 21:36:35 +02003329 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3330 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003331 u8 *mac_addr = NULL;
3332
3333 if (info->attrs[NL80211_ATTR_MAC])
3334 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3335
Johannes Berge80cf852009-05-11 14:43:13 +02003336 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003337 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003338 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003339 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3340 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003341
Johannes Berg4c476992010-10-04 21:36:35 +02003342 if (!rdev->ops->del_station)
3343 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003344
Hila Gonene35e4d22012-06-27 17:19:42 +03003345 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003346}
3347
Eric W. Biederman15e47302012-09-07 20:12:54 +00003348static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003349 int flags, struct net_device *dev,
3350 u8 *dst, u8 *next_hop,
3351 struct mpath_info *pinfo)
3352{
3353 void *hdr;
3354 struct nlattr *pinfoattr;
3355
Eric W. Biederman15e47302012-09-07 20:12:54 +00003356 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003357 if (!hdr)
3358 return -1;
3359
David S. Miller9360ffd2012-03-29 04:41:26 -04003360 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3361 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3362 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3363 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3364 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003365
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003366 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3367 if (!pinfoattr)
3368 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003369 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3370 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3371 pinfo->frame_qlen))
3372 goto nla_put_failure;
3373 if (((pinfo->filled & MPATH_INFO_SN) &&
3374 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3375 ((pinfo->filled & MPATH_INFO_METRIC) &&
3376 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3377 pinfo->metric)) ||
3378 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3379 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3380 pinfo->exptime)) ||
3381 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3382 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3383 pinfo->flags)) ||
3384 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3385 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3386 pinfo->discovery_timeout)) ||
3387 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3388 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3389 pinfo->discovery_retries)))
3390 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003391
3392 nla_nest_end(msg, pinfoattr);
3393
3394 return genlmsg_end(msg, hdr);
3395
3396 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003397 genlmsg_cancel(msg, hdr);
3398 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003399}
3400
3401static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003402 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003403{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003404 struct mpath_info pinfo;
3405 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003406 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003407 u8 dst[ETH_ALEN];
3408 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003409 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003410 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003411
Johannes Berg67748892010-10-04 21:14:06 +02003412 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3413 if (err)
3414 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003415
3416 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003417 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003418 goto out_err;
3419 }
3420
Jouni Malineneec60b02009-03-20 21:21:19 +02003421 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3422 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003423 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003424 }
3425
Johannes Bergbba95fe2008-07-29 13:22:51 +02003426 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003427 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3428 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003429 if (err == -ENOENT)
3430 break;
3431 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003432 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003433
Eric W. Biederman15e47302012-09-07 20:12:54 +00003434 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003435 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3436 netdev, dst, next_hop,
3437 &pinfo) < 0)
3438 goto out;
3439
3440 path_idx++;
3441 }
3442
3443
3444 out:
3445 cb->args[1] = path_idx;
3446 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003447 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003448 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003449 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003450}
3451
3452static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3453{
Johannes Berg4c476992010-10-04 21:36:35 +02003454 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003455 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003456 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003457 struct mpath_info pinfo;
3458 struct sk_buff *msg;
3459 u8 *dst = NULL;
3460 u8 next_hop[ETH_ALEN];
3461
3462 memset(&pinfo, 0, sizeof(pinfo));
3463
3464 if (!info->attrs[NL80211_ATTR_MAC])
3465 return -EINVAL;
3466
3467 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3468
Johannes Berg4c476992010-10-04 21:36:35 +02003469 if (!rdev->ops->get_mpath)
3470 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003471
Johannes Berg4c476992010-10-04 21:36:35 +02003472 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3473 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003474
Hila Gonene35e4d22012-06-27 17:19:42 +03003475 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003476 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003477 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003478
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003479 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003480 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003481 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003482
Eric W. Biederman15e47302012-09-07 20:12:54 +00003483 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003484 dev, dst, next_hop, &pinfo) < 0) {
3485 nlmsg_free(msg);
3486 return -ENOBUFS;
3487 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003488
Johannes Berg4c476992010-10-04 21:36:35 +02003489 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003490}
3491
3492static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3493{
Johannes Berg4c476992010-10-04 21:36:35 +02003494 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3495 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003496 u8 *dst = NULL;
3497 u8 *next_hop = NULL;
3498
3499 if (!info->attrs[NL80211_ATTR_MAC])
3500 return -EINVAL;
3501
3502 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3503 return -EINVAL;
3504
3505 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3506 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3507
Johannes Berg4c476992010-10-04 21:36:35 +02003508 if (!rdev->ops->change_mpath)
3509 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003510
Johannes Berg4c476992010-10-04 21:36:35 +02003511 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3512 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003513
Hila Gonene35e4d22012-06-27 17:19:42 +03003514 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003515}
Johannes Berg4c476992010-10-04 21:36:35 +02003516
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003517static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3518{
Johannes Berg4c476992010-10-04 21:36:35 +02003519 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3520 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003521 u8 *dst = NULL;
3522 u8 *next_hop = NULL;
3523
3524 if (!info->attrs[NL80211_ATTR_MAC])
3525 return -EINVAL;
3526
3527 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3528 return -EINVAL;
3529
3530 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3531 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3532
Johannes Berg4c476992010-10-04 21:36:35 +02003533 if (!rdev->ops->add_mpath)
3534 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003535
Johannes Berg4c476992010-10-04 21:36:35 +02003536 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3537 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003538
Hila Gonene35e4d22012-06-27 17:19:42 +03003539 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003540}
3541
3542static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3543{
Johannes Berg4c476992010-10-04 21:36:35 +02003544 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3545 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003546 u8 *dst = NULL;
3547
3548 if (info->attrs[NL80211_ATTR_MAC])
3549 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3550
Johannes Berg4c476992010-10-04 21:36:35 +02003551 if (!rdev->ops->del_mpath)
3552 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003553
Hila Gonene35e4d22012-06-27 17:19:42 +03003554 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003555}
3556
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003557static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3558{
Johannes Berg4c476992010-10-04 21:36:35 +02003559 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3560 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003561 struct bss_parameters params;
3562
3563 memset(&params, 0, sizeof(params));
3564 /* default to not changing parameters */
3565 params.use_cts_prot = -1;
3566 params.use_short_preamble = -1;
3567 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003568 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003569 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003570
3571 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3572 params.use_cts_prot =
3573 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3574 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3575 params.use_short_preamble =
3576 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3577 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3578 params.use_short_slot_time =
3579 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003580 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3581 params.basic_rates =
3582 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3583 params.basic_rates_len =
3584 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3585 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003586 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3587 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003588 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3589 params.ht_opmode =
3590 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003591
Johannes Berg4c476992010-10-04 21:36:35 +02003592 if (!rdev->ops->change_bss)
3593 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003594
Johannes Berg074ac8d2010-09-16 14:58:22 +02003595 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003596 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3597 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003598
Hila Gonene35e4d22012-06-27 17:19:42 +03003599 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003600}
3601
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003602static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003603 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3604 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3605 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3606 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3607 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3608 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3609};
3610
3611static int parse_reg_rule(struct nlattr *tb[],
3612 struct ieee80211_reg_rule *reg_rule)
3613{
3614 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3615 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3616
3617 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3618 return -EINVAL;
3619 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3620 return -EINVAL;
3621 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3622 return -EINVAL;
3623 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3624 return -EINVAL;
3625 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3626 return -EINVAL;
3627
3628 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3629
3630 freq_range->start_freq_khz =
3631 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3632 freq_range->end_freq_khz =
3633 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3634 freq_range->max_bandwidth_khz =
3635 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3636
3637 power_rule->max_eirp =
3638 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3639
3640 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3641 power_rule->max_antenna_gain =
3642 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3643
3644 return 0;
3645}
3646
3647static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3648{
3649 int r;
3650 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003651 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003652
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003653 /*
3654 * You should only get this when cfg80211 hasn't yet initialized
3655 * completely when built-in to the kernel right between the time
3656 * window between nl80211_init() and regulatory_init(), if that is
3657 * even possible.
3658 */
3659 mutex_lock(&cfg80211_mutex);
3660 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003661 mutex_unlock(&cfg80211_mutex);
3662 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003663 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003664 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003665
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003666 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3667 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003668
3669 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3670
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003671 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3672 user_reg_hint_type =
3673 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3674 else
3675 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3676
3677 switch (user_reg_hint_type) {
3678 case NL80211_USER_REG_HINT_USER:
3679 case NL80211_USER_REG_HINT_CELL_BASE:
3680 break;
3681 default:
3682 return -EINVAL;
3683 }
3684
3685 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003686
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003687 return r;
3688}
3689
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003690static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003691 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003692{
Johannes Berg4c476992010-10-04 21:36:35 +02003693 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003694 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003695 struct wireless_dev *wdev = dev->ieee80211_ptr;
3696 struct mesh_config cur_params;
3697 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003698 void *hdr;
3699 struct nlattr *pinfoattr;
3700 struct sk_buff *msg;
3701
Johannes Berg29cbe682010-12-03 09:20:44 +01003702 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3703 return -EOPNOTSUPP;
3704
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003705 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003706 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003707
Johannes Berg29cbe682010-12-03 09:20:44 +01003708 wdev_lock(wdev);
3709 /* If not connected, get default parameters */
3710 if (!wdev->mesh_id_len)
3711 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3712 else
Hila Gonene35e4d22012-06-27 17:19:42 +03003713 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01003714 wdev_unlock(wdev);
3715
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003716 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003717 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003718
3719 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003720 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003721 if (!msg)
3722 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003723 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003724 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003725 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003726 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003727 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003728 if (!pinfoattr)
3729 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003730 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3731 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3732 cur_params.dot11MeshRetryTimeout) ||
3733 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3734 cur_params.dot11MeshConfirmTimeout) ||
3735 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3736 cur_params.dot11MeshHoldingTimeout) ||
3737 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3738 cur_params.dot11MeshMaxPeerLinks) ||
3739 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3740 cur_params.dot11MeshMaxRetries) ||
3741 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3742 cur_params.dot11MeshTTL) ||
3743 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3744 cur_params.element_ttl) ||
3745 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3746 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003747 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3748 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003749 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3750 cur_params.dot11MeshHWMPmaxPREQretries) ||
3751 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3752 cur_params.path_refresh_time) ||
3753 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3754 cur_params.min_discovery_timeout) ||
3755 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3756 cur_params.dot11MeshHWMPactivePathTimeout) ||
3757 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3758 cur_params.dot11MeshHWMPpreqMinInterval) ||
3759 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3760 cur_params.dot11MeshHWMPperrMinInterval) ||
3761 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3762 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3763 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3764 cur_params.dot11MeshHWMPRootMode) ||
3765 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3766 cur_params.dot11MeshHWMPRannInterval) ||
3767 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3768 cur_params.dot11MeshGateAnnouncementProtocol) ||
3769 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3770 cur_params.dot11MeshForwarding) ||
3771 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003772 cur_params.rssi_threshold) ||
3773 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003774 cur_params.ht_opmode) ||
3775 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3776 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3777 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003778 cur_params.dot11MeshHWMProotInterval) ||
3779 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3780 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003781 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003782 nla_nest_end(msg, pinfoattr);
3783 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003784 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003785
Johannes Berg3b858752009-03-12 09:55:09 +01003786 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003787 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003788 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003789 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003790 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003791}
3792
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003793static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003794 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3795 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3796 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3797 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3798 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3799 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003800 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003801 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003802 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003803 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3804 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3805 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3806 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3807 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003808 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003809 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003810 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003811 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003812 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003813 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003814 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3815 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003816 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3817 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003818 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003819};
3820
Javier Cardonac80d5452010-12-16 17:37:49 -08003821static const struct nla_policy
3822 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003823 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003824 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3825 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003826 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003827 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003828 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003829 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003830};
3831
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003832static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003833 struct mesh_config *cfg,
3834 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003835{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003836 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003837 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003838
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003839#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3840do {\
3841 if (table[attr_num]) {\
3842 cfg->param = nla_fn(table[attr_num]); \
3843 mask |= (1 << (attr_num - 1)); \
3844 } \
3845} while (0);\
3846
3847
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003848 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003849 return -EINVAL;
3850 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003851 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003852 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003853 return -EINVAL;
3854
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003855 /* This makes sure that there aren't more than 32 mesh config
3856 * parameters (otherwise our bitfield scheme would not work.) */
3857 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3858
3859 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003860 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003861 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3862 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003863 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003864 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3865 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003866 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003867 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3868 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003869 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003870 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3871 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003872 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003873 mask, NL80211_MESHCONF_MAX_RETRIES,
3874 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003875 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003876 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003877 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003878 mask, NL80211_MESHCONF_ELEMENT_TTL,
3879 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003880 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003881 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3882 nla_get_u8);
3883 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3884 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3885 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003886 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003887 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3888 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003889 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003890 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3891 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003892 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003893 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3894 nla_get_u16);
3895 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3896 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3897 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003898 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003899 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3900 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003901 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003902 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3903 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003904 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003905 dot11MeshHWMPnetDiameterTraversalTime, mask,
3906 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3907 nla_get_u16);
3908 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3909 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3910 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3911 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3912 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003914 dot11MeshGateAnnouncementProtocol, mask,
3915 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3916 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003917 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003918 mask, NL80211_MESHCONF_FORWARDING,
3919 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003920 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003921 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3922 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003923 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003924 mask, NL80211_MESHCONF_HT_OPMODE,
3925 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003926 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3927 mask,
3928 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3929 nla_get_u32);
3930 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3931 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3932 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003933 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3934 dot11MeshHWMPconfirmationInterval, mask,
3935 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3936 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003937 if (mask_out)
3938 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003939
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003940 return 0;
3941
3942#undef FILL_IN_MESH_PARAM_IF_SET
3943}
3944
Javier Cardonac80d5452010-12-16 17:37:49 -08003945static int nl80211_parse_mesh_setup(struct genl_info *info,
3946 struct mesh_setup *setup)
3947{
3948 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3949
3950 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3951 return -EINVAL;
3952 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3953 info->attrs[NL80211_ATTR_MESH_SETUP],
3954 nl80211_mesh_setup_params_policy))
3955 return -EINVAL;
3956
Javier Cardonad299a1f2012-03-31 11:31:33 -07003957 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3958 setup->sync_method =
3959 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3960 IEEE80211_SYNC_METHOD_VENDOR :
3961 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3962
Javier Cardonac80d5452010-12-16 17:37:49 -08003963 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3964 setup->path_sel_proto =
3965 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3966 IEEE80211_PATH_PROTOCOL_VENDOR :
3967 IEEE80211_PATH_PROTOCOL_HWMP;
3968
3969 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3970 setup->path_metric =
3971 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3972 IEEE80211_PATH_METRIC_VENDOR :
3973 IEEE80211_PATH_METRIC_AIRTIME;
3974
Javier Cardona581a8b02011-04-07 15:08:27 -07003975
3976 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003977 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003978 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003979 if (!is_valid_ie_attr(ieattr))
3980 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003981 setup->ie = nla_data(ieattr);
3982 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003983 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003984 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3985 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003986
3987 return 0;
3988}
3989
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003990static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003991 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003992{
3993 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3994 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003995 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003996 struct mesh_config cfg;
3997 u32 mask;
3998 int err;
3999
Johannes Berg29cbe682010-12-03 09:20:44 +01004000 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4001 return -EOPNOTSUPP;
4002
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004003 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004004 return -EOPNOTSUPP;
4005
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004006 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004007 if (err)
4008 return err;
4009
Johannes Berg29cbe682010-12-03 09:20:44 +01004010 wdev_lock(wdev);
4011 if (!wdev->mesh_id_len)
4012 err = -ENOLINK;
4013
4014 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004015 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004016
4017 wdev_unlock(wdev);
4018
4019 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004020}
4021
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004022static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4023{
4024 struct sk_buff *msg;
4025 void *hdr = NULL;
4026 struct nlattr *nl_reg_rules;
4027 unsigned int i;
4028 int err = -EINVAL;
4029
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004030 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004031
4032 if (!cfg80211_regdomain)
4033 goto out;
4034
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004035 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004036 if (!msg) {
4037 err = -ENOBUFS;
4038 goto out;
4039 }
4040
Eric W. Biederman15e47302012-09-07 20:12:54 +00004041 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004042 NL80211_CMD_GET_REG);
4043 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004044 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004045
David S. Miller9360ffd2012-03-29 04:41:26 -04004046 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
4047 cfg80211_regdomain->alpha2) ||
4048 (cfg80211_regdomain->dfs_region &&
4049 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
4050 cfg80211_regdomain->dfs_region)))
4051 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004052
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004053 if (reg_last_request_cell_base() &&
4054 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4055 NL80211_USER_REG_HINT_CELL_BASE))
4056 goto nla_put_failure;
4057
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004058 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4059 if (!nl_reg_rules)
4060 goto nla_put_failure;
4061
4062 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
4063 struct nlattr *nl_reg_rule;
4064 const struct ieee80211_reg_rule *reg_rule;
4065 const struct ieee80211_freq_range *freq_range;
4066 const struct ieee80211_power_rule *power_rule;
4067
4068 reg_rule = &cfg80211_regdomain->reg_rules[i];
4069 freq_range = &reg_rule->freq_range;
4070 power_rule = &reg_rule->power_rule;
4071
4072 nl_reg_rule = nla_nest_start(msg, i);
4073 if (!nl_reg_rule)
4074 goto nla_put_failure;
4075
David S. Miller9360ffd2012-03-29 04:41:26 -04004076 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4077 reg_rule->flags) ||
4078 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4079 freq_range->start_freq_khz) ||
4080 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4081 freq_range->end_freq_khz) ||
4082 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4083 freq_range->max_bandwidth_khz) ||
4084 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4085 power_rule->max_antenna_gain) ||
4086 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4087 power_rule->max_eirp))
4088 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004089
4090 nla_nest_end(msg, nl_reg_rule);
4091 }
4092
4093 nla_nest_end(msg, nl_reg_rules);
4094
4095 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004096 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004097 goto out;
4098
4099nla_put_failure:
4100 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004101put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004102 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004103 err = -EMSGSIZE;
4104out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004105 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004106 return err;
4107}
4108
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004109static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4110{
4111 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4112 struct nlattr *nl_reg_rule;
4113 char *alpha2 = NULL;
4114 int rem_reg_rules = 0, r = 0;
4115 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004116 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004117 struct ieee80211_regdomain *rd = NULL;
4118
4119 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4120 return -EINVAL;
4121
4122 if (!info->attrs[NL80211_ATTR_REG_RULES])
4123 return -EINVAL;
4124
4125 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4126
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004127 if (info->attrs[NL80211_ATTR_DFS_REGION])
4128 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4129
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004130 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4131 rem_reg_rules) {
4132 num_rules++;
4133 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004134 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004135 }
4136
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004137 mutex_lock(&cfg80211_mutex);
4138
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004139 if (!reg_is_valid_request(alpha2)) {
4140 r = -EINVAL;
4141 goto bad_reg;
4142 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004143
4144 size_of_regd = sizeof(struct ieee80211_regdomain) +
4145 (num_rules * sizeof(struct ieee80211_reg_rule));
4146
4147 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004148 if (!rd) {
4149 r = -ENOMEM;
4150 goto bad_reg;
4151 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004152
4153 rd->n_reg_rules = num_rules;
4154 rd->alpha2[0] = alpha2[0];
4155 rd->alpha2[1] = alpha2[1];
4156
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004157 /*
4158 * Disable DFS master mode if the DFS region was
4159 * not supported or known on this kernel.
4160 */
4161 if (reg_supported_dfs_region(dfs_region))
4162 rd->dfs_region = dfs_region;
4163
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004164 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4165 rem_reg_rules) {
4166 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4167 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4168 reg_rule_policy);
4169 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4170 if (r)
4171 goto bad_reg;
4172
4173 rule_idx++;
4174
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004175 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4176 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004177 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004178 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004179 }
4180
4181 BUG_ON(rule_idx != num_rules);
4182
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004183 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004184
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004185 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004186
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004187 return r;
4188
Johannes Bergd2372b32008-10-24 20:32:20 +02004189 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004190 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004191 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004192 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004193}
4194
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004195static int validate_scan_freqs(struct nlattr *freqs)
4196{
4197 struct nlattr *attr1, *attr2;
4198 int n_channels = 0, tmp1, tmp2;
4199
4200 nla_for_each_nested(attr1, freqs, tmp1) {
4201 n_channels++;
4202 /*
4203 * Some hardware has a limited channel list for
4204 * scanning, and it is pretty much nonsensical
4205 * to scan for a channel twice, so disallow that
4206 * and don't require drivers to check that the
4207 * channel list they get isn't longer than what
4208 * they can scan, as long as they can scan all
4209 * the channels they registered at once.
4210 */
4211 nla_for_each_nested(attr2, freqs, tmp2)
4212 if (attr1 != attr2 &&
4213 nla_get_u32(attr1) == nla_get_u32(attr2))
4214 return 0;
4215 }
4216
4217 return n_channels;
4218}
4219
Johannes Berg2a519312009-02-10 21:25:55 +01004220static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4221{
Johannes Berg4c476992010-10-04 21:36:35 +02004222 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004223 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004224 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004225 struct nlattr *attr;
4226 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004227 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004228 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004229
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004230 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4231 return -EINVAL;
4232
Johannes Berg79c97e92009-07-07 03:56:12 +02004233 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004234
Johannes Berg4c476992010-10-04 21:36:35 +02004235 if (!rdev->ops->scan)
4236 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004237
Johannes Berg4c476992010-10-04 21:36:35 +02004238 if (rdev->scan_req)
4239 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004240
4241 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004242 n_channels = validate_scan_freqs(
4243 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004244 if (!n_channels)
4245 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004246 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004247 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004248 n_channels = 0;
4249
Johannes Berg2a519312009-02-10 21:25:55 +01004250 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4251 if (wiphy->bands[band])
4252 n_channels += wiphy->bands[band]->n_channels;
4253 }
4254
4255 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4256 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4257 n_ssids++;
4258
Johannes Berg4c476992010-10-04 21:36:35 +02004259 if (n_ssids > wiphy->max_scan_ssids)
4260 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004261
Jouni Malinen70692ad2009-02-16 19:39:13 +02004262 if (info->attrs[NL80211_ATTR_IE])
4263 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4264 else
4265 ie_len = 0;
4266
Johannes Berg4c476992010-10-04 21:36:35 +02004267 if (ie_len > wiphy->max_scan_ie_len)
4268 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004269
Johannes Berg2a519312009-02-10 21:25:55 +01004270 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004271 + sizeof(*request->ssids) * n_ssids
4272 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004273 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004274 if (!request)
4275 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004276
Johannes Berg2a519312009-02-10 21:25:55 +01004277 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004278 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004279 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004280 if (ie_len) {
4281 if (request->ssids)
4282 request->ie = (void *)(request->ssids + n_ssids);
4283 else
4284 request->ie = (void *)(request->channels + n_channels);
4285 }
Johannes Berg2a519312009-02-10 21:25:55 +01004286
Johannes Berg584991d2009-11-02 13:32:03 +01004287 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004288 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4289 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004290 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004291 struct ieee80211_channel *chan;
4292
4293 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4294
4295 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004296 err = -EINVAL;
4297 goto out_free;
4298 }
Johannes Berg584991d2009-11-02 13:32:03 +01004299
4300 /* ignore disabled channels */
4301 if (chan->flags & IEEE80211_CHAN_DISABLED)
4302 continue;
4303
4304 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004305 i++;
4306 }
4307 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004308 enum ieee80211_band band;
4309
Johannes Berg2a519312009-02-10 21:25:55 +01004310 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004311 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4312 int j;
4313 if (!wiphy->bands[band])
4314 continue;
4315 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004316 struct ieee80211_channel *chan;
4317
4318 chan = &wiphy->bands[band]->channels[j];
4319
4320 if (chan->flags & IEEE80211_CHAN_DISABLED)
4321 continue;
4322
4323 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004324 i++;
4325 }
4326 }
4327 }
4328
Johannes Berg584991d2009-11-02 13:32:03 +01004329 if (!i) {
4330 err = -EINVAL;
4331 goto out_free;
4332 }
4333
4334 request->n_channels = i;
4335
Johannes Berg2a519312009-02-10 21:25:55 +01004336 i = 0;
4337 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4338 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004339 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004340 err = -EINVAL;
4341 goto out_free;
4342 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004343 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004344 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004345 i++;
4346 }
4347 }
4348
Jouni Malinen70692ad2009-02-16 19:39:13 +02004349 if (info->attrs[NL80211_ATTR_IE]) {
4350 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a54b2009-04-01 11:58:36 +02004351 memcpy((void *)request->ie,
4352 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004353 request->ie_len);
4354 }
4355
Johannes Berg34850ab2011-07-18 18:08:35 +02004356 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004357 if (wiphy->bands[i])
4358 request->rates[i] =
4359 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004360
4361 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4362 nla_for_each_nested(attr,
4363 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4364 tmp) {
4365 enum ieee80211_band band = nla_type(attr);
4366
Dan Carpenter84404622011-07-29 11:52:18 +03004367 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004368 err = -EINVAL;
4369 goto out_free;
4370 }
4371 err = ieee80211_get_ratemask(wiphy->bands[band],
4372 nla_data(attr),
4373 nla_len(attr),
4374 &request->rates[band]);
4375 if (err)
4376 goto out_free;
4377 }
4378 }
4379
Sam Leffler46856bb2012-10-11 21:03:32 -07004380 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004381 request->flags = nla_get_u32(
4382 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004383 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4384 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4385 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4386 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004387 err = -EOPNOTSUPP;
4388 goto out_free;
4389 }
4390 }
Sam Lefflered4737712012-10-11 21:03:31 -07004391
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304392 request->no_cck =
4393 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4394
Johannes Bergfd014282012-06-18 19:17:03 +02004395 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004396 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004397 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004398
Johannes Berg79c97e92009-07-07 03:56:12 +02004399 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004400 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004401
Johannes Berg463d0182009-07-14 00:33:35 +02004402 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004403 nl80211_send_scan_start(rdev, wdev);
4404 if (wdev->netdev)
4405 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004406 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004407 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004408 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004409 kfree(request);
4410 }
Johannes Berg3b858752009-03-12 09:55:09 +01004411
Johannes Berg2a519312009-02-10 21:25:55 +01004412 return err;
4413}
4414
Luciano Coelho807f8a82011-05-11 17:09:35 +03004415static int nl80211_start_sched_scan(struct sk_buff *skb,
4416 struct genl_info *info)
4417{
4418 struct cfg80211_sched_scan_request *request;
4419 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4420 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004421 struct nlattr *attr;
4422 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004423 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004424 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004425 enum ieee80211_band band;
4426 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004427 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004428
4429 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4430 !rdev->ops->sched_scan_start)
4431 return -EOPNOTSUPP;
4432
4433 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4434 return -EINVAL;
4435
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004436 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4437 return -EINVAL;
4438
4439 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4440 if (interval == 0)
4441 return -EINVAL;
4442
Luciano Coelho807f8a82011-05-11 17:09:35 +03004443 wiphy = &rdev->wiphy;
4444
4445 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4446 n_channels = validate_scan_freqs(
4447 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4448 if (!n_channels)
4449 return -EINVAL;
4450 } else {
4451 n_channels = 0;
4452
4453 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4454 if (wiphy->bands[band])
4455 n_channels += wiphy->bands[band]->n_channels;
4456 }
4457
4458 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4459 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4460 tmp)
4461 n_ssids++;
4462
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004463 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004464 return -EINVAL;
4465
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004466 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4467 nla_for_each_nested(attr,
4468 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4469 tmp)
4470 n_match_sets++;
4471
4472 if (n_match_sets > wiphy->max_match_sets)
4473 return -EINVAL;
4474
Luciano Coelho807f8a82011-05-11 17:09:35 +03004475 if (info->attrs[NL80211_ATTR_IE])
4476 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4477 else
4478 ie_len = 0;
4479
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004480 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004481 return -EINVAL;
4482
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004483 mutex_lock(&rdev->sched_scan_mtx);
4484
4485 if (rdev->sched_scan_req) {
4486 err = -EINPROGRESS;
4487 goto out;
4488 }
4489
Luciano Coelho807f8a82011-05-11 17:09:35 +03004490 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004491 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004492 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004493 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004494 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004495 if (!request) {
4496 err = -ENOMEM;
4497 goto out;
4498 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004499
4500 if (n_ssids)
4501 request->ssids = (void *)&request->channels[n_channels];
4502 request->n_ssids = n_ssids;
4503 if (ie_len) {
4504 if (request->ssids)
4505 request->ie = (void *)(request->ssids + n_ssids);
4506 else
4507 request->ie = (void *)(request->channels + n_channels);
4508 }
4509
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004510 if (n_match_sets) {
4511 if (request->ie)
4512 request->match_sets = (void *)(request->ie + ie_len);
4513 else if (request->ssids)
4514 request->match_sets =
4515 (void *)(request->ssids + n_ssids);
4516 else
4517 request->match_sets =
4518 (void *)(request->channels + n_channels);
4519 }
4520 request->n_match_sets = n_match_sets;
4521
Luciano Coelho807f8a82011-05-11 17:09:35 +03004522 i = 0;
4523 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4524 /* user specified, bail out if channel not found */
4525 nla_for_each_nested(attr,
4526 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4527 tmp) {
4528 struct ieee80211_channel *chan;
4529
4530 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4531
4532 if (!chan) {
4533 err = -EINVAL;
4534 goto out_free;
4535 }
4536
4537 /* ignore disabled channels */
4538 if (chan->flags & IEEE80211_CHAN_DISABLED)
4539 continue;
4540
4541 request->channels[i] = chan;
4542 i++;
4543 }
4544 } else {
4545 /* all channels */
4546 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4547 int j;
4548 if (!wiphy->bands[band])
4549 continue;
4550 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4551 struct ieee80211_channel *chan;
4552
4553 chan = &wiphy->bands[band]->channels[j];
4554
4555 if (chan->flags & IEEE80211_CHAN_DISABLED)
4556 continue;
4557
4558 request->channels[i] = chan;
4559 i++;
4560 }
4561 }
4562 }
4563
4564 if (!i) {
4565 err = -EINVAL;
4566 goto out_free;
4567 }
4568
4569 request->n_channels = i;
4570
4571 i = 0;
4572 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4573 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4574 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004575 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004576 err = -EINVAL;
4577 goto out_free;
4578 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004579 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004580 memcpy(request->ssids[i].ssid, nla_data(attr),
4581 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004582 i++;
4583 }
4584 }
4585
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004586 i = 0;
4587 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4588 nla_for_each_nested(attr,
4589 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4590 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004591 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004592
4593 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4594 nla_data(attr), nla_len(attr),
4595 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004596 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004597 if (ssid) {
4598 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4599 err = -EINVAL;
4600 goto out_free;
4601 }
4602 memcpy(request->match_sets[i].ssid.ssid,
4603 nla_data(ssid), nla_len(ssid));
4604 request->match_sets[i].ssid.ssid_len =
4605 nla_len(ssid);
4606 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004607 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4608 if (rssi)
4609 request->rssi_thold = nla_get_u32(rssi);
4610 else
4611 request->rssi_thold =
4612 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004613 i++;
4614 }
4615 }
4616
Luciano Coelho807f8a82011-05-11 17:09:35 +03004617 if (info->attrs[NL80211_ATTR_IE]) {
4618 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4619 memcpy((void *)request->ie,
4620 nla_data(info->attrs[NL80211_ATTR_IE]),
4621 request->ie_len);
4622 }
4623
Sam Leffler46856bb2012-10-11 21:03:32 -07004624 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004625 request->flags = nla_get_u32(
4626 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004627 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4628 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4629 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4630 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004631 err = -EOPNOTSUPP;
4632 goto out_free;
4633 }
4634 }
Sam Lefflered4737712012-10-11 21:03:31 -07004635
Luciano Coelho807f8a82011-05-11 17:09:35 +03004636 request->dev = dev;
4637 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004638 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004639 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004640
Hila Gonene35e4d22012-06-27 17:19:42 +03004641 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004642 if (!err) {
4643 rdev->sched_scan_req = request;
4644 nl80211_send_sched_scan(rdev, dev,
4645 NL80211_CMD_START_SCHED_SCAN);
4646 goto out;
4647 }
4648
4649out_free:
4650 kfree(request);
4651out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004652 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004653 return err;
4654}
4655
4656static int nl80211_stop_sched_scan(struct sk_buff *skb,
4657 struct genl_info *info)
4658{
4659 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004660 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004661
4662 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4663 !rdev->ops->sched_scan_stop)
4664 return -EOPNOTSUPP;
4665
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004666 mutex_lock(&rdev->sched_scan_mtx);
4667 err = __cfg80211_stop_sched_scan(rdev, false);
4668 mutex_unlock(&rdev->sched_scan_mtx);
4669
4670 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004671}
4672
Johannes Berg9720bb32011-06-21 09:45:33 +02004673static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4674 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004675 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004676 struct wireless_dev *wdev,
4677 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004678{
Johannes Berg48ab9052009-07-10 18:42:31 +02004679 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004680 void *hdr;
4681 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004682
4683 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004684
Eric W. Biederman15e47302012-09-07 20:12:54 +00004685 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004686 NL80211_CMD_NEW_SCAN_RESULTS);
4687 if (!hdr)
4688 return -1;
4689
Johannes Berg9720bb32011-06-21 09:45:33 +02004690 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4691
David S. Miller9360ffd2012-03-29 04:41:26 -04004692 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4693 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4694 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004695
4696 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4697 if (!bss)
4698 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004699 if ((!is_zero_ether_addr(res->bssid) &&
4700 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4701 (res->information_elements && res->len_information_elements &&
4702 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4703 res->len_information_elements,
4704 res->information_elements)) ||
4705 (res->beacon_ies && res->len_beacon_ies &&
4706 res->beacon_ies != res->information_elements &&
4707 nla_put(msg, NL80211_BSS_BEACON_IES,
4708 res->len_beacon_ies, res->beacon_ies)))
4709 goto nla_put_failure;
4710 if (res->tsf &&
4711 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4712 goto nla_put_failure;
4713 if (res->beacon_interval &&
4714 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4715 goto nla_put_failure;
4716 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4717 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4718 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4719 jiffies_to_msecs(jiffies - intbss->ts)))
4720 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004721
Johannes Berg77965c972009-02-18 18:45:06 +01004722 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004723 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004724 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4725 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004726 break;
4727 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004728 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4729 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004730 break;
4731 default:
4732 break;
4733 }
4734
Johannes Berg48ab9052009-07-10 18:42:31 +02004735 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004736 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004737 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004738 if (intbss == wdev->current_bss &&
4739 nla_put_u32(msg, NL80211_BSS_STATUS,
4740 NL80211_BSS_STATUS_ASSOCIATED))
4741 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004742 break;
4743 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004744 if (intbss == wdev->current_bss &&
4745 nla_put_u32(msg, NL80211_BSS_STATUS,
4746 NL80211_BSS_STATUS_IBSS_JOINED))
4747 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004748 break;
4749 default:
4750 break;
4751 }
4752
Johannes Berg2a519312009-02-10 21:25:55 +01004753 nla_nest_end(msg, bss);
4754
4755 return genlmsg_end(msg, hdr);
4756
4757 nla_put_failure:
4758 genlmsg_cancel(msg, hdr);
4759 return -EMSGSIZE;
4760}
4761
4762static int nl80211_dump_scan(struct sk_buff *skb,
4763 struct netlink_callback *cb)
4764{
Johannes Berg48ab9052009-07-10 18:42:31 +02004765 struct cfg80211_registered_device *rdev;
4766 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004767 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004768 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004769 int start = cb->args[1], idx = 0;
4770 int err;
4771
Johannes Berg67748892010-10-04 21:14:06 +02004772 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4773 if (err)
4774 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004775
Johannes Berg48ab9052009-07-10 18:42:31 +02004776 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004777
Johannes Berg48ab9052009-07-10 18:42:31 +02004778 wdev_lock(wdev);
4779 spin_lock_bh(&rdev->bss_lock);
4780 cfg80211_bss_expire(rdev);
4781
Johannes Berg9720bb32011-06-21 09:45:33 +02004782 cb->seq = rdev->bss_generation;
4783
Johannes Berg48ab9052009-07-10 18:42:31 +02004784 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004785 if (++idx <= start)
4786 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004787 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004788 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004789 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004790 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004791 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004792 }
4793 }
4794
Johannes Berg48ab9052009-07-10 18:42:31 +02004795 spin_unlock_bh(&rdev->bss_lock);
4796 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004797
4798 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004799 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004800
Johannes Berg67748892010-10-04 21:14:06 +02004801 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004802}
4803
Eric W. Biederman15e47302012-09-07 20:12:54 +00004804static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004805 int flags, struct net_device *dev,
4806 struct survey_info *survey)
4807{
4808 void *hdr;
4809 struct nlattr *infoattr;
4810
Eric W. Biederman15e47302012-09-07 20:12:54 +00004811 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004812 NL80211_CMD_NEW_SURVEY_RESULTS);
4813 if (!hdr)
4814 return -ENOMEM;
4815
David S. Miller9360ffd2012-03-29 04:41:26 -04004816 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4817 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004818
4819 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4820 if (!infoattr)
4821 goto nla_put_failure;
4822
David S. Miller9360ffd2012-03-29 04:41:26 -04004823 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4824 survey->channel->center_freq))
4825 goto nla_put_failure;
4826
4827 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4828 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4829 goto nla_put_failure;
4830 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4831 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4832 goto nla_put_failure;
4833 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4834 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4835 survey->channel_time))
4836 goto nla_put_failure;
4837 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4838 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4839 survey->channel_time_busy))
4840 goto nla_put_failure;
4841 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4842 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4843 survey->channel_time_ext_busy))
4844 goto nla_put_failure;
4845 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4846 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4847 survey->channel_time_rx))
4848 goto nla_put_failure;
4849 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4850 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4851 survey->channel_time_tx))
4852 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004853
4854 nla_nest_end(msg, infoattr);
4855
4856 return genlmsg_end(msg, hdr);
4857
4858 nla_put_failure:
4859 genlmsg_cancel(msg, hdr);
4860 return -EMSGSIZE;
4861}
4862
4863static int nl80211_dump_survey(struct sk_buff *skb,
4864 struct netlink_callback *cb)
4865{
4866 struct survey_info survey;
4867 struct cfg80211_registered_device *dev;
4868 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004869 int survey_idx = cb->args[1];
4870 int res;
4871
Johannes Berg67748892010-10-04 21:14:06 +02004872 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4873 if (res)
4874 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004875
4876 if (!dev->ops->dump_survey) {
4877 res = -EOPNOTSUPP;
4878 goto out_err;
4879 }
4880
4881 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004882 struct ieee80211_channel *chan;
4883
Hila Gonene35e4d22012-06-27 17:19:42 +03004884 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01004885 if (res == -ENOENT)
4886 break;
4887 if (res)
4888 goto out_err;
4889
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004890 /* Survey without a channel doesn't make sense */
4891 if (!survey.channel) {
4892 res = -EINVAL;
4893 goto out;
4894 }
4895
4896 chan = ieee80211_get_channel(&dev->wiphy,
4897 survey.channel->center_freq);
4898 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4899 survey_idx++;
4900 continue;
4901 }
4902
Holger Schurig61fa7132009-11-11 12:25:40 +01004903 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004904 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01004905 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4906 netdev,
4907 &survey) < 0)
4908 goto out;
4909 survey_idx++;
4910 }
4911
4912 out:
4913 cb->args[1] = survey_idx;
4914 res = skb->len;
4915 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004916 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004917 return res;
4918}
4919
Samuel Ortizb23aa672009-07-01 21:26:54 +02004920static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4921{
4922 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4923 NL80211_WPA_VERSION_2));
4924}
4925
Jouni Malinen636a5d32009-03-19 13:39:22 +02004926static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4927{
Johannes Berg4c476992010-10-04 21:36:35 +02004928 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4929 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004930 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03004931 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
4932 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004933 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004934 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004935 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004936
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004937 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4938 return -EINVAL;
4939
4940 if (!info->attrs[NL80211_ATTR_MAC])
4941 return -EINVAL;
4942
Jouni Malinen17780922009-03-27 20:52:47 +02004943 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4944 return -EINVAL;
4945
Johannes Berg19957bb2009-07-02 17:20:43 +02004946 if (!info->attrs[NL80211_ATTR_SSID])
4947 return -EINVAL;
4948
4949 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4950 return -EINVAL;
4951
Johannes Bergfffd0932009-07-08 14:22:54 +02004952 err = nl80211_parse_key(info, &key);
4953 if (err)
4954 return err;
4955
4956 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004957 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4958 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004959 if (!key.p.key || !key.p.key_len)
4960 return -EINVAL;
4961 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4962 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4963 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4964 key.p.key_len != WLAN_KEY_LEN_WEP104))
4965 return -EINVAL;
4966 if (key.idx > 4)
4967 return -EINVAL;
4968 } else {
4969 key.p.key_len = 0;
4970 key.p.key = NULL;
4971 }
4972
Johannes Bergafea0b72010-08-10 09:46:42 +02004973 if (key.idx >= 0) {
4974 int i;
4975 bool ok = false;
4976 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4977 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4978 ok = true;
4979 break;
4980 }
4981 }
Johannes Berg4c476992010-10-04 21:36:35 +02004982 if (!ok)
4983 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004984 }
4985
Johannes Berg4c476992010-10-04 21:36:35 +02004986 if (!rdev->ops->auth)
4987 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004988
Johannes Berg074ac8d2010-09-16 14:58:22 +02004989 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004990 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4991 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004992
Johannes Berg19957bb2009-07-02 17:20:43 +02004993 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004994 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004995 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004996 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4997 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004998
Johannes Berg19957bb2009-07-02 17:20:43 +02004999 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5000 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5001
5002 if (info->attrs[NL80211_ATTR_IE]) {
5003 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5004 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5005 }
5006
5007 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005008 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005009 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005010
Jouni Malinene39e5b52012-09-30 19:29:39 +03005011 if (auth_type == NL80211_AUTHTYPE_SAE &&
5012 !info->attrs[NL80211_ATTR_SAE_DATA])
5013 return -EINVAL;
5014
5015 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5016 if (auth_type != NL80211_AUTHTYPE_SAE)
5017 return -EINVAL;
5018 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5019 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5020 /* need to include at least Auth Transaction and Status Code */
5021 if (sae_data_len < 4)
5022 return -EINVAL;
5023 }
5024
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005025 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5026
Johannes Berg95de8172012-01-20 13:55:25 +01005027 /*
5028 * Since we no longer track auth state, ignore
5029 * requests to only change local state.
5030 */
5031 if (local_state_change)
5032 return 0;
5033
Johannes Berg4c476992010-10-04 21:36:35 +02005034 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5035 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005036 key.p.key, key.p.key_len, key.idx,
5037 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005038}
5039
Johannes Bergc0692b82010-08-27 14:26:53 +03005040static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5041 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005042 struct cfg80211_crypto_settings *settings,
5043 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005044{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005045 memset(settings, 0, sizeof(*settings));
5046
Samuel Ortizb23aa672009-07-01 21:26:54 +02005047 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5048
Johannes Bergc0692b82010-08-27 14:26:53 +03005049 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5050 u16 proto;
5051 proto = nla_get_u16(
5052 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5053 settings->control_port_ethertype = cpu_to_be16(proto);
5054 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5055 proto != ETH_P_PAE)
5056 return -EINVAL;
5057 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5058 settings->control_port_no_encrypt = true;
5059 } else
5060 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5061
Samuel Ortizb23aa672009-07-01 21:26:54 +02005062 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5063 void *data;
5064 int len, i;
5065
5066 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5067 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5068 settings->n_ciphers_pairwise = len / sizeof(u32);
5069
5070 if (len % sizeof(u32))
5071 return -EINVAL;
5072
Johannes Berg3dc27d22009-07-02 21:36:37 +02005073 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005074 return -EINVAL;
5075
5076 memcpy(settings->ciphers_pairwise, data, len);
5077
5078 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005079 if (!cfg80211_supported_cipher_suite(
5080 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005081 settings->ciphers_pairwise[i]))
5082 return -EINVAL;
5083 }
5084
5085 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5086 settings->cipher_group =
5087 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005088 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5089 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005090 return -EINVAL;
5091 }
5092
5093 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5094 settings->wpa_versions =
5095 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5096 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5097 return -EINVAL;
5098 }
5099
5100 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5101 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005102 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005103
5104 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5105 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5106 settings->n_akm_suites = len / sizeof(u32);
5107
5108 if (len % sizeof(u32))
5109 return -EINVAL;
5110
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005111 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5112 return -EINVAL;
5113
Samuel Ortizb23aa672009-07-01 21:26:54 +02005114 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005115 }
5116
5117 return 0;
5118}
5119
Jouni Malinen636a5d32009-03-19 13:39:22 +02005120static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5121{
Johannes Berg4c476992010-10-04 21:36:35 +02005122 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5123 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005124 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005125 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005126 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005127 int err, ssid_len, ie_len = 0;
5128 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005129 u32 flags = 0;
5130 struct ieee80211_ht_cap *ht_capa = NULL;
5131 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005132
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005133 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5134 return -EINVAL;
5135
5136 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005137 !info->attrs[NL80211_ATTR_SSID] ||
5138 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005139 return -EINVAL;
5140
Johannes Berg4c476992010-10-04 21:36:35 +02005141 if (!rdev->ops->assoc)
5142 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005143
Johannes Berg074ac8d2010-09-16 14:58:22 +02005144 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005145 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5146 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005147
Johannes Berg19957bb2009-07-02 17:20:43 +02005148 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005149
Johannes Berg19957bb2009-07-02 17:20:43 +02005150 chan = ieee80211_get_channel(&rdev->wiphy,
5151 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005152 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5153 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005154
Johannes Berg19957bb2009-07-02 17:20:43 +02005155 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5156 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005157
5158 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005159 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5160 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005161 }
5162
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005163 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005164 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005165 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005166 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005167 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005168 else if (mfp != NL80211_MFP_NO)
5169 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005170 }
5171
Johannes Berg3e5d7642009-07-07 14:37:26 +02005172 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5173 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5174
Ben Greear7e7c8922011-11-18 11:31:59 -08005175 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5176 flags |= ASSOC_REQ_DISABLE_HT;
5177
5178 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5179 ht_capa_mask =
5180 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5181
5182 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5183 if (!ht_capa_mask)
5184 return -EINVAL;
5185 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5186 }
5187
Johannes Bergc0692b82010-08-27 14:26:53 +03005188 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005189 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005190 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5191 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005192 &crypto, flags, ht_capa,
5193 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005194
Jouni Malinen636a5d32009-03-19 13:39:22 +02005195 return err;
5196}
5197
5198static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5199{
Johannes Berg4c476992010-10-04 21:36:35 +02005200 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5201 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005202 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005203 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005204 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005205 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005206
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005207 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5208 return -EINVAL;
5209
5210 if (!info->attrs[NL80211_ATTR_MAC])
5211 return -EINVAL;
5212
5213 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5214 return -EINVAL;
5215
Johannes Berg4c476992010-10-04 21:36:35 +02005216 if (!rdev->ops->deauth)
5217 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005218
Johannes Berg074ac8d2010-09-16 14:58:22 +02005219 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005220 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5221 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005222
Johannes Berg19957bb2009-07-02 17:20:43 +02005223 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005224
Johannes Berg19957bb2009-07-02 17:20:43 +02005225 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5226 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005227 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005228 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005229 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005230
5231 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005232 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5233 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005234 }
5235
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005236 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5237
Johannes Berg4c476992010-10-04 21:36:35 +02005238 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5239 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005240}
5241
5242static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5243{
Johannes Berg4c476992010-10-04 21:36:35 +02005244 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5245 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005246 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005247 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005248 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005249 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005250
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005251 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5252 return -EINVAL;
5253
5254 if (!info->attrs[NL80211_ATTR_MAC])
5255 return -EINVAL;
5256
5257 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5258 return -EINVAL;
5259
Johannes Berg4c476992010-10-04 21:36:35 +02005260 if (!rdev->ops->disassoc)
5261 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005262
Johannes Berg074ac8d2010-09-16 14:58:22 +02005263 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005264 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5265 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005266
Johannes Berg19957bb2009-07-02 17:20:43 +02005267 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005268
Johannes Berg19957bb2009-07-02 17:20:43 +02005269 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5270 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005271 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005272 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005273 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005274
5275 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005276 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5277 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005278 }
5279
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005280 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5281
Johannes Berg4c476992010-10-04 21:36:35 +02005282 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5283 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005284}
5285
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005286static bool
5287nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5288 int mcast_rate[IEEE80211_NUM_BANDS],
5289 int rateval)
5290{
5291 struct wiphy *wiphy = &rdev->wiphy;
5292 bool found = false;
5293 int band, i;
5294
5295 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5296 struct ieee80211_supported_band *sband;
5297
5298 sband = wiphy->bands[band];
5299 if (!sband)
5300 continue;
5301
5302 for (i = 0; i < sband->n_bitrates; i++) {
5303 if (sband->bitrates[i].bitrate == rateval) {
5304 mcast_rate[band] = i + 1;
5305 found = true;
5306 break;
5307 }
5308 }
5309 }
5310
5311 return found;
5312}
5313
Johannes Berg04a773a2009-04-19 21:24:32 +02005314static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5315{
Johannes Berg4c476992010-10-04 21:36:35 +02005316 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5317 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005318 struct cfg80211_ibss_params ibss;
5319 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005320 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005321 int err;
5322
Johannes Berg8e30bc52009-04-22 17:45:38 +02005323 memset(&ibss, 0, sizeof(ibss));
5324
Johannes Berg04a773a2009-04-19 21:24:32 +02005325 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5326 return -EINVAL;
5327
5328 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5329 !info->attrs[NL80211_ATTR_SSID] ||
5330 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5331 return -EINVAL;
5332
Johannes Berg8e30bc52009-04-22 17:45:38 +02005333 ibss.beacon_interval = 100;
5334
5335 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5336 ibss.beacon_interval =
5337 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5338 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5339 return -EINVAL;
5340 }
5341
Johannes Berg4c476992010-10-04 21:36:35 +02005342 if (!rdev->ops->join_ibss)
5343 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005344
Johannes Berg4c476992010-10-04 21:36:35 +02005345 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5346 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005347
Johannes Berg79c97e92009-07-07 03:56:12 +02005348 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005349
Johannes Berg39193492011-09-16 13:45:25 +02005350 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005351 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005352
5353 if (!is_valid_ether_addr(ibss.bssid))
5354 return -EINVAL;
5355 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005356 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5357 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5358
5359 if (info->attrs[NL80211_ATTR_IE]) {
5360 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5361 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5362 }
5363
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005364 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5365 enum nl80211_channel_type channel_type;
5366
Johannes Bergcd6c65982012-05-10 21:27:18 +02005367 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005368 return -EINVAL;
5369
5370 if (channel_type != NL80211_CHAN_NO_HT &&
5371 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5372 return -EINVAL;
5373
5374 ibss.channel_type = channel_type;
5375 } else {
5376 ibss.channel_type = NL80211_CHAN_NO_HT;
5377 }
5378
5379 ibss.channel = rdev_freq_to_chan(rdev,
5380 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5381 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005382 if (!ibss.channel ||
5383 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005384 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5385 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005386
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005387 /* Both channels should be able to initiate communication */
5388 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5389 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5390 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5391 ibss.channel_type))
5392 return -EINVAL;
5393
Johannes Berg04a773a2009-04-19 21:24:32 +02005394 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005395 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005396
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005397 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5398 u8 *rates =
5399 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5400 int n_rates =
5401 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5402 struct ieee80211_supported_band *sband =
5403 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005404
Johannes Berg34850ab2011-07-18 18:08:35 +02005405 err = ieee80211_get_ratemask(sband, rates, n_rates,
5406 &ibss.basic_rates);
5407 if (err)
5408 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005409 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005410
5411 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5412 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5413 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5414 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005415
Johannes Berg4c476992010-10-04 21:36:35 +02005416 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305417 bool no_ht = false;
5418
Johannes Berg4c476992010-10-04 21:36:35 +02005419 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305420 info->attrs[NL80211_ATTR_KEYS],
5421 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005422 if (IS_ERR(connkeys))
5423 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305424
5425 if ((ibss.channel_type != NL80211_CHAN_NO_HT) && no_ht) {
5426 kfree(connkeys);
5427 return -EINVAL;
5428 }
Johannes Berg4c476992010-10-04 21:36:35 +02005429 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005430
Antonio Quartulli267335d2012-01-31 20:25:47 +01005431 ibss.control_port =
5432 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5433
Johannes Berg4c476992010-10-04 21:36:35 +02005434 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005435 if (err)
5436 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005437 return err;
5438}
5439
5440static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5441{
Johannes Berg4c476992010-10-04 21:36:35 +02005442 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5443 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005444
Johannes Berg4c476992010-10-04 21:36:35 +02005445 if (!rdev->ops->leave_ibss)
5446 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005447
Johannes Berg4c476992010-10-04 21:36:35 +02005448 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5449 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005450
Johannes Berg4c476992010-10-04 21:36:35 +02005451 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005452}
5453
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005454static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5455{
5456 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5457 struct net_device *dev = info->user_ptr[1];
5458 int mcast_rate[IEEE80211_NUM_BANDS];
5459 u32 nla_rate;
5460 int err;
5461
5462 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5463 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5464 return -EOPNOTSUPP;
5465
5466 if (!rdev->ops->set_mcast_rate)
5467 return -EOPNOTSUPP;
5468
5469 memset(mcast_rate, 0, sizeof(mcast_rate));
5470
5471 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5472 return -EINVAL;
5473
5474 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5475 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5476 return -EINVAL;
5477
5478 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5479
5480 return err;
5481}
5482
5483
Johannes Bergaff89a92009-07-01 21:26:51 +02005484#ifdef CONFIG_NL80211_TESTMODE
5485static struct genl_multicast_group nl80211_testmode_mcgrp = {
5486 .name = "testmode",
5487};
5488
5489static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5490{
Johannes Berg4c476992010-10-04 21:36:35 +02005491 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005492 int err;
5493
5494 if (!info->attrs[NL80211_ATTR_TESTDATA])
5495 return -EINVAL;
5496
Johannes Bergaff89a92009-07-01 21:26:51 +02005497 err = -EOPNOTSUPP;
5498 if (rdev->ops->testmode_cmd) {
5499 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005500 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005501 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5502 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5503 rdev->testmode_info = NULL;
5504 }
5505
Johannes Bergaff89a92009-07-01 21:26:51 +02005506 return err;
5507}
5508
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005509static int nl80211_testmode_dump(struct sk_buff *skb,
5510 struct netlink_callback *cb)
5511{
Johannes Berg00918d32011-12-13 17:22:05 +01005512 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005513 int err;
5514 long phy_idx;
5515 void *data = NULL;
5516 int data_len = 0;
5517
5518 if (cb->args[0]) {
5519 /*
5520 * 0 is a valid index, but not valid for args[0],
5521 * so we need to offset by 1.
5522 */
5523 phy_idx = cb->args[0] - 1;
5524 } else {
5525 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5526 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5527 nl80211_policy);
5528 if (err)
5529 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005530
Johannes Berg2bd7e352012-06-15 14:23:16 +02005531 mutex_lock(&cfg80211_mutex);
5532 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5533 nl80211_fam.attrbuf);
5534 if (IS_ERR(rdev)) {
5535 mutex_unlock(&cfg80211_mutex);
5536 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005537 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005538 phy_idx = rdev->wiphy_idx;
5539 rdev = NULL;
5540 mutex_unlock(&cfg80211_mutex);
5541
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005542 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5543 cb->args[1] =
5544 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5545 }
5546
5547 if (cb->args[1]) {
5548 data = nla_data((void *)cb->args[1]);
5549 data_len = nla_len((void *)cb->args[1]);
5550 }
5551
5552 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005553 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5554 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005555 mutex_unlock(&cfg80211_mutex);
5556 return -ENOENT;
5557 }
Johannes Berg00918d32011-12-13 17:22:05 +01005558 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005559 mutex_unlock(&cfg80211_mutex);
5560
Johannes Berg00918d32011-12-13 17:22:05 +01005561 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005562 err = -EOPNOTSUPP;
5563 goto out_err;
5564 }
5565
5566 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005567 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005568 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5569 NL80211_CMD_TESTMODE);
5570 struct nlattr *tmdata;
5571
David S. Miller9360ffd2012-03-29 04:41:26 -04005572 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005573 genlmsg_cancel(skb, hdr);
5574 break;
5575 }
5576
5577 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5578 if (!tmdata) {
5579 genlmsg_cancel(skb, hdr);
5580 break;
5581 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005582 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005583 nla_nest_end(skb, tmdata);
5584
5585 if (err == -ENOBUFS || err == -ENOENT) {
5586 genlmsg_cancel(skb, hdr);
5587 break;
5588 } else if (err) {
5589 genlmsg_cancel(skb, hdr);
5590 goto out_err;
5591 }
5592
5593 genlmsg_end(skb, hdr);
5594 }
5595
5596 err = skb->len;
5597 /* see above */
5598 cb->args[0] = phy_idx + 1;
5599 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005600 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005601 return err;
5602}
5603
Johannes Bergaff89a92009-07-01 21:26:51 +02005604static struct sk_buff *
5605__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005606 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005607{
5608 struct sk_buff *skb;
5609 void *hdr;
5610 struct nlattr *data;
5611
5612 skb = nlmsg_new(approxlen + 100, gfp);
5613 if (!skb)
5614 return NULL;
5615
Eric W. Biederman15e47302012-09-07 20:12:54 +00005616 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005617 if (!hdr) {
5618 kfree_skb(skb);
5619 return NULL;
5620 }
5621
David S. Miller9360ffd2012-03-29 04:41:26 -04005622 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5623 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005624 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5625
5626 ((void **)skb->cb)[0] = rdev;
5627 ((void **)skb->cb)[1] = hdr;
5628 ((void **)skb->cb)[2] = data;
5629
5630 return skb;
5631
5632 nla_put_failure:
5633 kfree_skb(skb);
5634 return NULL;
5635}
5636
5637struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5638 int approxlen)
5639{
5640 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5641
5642 if (WARN_ON(!rdev->testmode_info))
5643 return NULL;
5644
5645 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005646 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005647 rdev->testmode_info->snd_seq,
5648 GFP_KERNEL);
5649}
5650EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5651
5652int cfg80211_testmode_reply(struct sk_buff *skb)
5653{
5654 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5655 void *hdr = ((void **)skb->cb)[1];
5656 struct nlattr *data = ((void **)skb->cb)[2];
5657
5658 if (WARN_ON(!rdev->testmode_info)) {
5659 kfree_skb(skb);
5660 return -EINVAL;
5661 }
5662
5663 nla_nest_end(skb, data);
5664 genlmsg_end(skb, hdr);
5665 return genlmsg_reply(skb, rdev->testmode_info);
5666}
5667EXPORT_SYMBOL(cfg80211_testmode_reply);
5668
5669struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5670 int approxlen, gfp_t gfp)
5671{
5672 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5673
5674 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5675}
5676EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5677
5678void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5679{
5680 void *hdr = ((void **)skb->cb)[1];
5681 struct nlattr *data = ((void **)skb->cb)[2];
5682
5683 nla_nest_end(skb, data);
5684 genlmsg_end(skb, hdr);
5685 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5686}
5687EXPORT_SYMBOL(cfg80211_testmode_event);
5688#endif
5689
Samuel Ortizb23aa672009-07-01 21:26:54 +02005690static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5691{
Johannes Berg4c476992010-10-04 21:36:35 +02005692 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5693 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005694 struct cfg80211_connect_params connect;
5695 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005696 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005697 int err;
5698
5699 memset(&connect, 0, sizeof(connect));
5700
5701 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5702 return -EINVAL;
5703
5704 if (!info->attrs[NL80211_ATTR_SSID] ||
5705 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5706 return -EINVAL;
5707
5708 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5709 connect.auth_type =
5710 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005711 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5712 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005713 return -EINVAL;
5714 } else
5715 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5716
5717 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5718
Johannes Bergc0692b82010-08-27 14:26:53 +03005719 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005720 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005721 if (err)
5722 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005723
Johannes Berg074ac8d2010-09-16 14:58:22 +02005724 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005725 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5726 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005727
Johannes Berg79c97e92009-07-07 03:56:12 +02005728 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005729
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305730 connect.bg_scan_period = -1;
5731 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5732 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5733 connect.bg_scan_period =
5734 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5735 }
5736
Samuel Ortizb23aa672009-07-01 21:26:54 +02005737 if (info->attrs[NL80211_ATTR_MAC])
5738 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5739 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5740 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5741
5742 if (info->attrs[NL80211_ATTR_IE]) {
5743 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5744 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5745 }
5746
5747 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5748 connect.channel =
5749 ieee80211_get_channel(wiphy,
5750 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5751 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005752 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5753 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005754 }
5755
Johannes Bergfffd0932009-07-08 14:22:54 +02005756 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5757 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305758 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02005759 if (IS_ERR(connkeys))
5760 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005761 }
5762
Ben Greear7e7c8922011-11-18 11:31:59 -08005763 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5764 connect.flags |= ASSOC_REQ_DISABLE_HT;
5765
5766 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5767 memcpy(&connect.ht_capa_mask,
5768 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5769 sizeof(connect.ht_capa_mask));
5770
5771 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005772 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5773 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005774 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005775 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005776 memcpy(&connect.ht_capa,
5777 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5778 sizeof(connect.ht_capa));
5779 }
5780
Johannes Bergfffd0932009-07-08 14:22:54 +02005781 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005782 if (err)
5783 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005784 return err;
5785}
5786
5787static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5788{
Johannes Berg4c476992010-10-04 21:36:35 +02005789 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5790 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005791 u16 reason;
5792
5793 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5794 reason = WLAN_REASON_DEAUTH_LEAVING;
5795 else
5796 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5797
5798 if (reason == 0)
5799 return -EINVAL;
5800
Johannes Berg074ac8d2010-09-16 14:58:22 +02005801 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005802 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5803 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005804
Johannes Berg4c476992010-10-04 21:36:35 +02005805 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005806}
5807
Johannes Berg463d0182009-07-14 00:33:35 +02005808static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5809{
Johannes Berg4c476992010-10-04 21:36:35 +02005810 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005811 struct net *net;
5812 int err;
5813 u32 pid;
5814
5815 if (!info->attrs[NL80211_ATTR_PID])
5816 return -EINVAL;
5817
5818 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5819
Johannes Berg463d0182009-07-14 00:33:35 +02005820 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005821 if (IS_ERR(net))
5822 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005823
5824 err = 0;
5825
5826 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005827 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5828 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005829
Johannes Berg463d0182009-07-14 00:33:35 +02005830 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005831 return err;
5832}
5833
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005834static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5835{
Johannes Berg4c476992010-10-04 21:36:35 +02005836 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005837 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5838 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005839 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005840 struct cfg80211_pmksa pmksa;
5841
5842 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5843
5844 if (!info->attrs[NL80211_ATTR_MAC])
5845 return -EINVAL;
5846
5847 if (!info->attrs[NL80211_ATTR_PMKID])
5848 return -EINVAL;
5849
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005850 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5851 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5852
Johannes Berg074ac8d2010-09-16 14:58:22 +02005853 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005854 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5855 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005856
5857 switch (info->genlhdr->cmd) {
5858 case NL80211_CMD_SET_PMKSA:
5859 rdev_ops = rdev->ops->set_pmksa;
5860 break;
5861 case NL80211_CMD_DEL_PMKSA:
5862 rdev_ops = rdev->ops->del_pmksa;
5863 break;
5864 default:
5865 WARN_ON(1);
5866 break;
5867 }
5868
Johannes Berg4c476992010-10-04 21:36:35 +02005869 if (!rdev_ops)
5870 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005871
Johannes Berg4c476992010-10-04 21:36:35 +02005872 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005873}
5874
5875static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5876{
Johannes Berg4c476992010-10-04 21:36:35 +02005877 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5878 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005879
Johannes Berg074ac8d2010-09-16 14:58:22 +02005880 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005881 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5882 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005883
Johannes Berg4c476992010-10-04 21:36:35 +02005884 if (!rdev->ops->flush_pmksa)
5885 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005886
Hila Gonene35e4d22012-06-27 17:19:42 +03005887 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005888}
5889
Arik Nemtsov109086c2011-09-28 14:12:50 +03005890static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5891{
5892 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5893 struct net_device *dev = info->user_ptr[1];
5894 u8 action_code, dialog_token;
5895 u16 status_code;
5896 u8 *peer;
5897
5898 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5899 !rdev->ops->tdls_mgmt)
5900 return -EOPNOTSUPP;
5901
5902 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5903 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5904 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5905 !info->attrs[NL80211_ATTR_IE] ||
5906 !info->attrs[NL80211_ATTR_MAC])
5907 return -EINVAL;
5908
5909 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5910 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5911 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5912 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5913
Hila Gonene35e4d22012-06-27 17:19:42 +03005914 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
5915 dialog_token, status_code,
5916 nla_data(info->attrs[NL80211_ATTR_IE]),
5917 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03005918}
5919
5920static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5921{
5922 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5923 struct net_device *dev = info->user_ptr[1];
5924 enum nl80211_tdls_operation operation;
5925 u8 *peer;
5926
5927 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5928 !rdev->ops->tdls_oper)
5929 return -EOPNOTSUPP;
5930
5931 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5932 !info->attrs[NL80211_ATTR_MAC])
5933 return -EINVAL;
5934
5935 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5936 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5937
Hila Gonene35e4d22012-06-27 17:19:42 +03005938 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03005939}
5940
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005941static int nl80211_remain_on_channel(struct sk_buff *skb,
5942 struct genl_info *info)
5943{
Johannes Berg4c476992010-10-04 21:36:35 +02005944 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005945 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005946 struct ieee80211_channel *chan;
5947 struct sk_buff *msg;
5948 void *hdr;
5949 u64 cookie;
5950 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5951 u32 freq, duration;
5952 int err;
5953
5954 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5955 !info->attrs[NL80211_ATTR_DURATION])
5956 return -EINVAL;
5957
5958 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5959
Johannes Berg7c4ef712011-11-18 15:33:48 +01005960 if (!rdev->ops->remain_on_channel ||
5961 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005962 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005963
Johannes Bergebf348f2012-06-01 12:50:54 +02005964 /*
5965 * We should be on that channel for at least a minimum amount of
5966 * time (10ms) but no longer than the driver supports.
5967 */
5968 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5969 duration > rdev->wiphy.max_remain_on_channel_duration)
5970 return -EINVAL;
5971
Johannes Bergcd6c65982012-05-10 21:27:18 +02005972 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5973 !nl80211_valid_channel_type(info, &channel_type))
5974 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005975
5976 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5977 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005978 if (chan == NULL)
5979 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005980
5981 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005982 if (!msg)
5983 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005984
Eric W. Biederman15e47302012-09-07 20:12:54 +00005985 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005986 NL80211_CMD_REMAIN_ON_CHANNEL);
5987
5988 if (IS_ERR(hdr)) {
5989 err = PTR_ERR(hdr);
5990 goto free_msg;
5991 }
5992
Hila Gonene35e4d22012-06-27 17:19:42 +03005993 err = rdev_remain_on_channel(rdev, wdev, chan, channel_type, duration,
5994 &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005995
5996 if (err)
5997 goto free_msg;
5998
David S. Miller9360ffd2012-03-29 04:41:26 -04005999 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6000 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006001
6002 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006003
6004 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006005
6006 nla_put_failure:
6007 err = -ENOBUFS;
6008 free_msg:
6009 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006010 return err;
6011}
6012
6013static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6014 struct genl_info *info)
6015{
Johannes Berg4c476992010-10-04 21:36:35 +02006016 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006017 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006018 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006019
6020 if (!info->attrs[NL80211_ATTR_COOKIE])
6021 return -EINVAL;
6022
Johannes Berg4c476992010-10-04 21:36:35 +02006023 if (!rdev->ops->cancel_remain_on_channel)
6024 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006025
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006026 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6027
Hila Gonene35e4d22012-06-27 17:19:42 +03006028 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006029}
6030
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006031static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6032 u8 *rates, u8 rates_len)
6033{
6034 u8 i;
6035 u32 mask = 0;
6036
6037 for (i = 0; i < rates_len; i++) {
6038 int rate = (rates[i] & 0x7f) * 5;
6039 int ridx;
6040 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6041 struct ieee80211_rate *srate =
6042 &sband->bitrates[ridx];
6043 if (rate == srate->bitrate) {
6044 mask |= 1 << ridx;
6045 break;
6046 }
6047 }
6048 if (ridx == sband->n_bitrates)
6049 return 0; /* rate not found */
6050 }
6051
6052 return mask;
6053}
6054
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006055static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6056 u8 *rates, u8 rates_len,
6057 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6058{
6059 u8 i;
6060
6061 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6062
6063 for (i = 0; i < rates_len; i++) {
6064 int ridx, rbit;
6065
6066 ridx = rates[i] / 8;
6067 rbit = BIT(rates[i] % 8);
6068
6069 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006070 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006071 return false;
6072
6073 /* check availability */
6074 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6075 mcs[ridx] |= rbit;
6076 else
6077 return false;
6078 }
6079
6080 return true;
6081}
6082
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006083static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006084 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6085 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006086 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6087 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006088};
6089
6090static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6091 struct genl_info *info)
6092{
6093 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006094 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006095 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006096 int rem, i;
6097 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006098 struct nlattr *tx_rates;
6099 struct ieee80211_supported_band *sband;
6100
6101 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6102 return -EINVAL;
6103
Johannes Berg4c476992010-10-04 21:36:35 +02006104 if (!rdev->ops->set_bitrate_mask)
6105 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006106
6107 memset(&mask, 0, sizeof(mask));
6108 /* Default to all rates enabled */
6109 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6110 sband = rdev->wiphy.bands[i];
6111 mask.control[i].legacy =
6112 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006113 if (sband)
6114 memcpy(mask.control[i].mcs,
6115 sband->ht_cap.mcs.rx_mask,
6116 sizeof(mask.control[i].mcs));
6117 else
6118 memset(mask.control[i].mcs, 0,
6119 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006120 }
6121
6122 /*
6123 * The nested attribute uses enum nl80211_band as the index. This maps
6124 * directly to the enum ieee80211_band values used in cfg80211.
6125 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006126 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006127 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6128 {
6129 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006130 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6131 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006132 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006133 if (sband == NULL)
6134 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006135 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6136 nla_len(tx_rates), nl80211_txattr_policy);
6137 if (tb[NL80211_TXRATE_LEGACY]) {
6138 mask.control[band].legacy = rateset_to_mask(
6139 sband,
6140 nla_data(tb[NL80211_TXRATE_LEGACY]),
6141 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306142 if ((mask.control[band].legacy == 0) &&
6143 nla_len(tb[NL80211_TXRATE_LEGACY]))
6144 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006145 }
6146 if (tb[NL80211_TXRATE_MCS]) {
6147 if (!ht_rateset_to_mask(
6148 sband,
6149 nla_data(tb[NL80211_TXRATE_MCS]),
6150 nla_len(tb[NL80211_TXRATE_MCS]),
6151 mask.control[band].mcs))
6152 return -EINVAL;
6153 }
6154
6155 if (mask.control[band].legacy == 0) {
6156 /* don't allow empty legacy rates if HT
6157 * is not even supported. */
6158 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6159 return -EINVAL;
6160
6161 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6162 if (mask.control[band].mcs[i])
6163 break;
6164
6165 /* legacy and mcs rates may not be both empty */
6166 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006167 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006168 }
6169 }
6170
Hila Gonene35e4d22012-06-27 17:19:42 +03006171 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006172}
6173
Johannes Berg2e161f782010-08-12 15:38:38 +02006174static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006175{
Johannes Berg4c476992010-10-04 21:36:35 +02006176 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006177 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f782010-08-12 15:38:38 +02006178 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006179
6180 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6181 return -EINVAL;
6182
Johannes Berg2e161f782010-08-12 15:38:38 +02006183 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6184 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006185
Johannes Berg71bbc992012-06-15 15:30:18 +02006186 switch (wdev->iftype) {
6187 case NL80211_IFTYPE_STATION:
6188 case NL80211_IFTYPE_ADHOC:
6189 case NL80211_IFTYPE_P2P_CLIENT:
6190 case NL80211_IFTYPE_AP:
6191 case NL80211_IFTYPE_AP_VLAN:
6192 case NL80211_IFTYPE_MESH_POINT:
6193 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006194 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006195 break;
6196 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006197 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006198 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006199
6200 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006201 if (!rdev->ops->mgmt_tx)
6202 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006203
Eric W. Biederman15e47302012-09-07 20:12:54 +00006204 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006205 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6206 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006207}
6208
Johannes Berg2e161f782010-08-12 15:38:38 +02006209static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006210{
Johannes Berg4c476992010-10-04 21:36:35 +02006211 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006212 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02006213 struct ieee80211_channel *chan;
6214 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02006215 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02006216 u32 freq;
6217 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006218 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006219 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006220 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006221 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006222 bool offchan, no_cck, dont_wait_for_ack;
6223
6224 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006225
6226 if (!info->attrs[NL80211_ATTR_FRAME] ||
6227 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6228 return -EINVAL;
6229
Johannes Berg4c476992010-10-04 21:36:35 +02006230 if (!rdev->ops->mgmt_tx)
6231 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006232
Johannes Berg71bbc992012-06-15 15:30:18 +02006233 switch (wdev->iftype) {
6234 case NL80211_IFTYPE_STATION:
6235 case NL80211_IFTYPE_ADHOC:
6236 case NL80211_IFTYPE_P2P_CLIENT:
6237 case NL80211_IFTYPE_AP:
6238 case NL80211_IFTYPE_AP_VLAN:
6239 case NL80211_IFTYPE_MESH_POINT:
6240 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006241 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006242 break;
6243 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006244 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006245 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006246
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006247 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006248 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006249 return -EINVAL;
6250 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006251
6252 /*
6253 * We should wait on the channel for at least a minimum amount
6254 * of time (10ms) but no longer than the driver supports.
6255 */
6256 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6257 wait > rdev->wiphy.max_remain_on_channel_duration)
6258 return -EINVAL;
6259
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006260 }
6261
Jouni Malinen026331c2010-02-15 12:53:10 +02006262 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c65982012-05-10 21:27:18 +02006263 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02006264 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02006265 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02006266 }
6267
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006268 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6269
Johannes Berg7c4ef712011-11-18 15:33:48 +01006270 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6271 return -EINVAL;
6272
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306273 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6274
Jouni Malinen026331c2010-02-15 12:53:10 +02006275 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6276 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02006277 if (chan == NULL)
6278 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006279
Johannes Berge247bd902011-11-04 11:18:21 +01006280 if (!dont_wait_for_ack) {
6281 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6282 if (!msg)
6283 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006284
Eric W. Biederman15e47302012-09-07 20:12:54 +00006285 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006286 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006287
Johannes Berge247bd902011-11-04 11:18:21 +01006288 if (IS_ERR(hdr)) {
6289 err = PTR_ERR(hdr);
6290 goto free_msg;
6291 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006292 }
Johannes Berge247bd902011-11-04 11:18:21 +01006293
Johannes Berg71bbc992012-06-15 15:30:18 +02006294 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chan, offchan, channel_type,
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006295 channel_type_valid, wait,
Johannes Berg2e161f782010-08-12 15:38:38 +02006296 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6297 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006298 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006299 if (err)
6300 goto free_msg;
6301
Johannes Berge247bd902011-11-04 11:18:21 +01006302 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006303 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6304 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006305
Johannes Berge247bd902011-11-04 11:18:21 +01006306 genlmsg_end(msg, hdr);
6307 return genlmsg_reply(msg, info);
6308 }
6309
6310 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006311
6312 nla_put_failure:
6313 err = -ENOBUFS;
6314 free_msg:
6315 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006316 return err;
6317}
6318
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006319static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6320{
6321 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006322 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006323 u64 cookie;
6324
6325 if (!info->attrs[NL80211_ATTR_COOKIE])
6326 return -EINVAL;
6327
6328 if (!rdev->ops->mgmt_tx_cancel_wait)
6329 return -EOPNOTSUPP;
6330
Johannes Berg71bbc992012-06-15 15:30:18 +02006331 switch (wdev->iftype) {
6332 case NL80211_IFTYPE_STATION:
6333 case NL80211_IFTYPE_ADHOC:
6334 case NL80211_IFTYPE_P2P_CLIENT:
6335 case NL80211_IFTYPE_AP:
6336 case NL80211_IFTYPE_AP_VLAN:
6337 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006338 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006339 break;
6340 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006341 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006342 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006343
6344 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6345
Hila Gonene35e4d22012-06-27 17:19:42 +03006346 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006347}
6348
Kalle Valoffb9eb32010-02-17 17:58:10 +02006349static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6350{
Johannes Berg4c476992010-10-04 21:36:35 +02006351 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006352 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006353 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006354 u8 ps_state;
6355 bool state;
6356 int err;
6357
Johannes Berg4c476992010-10-04 21:36:35 +02006358 if (!info->attrs[NL80211_ATTR_PS_STATE])
6359 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006360
6361 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6362
Johannes Berg4c476992010-10-04 21:36:35 +02006363 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6364 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006365
6366 wdev = dev->ieee80211_ptr;
6367
Johannes Berg4c476992010-10-04 21:36:35 +02006368 if (!rdev->ops->set_power_mgmt)
6369 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006370
6371 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6372
6373 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006374 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006375
Hila Gonene35e4d22012-06-27 17:19:42 +03006376 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006377 if (!err)
6378 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006379 return err;
6380}
6381
6382static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6383{
Johannes Berg4c476992010-10-04 21:36:35 +02006384 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006385 enum nl80211_ps_state ps_state;
6386 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006387 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006388 struct sk_buff *msg;
6389 void *hdr;
6390 int err;
6391
Kalle Valoffb9eb32010-02-17 17:58:10 +02006392 wdev = dev->ieee80211_ptr;
6393
Johannes Berg4c476992010-10-04 21:36:35 +02006394 if (!rdev->ops->set_power_mgmt)
6395 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006396
6397 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006398 if (!msg)
6399 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006400
Eric W. Biederman15e47302012-09-07 20:12:54 +00006401 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006402 NL80211_CMD_GET_POWER_SAVE);
6403 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006404 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006405 goto free_msg;
6406 }
6407
6408 if (wdev->ps)
6409 ps_state = NL80211_PS_ENABLED;
6410 else
6411 ps_state = NL80211_PS_DISABLED;
6412
David S. Miller9360ffd2012-03-29 04:41:26 -04006413 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6414 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006415
6416 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006417 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006418
Johannes Berg4c476992010-10-04 21:36:35 +02006419 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006420 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006421 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006422 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006423 return err;
6424}
6425
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006426static struct nla_policy
6427nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6428 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6429 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6430 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006431 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6432 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6433 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006434};
6435
Thomas Pedersen84f10702012-07-12 16:17:33 -07006436static int nl80211_set_cqm_txe(struct genl_info *info,
6437 u32 rate, u32 pkts, u32 intvl)
6438{
6439 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6440 struct wireless_dev *wdev;
6441 struct net_device *dev = info->user_ptr[1];
6442
6443 if ((rate < 0 || rate > 100) ||
6444 (intvl < 0 || intvl > NL80211_CQM_TXE_MAX_INTVL))
6445 return -EINVAL;
6446
6447 wdev = dev->ieee80211_ptr;
6448
6449 if (!rdev->ops->set_cqm_txe_config)
6450 return -EOPNOTSUPP;
6451
6452 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6453 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6454 return -EOPNOTSUPP;
6455
Hila Gonene35e4d22012-06-27 17:19:42 +03006456 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006457}
6458
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006459static int nl80211_set_cqm_rssi(struct genl_info *info,
6460 s32 threshold, u32 hysteresis)
6461{
Johannes Berg4c476992010-10-04 21:36:35 +02006462 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006463 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006464 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006465
6466 if (threshold > 0)
6467 return -EINVAL;
6468
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006469 wdev = dev->ieee80211_ptr;
6470
Johannes Berg4c476992010-10-04 21:36:35 +02006471 if (!rdev->ops->set_cqm_rssi_config)
6472 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006473
Johannes Berg074ac8d2010-09-16 14:58:22 +02006474 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006475 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6476 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006477
Hila Gonene35e4d22012-06-27 17:19:42 +03006478 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006479}
6480
6481static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6482{
6483 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6484 struct nlattr *cqm;
6485 int err;
6486
6487 cqm = info->attrs[NL80211_ATTR_CQM];
6488 if (!cqm) {
6489 err = -EINVAL;
6490 goto out;
6491 }
6492
6493 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6494 nl80211_attr_cqm_policy);
6495 if (err)
6496 goto out;
6497
6498 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6499 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6500 s32 threshold;
6501 u32 hysteresis;
6502 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6503 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6504 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006505 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6506 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6507 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6508 u32 rate, pkts, intvl;
6509 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6510 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6511 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6512 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006513 } else
6514 err = -EINVAL;
6515
6516out:
6517 return err;
6518}
6519
Johannes Berg29cbe682010-12-03 09:20:44 +01006520static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6521{
6522 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6523 struct net_device *dev = info->user_ptr[1];
6524 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006525 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006526 int err;
6527
6528 /* start with default */
6529 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006530 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006531
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006532 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006533 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006534 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006535 if (err)
6536 return err;
6537 }
6538
6539 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6540 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6541 return -EINVAL;
6542
Javier Cardonac80d5452010-12-16 17:37:49 -08006543 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6544 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6545
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006546 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6547 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6548 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6549 return -EINVAL;
6550
Javier Cardonac80d5452010-12-16 17:37:49 -08006551 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6552 /* parse additional setup parameters if given */
6553 err = nl80211_parse_mesh_setup(info, &setup);
6554 if (err)
6555 return err;
6556 }
6557
Johannes Bergcc1d2802012-05-16 23:50:20 +02006558 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6559 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6560
6561 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6562 !nl80211_valid_channel_type(info, &channel_type))
6563 return -EINVAL;
6564
6565 setup.channel = rdev_freq_to_chan(rdev,
6566 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6567 channel_type);
6568 if (!setup.channel)
6569 return -EINVAL;
6570 setup.channel_type = channel_type;
6571 } else {
6572 /* cfg80211_join_mesh() will sort it out */
6573 setup.channel = NULL;
6574 }
6575
Javier Cardonac80d5452010-12-16 17:37:49 -08006576 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006577}
6578
6579static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6580{
6581 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6582 struct net_device *dev = info->user_ptr[1];
6583
6584 return cfg80211_leave_mesh(rdev, dev);
6585}
6586
Johannes Bergdfb89c52012-06-27 09:23:48 +02006587#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006588static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6589{
6590 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6591 struct sk_buff *msg;
6592 void *hdr;
6593
6594 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6595 return -EOPNOTSUPP;
6596
6597 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6598 if (!msg)
6599 return -ENOMEM;
6600
Eric W. Biederman15e47302012-09-07 20:12:54 +00006601 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006602 NL80211_CMD_GET_WOWLAN);
6603 if (!hdr)
6604 goto nla_put_failure;
6605
6606 if (rdev->wowlan) {
6607 struct nlattr *nl_wowlan;
6608
6609 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6610 if (!nl_wowlan)
6611 goto nla_put_failure;
6612
David S. Miller9360ffd2012-03-29 04:41:26 -04006613 if ((rdev->wowlan->any &&
6614 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6615 (rdev->wowlan->disconnect &&
6616 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6617 (rdev->wowlan->magic_pkt &&
6618 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6619 (rdev->wowlan->gtk_rekey_failure &&
6620 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6621 (rdev->wowlan->eap_identity_req &&
6622 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6623 (rdev->wowlan->four_way_handshake &&
6624 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6625 (rdev->wowlan->rfkill_release &&
6626 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6627 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006628 if (rdev->wowlan->n_patterns) {
6629 struct nlattr *nl_pats, *nl_pat;
6630 int i, pat_len;
6631
6632 nl_pats = nla_nest_start(msg,
6633 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6634 if (!nl_pats)
6635 goto nla_put_failure;
6636
6637 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6638 nl_pat = nla_nest_start(msg, i + 1);
6639 if (!nl_pat)
6640 goto nla_put_failure;
6641 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006642 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6643 DIV_ROUND_UP(pat_len, 8),
6644 rdev->wowlan->patterns[i].mask) ||
6645 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6646 pat_len,
6647 rdev->wowlan->patterns[i].pattern))
6648 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006649 nla_nest_end(msg, nl_pat);
6650 }
6651 nla_nest_end(msg, nl_pats);
6652 }
6653
6654 nla_nest_end(msg, nl_wowlan);
6655 }
6656
6657 genlmsg_end(msg, hdr);
6658 return genlmsg_reply(msg, info);
6659
6660nla_put_failure:
6661 nlmsg_free(msg);
6662 return -ENOBUFS;
6663}
6664
6665static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6666{
6667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6668 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006669 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006670 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006671 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6672 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006673 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006674
6675 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6676 return -EOPNOTSUPP;
6677
Johannes Bergae33bd82012-07-12 16:25:02 +02006678 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6679 cfg80211_rdev_free_wowlan(rdev);
6680 rdev->wowlan = NULL;
6681 goto set_wakeup;
6682 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006683
6684 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6685 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6686 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6687 nl80211_wowlan_policy);
6688 if (err)
6689 return err;
6690
6691 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6692 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6693 return -EINVAL;
6694 new_triggers.any = true;
6695 }
6696
6697 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6698 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6699 return -EINVAL;
6700 new_triggers.disconnect = true;
6701 }
6702
6703 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6704 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6705 return -EINVAL;
6706 new_triggers.magic_pkt = true;
6707 }
6708
Johannes Berg77dbbb12011-07-13 10:48:55 +02006709 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6710 return -EINVAL;
6711
6712 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6713 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6714 return -EINVAL;
6715 new_triggers.gtk_rekey_failure = true;
6716 }
6717
6718 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6719 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6720 return -EINVAL;
6721 new_triggers.eap_identity_req = true;
6722 }
6723
6724 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6725 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6726 return -EINVAL;
6727 new_triggers.four_way_handshake = true;
6728 }
6729
6730 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6731 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6732 return -EINVAL;
6733 new_triggers.rfkill_release = true;
6734 }
6735
Johannes Bergff1b6e62011-05-04 15:37:28 +02006736 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6737 struct nlattr *pat;
6738 int n_patterns = 0;
6739 int rem, pat_len, mask_len;
6740 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6741
6742 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6743 rem)
6744 n_patterns++;
6745 if (n_patterns > wowlan->n_patterns)
6746 return -EINVAL;
6747
6748 new_triggers.patterns = kcalloc(n_patterns,
6749 sizeof(new_triggers.patterns[0]),
6750 GFP_KERNEL);
6751 if (!new_triggers.patterns)
6752 return -ENOMEM;
6753
6754 new_triggers.n_patterns = n_patterns;
6755 i = 0;
6756
6757 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6758 rem) {
6759 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6760 nla_data(pat), nla_len(pat), NULL);
6761 err = -EINVAL;
6762 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6763 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6764 goto error;
6765 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6766 mask_len = DIV_ROUND_UP(pat_len, 8);
6767 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6768 mask_len)
6769 goto error;
6770 if (pat_len > wowlan->pattern_max_len ||
6771 pat_len < wowlan->pattern_min_len)
6772 goto error;
6773
6774 new_triggers.patterns[i].mask =
6775 kmalloc(mask_len + pat_len, GFP_KERNEL);
6776 if (!new_triggers.patterns[i].mask) {
6777 err = -ENOMEM;
6778 goto error;
6779 }
6780 new_triggers.patterns[i].pattern =
6781 new_triggers.patterns[i].mask + mask_len;
6782 memcpy(new_triggers.patterns[i].mask,
6783 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6784 mask_len);
6785 new_triggers.patterns[i].pattern_len = pat_len;
6786 memcpy(new_triggers.patterns[i].pattern,
6787 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6788 pat_len);
6789 i++;
6790 }
6791 }
6792
Johannes Bergae33bd82012-07-12 16:25:02 +02006793 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6794 if (!ntrig) {
6795 err = -ENOMEM;
6796 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006797 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006798 cfg80211_rdev_free_wowlan(rdev);
6799 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006800
Johannes Bergae33bd82012-07-12 16:25:02 +02006801 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006802 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03006803 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02006804
Johannes Bergff1b6e62011-05-04 15:37:28 +02006805 return 0;
6806 error:
6807 for (i = 0; i < new_triggers.n_patterns; i++)
6808 kfree(new_triggers.patterns[i].mask);
6809 kfree(new_triggers.patterns);
6810 return err;
6811}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006812#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006813
Johannes Berge5497d72011-07-05 16:35:40 +02006814static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6815{
6816 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6817 struct net_device *dev = info->user_ptr[1];
6818 struct wireless_dev *wdev = dev->ieee80211_ptr;
6819 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6820 struct cfg80211_gtk_rekey_data rekey_data;
6821 int err;
6822
6823 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6824 return -EINVAL;
6825
6826 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6827 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6828 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6829 nl80211_rekey_policy);
6830 if (err)
6831 return err;
6832
6833 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6834 return -ERANGE;
6835 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6836 return -ERANGE;
6837 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6838 return -ERANGE;
6839
6840 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6841 NL80211_KEK_LEN);
6842 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6843 NL80211_KCK_LEN);
6844 memcpy(rekey_data.replay_ctr,
6845 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6846 NL80211_REPLAY_CTR_LEN);
6847
6848 wdev_lock(wdev);
6849 if (!wdev->current_bss) {
6850 err = -ENOTCONN;
6851 goto out;
6852 }
6853
6854 if (!rdev->ops->set_rekey_data) {
6855 err = -EOPNOTSUPP;
6856 goto out;
6857 }
6858
Hila Gonene35e4d22012-06-27 17:19:42 +03006859 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02006860 out:
6861 wdev_unlock(wdev);
6862 return err;
6863}
6864
Johannes Berg28946da2011-11-04 11:18:12 +01006865static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6866 struct genl_info *info)
6867{
6868 struct net_device *dev = info->user_ptr[1];
6869 struct wireless_dev *wdev = dev->ieee80211_ptr;
6870
6871 if (wdev->iftype != NL80211_IFTYPE_AP &&
6872 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6873 return -EINVAL;
6874
Eric W. Biederman15e47302012-09-07 20:12:54 +00006875 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01006876 return -EBUSY;
6877
Eric W. Biederman15e47302012-09-07 20:12:54 +00006878 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01006879 return 0;
6880}
6881
Johannes Berg7f6cf312011-11-04 11:18:15 +01006882static int nl80211_probe_client(struct sk_buff *skb,
6883 struct genl_info *info)
6884{
6885 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6886 struct net_device *dev = info->user_ptr[1];
6887 struct wireless_dev *wdev = dev->ieee80211_ptr;
6888 struct sk_buff *msg;
6889 void *hdr;
6890 const u8 *addr;
6891 u64 cookie;
6892 int err;
6893
6894 if (wdev->iftype != NL80211_IFTYPE_AP &&
6895 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6896 return -EOPNOTSUPP;
6897
6898 if (!info->attrs[NL80211_ATTR_MAC])
6899 return -EINVAL;
6900
6901 if (!rdev->ops->probe_client)
6902 return -EOPNOTSUPP;
6903
6904 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6905 if (!msg)
6906 return -ENOMEM;
6907
Eric W. Biederman15e47302012-09-07 20:12:54 +00006908 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01006909 NL80211_CMD_PROBE_CLIENT);
6910
6911 if (IS_ERR(hdr)) {
6912 err = PTR_ERR(hdr);
6913 goto free_msg;
6914 }
6915
6916 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6917
Hila Gonene35e4d22012-06-27 17:19:42 +03006918 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01006919 if (err)
6920 goto free_msg;
6921
David S. Miller9360ffd2012-03-29 04:41:26 -04006922 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6923 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006924
6925 genlmsg_end(msg, hdr);
6926
6927 return genlmsg_reply(msg, info);
6928
6929 nla_put_failure:
6930 err = -ENOBUFS;
6931 free_msg:
6932 nlmsg_free(msg);
6933 return err;
6934}
6935
Johannes Berg5e760232011-11-04 11:18:17 +01006936static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6937{
6938 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07006939 struct cfg80211_beacon_registration *reg, *nreg;
6940 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01006941
6942 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6943 return -EOPNOTSUPP;
6944
Ben Greear37c73b52012-10-26 14:49:25 -07006945 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
6946 if (!nreg)
6947 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01006948
Ben Greear37c73b52012-10-26 14:49:25 -07006949 /* First, check if already registered. */
6950 spin_lock_bh(&rdev->beacon_registrations_lock);
6951 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
6952 if (reg->nlportid == info->snd_portid) {
6953 rv = -EALREADY;
6954 goto out_err;
6955 }
6956 }
6957 /* Add it to the list */
6958 nreg->nlportid = info->snd_portid;
6959 list_add(&nreg->list, &rdev->beacon_registrations);
6960
6961 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01006962
6963 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07006964out_err:
6965 spin_unlock_bh(&rdev->beacon_registrations_lock);
6966 kfree(nreg);
6967 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01006968}
6969
Johannes Berg98104fde2012-06-16 00:19:54 +02006970static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
6971{
6972 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6973 struct wireless_dev *wdev = info->user_ptr[1];
6974 int err;
6975
6976 if (!rdev->ops->start_p2p_device)
6977 return -EOPNOTSUPP;
6978
6979 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
6980 return -EOPNOTSUPP;
6981
6982 if (wdev->p2p_started)
6983 return 0;
6984
6985 mutex_lock(&rdev->devlist_mtx);
6986 err = cfg80211_can_add_interface(rdev, wdev->iftype);
6987 mutex_unlock(&rdev->devlist_mtx);
6988 if (err)
6989 return err;
6990
Johannes Bergeeb126e2012-10-23 15:16:50 +02006991 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02006992 if (err)
6993 return err;
6994
6995 wdev->p2p_started = true;
6996 mutex_lock(&rdev->devlist_mtx);
6997 rdev->opencount++;
6998 mutex_unlock(&rdev->devlist_mtx);
6999
7000 return 0;
7001}
7002
7003static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7004{
7005 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7006 struct wireless_dev *wdev = info->user_ptr[1];
7007
7008 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7009 return -EOPNOTSUPP;
7010
7011 if (!rdev->ops->stop_p2p_device)
7012 return -EOPNOTSUPP;
7013
7014 if (!wdev->p2p_started)
7015 return 0;
7016
Johannes Bergeeb126e2012-10-23 15:16:50 +02007017 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007018 wdev->p2p_started = false;
7019
7020 mutex_lock(&rdev->devlist_mtx);
7021 rdev->opencount--;
7022 mutex_unlock(&rdev->devlist_mtx);
7023
7024 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7025 rdev->scan_req->aborted = true;
7026 ___cfg80211_scan_done(rdev, true);
7027 }
7028
7029 return 0;
7030}
7031
Johannes Berg4c476992010-10-04 21:36:35 +02007032#define NL80211_FLAG_NEED_WIPHY 0x01
7033#define NL80211_FLAG_NEED_NETDEV 0x02
7034#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007035#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7036#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7037 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007038#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007039/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007040#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7041 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007042
7043static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7044 struct genl_info *info)
7045{
7046 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007047 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007048 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007049 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7050
7051 if (rtnl)
7052 rtnl_lock();
7053
7054 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007055 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007056 if (IS_ERR(rdev)) {
7057 if (rtnl)
7058 rtnl_unlock();
7059 return PTR_ERR(rdev);
7060 }
7061 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007062 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7063 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007064 mutex_lock(&cfg80211_mutex);
7065 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7066 info->attrs);
7067 if (IS_ERR(wdev)) {
7068 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007069 if (rtnl)
7070 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007071 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007072 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007073
Johannes Berg89a54e42012-06-15 14:33:17 +02007074 dev = wdev->netdev;
7075 rdev = wiphy_to_dev(wdev->wiphy);
7076
Johannes Berg1bf614e2012-06-15 15:23:36 +02007077 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7078 if (!dev) {
7079 mutex_unlock(&cfg80211_mutex);
7080 if (rtnl)
7081 rtnl_unlock();
7082 return -EINVAL;
7083 }
7084
7085 info->user_ptr[1] = dev;
7086 } else {
7087 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007088 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007089
Johannes Berg1bf614e2012-06-15 15:23:36 +02007090 if (dev) {
7091 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7092 !netif_running(dev)) {
7093 mutex_unlock(&cfg80211_mutex);
7094 if (rtnl)
7095 rtnl_unlock();
7096 return -ENETDOWN;
7097 }
7098
7099 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007100 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7101 if (!wdev->p2p_started) {
7102 mutex_unlock(&cfg80211_mutex);
7103 if (rtnl)
7104 rtnl_unlock();
7105 return -ENETDOWN;
7106 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007107 }
7108
Johannes Berg89a54e42012-06-15 14:33:17 +02007109 cfg80211_lock_rdev(rdev);
7110
7111 mutex_unlock(&cfg80211_mutex);
7112
Johannes Berg4c476992010-10-04 21:36:35 +02007113 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007114 }
7115
7116 return 0;
7117}
7118
7119static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7120 struct genl_info *info)
7121{
7122 if (info->user_ptr[0])
7123 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007124 if (info->user_ptr[1]) {
7125 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7126 struct wireless_dev *wdev = info->user_ptr[1];
7127
7128 if (wdev->netdev)
7129 dev_put(wdev->netdev);
7130 } else {
7131 dev_put(info->user_ptr[1]);
7132 }
7133 }
Johannes Berg4c476992010-10-04 21:36:35 +02007134 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7135 rtnl_unlock();
7136}
7137
Johannes Berg55682962007-09-20 13:09:35 -04007138static struct genl_ops nl80211_ops[] = {
7139 {
7140 .cmd = NL80211_CMD_GET_WIPHY,
7141 .doit = nl80211_get_wiphy,
7142 .dumpit = nl80211_dump_wiphy,
7143 .policy = nl80211_policy,
7144 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007145 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007146 },
7147 {
7148 .cmd = NL80211_CMD_SET_WIPHY,
7149 .doit = nl80211_set_wiphy,
7150 .policy = nl80211_policy,
7151 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007152 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007153 },
7154 {
7155 .cmd = NL80211_CMD_GET_INTERFACE,
7156 .doit = nl80211_get_interface,
7157 .dumpit = nl80211_dump_interface,
7158 .policy = nl80211_policy,
7159 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007160 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007161 },
7162 {
7163 .cmd = NL80211_CMD_SET_INTERFACE,
7164 .doit = nl80211_set_interface,
7165 .policy = nl80211_policy,
7166 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007167 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7168 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007169 },
7170 {
7171 .cmd = NL80211_CMD_NEW_INTERFACE,
7172 .doit = nl80211_new_interface,
7173 .policy = nl80211_policy,
7174 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007175 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7176 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007177 },
7178 {
7179 .cmd = NL80211_CMD_DEL_INTERFACE,
7180 .doit = nl80211_del_interface,
7181 .policy = nl80211_policy,
7182 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007183 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007184 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007185 },
Johannes Berg41ade002007-12-19 02:03:29 +01007186 {
7187 .cmd = NL80211_CMD_GET_KEY,
7188 .doit = nl80211_get_key,
7189 .policy = nl80211_policy,
7190 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007191 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007192 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007193 },
7194 {
7195 .cmd = NL80211_CMD_SET_KEY,
7196 .doit = nl80211_set_key,
7197 .policy = nl80211_policy,
7198 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007199 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007200 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007201 },
7202 {
7203 .cmd = NL80211_CMD_NEW_KEY,
7204 .doit = nl80211_new_key,
7205 .policy = nl80211_policy,
7206 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007207 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007208 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007209 },
7210 {
7211 .cmd = NL80211_CMD_DEL_KEY,
7212 .doit = nl80211_del_key,
7213 .policy = nl80211_policy,
7214 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007215 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007216 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007217 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007218 {
7219 .cmd = NL80211_CMD_SET_BEACON,
7220 .policy = nl80211_policy,
7221 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007222 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007223 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007224 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007225 },
7226 {
Johannes Berg88600202012-02-13 15:17:18 +01007227 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007228 .policy = nl80211_policy,
7229 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007230 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007231 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007232 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007233 },
7234 {
Johannes Berg88600202012-02-13 15:17:18 +01007235 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007236 .policy = nl80211_policy,
7237 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007238 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007239 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007240 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007241 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007242 {
7243 .cmd = NL80211_CMD_GET_STATION,
7244 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007245 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007246 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007247 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7248 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007249 },
7250 {
7251 .cmd = NL80211_CMD_SET_STATION,
7252 .doit = nl80211_set_station,
7253 .policy = nl80211_policy,
7254 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007255 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007256 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007257 },
7258 {
7259 .cmd = NL80211_CMD_NEW_STATION,
7260 .doit = nl80211_new_station,
7261 .policy = nl80211_policy,
7262 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007263 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007264 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007265 },
7266 {
7267 .cmd = NL80211_CMD_DEL_STATION,
7268 .doit = nl80211_del_station,
7269 .policy = nl80211_policy,
7270 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007271 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007272 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007273 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007274 {
7275 .cmd = NL80211_CMD_GET_MPATH,
7276 .doit = nl80211_get_mpath,
7277 .dumpit = nl80211_dump_mpath,
7278 .policy = nl80211_policy,
7279 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007280 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007281 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007282 },
7283 {
7284 .cmd = NL80211_CMD_SET_MPATH,
7285 .doit = nl80211_set_mpath,
7286 .policy = nl80211_policy,
7287 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007288 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007289 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007290 },
7291 {
7292 .cmd = NL80211_CMD_NEW_MPATH,
7293 .doit = nl80211_new_mpath,
7294 .policy = nl80211_policy,
7295 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007296 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007297 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007298 },
7299 {
7300 .cmd = NL80211_CMD_DEL_MPATH,
7301 .doit = nl80211_del_mpath,
7302 .policy = nl80211_policy,
7303 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007304 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007305 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007306 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007307 {
7308 .cmd = NL80211_CMD_SET_BSS,
7309 .doit = nl80211_set_bss,
7310 .policy = nl80211_policy,
7311 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007312 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007313 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007314 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007315 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007316 .cmd = NL80211_CMD_GET_REG,
7317 .doit = nl80211_get_reg,
7318 .policy = nl80211_policy,
7319 /* can be retrieved by unprivileged users */
7320 },
7321 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007322 .cmd = NL80211_CMD_SET_REG,
7323 .doit = nl80211_set_reg,
7324 .policy = nl80211_policy,
7325 .flags = GENL_ADMIN_PERM,
7326 },
7327 {
7328 .cmd = NL80211_CMD_REQ_SET_REG,
7329 .doit = nl80211_req_set_reg,
7330 .policy = nl80211_policy,
7331 .flags = GENL_ADMIN_PERM,
7332 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007333 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007334 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7335 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007336 .policy = nl80211_policy,
7337 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007338 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007339 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007340 },
7341 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007342 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7343 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007344 .policy = nl80211_policy,
7345 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007346 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007347 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007348 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007349 {
Johannes Berg2a519312009-02-10 21:25:55 +01007350 .cmd = NL80211_CMD_TRIGGER_SCAN,
7351 .doit = nl80211_trigger_scan,
7352 .policy = nl80211_policy,
7353 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007354 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007355 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007356 },
7357 {
7358 .cmd = NL80211_CMD_GET_SCAN,
7359 .policy = nl80211_policy,
7360 .dumpit = nl80211_dump_scan,
7361 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007362 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007363 .cmd = NL80211_CMD_START_SCHED_SCAN,
7364 .doit = nl80211_start_sched_scan,
7365 .policy = nl80211_policy,
7366 .flags = GENL_ADMIN_PERM,
7367 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7368 NL80211_FLAG_NEED_RTNL,
7369 },
7370 {
7371 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7372 .doit = nl80211_stop_sched_scan,
7373 .policy = nl80211_policy,
7374 .flags = GENL_ADMIN_PERM,
7375 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7376 NL80211_FLAG_NEED_RTNL,
7377 },
7378 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007379 .cmd = NL80211_CMD_AUTHENTICATE,
7380 .doit = nl80211_authenticate,
7381 .policy = nl80211_policy,
7382 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007383 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007384 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007385 },
7386 {
7387 .cmd = NL80211_CMD_ASSOCIATE,
7388 .doit = nl80211_associate,
7389 .policy = nl80211_policy,
7390 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007391 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007392 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007393 },
7394 {
7395 .cmd = NL80211_CMD_DEAUTHENTICATE,
7396 .doit = nl80211_deauthenticate,
7397 .policy = nl80211_policy,
7398 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007399 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007400 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007401 },
7402 {
7403 .cmd = NL80211_CMD_DISASSOCIATE,
7404 .doit = nl80211_disassociate,
7405 .policy = nl80211_policy,
7406 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007407 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007408 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007409 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007410 {
7411 .cmd = NL80211_CMD_JOIN_IBSS,
7412 .doit = nl80211_join_ibss,
7413 .policy = nl80211_policy,
7414 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007415 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007416 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007417 },
7418 {
7419 .cmd = NL80211_CMD_LEAVE_IBSS,
7420 .doit = nl80211_leave_ibss,
7421 .policy = nl80211_policy,
7422 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007423 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007424 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007425 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007426#ifdef CONFIG_NL80211_TESTMODE
7427 {
7428 .cmd = NL80211_CMD_TESTMODE,
7429 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007430 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007431 .policy = nl80211_policy,
7432 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007433 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7434 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007435 },
7436#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007437 {
7438 .cmd = NL80211_CMD_CONNECT,
7439 .doit = nl80211_connect,
7440 .policy = nl80211_policy,
7441 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007442 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007443 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007444 },
7445 {
7446 .cmd = NL80211_CMD_DISCONNECT,
7447 .doit = nl80211_disconnect,
7448 .policy = nl80211_policy,
7449 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007450 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007451 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007452 },
Johannes Berg463d0182009-07-14 00:33:35 +02007453 {
7454 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7455 .doit = nl80211_wiphy_netns,
7456 .policy = nl80211_policy,
7457 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007458 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7459 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007460 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007461 {
7462 .cmd = NL80211_CMD_GET_SURVEY,
7463 .policy = nl80211_policy,
7464 .dumpit = nl80211_dump_survey,
7465 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007466 {
7467 .cmd = NL80211_CMD_SET_PMKSA,
7468 .doit = nl80211_setdel_pmksa,
7469 .policy = nl80211_policy,
7470 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007471 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007472 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007473 },
7474 {
7475 .cmd = NL80211_CMD_DEL_PMKSA,
7476 .doit = nl80211_setdel_pmksa,
7477 .policy = nl80211_policy,
7478 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007479 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007480 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007481 },
7482 {
7483 .cmd = NL80211_CMD_FLUSH_PMKSA,
7484 .doit = nl80211_flush_pmksa,
7485 .policy = nl80211_policy,
7486 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007487 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007488 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007489 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007490 {
7491 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7492 .doit = nl80211_remain_on_channel,
7493 .policy = nl80211_policy,
7494 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007495 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007496 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007497 },
7498 {
7499 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7500 .doit = nl80211_cancel_remain_on_channel,
7501 .policy = nl80211_policy,
7502 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007503 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007504 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007505 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007506 {
7507 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7508 .doit = nl80211_set_tx_bitrate_mask,
7509 .policy = nl80211_policy,
7510 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007511 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7512 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007513 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007514 {
Johannes Berg2e161f782010-08-12 15:38:38 +02007515 .cmd = NL80211_CMD_REGISTER_FRAME,
7516 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007517 .policy = nl80211_policy,
7518 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007519 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007520 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007521 },
7522 {
Johannes Berg2e161f782010-08-12 15:38:38 +02007523 .cmd = NL80211_CMD_FRAME,
7524 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007525 .policy = nl80211_policy,
7526 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007527 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007528 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007529 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007530 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007531 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7532 .doit = nl80211_tx_mgmt_cancel_wait,
7533 .policy = nl80211_policy,
7534 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007535 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007536 NL80211_FLAG_NEED_RTNL,
7537 },
7538 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007539 .cmd = NL80211_CMD_SET_POWER_SAVE,
7540 .doit = nl80211_set_power_save,
7541 .policy = nl80211_policy,
7542 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007543 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7544 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007545 },
7546 {
7547 .cmd = NL80211_CMD_GET_POWER_SAVE,
7548 .doit = nl80211_get_power_save,
7549 .policy = nl80211_policy,
7550 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007551 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7552 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007553 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007554 {
7555 .cmd = NL80211_CMD_SET_CQM,
7556 .doit = nl80211_set_cqm,
7557 .policy = nl80211_policy,
7558 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007559 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7560 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007561 },
Johannes Bergf444de02010-05-05 15:25:02 +02007562 {
7563 .cmd = NL80211_CMD_SET_CHANNEL,
7564 .doit = nl80211_set_channel,
7565 .policy = nl80211_policy,
7566 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007567 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7568 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007569 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007570 {
7571 .cmd = NL80211_CMD_SET_WDS_PEER,
7572 .doit = nl80211_set_wds_peer,
7573 .policy = nl80211_policy,
7574 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007575 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7576 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007577 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007578 {
7579 .cmd = NL80211_CMD_JOIN_MESH,
7580 .doit = nl80211_join_mesh,
7581 .policy = nl80211_policy,
7582 .flags = GENL_ADMIN_PERM,
7583 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7584 NL80211_FLAG_NEED_RTNL,
7585 },
7586 {
7587 .cmd = NL80211_CMD_LEAVE_MESH,
7588 .doit = nl80211_leave_mesh,
7589 .policy = nl80211_policy,
7590 .flags = GENL_ADMIN_PERM,
7591 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7592 NL80211_FLAG_NEED_RTNL,
7593 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007594#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007595 {
7596 .cmd = NL80211_CMD_GET_WOWLAN,
7597 .doit = nl80211_get_wowlan,
7598 .policy = nl80211_policy,
7599 /* can be retrieved by unprivileged users */
7600 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7601 NL80211_FLAG_NEED_RTNL,
7602 },
7603 {
7604 .cmd = NL80211_CMD_SET_WOWLAN,
7605 .doit = nl80211_set_wowlan,
7606 .policy = nl80211_policy,
7607 .flags = GENL_ADMIN_PERM,
7608 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7609 NL80211_FLAG_NEED_RTNL,
7610 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007611#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007612 {
7613 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7614 .doit = nl80211_set_rekey_data,
7615 .policy = nl80211_policy,
7616 .flags = GENL_ADMIN_PERM,
7617 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7618 NL80211_FLAG_NEED_RTNL,
7619 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007620 {
7621 .cmd = NL80211_CMD_TDLS_MGMT,
7622 .doit = nl80211_tdls_mgmt,
7623 .policy = nl80211_policy,
7624 .flags = GENL_ADMIN_PERM,
7625 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7626 NL80211_FLAG_NEED_RTNL,
7627 },
7628 {
7629 .cmd = NL80211_CMD_TDLS_OPER,
7630 .doit = nl80211_tdls_oper,
7631 .policy = nl80211_policy,
7632 .flags = GENL_ADMIN_PERM,
7633 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7634 NL80211_FLAG_NEED_RTNL,
7635 },
Johannes Berg28946da2011-11-04 11:18:12 +01007636 {
7637 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7638 .doit = nl80211_register_unexpected_frame,
7639 .policy = nl80211_policy,
7640 .flags = GENL_ADMIN_PERM,
7641 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7642 NL80211_FLAG_NEED_RTNL,
7643 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007644 {
7645 .cmd = NL80211_CMD_PROBE_CLIENT,
7646 .doit = nl80211_probe_client,
7647 .policy = nl80211_policy,
7648 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007649 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007650 NL80211_FLAG_NEED_RTNL,
7651 },
Johannes Berg5e760232011-11-04 11:18:17 +01007652 {
7653 .cmd = NL80211_CMD_REGISTER_BEACONS,
7654 .doit = nl80211_register_beacons,
7655 .policy = nl80211_policy,
7656 .flags = GENL_ADMIN_PERM,
7657 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7658 NL80211_FLAG_NEED_RTNL,
7659 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007660 {
7661 .cmd = NL80211_CMD_SET_NOACK_MAP,
7662 .doit = nl80211_set_noack_map,
7663 .policy = nl80211_policy,
7664 .flags = GENL_ADMIN_PERM,
7665 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7666 NL80211_FLAG_NEED_RTNL,
7667 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007668 {
7669 .cmd = NL80211_CMD_START_P2P_DEVICE,
7670 .doit = nl80211_start_p2p_device,
7671 .policy = nl80211_policy,
7672 .flags = GENL_ADMIN_PERM,
7673 .internal_flags = NL80211_FLAG_NEED_WDEV |
7674 NL80211_FLAG_NEED_RTNL,
7675 },
7676 {
7677 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7678 .doit = nl80211_stop_p2p_device,
7679 .policy = nl80211_policy,
7680 .flags = GENL_ADMIN_PERM,
7681 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7682 NL80211_FLAG_NEED_RTNL,
7683 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007684 {
7685 .cmd = NL80211_CMD_SET_MCAST_RATE,
7686 .doit = nl80211_set_mcast_rate,
7687 .policy = nl80211_policy,
7688 .flags = GENL_ADMIN_PERM,
7689 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7690 NL80211_FLAG_NEED_RTNL,
7691 },
Johannes Berg55682962007-09-20 13:09:35 -04007692};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007693
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007694static struct genl_multicast_group nl80211_mlme_mcgrp = {
7695 .name = "mlme",
7696};
Johannes Berg55682962007-09-20 13:09:35 -04007697
7698/* multicast groups */
7699static struct genl_multicast_group nl80211_config_mcgrp = {
7700 .name = "config",
7701};
Johannes Berg2a519312009-02-10 21:25:55 +01007702static struct genl_multicast_group nl80211_scan_mcgrp = {
7703 .name = "scan",
7704};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007705static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7706 .name = "regulatory",
7707};
Johannes Berg55682962007-09-20 13:09:35 -04007708
7709/* notification functions */
7710
7711void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7712{
7713 struct sk_buff *msg;
7714
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007715 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007716 if (!msg)
7717 return;
7718
7719 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7720 nlmsg_free(msg);
7721 return;
7722 }
7723
Johannes Berg463d0182009-07-14 00:33:35 +02007724 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7725 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007726}
7727
Johannes Berg362a4152009-05-24 16:43:15 +02007728static int nl80211_add_scan_req(struct sk_buff *msg,
7729 struct cfg80211_registered_device *rdev)
7730{
7731 struct cfg80211_scan_request *req = rdev->scan_req;
7732 struct nlattr *nest;
7733 int i;
7734
Johannes Berg667503d2009-07-07 03:56:11 +02007735 ASSERT_RDEV_LOCK(rdev);
7736
Johannes Berg362a4152009-05-24 16:43:15 +02007737 if (WARN_ON(!req))
7738 return 0;
7739
7740 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7741 if (!nest)
7742 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007743 for (i = 0; i < req->n_ssids; i++) {
7744 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7745 goto nla_put_failure;
7746 }
Johannes Berg362a4152009-05-24 16:43:15 +02007747 nla_nest_end(msg, nest);
7748
7749 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7750 if (!nest)
7751 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007752 for (i = 0; i < req->n_channels; i++) {
7753 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7754 goto nla_put_failure;
7755 }
Johannes Berg362a4152009-05-24 16:43:15 +02007756 nla_nest_end(msg, nest);
7757
David S. Miller9360ffd2012-03-29 04:41:26 -04007758 if (req->ie &&
7759 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7760 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007761
Sam Lefflered4737712012-10-11 21:03:31 -07007762 if (req->flags)
7763 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7764
Johannes Berg362a4152009-05-24 16:43:15 +02007765 return 0;
7766 nla_put_failure:
7767 return -ENOBUFS;
7768}
7769
Johannes Berga538e2d2009-06-16 19:56:42 +02007770static int nl80211_send_scan_msg(struct sk_buff *msg,
7771 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007772 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007773 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007774 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007775{
7776 void *hdr;
7777
Eric W. Biederman15e47302012-09-07 20:12:54 +00007778 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007779 if (!hdr)
7780 return -1;
7781
David S. Miller9360ffd2012-03-29 04:41:26 -04007782 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007783 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7784 wdev->netdev->ifindex)) ||
7785 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007786 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007787
Johannes Berg362a4152009-05-24 16:43:15 +02007788 /* ignore errors and send incomplete event anyway */
7789 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007790
7791 return genlmsg_end(msg, hdr);
7792
7793 nla_put_failure:
7794 genlmsg_cancel(msg, hdr);
7795 return -EMSGSIZE;
7796}
7797
Luciano Coelho807f8a82011-05-11 17:09:35 +03007798static int
7799nl80211_send_sched_scan_msg(struct sk_buff *msg,
7800 struct cfg80211_registered_device *rdev,
7801 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007802 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007803{
7804 void *hdr;
7805
Eric W. Biederman15e47302012-09-07 20:12:54 +00007806 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007807 if (!hdr)
7808 return -1;
7809
David S. Miller9360ffd2012-03-29 04:41:26 -04007810 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7811 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7812 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007813
7814 return genlmsg_end(msg, hdr);
7815
7816 nla_put_failure:
7817 genlmsg_cancel(msg, hdr);
7818 return -EMSGSIZE;
7819}
7820
Johannes Berga538e2d2009-06-16 19:56:42 +02007821void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007822 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007823{
7824 struct sk_buff *msg;
7825
Thomas Graf58050fc2012-06-28 03:57:45 +00007826 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007827 if (!msg)
7828 return;
7829
Johannes Bergfd014282012-06-18 19:17:03 +02007830 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007831 NL80211_CMD_TRIGGER_SCAN) < 0) {
7832 nlmsg_free(msg);
7833 return;
7834 }
7835
Johannes Berg463d0182009-07-14 00:33:35 +02007836 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7837 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007838}
7839
Johannes Berg2a519312009-02-10 21:25:55 +01007840void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007841 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007842{
7843 struct sk_buff *msg;
7844
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007845 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007846 if (!msg)
7847 return;
7848
Johannes Bergfd014282012-06-18 19:17:03 +02007849 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007850 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007851 nlmsg_free(msg);
7852 return;
7853 }
7854
Johannes Berg463d0182009-07-14 00:33:35 +02007855 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7856 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007857}
7858
7859void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007860 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007861{
7862 struct sk_buff *msg;
7863
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007864 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007865 if (!msg)
7866 return;
7867
Johannes Bergfd014282012-06-18 19:17:03 +02007868 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007869 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007870 nlmsg_free(msg);
7871 return;
7872 }
7873
Johannes Berg463d0182009-07-14 00:33:35 +02007874 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7875 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007876}
7877
Luciano Coelho807f8a82011-05-11 17:09:35 +03007878void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7879 struct net_device *netdev)
7880{
7881 struct sk_buff *msg;
7882
7883 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7884 if (!msg)
7885 return;
7886
7887 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7888 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7889 nlmsg_free(msg);
7890 return;
7891 }
7892
7893 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7894 nl80211_scan_mcgrp.id, GFP_KERNEL);
7895}
7896
7897void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7898 struct net_device *netdev, u32 cmd)
7899{
7900 struct sk_buff *msg;
7901
Thomas Graf58050fc2012-06-28 03:57:45 +00007902 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007903 if (!msg)
7904 return;
7905
7906 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7907 nlmsg_free(msg);
7908 return;
7909 }
7910
7911 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7912 nl80211_scan_mcgrp.id, GFP_KERNEL);
7913}
7914
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007915/*
7916 * This can happen on global regulatory changes or device specific settings
7917 * based on custom world regulatory domains.
7918 */
7919void nl80211_send_reg_change_event(struct regulatory_request *request)
7920{
7921 struct sk_buff *msg;
7922 void *hdr;
7923
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007924 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007925 if (!msg)
7926 return;
7927
7928 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7929 if (!hdr) {
7930 nlmsg_free(msg);
7931 return;
7932 }
7933
7934 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007935 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7936 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007937
David S. Miller9360ffd2012-03-29 04:41:26 -04007938 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7939 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7940 NL80211_REGDOM_TYPE_WORLD))
7941 goto nla_put_failure;
7942 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7943 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7944 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7945 goto nla_put_failure;
7946 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7947 request->intersect) {
7948 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7949 NL80211_REGDOM_TYPE_INTERSECTION))
7950 goto nla_put_failure;
7951 } else {
7952 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7953 NL80211_REGDOM_TYPE_COUNTRY) ||
7954 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7955 request->alpha2))
7956 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007957 }
7958
David S. Miller9360ffd2012-03-29 04:41:26 -04007959 if (wiphy_idx_valid(request->wiphy_idx) &&
7960 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7961 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007962
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007963 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007964
Johannes Bergbc43b282009-07-25 10:54:13 +02007965 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007966 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007967 GFP_ATOMIC);
7968 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007969
7970 return;
7971
7972nla_put_failure:
7973 genlmsg_cancel(msg, hdr);
7974 nlmsg_free(msg);
7975}
7976
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007977static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7978 struct net_device *netdev,
7979 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007980 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007981{
7982 struct sk_buff *msg;
7983 void *hdr;
7984
Johannes Berge6d6e342009-07-01 21:26:47 +02007985 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007986 if (!msg)
7987 return;
7988
7989 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7990 if (!hdr) {
7991 nlmsg_free(msg);
7992 return;
7993 }
7994
David S. Miller9360ffd2012-03-29 04:41:26 -04007995 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7996 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7997 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7998 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007999
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008000 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008001
Johannes Berg463d0182009-07-14 00:33:35 +02008002 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8003 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008004 return;
8005
8006 nla_put_failure:
8007 genlmsg_cancel(msg, hdr);
8008 nlmsg_free(msg);
8009}
8010
8011void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008012 struct net_device *netdev, const u8 *buf,
8013 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008014{
8015 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008016 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008017}
8018
8019void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8020 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008021 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008022{
Johannes Berge6d6e342009-07-01 21:26:47 +02008023 nl80211_send_mlme_event(rdev, netdev, buf, len,
8024 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008025}
8026
Jouni Malinen53b46b82009-03-27 20:53:56 +02008027void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008028 struct net_device *netdev, const u8 *buf,
8029 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008030{
8031 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008032 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008033}
8034
Jouni Malinen53b46b82009-03-27 20:53:56 +02008035void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8036 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008037 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008038{
8039 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008040 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008041}
8042
Jouni Malinencf4e5942010-12-16 00:52:40 +02008043void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8044 struct net_device *netdev, const u8 *buf,
8045 size_t len, gfp_t gfp)
8046{
8047 nl80211_send_mlme_event(rdev, netdev, buf, len,
8048 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8049}
8050
8051void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8052 struct net_device *netdev, const u8 *buf,
8053 size_t len, gfp_t gfp)
8054{
8055 nl80211_send_mlme_event(rdev, netdev, buf, len,
8056 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8057}
8058
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008059static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8060 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008061 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008062{
8063 struct sk_buff *msg;
8064 void *hdr;
8065
Johannes Berge6d6e342009-07-01 21:26:47 +02008066 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008067 if (!msg)
8068 return;
8069
8070 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8071 if (!hdr) {
8072 nlmsg_free(msg);
8073 return;
8074 }
8075
David S. Miller9360ffd2012-03-29 04:41:26 -04008076 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8077 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8078 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8079 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8080 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008081
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008082 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008083
Johannes Berg463d0182009-07-14 00:33:35 +02008084 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8085 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008086 return;
8087
8088 nla_put_failure:
8089 genlmsg_cancel(msg, hdr);
8090 nlmsg_free(msg);
8091}
8092
8093void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008094 struct net_device *netdev, const u8 *addr,
8095 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008096{
8097 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008098 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008099}
8100
8101void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008102 struct net_device *netdev, const u8 *addr,
8103 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008104{
Johannes Berge6d6e342009-07-01 21:26:47 +02008105 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8106 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008107}
8108
Samuel Ortizb23aa672009-07-01 21:26:54 +02008109void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8110 struct net_device *netdev, const u8 *bssid,
8111 const u8 *req_ie, size_t req_ie_len,
8112 const u8 *resp_ie, size_t resp_ie_len,
8113 u16 status, gfp_t gfp)
8114{
8115 struct sk_buff *msg;
8116 void *hdr;
8117
Thomas Graf58050fc2012-06-28 03:57:45 +00008118 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008119 if (!msg)
8120 return;
8121
8122 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8123 if (!hdr) {
8124 nlmsg_free(msg);
8125 return;
8126 }
8127
David S. Miller9360ffd2012-03-29 04:41:26 -04008128 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8129 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8130 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8131 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8132 (req_ie &&
8133 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8134 (resp_ie &&
8135 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8136 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008137
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008138 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008139
Johannes Berg463d0182009-07-14 00:33:35 +02008140 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8141 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008142 return;
8143
8144 nla_put_failure:
8145 genlmsg_cancel(msg, hdr);
8146 nlmsg_free(msg);
8147
8148}
8149
8150void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8151 struct net_device *netdev, const u8 *bssid,
8152 const u8 *req_ie, size_t req_ie_len,
8153 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8154{
8155 struct sk_buff *msg;
8156 void *hdr;
8157
Thomas Graf58050fc2012-06-28 03:57:45 +00008158 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008159 if (!msg)
8160 return;
8161
8162 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8163 if (!hdr) {
8164 nlmsg_free(msg);
8165 return;
8166 }
8167
David S. Miller9360ffd2012-03-29 04:41:26 -04008168 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8169 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8170 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8171 (req_ie &&
8172 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8173 (resp_ie &&
8174 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8175 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008176
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008177 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008178
Johannes Berg463d0182009-07-14 00:33:35 +02008179 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8180 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008181 return;
8182
8183 nla_put_failure:
8184 genlmsg_cancel(msg, hdr);
8185 nlmsg_free(msg);
8186
8187}
8188
8189void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8190 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02008191 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008192{
8193 struct sk_buff *msg;
8194 void *hdr;
8195
Thomas Graf58050fc2012-06-28 03:57:45 +00008196 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008197 if (!msg)
8198 return;
8199
8200 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8201 if (!hdr) {
8202 nlmsg_free(msg);
8203 return;
8204 }
8205
David S. Miller9360ffd2012-03-29 04:41:26 -04008206 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8207 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8208 (from_ap && reason &&
8209 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8210 (from_ap &&
8211 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8212 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8213 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008214
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008215 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008216
Johannes Berg463d0182009-07-14 00:33:35 +02008217 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8218 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008219 return;
8220
8221 nla_put_failure:
8222 genlmsg_cancel(msg, hdr);
8223 nlmsg_free(msg);
8224
8225}
8226
Johannes Berg04a773a2009-04-19 21:24:32 +02008227void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8228 struct net_device *netdev, const u8 *bssid,
8229 gfp_t gfp)
8230{
8231 struct sk_buff *msg;
8232 void *hdr;
8233
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008234 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008235 if (!msg)
8236 return;
8237
8238 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8239 if (!hdr) {
8240 nlmsg_free(msg);
8241 return;
8242 }
8243
David S. Miller9360ffd2012-03-29 04:41:26 -04008244 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8245 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8246 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8247 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008248
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008249 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008250
Johannes Berg463d0182009-07-14 00:33:35 +02008251 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8252 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008253 return;
8254
8255 nla_put_failure:
8256 genlmsg_cancel(msg, hdr);
8257 nlmsg_free(msg);
8258}
8259
Javier Cardonac93b5e72011-04-07 15:08:34 -07008260void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8261 struct net_device *netdev,
8262 const u8 *macaddr, const u8* ie, u8 ie_len,
8263 gfp_t gfp)
8264{
8265 struct sk_buff *msg;
8266 void *hdr;
8267
8268 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8269 if (!msg)
8270 return;
8271
8272 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8273 if (!hdr) {
8274 nlmsg_free(msg);
8275 return;
8276 }
8277
David S. Miller9360ffd2012-03-29 04:41:26 -04008278 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8279 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8280 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8281 (ie_len && ie &&
8282 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8283 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008284
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008285 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008286
8287 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8288 nl80211_mlme_mcgrp.id, gfp);
8289 return;
8290
8291 nla_put_failure:
8292 genlmsg_cancel(msg, hdr);
8293 nlmsg_free(msg);
8294}
8295
Jouni Malinena3b8b052009-03-27 21:59:49 +02008296void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8297 struct net_device *netdev, const u8 *addr,
8298 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008299 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008300{
8301 struct sk_buff *msg;
8302 void *hdr;
8303
Johannes Berge6d6e342009-07-01 21:26:47 +02008304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008305 if (!msg)
8306 return;
8307
8308 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8309 if (!hdr) {
8310 nlmsg_free(msg);
8311 return;
8312 }
8313
David S. Miller9360ffd2012-03-29 04:41:26 -04008314 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8315 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8316 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8317 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8318 (key_id != -1 &&
8319 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8320 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8321 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008322
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008323 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008324
Johannes Berg463d0182009-07-14 00:33:35 +02008325 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8326 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008327 return;
8328
8329 nla_put_failure:
8330 genlmsg_cancel(msg, hdr);
8331 nlmsg_free(msg);
8332}
8333
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008334void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8335 struct ieee80211_channel *channel_before,
8336 struct ieee80211_channel *channel_after)
8337{
8338 struct sk_buff *msg;
8339 void *hdr;
8340 struct nlattr *nl_freq;
8341
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008342 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008343 if (!msg)
8344 return;
8345
8346 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8347 if (!hdr) {
8348 nlmsg_free(msg);
8349 return;
8350 }
8351
8352 /*
8353 * Since we are applying the beacon hint to a wiphy we know its
8354 * wiphy_idx is valid
8355 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008356 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8357 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008358
8359 /* Before */
8360 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8361 if (!nl_freq)
8362 goto nla_put_failure;
8363 if (nl80211_msg_put_channel(msg, channel_before))
8364 goto nla_put_failure;
8365 nla_nest_end(msg, nl_freq);
8366
8367 /* After */
8368 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8369 if (!nl_freq)
8370 goto nla_put_failure;
8371 if (nl80211_msg_put_channel(msg, channel_after))
8372 goto nla_put_failure;
8373 nla_nest_end(msg, nl_freq);
8374
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008375 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008376
Johannes Berg463d0182009-07-14 00:33:35 +02008377 rcu_read_lock();
8378 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8379 GFP_ATOMIC);
8380 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008381
8382 return;
8383
8384nla_put_failure:
8385 genlmsg_cancel(msg, hdr);
8386 nlmsg_free(msg);
8387}
8388
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008389static void nl80211_send_remain_on_chan_event(
8390 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008391 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008392 struct ieee80211_channel *chan,
8393 enum nl80211_channel_type channel_type,
8394 unsigned int duration, gfp_t gfp)
8395{
8396 struct sk_buff *msg;
8397 void *hdr;
8398
8399 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8400 if (!msg)
8401 return;
8402
8403 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8404 if (!hdr) {
8405 nlmsg_free(msg);
8406 return;
8407 }
8408
David S. Miller9360ffd2012-03-29 04:41:26 -04008409 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008410 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8411 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008412 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008413 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
8414 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
8415 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8416 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008417
David S. Miller9360ffd2012-03-29 04:41:26 -04008418 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8419 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8420 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008421
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008422 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008423
8424 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8425 nl80211_mlme_mcgrp.id, gfp);
8426 return;
8427
8428 nla_put_failure:
8429 genlmsg_cancel(msg, hdr);
8430 nlmsg_free(msg);
8431}
8432
8433void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008434 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008435 struct ieee80211_channel *chan,
8436 enum nl80211_channel_type channel_type,
8437 unsigned int duration, gfp_t gfp)
8438{
8439 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008440 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008441 channel_type, duration, gfp);
8442}
8443
8444void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008445 struct cfg80211_registered_device *rdev,
8446 struct wireless_dev *wdev,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008447 u64 cookie, struct ieee80211_channel *chan,
8448 enum nl80211_channel_type channel_type, gfp_t gfp)
8449{
8450 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008451 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008452 channel_type, 0, gfp);
8453}
8454
Johannes Berg98b62182009-12-23 13:15:44 +01008455void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8456 struct net_device *dev, const u8 *mac_addr,
8457 struct station_info *sinfo, gfp_t gfp)
8458{
8459 struct sk_buff *msg;
8460
Thomas Graf58050fc2012-06-28 03:57:45 +00008461 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008462 if (!msg)
8463 return;
8464
John W. Linville66266b32012-03-15 13:25:41 -04008465 if (nl80211_send_station(msg, 0, 0, 0,
8466 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008467 nlmsg_free(msg);
8468 return;
8469 }
8470
8471 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8472 nl80211_mlme_mcgrp.id, gfp);
8473}
8474
Jouni Malinenec15e682011-03-23 15:29:52 +02008475void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8476 struct net_device *dev, const u8 *mac_addr,
8477 gfp_t gfp)
8478{
8479 struct sk_buff *msg;
8480 void *hdr;
8481
Thomas Graf58050fc2012-06-28 03:57:45 +00008482 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008483 if (!msg)
8484 return;
8485
8486 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8487 if (!hdr) {
8488 nlmsg_free(msg);
8489 return;
8490 }
8491
David S. Miller9360ffd2012-03-29 04:41:26 -04008492 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8493 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8494 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008495
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008496 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008497
8498 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8499 nl80211_mlme_mcgrp.id, gfp);
8500 return;
8501
8502 nla_put_failure:
8503 genlmsg_cancel(msg, hdr);
8504 nlmsg_free(msg);
8505}
8506
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308507void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8508 struct net_device *dev, const u8 *mac_addr,
8509 enum nl80211_connect_failed_reason reason,
8510 gfp_t gfp)
8511{
8512 struct sk_buff *msg;
8513 void *hdr;
8514
8515 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8516 if (!msg)
8517 return;
8518
8519 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8520 if (!hdr) {
8521 nlmsg_free(msg);
8522 return;
8523 }
8524
8525 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8526 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8527 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8528 goto nla_put_failure;
8529
8530 genlmsg_end(msg, hdr);
8531
8532 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8533 nl80211_mlme_mcgrp.id, gfp);
8534 return;
8535
8536 nla_put_failure:
8537 genlmsg_cancel(msg, hdr);
8538 nlmsg_free(msg);
8539}
8540
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008541static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8542 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008543{
8544 struct wireless_dev *wdev = dev->ieee80211_ptr;
8545 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8546 struct sk_buff *msg;
8547 void *hdr;
8548 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008549 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008550
Eric W. Biederman15e47302012-09-07 20:12:54 +00008551 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008552 return false;
8553
8554 msg = nlmsg_new(100, gfp);
8555 if (!msg)
8556 return true;
8557
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008558 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008559 if (!hdr) {
8560 nlmsg_free(msg);
8561 return true;
8562 }
8563
David S. Miller9360ffd2012-03-29 04:41:26 -04008564 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8565 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8566 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8567 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008568
8569 err = genlmsg_end(msg, hdr);
8570 if (err < 0) {
8571 nlmsg_free(msg);
8572 return true;
8573 }
8574
Eric W. Biederman15e47302012-09-07 20:12:54 +00008575 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008576 return true;
8577
8578 nla_put_failure:
8579 genlmsg_cancel(msg, hdr);
8580 nlmsg_free(msg);
8581 return true;
8582}
8583
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008584bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8585{
8586 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8587 addr, gfp);
8588}
8589
8590bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8591 const u8 *addr, gfp_t gfp)
8592{
8593 return __nl80211_unexpected_frame(dev,
8594 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8595 addr, gfp);
8596}
8597
Johannes Berg2e161f782010-08-12 15:38:38 +02008598int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008599 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008600 int freq, int sig_dbm,
8601 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008602{
Johannes Berg71bbc992012-06-15 15:30:18 +02008603 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008604 struct sk_buff *msg;
8605 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008606
8607 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8608 if (!msg)
8609 return -ENOMEM;
8610
Johannes Berg2e161f782010-08-12 15:38:38 +02008611 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008612 if (!hdr) {
8613 nlmsg_free(msg);
8614 return -ENOMEM;
8615 }
8616
David S. Miller9360ffd2012-03-29 04:41:26 -04008617 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008618 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8619 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008620 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8621 (sig_dbm &&
8622 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8623 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8624 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008625
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008626 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008627
Eric W. Biederman15e47302012-09-07 20:12:54 +00008628 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008629
8630 nla_put_failure:
8631 genlmsg_cancel(msg, hdr);
8632 nlmsg_free(msg);
8633 return -ENOBUFS;
8634}
8635
Johannes Berg2e161f782010-08-12 15:38:38 +02008636void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008637 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f782010-08-12 15:38:38 +02008638 const u8 *buf, size_t len, bool ack,
8639 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008640{
Johannes Berg71bbc992012-06-15 15:30:18 +02008641 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008642 struct sk_buff *msg;
8643 void *hdr;
8644
8645 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8646 if (!msg)
8647 return;
8648
Johannes Berg2e161f782010-08-12 15:38:38 +02008649 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008650 if (!hdr) {
8651 nlmsg_free(msg);
8652 return;
8653 }
8654
David S. Miller9360ffd2012-03-29 04:41:26 -04008655 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008656 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8657 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008658 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8659 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8660 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8661 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008662
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008663 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008664
8665 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8666 return;
8667
8668 nla_put_failure:
8669 genlmsg_cancel(msg, hdr);
8670 nlmsg_free(msg);
8671}
8672
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008673void
8674nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8675 struct net_device *netdev,
8676 enum nl80211_cqm_rssi_threshold_event rssi_event,
8677 gfp_t gfp)
8678{
8679 struct sk_buff *msg;
8680 struct nlattr *pinfoattr;
8681 void *hdr;
8682
Thomas Graf58050fc2012-06-28 03:57:45 +00008683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008684 if (!msg)
8685 return;
8686
8687 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8688 if (!hdr) {
8689 nlmsg_free(msg);
8690 return;
8691 }
8692
David S. Miller9360ffd2012-03-29 04:41:26 -04008693 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8694 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8695 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008696
8697 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8698 if (!pinfoattr)
8699 goto nla_put_failure;
8700
David S. Miller9360ffd2012-03-29 04:41:26 -04008701 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8702 rssi_event))
8703 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008704
8705 nla_nest_end(msg, pinfoattr);
8706
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008707 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008708
8709 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8710 nl80211_mlme_mcgrp.id, gfp);
8711 return;
8712
8713 nla_put_failure:
8714 genlmsg_cancel(msg, hdr);
8715 nlmsg_free(msg);
8716}
8717
Johannes Berge5497d72011-07-05 16:35:40 +02008718void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8719 struct net_device *netdev, const u8 *bssid,
8720 const u8 *replay_ctr, gfp_t gfp)
8721{
8722 struct sk_buff *msg;
8723 struct nlattr *rekey_attr;
8724 void *hdr;
8725
Thomas Graf58050fc2012-06-28 03:57:45 +00008726 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008727 if (!msg)
8728 return;
8729
8730 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8731 if (!hdr) {
8732 nlmsg_free(msg);
8733 return;
8734 }
8735
David S. Miller9360ffd2012-03-29 04:41:26 -04008736 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8737 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8738 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8739 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008740
8741 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8742 if (!rekey_attr)
8743 goto nla_put_failure;
8744
David S. Miller9360ffd2012-03-29 04:41:26 -04008745 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8746 NL80211_REPLAY_CTR_LEN, replay_ctr))
8747 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008748
8749 nla_nest_end(msg, rekey_attr);
8750
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008751 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008752
8753 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8754 nl80211_mlme_mcgrp.id, gfp);
8755 return;
8756
8757 nla_put_failure:
8758 genlmsg_cancel(msg, hdr);
8759 nlmsg_free(msg);
8760}
8761
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008762void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8763 struct net_device *netdev, int index,
8764 const u8 *bssid, bool preauth, gfp_t gfp)
8765{
8766 struct sk_buff *msg;
8767 struct nlattr *attr;
8768 void *hdr;
8769
Thomas Graf58050fc2012-06-28 03:57:45 +00008770 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008771 if (!msg)
8772 return;
8773
8774 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8775 if (!hdr) {
8776 nlmsg_free(msg);
8777 return;
8778 }
8779
David S. Miller9360ffd2012-03-29 04:41:26 -04008780 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8781 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8782 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008783
8784 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8785 if (!attr)
8786 goto nla_put_failure;
8787
David S. Miller9360ffd2012-03-29 04:41:26 -04008788 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8789 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8790 (preauth &&
8791 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8792 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008793
8794 nla_nest_end(msg, attr);
8795
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008796 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008797
8798 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8799 nl80211_mlme_mcgrp.id, gfp);
8800 return;
8801
8802 nla_put_failure:
8803 genlmsg_cancel(msg, hdr);
8804 nlmsg_free(msg);
8805}
8806
Thomas Pedersen53145262012-04-06 13:35:47 -07008807void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8808 struct net_device *netdev, int freq,
8809 enum nl80211_channel_type type, gfp_t gfp)
8810{
8811 struct sk_buff *msg;
8812 void *hdr;
8813
Thomas Graf58050fc2012-06-28 03:57:45 +00008814 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008815 if (!msg)
8816 return;
8817
8818 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8819 if (!hdr) {
8820 nlmsg_free(msg);
8821 return;
8822 }
8823
John W. Linville7eab0f62012-04-12 14:25:14 -04008824 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8825 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8826 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8827 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008828
8829 genlmsg_end(msg, hdr);
8830
8831 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8832 nl80211_mlme_mcgrp.id, gfp);
8833 return;
8834
8835 nla_put_failure:
8836 genlmsg_cancel(msg, hdr);
8837 nlmsg_free(msg);
8838}
8839
Johannes Bergc063dbf2010-11-24 08:10:05 +01008840void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008841nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8842 struct net_device *netdev, const u8 *peer,
8843 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8844{
8845 struct sk_buff *msg;
8846 struct nlattr *pinfoattr;
8847 void *hdr;
8848
8849 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8850 if (!msg)
8851 return;
8852
8853 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8854 if (!hdr) {
8855 nlmsg_free(msg);
8856 return;
8857 }
8858
8859 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8860 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8861 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8862 goto nla_put_failure;
8863
8864 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8865 if (!pinfoattr)
8866 goto nla_put_failure;
8867
8868 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8869 goto nla_put_failure;
8870
8871 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8872 goto nla_put_failure;
8873
8874 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8875 goto nla_put_failure;
8876
8877 nla_nest_end(msg, pinfoattr);
8878
8879 genlmsg_end(msg, hdr);
8880
8881 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8882 nl80211_mlme_mcgrp.id, gfp);
8883 return;
8884
8885 nla_put_failure:
8886 genlmsg_cancel(msg, hdr);
8887 nlmsg_free(msg);
8888}
8889
8890void
Johannes Bergc063dbf2010-11-24 08:10:05 +01008891nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8892 struct net_device *netdev, const u8 *peer,
8893 u32 num_packets, gfp_t gfp)
8894{
8895 struct sk_buff *msg;
8896 struct nlattr *pinfoattr;
8897 void *hdr;
8898
Thomas Graf58050fc2012-06-28 03:57:45 +00008899 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008900 if (!msg)
8901 return;
8902
8903 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8904 if (!hdr) {
8905 nlmsg_free(msg);
8906 return;
8907 }
8908
David S. Miller9360ffd2012-03-29 04:41:26 -04008909 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8910 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8911 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8912 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008913
8914 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8915 if (!pinfoattr)
8916 goto nla_put_failure;
8917
David S. Miller9360ffd2012-03-29 04:41:26 -04008918 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8919 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008920
8921 nla_nest_end(msg, pinfoattr);
8922
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008923 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008924
8925 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8926 nl80211_mlme_mcgrp.id, gfp);
8927 return;
8928
8929 nla_put_failure:
8930 genlmsg_cancel(msg, hdr);
8931 nlmsg_free(msg);
8932}
8933
Johannes Berg7f6cf312011-11-04 11:18:15 +01008934void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8935 u64 cookie, bool acked, gfp_t gfp)
8936{
8937 struct wireless_dev *wdev = dev->ieee80211_ptr;
8938 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8939 struct sk_buff *msg;
8940 void *hdr;
8941 int err;
8942
Beni Lev4ee3e062012-08-27 12:49:39 +03008943 trace_cfg80211_probe_status(dev, addr, cookie, acked);
8944
Thomas Graf58050fc2012-06-28 03:57:45 +00008945 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03008946
Johannes Berg7f6cf312011-11-04 11:18:15 +01008947 if (!msg)
8948 return;
8949
8950 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8951 if (!hdr) {
8952 nlmsg_free(msg);
8953 return;
8954 }
8955
David S. Miller9360ffd2012-03-29 04:41:26 -04008956 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8957 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8958 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8959 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8960 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8961 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008962
8963 err = genlmsg_end(msg, hdr);
8964 if (err < 0) {
8965 nlmsg_free(msg);
8966 return;
8967 }
8968
8969 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8970 nl80211_mlme_mcgrp.id, gfp);
8971 return;
8972
8973 nla_put_failure:
8974 genlmsg_cancel(msg, hdr);
8975 nlmsg_free(msg);
8976}
8977EXPORT_SYMBOL(cfg80211_probe_status);
8978
Johannes Berg5e760232011-11-04 11:18:17 +01008979void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8980 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07008981 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01008982{
8983 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8984 struct sk_buff *msg;
8985 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07008986 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01008987
Beni Lev4ee3e062012-08-27 12:49:39 +03008988 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
8989
Ben Greear37c73b52012-10-26 14:49:25 -07008990 spin_lock_bh(&rdev->beacon_registrations_lock);
8991 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8992 msg = nlmsg_new(len + 100, GFP_ATOMIC);
8993 if (!msg) {
8994 spin_unlock_bh(&rdev->beacon_registrations_lock);
8995 return;
8996 }
Johannes Berg5e760232011-11-04 11:18:17 +01008997
Ben Greear37c73b52012-10-26 14:49:25 -07008998 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8999 if (!hdr)
9000 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01009001
Ben Greear37c73b52012-10-26 14:49:25 -07009002 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9003 (freq &&
9004 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9005 (sig_dbm &&
9006 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9007 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9008 goto nla_put_failure;
9009
9010 genlmsg_end(msg, hdr);
9011
9012 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009013 }
Ben Greear37c73b52012-10-26 14:49:25 -07009014 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009015 return;
9016
9017 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009018 spin_unlock_bh(&rdev->beacon_registrations_lock);
9019 if (hdr)
9020 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009021 nlmsg_free(msg);
9022}
9023EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9024
Jouni Malinen026331c2010-02-15 12:53:10 +02009025static int nl80211_netlink_notify(struct notifier_block * nb,
9026 unsigned long state,
9027 void *_notify)
9028{
9029 struct netlink_notify *notify = _notify;
9030 struct cfg80211_registered_device *rdev;
9031 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009032 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009033
9034 if (state != NETLINK_URELEASE)
9035 return NOTIFY_DONE;
9036
9037 rcu_read_lock();
9038
Johannes Berg5e760232011-11-04 11:18:17 +01009039 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009040 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009041 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009042
9043 spin_lock_bh(&rdev->beacon_registrations_lock);
9044 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9045 list) {
9046 if (reg->nlportid == notify->portid) {
9047 list_del(&reg->list);
9048 kfree(reg);
9049 break;
9050 }
9051 }
9052 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009053 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009054
9055 rcu_read_unlock();
9056
9057 return NOTIFY_DONE;
9058}
9059
9060static struct notifier_block nl80211_netlink_notifier = {
9061 .notifier_call = nl80211_netlink_notify,
9062};
9063
Johannes Berg55682962007-09-20 13:09:35 -04009064/* initialisation/exit functions */
9065
9066int nl80211_init(void)
9067{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009068 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009069
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009070 err = genl_register_family_with_ops(&nl80211_fam,
9071 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009072 if (err)
9073 return err;
9074
Johannes Berg55682962007-09-20 13:09:35 -04009075 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9076 if (err)
9077 goto err_out;
9078
Johannes Berg2a519312009-02-10 21:25:55 +01009079 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9080 if (err)
9081 goto err_out;
9082
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009083 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9084 if (err)
9085 goto err_out;
9086
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009087 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9088 if (err)
9089 goto err_out;
9090
Johannes Bergaff89a92009-07-01 21:26:51 +02009091#ifdef CONFIG_NL80211_TESTMODE
9092 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9093 if (err)
9094 goto err_out;
9095#endif
9096
Jouni Malinen026331c2010-02-15 12:53:10 +02009097 err = netlink_register_notifier(&nl80211_netlink_notifier);
9098 if (err)
9099 goto err_out;
9100
Johannes Berg55682962007-09-20 13:09:35 -04009101 return 0;
9102 err_out:
9103 genl_unregister_family(&nl80211_fam);
9104 return err;
9105}
9106
9107void nl80211_exit(void)
9108{
Jouni Malinen026331c2010-02-15 12:53:10 +02009109 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009110 genl_unregister_family(&nl80211_fam);
9111}