blob: 999108cd947c5991facde7a5494ee3e0389ffe22 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030025#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040026
Jouni Malinen5fb628e2011-08-10 23:54:35 +030027static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg89a54e42012-06-15 14:33:17 +020049/* returns ERR_PTR values */
50static struct wireless_dev *
51__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040052{
Johannes Berg89a54e42012-06-15 14:33:17 +020053 struct cfg80211_registered_device *rdev;
54 struct wireless_dev *result = NULL;
55 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
56 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
57 u64 wdev_id;
58 int wiphy_idx = -1;
59 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040060
Johannes Berg89a54e42012-06-15 14:33:17 +020061 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040062
Johannes Berg89a54e42012-06-15 14:33:17 +020063 if (!have_ifidx && !have_wdev_id)
64 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040065
Johannes Berg89a54e42012-06-15 14:33:17 +020066 if (have_ifidx)
67 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
68 if (have_wdev_id) {
69 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
70 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040071 }
72
Johannes Berg89a54e42012-06-15 14:33:17 +020073 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
74 struct wireless_dev *wdev;
75
76 if (wiphy_net(&rdev->wiphy) != netns)
77 continue;
78
79 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
80 continue;
81
82 mutex_lock(&rdev->devlist_mtx);
83 list_for_each_entry(wdev, &rdev->wdev_list, list) {
84 if (have_ifidx && wdev->netdev &&
85 wdev->netdev->ifindex == ifidx) {
86 result = wdev;
87 break;
88 }
89 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
90 result = wdev;
91 break;
92 }
93 }
94 mutex_unlock(&rdev->devlist_mtx);
95
96 if (result)
97 break;
98 }
99
100 if (result)
101 return result;
102 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400103}
104
Johannes Berga9455402012-06-15 13:32:49 +0200105static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200106__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200107{
Johannes Berg7fee4772012-06-15 14:09:58 +0200108 struct cfg80211_registered_device *rdev = NULL, *tmp;
109 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200110
111 assert_cfg80211_lock();
112
Johannes Berg878d9ec2012-06-15 14:18:32 +0200113 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200114 !attrs[NL80211_ATTR_IFINDEX] &&
115 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200116 return ERR_PTR(-EINVAL);
117
Johannes Berg878d9ec2012-06-15 14:18:32 +0200118 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200119 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200120 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200121
Johannes Berg89a54e42012-06-15 14:33:17 +0200122 if (attrs[NL80211_ATTR_WDEV]) {
123 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
124 struct wireless_dev *wdev;
125 bool found = false;
126
127 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
128 if (tmp) {
129 /* make sure wdev exists */
130 mutex_lock(&tmp->devlist_mtx);
131 list_for_each_entry(wdev, &tmp->wdev_list, list) {
132 if (wdev->identifier != (u32)wdev_id)
133 continue;
134 found = true;
135 break;
136 }
137 mutex_unlock(&tmp->devlist_mtx);
138
139 if (!found)
140 tmp = NULL;
141
142 if (rdev && tmp != rdev)
143 return ERR_PTR(-EINVAL);
144 rdev = tmp;
145 }
146 }
147
Johannes Berg878d9ec2012-06-15 14:18:32 +0200148 if (attrs[NL80211_ATTR_IFINDEX]) {
149 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200150 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200151 if (netdev) {
152 if (netdev->ieee80211_ptr)
153 tmp = wiphy_to_dev(
154 netdev->ieee80211_ptr->wiphy);
155 else
156 tmp = NULL;
157
158 dev_put(netdev);
159
160 /* not wireless device -- return error */
161 if (!tmp)
162 return ERR_PTR(-EINVAL);
163
164 /* mismatch -- return error */
165 if (rdev && tmp != rdev)
166 return ERR_PTR(-EINVAL);
167
168 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200169 }
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
171
Johannes Berg4f7eff12012-06-15 14:14:22 +0200172 if (!rdev)
173 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200174
Johannes Berg4f7eff12012-06-15 14:14:22 +0200175 if (netns != wiphy_net(&rdev->wiphy))
176 return ERR_PTR(-ENODEV);
177
178 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200179}
180
181/*
182 * This function returns a pointer to the driver
183 * that the genl_info item that is passed refers to.
184 * If successful, it returns non-NULL and also locks
185 * the driver's mutex!
186 *
187 * This means that you need to call cfg80211_unlock_rdev()
188 * before being allowed to acquire &cfg80211_mutex!
189 *
190 * This is necessary because we need to lock the global
191 * mutex to get an item off the list safely, and then
192 * we lock the rdev mutex so it doesn't go away under us.
193 *
194 * We don't want to keep cfg80211_mutex locked
195 * for all the time in order to allow requests on
196 * other interfaces to go through at the same time.
197 *
198 * The result of this can be a PTR_ERR and hence must
199 * be checked with IS_ERR() for errors.
200 */
201static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200202cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200203{
204 struct cfg80211_registered_device *rdev;
205
206 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200207 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208
209 /* if it is not an error we grab the lock on
210 * it to assure it won't be going away while
211 * we operate on it */
212 if (!IS_ERR(rdev))
213 mutex_lock(&rdev->mtx);
214
215 mutex_unlock(&cfg80211_mutex);
216
217 return rdev;
218}
219
Johannes Berg55682962007-09-20 13:09:35 -0400220/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000221static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400222 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
223 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700224 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200225 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200226 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530227 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200228 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
229 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
230 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
231 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100232 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400233
234 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
235 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
236 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100237
Eliad Pellere007b852011-11-24 18:13:56 +0200238 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
239 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100240
Johannes Bergb9454e82009-07-08 13:29:08 +0200241 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100242 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
243 .len = WLAN_MAX_KEY_LEN },
244 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
245 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
246 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200247 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200248 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100249
250 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
251 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
252 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
253 .len = IEEE80211_MAX_DATA_LEN },
254 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
255 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100256 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
257 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
258 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
259 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
260 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200263 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100264 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800265 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100266 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300267
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700268 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
269 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
270
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300271 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
272 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
273 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200274 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
275 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100276 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300277
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800278 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700279 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700280
Johannes Berg6c739412011-11-03 09:27:01 +0100281 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200282
283 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
284 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100286 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
287 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200288
289 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
290 .len = IEEE80211_MAX_SSID_LEN },
291 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
292 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200293 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300294 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300295 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300296 [NL80211_ATTR_STA_FLAGS2] = {
297 .len = sizeof(struct nl80211_sta_flag_update),
298 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300299 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300300 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
301 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200302 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
303 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
304 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200305 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100306 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100307 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
308 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100309 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
310 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200311 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200312 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
313 .len = IEEE80211_MAX_DATA_LEN },
314 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200315 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200316 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300317 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200318 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300319 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
320 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200321 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900322 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
323 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100324 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100325 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100326 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200327 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700328 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300329 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200330 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200331 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300332 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300333 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
334 .len = IEEE80211_MAX_DATA_LEN },
335 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
336 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530337 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300338 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530339 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300340 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
341 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
342 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
343 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
344 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100345 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200346 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
347 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700348 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800349 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
351 .len = NL80211_HT_CAPABILITY_LEN
352 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100353 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530354 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530355 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200356 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700357 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300358 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000359 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700360 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400361};
362
Johannes Berge31b8212010-10-05 19:39:30 +0200363/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000364static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200365 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200366 [NL80211_KEY_IDX] = { .type = NLA_U8 },
367 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200368 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200369 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
370 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200371 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100372 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
373};
374
375/* policy for the key default flags */
376static const struct nla_policy
377nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
378 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
379 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200380};
381
Johannes Bergff1b6e62011-05-04 15:37:28 +0200382/* policy for WoWLAN attributes */
383static const struct nla_policy
384nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
385 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
386 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
387 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
388 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200389 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
390 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
391 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
392 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200393};
394
Johannes Berge5497d72011-07-05 16:35:40 +0200395/* policy for GTK rekey offload attributes */
396static const struct nla_policy
397nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
398 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
399 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
400 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
401};
402
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300403static const struct nla_policy
404nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200405 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300406 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700407 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300408};
409
Holger Schuriga0438972009-11-11 11:30:02 +0100410/* ifidx get helper */
411static int nl80211_get_ifidx(struct netlink_callback *cb)
412{
413 int res;
414
415 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
416 nl80211_fam.attrbuf, nl80211_fam.maxattr,
417 nl80211_policy);
418 if (res)
419 return res;
420
421 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
422 return -EINVAL;
423
424 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
425 if (!res)
426 return -EINVAL;
427 return res;
428}
429
Johannes Berg67748892010-10-04 21:14:06 +0200430static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
431 struct netlink_callback *cb,
432 struct cfg80211_registered_device **rdev,
433 struct net_device **dev)
434{
435 int ifidx = cb->args[0];
436 int err;
437
438 if (!ifidx)
439 ifidx = nl80211_get_ifidx(cb);
440 if (ifidx < 0)
441 return ifidx;
442
443 cb->args[0] = ifidx;
444
445 rtnl_lock();
446
447 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
448 if (!*dev) {
449 err = -ENODEV;
450 goto out_rtnl;
451 }
452
453 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100454 if (IS_ERR(*rdev)) {
455 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200456 goto out_rtnl;
457 }
458
459 return 0;
460 out_rtnl:
461 rtnl_unlock();
462 return err;
463}
464
465static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
466{
467 cfg80211_unlock_rdev(rdev);
468 rtnl_unlock();
469}
470
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100471/* IE validation */
472static bool is_valid_ie_attr(const struct nlattr *attr)
473{
474 const u8 *pos;
475 int len;
476
477 if (!attr)
478 return true;
479
480 pos = nla_data(attr);
481 len = nla_len(attr);
482
483 while (len) {
484 u8 elemlen;
485
486 if (len < 2)
487 return false;
488 len -= 2;
489
490 elemlen = pos[1];
491 if (elemlen > len)
492 return false;
493
494 len -= elemlen;
495 pos += 2 + elemlen;
496 }
497
498 return true;
499}
500
Johannes Berg55682962007-09-20 13:09:35 -0400501/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000502static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400503 int flags, u8 cmd)
504{
505 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000506 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400507}
508
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400509static int nl80211_msg_put_channel(struct sk_buff *msg,
510 struct ieee80211_channel *chan)
511{
David S. Miller9360ffd2012-03-29 04:41:26 -0400512 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
513 chan->center_freq))
514 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400515
David S. Miller9360ffd2012-03-29 04:41:26 -0400516 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
517 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
518 goto nla_put_failure;
519 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
520 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
521 goto nla_put_failure;
522 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
523 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
524 goto nla_put_failure;
525 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
526 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
527 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400528
David S. Miller9360ffd2012-03-29 04:41:26 -0400529 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
530 DBM_TO_MBM(chan->max_power)))
531 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400532
533 return 0;
534
535 nla_put_failure:
536 return -ENOBUFS;
537}
538
Johannes Berg55682962007-09-20 13:09:35 -0400539/* netlink command implementations */
540
Johannes Bergb9454e82009-07-08 13:29:08 +0200541struct key_parse {
542 struct key_params p;
543 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200544 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200545 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100546 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200547};
548
549static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
550{
551 struct nlattr *tb[NL80211_KEY_MAX + 1];
552 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
553 nl80211_key_policy);
554 if (err)
555 return err;
556
557 k->def = !!tb[NL80211_KEY_DEFAULT];
558 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
559
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100560 if (k->def) {
561 k->def_uni = true;
562 k->def_multi = true;
563 }
564 if (k->defmgmt)
565 k->def_multi = true;
566
Johannes Bergb9454e82009-07-08 13:29:08 +0200567 if (tb[NL80211_KEY_IDX])
568 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
569
570 if (tb[NL80211_KEY_DATA]) {
571 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
572 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
573 }
574
575 if (tb[NL80211_KEY_SEQ]) {
576 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
577 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
578 }
579
580 if (tb[NL80211_KEY_CIPHER])
581 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
582
Johannes Berge31b8212010-10-05 19:39:30 +0200583 if (tb[NL80211_KEY_TYPE]) {
584 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
585 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
586 return -EINVAL;
587 }
588
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100589 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
590 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100591 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
592 tb[NL80211_KEY_DEFAULT_TYPES],
593 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100594 if (err)
595 return err;
596
597 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
598 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
599 }
600
Johannes Bergb9454e82009-07-08 13:29:08 +0200601 return 0;
602}
603
604static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
605{
606 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
607 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
608 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
609 }
610
611 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
612 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
613 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
614 }
615
616 if (info->attrs[NL80211_ATTR_KEY_IDX])
617 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
618
619 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
620 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
621
622 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
623 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
624
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100625 if (k->def) {
626 k->def_uni = true;
627 k->def_multi = true;
628 }
629 if (k->defmgmt)
630 k->def_multi = true;
631
Johannes Berge31b8212010-10-05 19:39:30 +0200632 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
633 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
634 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
635 return -EINVAL;
636 }
637
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100638 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
639 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
640 int err = nla_parse_nested(
641 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
642 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
643 nl80211_key_default_policy);
644 if (err)
645 return err;
646
647 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
648 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
649 }
650
Johannes Bergb9454e82009-07-08 13:29:08 +0200651 return 0;
652}
653
654static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
655{
656 int err;
657
658 memset(k, 0, sizeof(*k));
659 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200660 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200661
662 if (info->attrs[NL80211_ATTR_KEY])
663 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
664 else
665 err = nl80211_parse_key_old(info, k);
666
667 if (err)
668 return err;
669
670 if (k->def && k->defmgmt)
671 return -EINVAL;
672
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100673 if (k->defmgmt) {
674 if (k->def_uni || !k->def_multi)
675 return -EINVAL;
676 }
677
Johannes Bergb9454e82009-07-08 13:29:08 +0200678 if (k->idx != -1) {
679 if (k->defmgmt) {
680 if (k->idx < 4 || k->idx > 5)
681 return -EINVAL;
682 } else if (k->def) {
683 if (k->idx < 0 || k->idx > 3)
684 return -EINVAL;
685 } else {
686 if (k->idx < 0 || k->idx > 5)
687 return -EINVAL;
688 }
689 }
690
691 return 0;
692}
693
Johannes Bergfffd0932009-07-08 14:22:54 +0200694static struct cfg80211_cached_keys *
695nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530696 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200697{
698 struct key_parse parse;
699 struct nlattr *key;
700 struct cfg80211_cached_keys *result;
701 int rem, err, def = 0;
702
703 result = kzalloc(sizeof(*result), GFP_KERNEL);
704 if (!result)
705 return ERR_PTR(-ENOMEM);
706
707 result->def = -1;
708 result->defmgmt = -1;
709
710 nla_for_each_nested(key, keys, rem) {
711 memset(&parse, 0, sizeof(parse));
712 parse.idx = -1;
713
714 err = nl80211_parse_key_new(key, &parse);
715 if (err)
716 goto error;
717 err = -EINVAL;
718 if (!parse.p.key)
719 goto error;
720 if (parse.idx < 0 || parse.idx > 4)
721 goto error;
722 if (parse.def) {
723 if (def)
724 goto error;
725 def = 1;
726 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100727 if (!parse.def_uni || !parse.def_multi)
728 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200729 } else if (parse.defmgmt)
730 goto error;
731 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200732 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200733 if (err)
734 goto error;
735 result->params[parse.idx].cipher = parse.p.cipher;
736 result->params[parse.idx].key_len = parse.p.key_len;
737 result->params[parse.idx].key = result->data[parse.idx];
738 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530739
740 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
741 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
742 if (no_ht)
743 *no_ht = true;
744 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200745 }
746
747 return result;
748 error:
749 kfree(result);
750 return ERR_PTR(err);
751}
752
753static int nl80211_key_allowed(struct wireless_dev *wdev)
754{
755 ASSERT_WDEV_LOCK(wdev);
756
Johannes Bergfffd0932009-07-08 14:22:54 +0200757 switch (wdev->iftype) {
758 case NL80211_IFTYPE_AP:
759 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200760 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700761 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200762 break;
763 case NL80211_IFTYPE_ADHOC:
764 if (!wdev->current_bss)
765 return -ENOLINK;
766 break;
767 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200768 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200769 if (wdev->sme_state != CFG80211_SME_CONNECTED)
770 return -ENOLINK;
771 break;
772 default:
773 return -EINVAL;
774 }
775
776 return 0;
777}
778
Johannes Berg7527a782011-05-13 10:58:57 +0200779static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
780{
781 struct nlattr *nl_modes = nla_nest_start(msg, attr);
782 int i;
783
784 if (!nl_modes)
785 goto nla_put_failure;
786
787 i = 0;
788 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400789 if ((ifmodes & 1) && nla_put_flag(msg, i))
790 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200791 ifmodes >>= 1;
792 i++;
793 }
794
795 nla_nest_end(msg, nl_modes);
796 return 0;
797
798nla_put_failure:
799 return -ENOBUFS;
800}
801
802static int nl80211_put_iface_combinations(struct wiphy *wiphy,
803 struct sk_buff *msg)
804{
805 struct nlattr *nl_combis;
806 int i, j;
807
808 nl_combis = nla_nest_start(msg,
809 NL80211_ATTR_INTERFACE_COMBINATIONS);
810 if (!nl_combis)
811 goto nla_put_failure;
812
813 for (i = 0; i < wiphy->n_iface_combinations; i++) {
814 const struct ieee80211_iface_combination *c;
815 struct nlattr *nl_combi, *nl_limits;
816
817 c = &wiphy->iface_combinations[i];
818
819 nl_combi = nla_nest_start(msg, i + 1);
820 if (!nl_combi)
821 goto nla_put_failure;
822
823 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
824 if (!nl_limits)
825 goto nla_put_failure;
826
827 for (j = 0; j < c->n_limits; j++) {
828 struct nlattr *nl_limit;
829
830 nl_limit = nla_nest_start(msg, j + 1);
831 if (!nl_limit)
832 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400833 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
834 c->limits[j].max))
835 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200836 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
837 c->limits[j].types))
838 goto nla_put_failure;
839 nla_nest_end(msg, nl_limit);
840 }
841
842 nla_nest_end(msg, nl_limits);
843
David S. Miller9360ffd2012-03-29 04:41:26 -0400844 if (c->beacon_int_infra_match &&
845 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
846 goto nla_put_failure;
847 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
848 c->num_different_channels) ||
849 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
850 c->max_interfaces))
851 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200852
853 nla_nest_end(msg, nl_combi);
854 }
855
856 nla_nest_end(msg, nl_combis);
857
858 return 0;
859nla_put_failure:
860 return -ENOBUFS;
861}
862
Eric W. Biederman15e47302012-09-07 20:12:54 +0000863static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400864 struct cfg80211_registered_device *dev)
865{
866 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100867 struct nlattr *nl_bands, *nl_band;
868 struct nlattr *nl_freqs, *nl_freq;
869 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100870 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100871 enum ieee80211_band band;
872 struct ieee80211_channel *chan;
873 struct ieee80211_rate *rate;
874 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200875 const struct ieee80211_txrx_stypes *mgmt_stypes =
876 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400877
Eric W. Biederman15e47302012-09-07 20:12:54 +0000878 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400879 if (!hdr)
880 return -1;
881
David S. Miller9360ffd2012-03-29 04:41:26 -0400882 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
883 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
884 nla_put_u32(msg, NL80211_ATTR_GENERATION,
885 cfg80211_rdev_list_generation) ||
886 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
887 dev->wiphy.retry_short) ||
888 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
889 dev->wiphy.retry_long) ||
890 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
891 dev->wiphy.frag_threshold) ||
892 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
893 dev->wiphy.rts_threshold) ||
894 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
895 dev->wiphy.coverage_class) ||
896 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
897 dev->wiphy.max_scan_ssids) ||
898 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
899 dev->wiphy.max_sched_scan_ssids) ||
900 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
901 dev->wiphy.max_scan_ie_len) ||
902 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
903 dev->wiphy.max_sched_scan_ie_len) ||
904 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
905 dev->wiphy.max_match_sets))
906 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200907
David S. Miller9360ffd2012-03-29 04:41:26 -0400908 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
909 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
910 goto nla_put_failure;
911 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
912 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
913 goto nla_put_failure;
914 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
915 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
916 goto nla_put_failure;
917 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
918 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
919 goto nla_put_failure;
920 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
921 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
922 goto nla_put_failure;
923 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
924 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
925 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200926
David S. Miller9360ffd2012-03-29 04:41:26 -0400927 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
928 sizeof(u32) * dev->wiphy.n_cipher_suites,
929 dev->wiphy.cipher_suites))
930 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100931
David S. Miller9360ffd2012-03-29 04:41:26 -0400932 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
933 dev->wiphy.max_num_pmkids))
934 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530935
David S. Miller9360ffd2012-03-29 04:41:26 -0400936 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
937 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
938 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200939
David S. Miller9360ffd2012-03-29 04:41:26 -0400940 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
941 dev->wiphy.available_antennas_tx) ||
942 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
943 dev->wiphy.available_antennas_rx))
944 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100945
David S. Miller9360ffd2012-03-29 04:41:26 -0400946 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
947 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
948 dev->wiphy.probe_resp_offload))
949 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200950
Bruno Randolf7f531e02010-12-16 11:30:22 +0900951 if ((dev->wiphy.available_antennas_tx ||
952 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900953 u32 tx_ant = 0, rx_ant = 0;
954 int res;
Hila Gonene35e4d22012-06-27 17:19:42 +0300955 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900956 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400957 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
958 tx_ant) ||
959 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
960 rx_ant))
961 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900962 }
963 }
964
Johannes Berg7527a782011-05-13 10:58:57 +0200965 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
966 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700967 goto nla_put_failure;
968
Johannes Bergee688b002008-01-24 19:38:39 +0100969 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
970 if (!nl_bands)
971 goto nla_put_failure;
972
973 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
974 if (!dev->wiphy.bands[band])
975 continue;
976
977 nl_band = nla_nest_start(msg, band);
978 if (!nl_band)
979 goto nla_put_failure;
980
Johannes Bergd51626d2008-10-09 12:20:13 +0200981 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400982 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
983 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
984 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
985 &dev->wiphy.bands[band]->ht_cap.mcs) ||
986 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
987 dev->wiphy.bands[band]->ht_cap.cap) ||
988 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
989 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
990 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
991 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
992 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200993
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000994 /* add VHT info */
995 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
996 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
997 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
998 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
999 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1000 dev->wiphy.bands[band]->vht_cap.cap)))
1001 goto nla_put_failure;
1002
Johannes Bergee688b002008-01-24 19:38:39 +01001003 /* add frequencies */
1004 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1005 if (!nl_freqs)
1006 goto nla_put_failure;
1007
1008 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1009 nl_freq = nla_nest_start(msg, i);
1010 if (!nl_freq)
1011 goto nla_put_failure;
1012
1013 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001014
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001015 if (nl80211_msg_put_channel(msg, chan))
1016 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001017
Johannes Bergee688b002008-01-24 19:38:39 +01001018 nla_nest_end(msg, nl_freq);
1019 }
1020
1021 nla_nest_end(msg, nl_freqs);
1022
1023 /* add bitrates */
1024 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1025 if (!nl_rates)
1026 goto nla_put_failure;
1027
1028 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1029 nl_rate = nla_nest_start(msg, i);
1030 if (!nl_rate)
1031 goto nla_put_failure;
1032
1033 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001034 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1035 rate->bitrate))
1036 goto nla_put_failure;
1037 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1038 nla_put_flag(msg,
1039 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1040 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001041
1042 nla_nest_end(msg, nl_rate);
1043 }
1044
1045 nla_nest_end(msg, nl_rates);
1046
1047 nla_nest_end(msg, nl_band);
1048 }
1049 nla_nest_end(msg, nl_bands);
1050
Johannes Berg8fdc6212009-03-14 09:34:01 +01001051 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1052 if (!nl_cmds)
1053 goto nla_put_failure;
1054
1055 i = 0;
1056#define CMD(op, n) \
1057 do { \
1058 if (dev->ops->op) { \
1059 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001060 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1061 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001062 } \
1063 } while (0)
1064
1065 CMD(add_virtual_intf, NEW_INTERFACE);
1066 CMD(change_virtual_intf, SET_INTERFACE);
1067 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001068 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001069 CMD(add_station, NEW_STATION);
1070 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001071 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001072 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001073 CMD(auth, AUTHENTICATE);
1074 CMD(assoc, ASSOCIATE);
1075 CMD(deauth, DEAUTHENTICATE);
1076 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001077 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001078 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001079 CMD(set_pmksa, SET_PMKSA);
1080 CMD(del_pmksa, DEL_PMKSA);
1081 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001082 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1083 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001084 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001085 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001086 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001087 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001088 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001089 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1090 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001091 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001092 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001093 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001094 i++;
1095 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1096 goto nla_put_failure;
1097 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001098 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001099 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1100 CMD(tdls_mgmt, TDLS_MGMT);
1101 CMD(tdls_oper, TDLS_OPER);
1102 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001103 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1104 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001105 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001106 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001107 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1108 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001109 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1110 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001111 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001112 CMD(start_p2p_device, START_P2P_DEVICE);
Antonio Quartullif4e583c2012-11-02 13:27:48 +01001113 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001114
Kalle Valo4745fc02011-11-17 19:06:10 +02001115#ifdef CONFIG_NL80211_TESTMODE
1116 CMD(testmode_cmd, TESTMODE);
1117#endif
1118
Johannes Berg8fdc6212009-03-14 09:34:01 +01001119#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001120
Johannes 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 Berg2e161f72010-08-12 15:38:38 +02001145 if (mgmt_stypes) {
1146 u16 stypes;
1147 struct nlattr *nl_ftypes, *nl_ifs;
1148 enum nl80211_iftype ift;
1149
1150 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1151 if (!nl_ifs)
1152 goto nla_put_failure;
1153
1154 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1155 nl_ftypes = nla_nest_start(msg, ift);
1156 if (!nl_ftypes)
1157 goto nla_put_failure;
1158 i = 0;
1159 stypes = mgmt_stypes[ift].tx;
1160 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001161 if ((stypes & 1) &&
1162 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1163 (i << 4) | IEEE80211_FTYPE_MGMT))
1164 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001165 stypes >>= 1;
1166 i++;
1167 }
1168 nla_nest_end(msg, nl_ftypes);
1169 }
1170
Johannes Berg74b70a42010-08-24 12:15:53 +02001171 nla_nest_end(msg, nl_ifs);
1172
Johannes Berg2e161f72010-08-12 15:38:38 +02001173 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1174 if (!nl_ifs)
1175 goto nla_put_failure;
1176
1177 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1178 nl_ftypes = nla_nest_start(msg, ift);
1179 if (!nl_ftypes)
1180 goto nla_put_failure;
1181 i = 0;
1182 stypes = mgmt_stypes[ift].rx;
1183 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001184 if ((stypes & 1) &&
1185 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1186 (i << 4) | IEEE80211_FTYPE_MGMT))
1187 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001188 stypes >>= 1;
1189 i++;
1190 }
1191 nla_nest_end(msg, nl_ftypes);
1192 }
1193 nla_nest_end(msg, nl_ifs);
1194 }
1195
Johannes Bergdfb89c52012-06-27 09:23:48 +02001196#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001197 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1198 struct nlattr *nl_wowlan;
1199
1200 nl_wowlan = nla_nest_start(msg,
1201 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1202 if (!nl_wowlan)
1203 goto nla_put_failure;
1204
David S. Miller9360ffd2012-03-29 04:41:26 -04001205 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1206 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1207 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1208 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1209 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1210 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1211 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1212 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1213 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1214 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1215 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1216 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1217 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1218 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1219 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1220 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1221 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001222 if (dev->wiphy.wowlan.n_patterns) {
1223 struct nl80211_wowlan_pattern_support pat = {
1224 .max_patterns = dev->wiphy.wowlan.n_patterns,
1225 .min_pattern_len =
1226 dev->wiphy.wowlan.pattern_min_len,
1227 .max_pattern_len =
1228 dev->wiphy.wowlan.pattern_max_len,
1229 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001230 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1231 sizeof(pat), &pat))
1232 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001233 }
1234
1235 nla_nest_end(msg, nl_wowlan);
1236 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001237#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001238
Johannes Berg7527a782011-05-13 10:58:57 +02001239 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1240 dev->wiphy.software_iftypes))
1241 goto nla_put_failure;
1242
1243 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1244 goto nla_put_failure;
1245
David S. Miller9360ffd2012-03-29 04:41:26 -04001246 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1247 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1248 dev->wiphy.ap_sme_capa))
1249 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001250
David S. Miller9360ffd2012-03-29 04:41:26 -04001251 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1252 dev->wiphy.features))
1253 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001254
David S. Miller9360ffd2012-03-29 04:41:26 -04001255 if (dev->wiphy.ht_capa_mod_mask &&
1256 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1257 sizeof(*dev->wiphy.ht_capa_mod_mask),
1258 dev->wiphy.ht_capa_mod_mask))
1259 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001260
Johannes Berg55682962007-09-20 13:09:35 -04001261 return genlmsg_end(msg, hdr);
1262
1263 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001264 genlmsg_cancel(msg, hdr);
1265 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001266}
1267
1268static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1269{
1270 int idx = 0;
1271 int start = cb->args[0];
1272 struct cfg80211_registered_device *dev;
1273
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001274 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001275 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001276 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1277 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001278 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001279 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001280 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001281 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001282 dev) < 0) {
1283 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001284 break;
Julius Volzb4637272008-07-08 14:02:19 +02001285 }
Johannes Berg55682962007-09-20 13:09:35 -04001286 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001287 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001288
1289 cb->args[0] = idx;
1290
1291 return skb->len;
1292}
1293
1294static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1295{
1296 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001297 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001298
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001299 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001300 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001301 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001302
Eric W. Biederman15e47302012-09-07 20:12:54 +00001303 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001304 nlmsg_free(msg);
1305 return -ENOBUFS;
1306 }
Johannes Berg55682962007-09-20 13:09:35 -04001307
Johannes Berg134e6372009-07-10 09:51:34 +00001308 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001309}
1310
Jouni Malinen31888482008-10-30 16:59:24 +02001311static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1312 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1313 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1314 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1315 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1316 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1317};
1318
1319static int parse_txq_params(struct nlattr *tb[],
1320 struct ieee80211_txq_params *txq_params)
1321{
Johannes Berga3304b02012-03-28 11:04:24 +02001322 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001323 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1324 !tb[NL80211_TXQ_ATTR_AIFS])
1325 return -EINVAL;
1326
Johannes Berga3304b02012-03-28 11:04:24 +02001327 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001328 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1329 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1330 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1331 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1332
Johannes Berga3304b02012-03-28 11:04:24 +02001333 if (txq_params->ac >= NL80211_NUM_ACS)
1334 return -EINVAL;
1335
Jouni Malinen31888482008-10-30 16:59:24 +02001336 return 0;
1337}
1338
Johannes Bergf444de02010-05-05 15:25:02 +02001339static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1340{
1341 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001342 * You can only set the channel explicitly for WDS interfaces,
1343 * all others have their channel managed via their respective
1344 * "establish a connection" command (connect, join, ...)
1345 *
1346 * For AP/GO and mesh mode, the channel can be set with the
1347 * channel userspace API, but is only stored and passed to the
1348 * low-level driver when the AP starts or the mesh is joined.
1349 * This is for backward compatibility, userspace can also give
1350 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001351 *
1352 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001353 * whatever else is going on, so they have their own special
1354 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001355 */
1356 return !wdev ||
1357 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001358 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001359 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1360 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001361}
1362
Johannes Bergcd6c6592012-05-10 21:27:18 +02001363static bool nl80211_valid_channel_type(struct genl_info *info,
1364 enum nl80211_channel_type *channel_type)
1365{
1366 enum nl80211_channel_type tmp;
1367
1368 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1369 return false;
1370
1371 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1372 if (tmp != NL80211_CHAN_NO_HT &&
1373 tmp != NL80211_CHAN_HT20 &&
1374 tmp != NL80211_CHAN_HT40PLUS &&
1375 tmp != NL80211_CHAN_HT40MINUS)
1376 return false;
1377
1378 if (channel_type)
1379 *channel_type = tmp;
1380
1381 return true;
1382}
1383
Johannes Berg683b6d32012-11-08 21:25:48 +01001384static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1385 struct genl_info *info,
1386 struct cfg80211_chan_def *chandef)
1387{
1388 struct ieee80211_sta_ht_cap *ht_cap;
1389 struct ieee80211_channel *sc;
1390 u32 control_freq;
1391 int offs;
1392
1393 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1394 return -EINVAL;
1395
1396 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1397
1398 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
1399 chandef->_type = NL80211_CHAN_NO_HT;
1400
1401 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1402 !nl80211_valid_channel_type(info, &chandef->_type))
1403 return -EINVAL;
1404
1405 /* Primary channel not allowed */
1406 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1407 return -EINVAL;
1408
1409 ht_cap = &rdev->wiphy.bands[chandef->chan->band]->ht_cap;
1410
1411 switch (chandef->_type) {
1412 case NL80211_CHAN_NO_HT:
1413 break;
1414 case NL80211_CHAN_HT40MINUS:
1415 if (chandef->chan->flags & IEEE80211_CHAN_NO_HT40MINUS)
1416 return -EINVAL;
1417 offs = -20;
1418 /* fall through */
1419 case NL80211_CHAN_HT40PLUS:
1420 if (chandef->_type == NL80211_CHAN_HT40PLUS) {
1421 if (chandef->chan->flags & IEEE80211_CHAN_NO_HT40PLUS)
1422 return -EINVAL;
1423 offs = 20;
1424 }
1425 if (!(ht_cap->cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) ||
1426 ht_cap->cap & IEEE80211_HT_CAP_40MHZ_INTOLERANT)
1427 return -EINVAL;
1428
1429 sc = ieee80211_get_channel(&rdev->wiphy,
1430 chandef->chan->center_freq + offs);
1431 if (!sc || sc->flags & IEEE80211_CHAN_DISABLED)
1432 return -EINVAL;
1433 /* fall through */
1434 case NL80211_CHAN_HT20:
1435 if (!ht_cap->ht_supported)
1436 return -EINVAL;
1437 break;
1438 }
1439
1440 return 0;
1441}
1442
Johannes Bergf444de02010-05-05 15:25:02 +02001443static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1444 struct wireless_dev *wdev,
1445 struct genl_info *info)
1446{
Johannes Berg683b6d32012-11-08 21:25:48 +01001447 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001448 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001449 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1450
1451 if (wdev)
1452 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001453
Johannes Bergf444de02010-05-05 15:25:02 +02001454 if (!nl80211_can_set_dev_channel(wdev))
1455 return -EOPNOTSUPP;
1456
Johannes Berg683b6d32012-11-08 21:25:48 +01001457 result = nl80211_parse_chandef(rdev, info, &chandef);
1458 if (result)
1459 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001460
1461 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001462 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001463 case NL80211_IFTYPE_AP:
1464 case NL80211_IFTYPE_P2P_GO:
1465 if (wdev->beacon_interval) {
1466 result = -EBUSY;
1467 break;
1468 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001469 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001470 result = -EINVAL;
1471 break;
1472 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001473 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001474 result = 0;
1475 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001476 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001477 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001478 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001479 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001480 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001481 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001482 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001483 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001484 }
1485 mutex_unlock(&rdev->devlist_mtx);
1486
1487 return result;
1488}
1489
1490static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1491{
Johannes Berg4c476992010-10-04 21:36:35 +02001492 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1493 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001494
Johannes Berg4c476992010-10-04 21:36:35 +02001495 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001496}
1497
Bill Jordane8347eb2010-10-01 13:54:28 -04001498static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1499{
Johannes Berg43b19952010-10-07 13:10:30 +02001500 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1501 struct net_device *dev = info->user_ptr[1];
1502 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001503 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001504
1505 if (!info->attrs[NL80211_ATTR_MAC])
1506 return -EINVAL;
1507
Johannes Berg43b19952010-10-07 13:10:30 +02001508 if (netif_running(dev))
1509 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001510
Johannes Berg43b19952010-10-07 13:10:30 +02001511 if (!rdev->ops->set_wds_peer)
1512 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001513
Johannes Berg43b19952010-10-07 13:10:30 +02001514 if (wdev->iftype != NL80211_IFTYPE_WDS)
1515 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001516
1517 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001518 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001519}
1520
1521
Johannes Berg55682962007-09-20 13:09:35 -04001522static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1523{
1524 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001525 struct net_device *netdev = NULL;
1526 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001527 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001528 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001529 u32 changed;
1530 u8 retry_short = 0, retry_long = 0;
1531 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001532 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001533
Johannes Bergf444de02010-05-05 15:25:02 +02001534 /*
1535 * Try to find the wiphy and netdev. Normally this
1536 * function shouldn't need the netdev, but this is
1537 * done for backward compatibility -- previously
1538 * setting the channel was done per wiphy, but now
1539 * it is per netdev. Previous userland like hostapd
1540 * also passed a netdev to set_wiphy, so that it is
1541 * possible to let that go to the right netdev!
1542 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001543 mutex_lock(&cfg80211_mutex);
1544
Johannes Bergf444de02010-05-05 15:25:02 +02001545 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1546 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1547
1548 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1549 if (netdev && netdev->ieee80211_ptr) {
1550 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1551 mutex_lock(&rdev->mtx);
1552 } else
1553 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001554 }
1555
Johannes Bergf444de02010-05-05 15:25:02 +02001556 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001557 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1558 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001559 if (IS_ERR(rdev)) {
1560 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001561 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001562 }
1563 wdev = NULL;
1564 netdev = NULL;
1565 result = 0;
1566
1567 mutex_lock(&rdev->mtx);
Johannes Berg71fe96b2012-10-24 10:04:58 +02001568 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001569 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001570
1571 /*
1572 * end workaround code, by now the rdev is available
1573 * and locked, and wdev may or may not be NULL.
1574 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001575
1576 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001577 result = cfg80211_dev_rename(
1578 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001579
1580 mutex_unlock(&cfg80211_mutex);
1581
1582 if (result)
1583 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001584
Jouni Malinen31888482008-10-30 16:59:24 +02001585 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1586 struct ieee80211_txq_params txq_params;
1587 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1588
1589 if (!rdev->ops->set_txq_params) {
1590 result = -EOPNOTSUPP;
1591 goto bad_res;
1592 }
1593
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001594 if (!netdev) {
1595 result = -EINVAL;
1596 goto bad_res;
1597 }
1598
Johannes Berg133a3ff2011-11-03 14:50:13 +01001599 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1600 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1601 result = -EINVAL;
1602 goto bad_res;
1603 }
1604
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001605 if (!netif_running(netdev)) {
1606 result = -ENETDOWN;
1607 goto bad_res;
1608 }
1609
Jouni Malinen31888482008-10-30 16:59:24 +02001610 nla_for_each_nested(nl_txq_params,
1611 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1612 rem_txq_params) {
1613 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1614 nla_data(nl_txq_params),
1615 nla_len(nl_txq_params),
1616 txq_params_policy);
1617 result = parse_txq_params(tb, &txq_params);
1618 if (result)
1619 goto bad_res;
1620
Hila Gonene35e4d22012-06-27 17:19:42 +03001621 result = rdev_set_txq_params(rdev, netdev,
1622 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001623 if (result)
1624 goto bad_res;
1625 }
1626 }
1627
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001628 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001629 result = __nl80211_set_channel(rdev,
1630 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1631 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001632 if (result)
1633 goto bad_res;
1634 }
1635
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001636 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001637 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001638 enum nl80211_tx_power_setting type;
1639 int idx, mbm = 0;
1640
Johannes Bergc8442112012-10-24 10:17:18 +02001641 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1642 txp_wdev = NULL;
1643
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001644 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001645 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001646 goto bad_res;
1647 }
1648
1649 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1650 type = nla_get_u32(info->attrs[idx]);
1651
1652 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1653 (type != NL80211_TX_POWER_AUTOMATIC)) {
1654 result = -EINVAL;
1655 goto bad_res;
1656 }
1657
1658 if (type != NL80211_TX_POWER_AUTOMATIC) {
1659 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1660 mbm = nla_get_u32(info->attrs[idx]);
1661 }
1662
Johannes Bergc8442112012-10-24 10:17:18 +02001663 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001664 if (result)
1665 goto bad_res;
1666 }
1667
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001668 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1669 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1670 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001671 if ((!rdev->wiphy.available_antennas_tx &&
1672 !rdev->wiphy.available_antennas_rx) ||
1673 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001674 result = -EOPNOTSUPP;
1675 goto bad_res;
1676 }
1677
1678 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1679 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1680
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001681 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001682 * available antenna masks, except for the "all" mask */
1683 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1684 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001685 result = -EINVAL;
1686 goto bad_res;
1687 }
1688
Bruno Randolf7f531e02010-12-16 11:30:22 +09001689 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1690 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001691
Hila Gonene35e4d22012-06-27 17:19:42 +03001692 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001693 if (result)
1694 goto bad_res;
1695 }
1696
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001697 changed = 0;
1698
1699 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1700 retry_short = nla_get_u8(
1701 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1702 if (retry_short == 0) {
1703 result = -EINVAL;
1704 goto bad_res;
1705 }
1706 changed |= WIPHY_PARAM_RETRY_SHORT;
1707 }
1708
1709 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1710 retry_long = nla_get_u8(
1711 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1712 if (retry_long == 0) {
1713 result = -EINVAL;
1714 goto bad_res;
1715 }
1716 changed |= WIPHY_PARAM_RETRY_LONG;
1717 }
1718
1719 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1720 frag_threshold = nla_get_u32(
1721 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1722 if (frag_threshold < 256) {
1723 result = -EINVAL;
1724 goto bad_res;
1725 }
1726 if (frag_threshold != (u32) -1) {
1727 /*
1728 * Fragments (apart from the last one) are required to
1729 * have even length. Make the fragmentation code
1730 * simpler by stripping LSB should someone try to use
1731 * odd threshold value.
1732 */
1733 frag_threshold &= ~0x1;
1734 }
1735 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1736 }
1737
1738 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1739 rts_threshold = nla_get_u32(
1740 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1741 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1742 }
1743
Lukáš Turek81077e82009-12-21 22:50:47 +01001744 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1745 coverage_class = nla_get_u8(
1746 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1747 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1748 }
1749
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001750 if (changed) {
1751 u8 old_retry_short, old_retry_long;
1752 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001753 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001754
1755 if (!rdev->ops->set_wiphy_params) {
1756 result = -EOPNOTSUPP;
1757 goto bad_res;
1758 }
1759
1760 old_retry_short = rdev->wiphy.retry_short;
1761 old_retry_long = rdev->wiphy.retry_long;
1762 old_frag_threshold = rdev->wiphy.frag_threshold;
1763 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001764 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001765
1766 if (changed & WIPHY_PARAM_RETRY_SHORT)
1767 rdev->wiphy.retry_short = retry_short;
1768 if (changed & WIPHY_PARAM_RETRY_LONG)
1769 rdev->wiphy.retry_long = retry_long;
1770 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1771 rdev->wiphy.frag_threshold = frag_threshold;
1772 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1773 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001774 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1775 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001776
Hila Gonene35e4d22012-06-27 17:19:42 +03001777 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001778 if (result) {
1779 rdev->wiphy.retry_short = old_retry_short;
1780 rdev->wiphy.retry_long = old_retry_long;
1781 rdev->wiphy.frag_threshold = old_frag_threshold;
1782 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001783 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001784 }
1785 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001786
Johannes Berg306d6112008-12-08 12:39:04 +01001787 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001788 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001789 if (netdev)
1790 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001791 return result;
1792}
1793
Johannes Berg71bbc992012-06-15 15:30:18 +02001794static inline u64 wdev_id(struct wireless_dev *wdev)
1795{
1796 return (u64)wdev->identifier |
1797 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1798}
Johannes Berg55682962007-09-20 13:09:35 -04001799
Johannes Berg683b6d32012-11-08 21:25:48 +01001800static int nl80211_send_chandef(struct sk_buff *msg,
1801 struct cfg80211_chan_def *chandef)
1802{
1803 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1804 chandef->chan->center_freq))
1805 return -ENOBUFS;
1806 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, chandef->_type))
1807 return -ENOBUFS;
1808 return 0;
1809}
1810
Eric W. Biederman15e47302012-09-07 20:12:54 +00001811static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001812 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001813 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001814{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001815 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001816 void *hdr;
1817
Eric W. Biederman15e47302012-09-07 20:12:54 +00001818 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001819 if (!hdr)
1820 return -1;
1821
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001822 if (dev &&
1823 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001824 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001825 goto nla_put_failure;
1826
1827 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1828 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001829 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001830 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001831 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1832 rdev->devlist_generation ^
1833 (cfg80211_rdev_list_generation << 2)))
1834 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001835
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001836 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01001837 int ret;
1838 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001839
Johannes Berg683b6d32012-11-08 21:25:48 +01001840 ret = rdev_get_channel(rdev, wdev, &chandef);
1841 if (ret == 0) {
1842 if (nl80211_send_chandef(msg, &chandef))
1843 goto nla_put_failure;
1844 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001845 }
1846
Antonio Quartullib84e7a02012-11-07 12:52:20 +01001847 if (wdev->ssid_len) {
1848 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
1849 goto nla_put_failure;
1850 }
1851
Johannes Berg55682962007-09-20 13:09:35 -04001852 return genlmsg_end(msg, hdr);
1853
1854 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001855 genlmsg_cancel(msg, hdr);
1856 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001857}
1858
1859static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1860{
1861 int wp_idx = 0;
1862 int if_idx = 0;
1863 int wp_start = cb->args[0];
1864 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001865 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001866 struct wireless_dev *wdev;
1867
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001868 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001869 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1870 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001871 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001872 if (wp_idx < wp_start) {
1873 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001874 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001875 }
Johannes Berg55682962007-09-20 13:09:35 -04001876 if_idx = 0;
1877
Johannes Bergf5ea9122009-08-07 16:17:38 +02001878 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001879 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001880 if (if_idx < if_start) {
1881 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001882 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001883 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001884 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001885 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001886 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001887 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001888 goto out;
1889 }
1890 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001891 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001892 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001893
1894 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001895 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001896 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001897 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001898
1899 cb->args[0] = wp_idx;
1900 cb->args[1] = if_idx;
1901
1902 return skb->len;
1903}
1904
1905static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1906{
1907 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001908 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001909 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001910
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001911 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001912 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001913 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001914
Eric W. Biederman15e47302012-09-07 20:12:54 +00001915 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001916 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001917 nlmsg_free(msg);
1918 return -ENOBUFS;
1919 }
Johannes Berg55682962007-09-20 13:09:35 -04001920
Johannes Berg134e6372009-07-10 09:51:34 +00001921 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001922}
1923
Michael Wu66f7ac52008-01-31 19:48:22 +01001924static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1925 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1926 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1927 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1928 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1929 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1930};
1931
1932static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1933{
1934 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1935 int flag;
1936
1937 *mntrflags = 0;
1938
1939 if (!nla)
1940 return -EINVAL;
1941
1942 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1943 nla, mntr_flags_policy))
1944 return -EINVAL;
1945
1946 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1947 if (flags[flag])
1948 *mntrflags |= (1<<flag);
1949
1950 return 0;
1951}
1952
Johannes Berg9bc383d2009-11-19 11:55:19 +01001953static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001954 struct net_device *netdev, u8 use_4addr,
1955 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001956{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001957 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001958 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001959 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001960 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001961 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001962
1963 switch (iftype) {
1964 case NL80211_IFTYPE_AP_VLAN:
1965 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1966 return 0;
1967 break;
1968 case NL80211_IFTYPE_STATION:
1969 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1970 return 0;
1971 break;
1972 default:
1973 break;
1974 }
1975
1976 return -EOPNOTSUPP;
1977}
1978
Johannes Berg55682962007-09-20 13:09:35 -04001979static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1980{
Johannes Berg4c476992010-10-04 21:36:35 +02001981 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001982 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001983 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001984 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001985 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001986 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001987 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001988
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001989 memset(&params, 0, sizeof(params));
1990
Johannes Berg04a773a2009-04-19 21:24:32 +02001991 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001992
Johannes Berg723b0382008-09-16 20:22:09 +02001993 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001994 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001995 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001996 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001997 if (ntype > NL80211_IFTYPE_MAX)
1998 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001999 }
2000
Johannes Berg92ffe052008-09-16 20:39:36 +02002001 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002002 struct wireless_dev *wdev = dev->ieee80211_ptr;
2003
Johannes Berg4c476992010-10-04 21:36:35 +02002004 if (ntype != NL80211_IFTYPE_MESH_POINT)
2005 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002006 if (netif_running(dev))
2007 return -EBUSY;
2008
2009 wdev_lock(wdev);
2010 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2011 IEEE80211_MAX_MESH_ID_LEN);
2012 wdev->mesh_id_up_len =
2013 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2014 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2015 wdev->mesh_id_up_len);
2016 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002017 }
2018
Felix Fietkau8b787642009-11-10 18:53:10 +01002019 if (info->attrs[NL80211_ATTR_4ADDR]) {
2020 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2021 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002022 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002023 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002024 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002025 } else {
2026 params.use_4addr = -1;
2027 }
2028
Johannes Berg92ffe052008-09-16 20:39:36 +02002029 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002030 if (ntype != NL80211_IFTYPE_MONITOR)
2031 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002032 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2033 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002034 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002035 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002036
2037 flags = &_flags;
2038 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002039 }
Johannes Berg3b858752009-03-12 09:55:09 +01002040
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002041 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002042 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002043 else
2044 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002045
Johannes Berg9bc383d2009-11-19 11:55:19 +01002046 if (!err && params.use_4addr != -1)
2047 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2048
Johannes Berg55682962007-09-20 13:09:35 -04002049 return err;
2050}
2051
2052static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2053{
Johannes Berg4c476992010-10-04 21:36:35 +02002054 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002055 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002056 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002057 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002058 int err;
2059 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002060 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002061
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002062 memset(&params, 0, sizeof(params));
2063
Johannes Berg55682962007-09-20 13:09:35 -04002064 if (!info->attrs[NL80211_ATTR_IFNAME])
2065 return -EINVAL;
2066
2067 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2068 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2069 if (type > NL80211_IFTYPE_MAX)
2070 return -EINVAL;
2071 }
2072
Johannes Berg79c97e92009-07-07 03:56:12 +02002073 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002074 !(rdev->wiphy.interface_modes & (1 << type)))
2075 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002076
Johannes Berg9bc383d2009-11-19 11:55:19 +01002077 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002078 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002079 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002080 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002081 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002082 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002083
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002084 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2085 if (!msg)
2086 return -ENOMEM;
2087
Michael Wu66f7ac52008-01-31 19:48:22 +01002088 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2089 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2090 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002091 wdev = rdev_add_virtual_intf(rdev,
2092 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2093 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002094 if (IS_ERR(wdev)) {
2095 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002096 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002097 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002098
Johannes Berg98104fde2012-06-16 00:19:54 +02002099 switch (type) {
2100 case NL80211_IFTYPE_MESH_POINT:
2101 if (!info->attrs[NL80211_ATTR_MESH_ID])
2102 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002103 wdev_lock(wdev);
2104 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2105 IEEE80211_MAX_MESH_ID_LEN);
2106 wdev->mesh_id_up_len =
2107 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2108 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2109 wdev->mesh_id_up_len);
2110 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002111 break;
2112 case NL80211_IFTYPE_P2P_DEVICE:
2113 /*
2114 * P2P Device doesn't have a netdev, so doesn't go
2115 * through the netdev notifier and must be added here
2116 */
2117 mutex_init(&wdev->mtx);
2118 INIT_LIST_HEAD(&wdev->event_list);
2119 spin_lock_init(&wdev->event_lock);
2120 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2121 spin_lock_init(&wdev->mgmt_registrations_lock);
2122
2123 mutex_lock(&rdev->devlist_mtx);
2124 wdev->identifier = ++rdev->wdev_id;
2125 list_add_rcu(&wdev->list, &rdev->wdev_list);
2126 rdev->devlist_generation++;
2127 mutex_unlock(&rdev->devlist_mtx);
2128 break;
2129 default:
2130 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002131 }
2132
Eric W. Biederman15e47302012-09-07 20:12:54 +00002133 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002134 rdev, wdev) < 0) {
2135 nlmsg_free(msg);
2136 return -ENOBUFS;
2137 }
2138
2139 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002140}
2141
2142static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2143{
Johannes Berg4c476992010-10-04 21:36:35 +02002144 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002145 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002146
Johannes Berg4c476992010-10-04 21:36:35 +02002147 if (!rdev->ops->del_virtual_intf)
2148 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002149
Johannes Berg84efbb82012-06-16 00:00:26 +02002150 /*
2151 * If we remove a wireless device without a netdev then clear
2152 * user_ptr[1] so that nl80211_post_doit won't dereference it
2153 * to check if it needs to do dev_put(). Otherwise it crashes
2154 * since the wdev has been freed, unlike with a netdev where
2155 * we need the dev_put() for the netdev to really be freed.
2156 */
2157 if (!wdev->netdev)
2158 info->user_ptr[1] = NULL;
2159
Hila Gonene35e4d22012-06-27 17:19:42 +03002160 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002161}
2162
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002163static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2164{
2165 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2166 struct net_device *dev = info->user_ptr[1];
2167 u16 noack_map;
2168
2169 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2170 return -EINVAL;
2171
2172 if (!rdev->ops->set_noack_map)
2173 return -EOPNOTSUPP;
2174
2175 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2176
Hila Gonene35e4d22012-06-27 17:19:42 +03002177 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002178}
2179
Johannes Berg41ade002007-12-19 02:03:29 +01002180struct get_key_cookie {
2181 struct sk_buff *msg;
2182 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002183 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002184};
2185
2186static void get_key_callback(void *c, struct key_params *params)
2187{
Johannes Bergb9454e82009-07-08 13:29:08 +02002188 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002189 struct get_key_cookie *cookie = c;
2190
David S. Miller9360ffd2012-03-29 04:41:26 -04002191 if ((params->key &&
2192 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2193 params->key_len, params->key)) ||
2194 (params->seq &&
2195 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2196 params->seq_len, params->seq)) ||
2197 (params->cipher &&
2198 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2199 params->cipher)))
2200 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002201
Johannes Bergb9454e82009-07-08 13:29:08 +02002202 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2203 if (!key)
2204 goto nla_put_failure;
2205
David S. Miller9360ffd2012-03-29 04:41:26 -04002206 if ((params->key &&
2207 nla_put(cookie->msg, NL80211_KEY_DATA,
2208 params->key_len, params->key)) ||
2209 (params->seq &&
2210 nla_put(cookie->msg, NL80211_KEY_SEQ,
2211 params->seq_len, params->seq)) ||
2212 (params->cipher &&
2213 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2214 params->cipher)))
2215 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002216
David S. Miller9360ffd2012-03-29 04:41:26 -04002217 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2218 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002219
2220 nla_nest_end(cookie->msg, key);
2221
Johannes Berg41ade002007-12-19 02:03:29 +01002222 return;
2223 nla_put_failure:
2224 cookie->error = 1;
2225}
2226
2227static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2228{
Johannes Berg4c476992010-10-04 21:36:35 +02002229 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002230 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002231 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002232 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002233 const u8 *mac_addr = NULL;
2234 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002235 struct get_key_cookie cookie = {
2236 .error = 0,
2237 };
2238 void *hdr;
2239 struct sk_buff *msg;
2240
2241 if (info->attrs[NL80211_ATTR_KEY_IDX])
2242 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2243
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002244 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002245 return -EINVAL;
2246
2247 if (info->attrs[NL80211_ATTR_MAC])
2248 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2249
Johannes Berge31b8212010-10-05 19:39:30 +02002250 pairwise = !!mac_addr;
2251 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2252 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2253 if (kt >= NUM_NL80211_KEYTYPES)
2254 return -EINVAL;
2255 if (kt != NL80211_KEYTYPE_GROUP &&
2256 kt != NL80211_KEYTYPE_PAIRWISE)
2257 return -EINVAL;
2258 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2259 }
2260
Johannes Berg4c476992010-10-04 21:36:35 +02002261 if (!rdev->ops->get_key)
2262 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002263
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002264 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002265 if (!msg)
2266 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002267
Eric W. Biederman15e47302012-09-07 20:12:54 +00002268 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002269 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002270 if (IS_ERR(hdr))
2271 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002272
2273 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002274 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002275
David S. Miller9360ffd2012-03-29 04:41:26 -04002276 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2277 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2278 goto nla_put_failure;
2279 if (mac_addr &&
2280 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2281 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002282
Johannes Berge31b8212010-10-05 19:39:30 +02002283 if (pairwise && mac_addr &&
2284 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2285 return -ENOENT;
2286
Hila Gonene35e4d22012-06-27 17:19:42 +03002287 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2288 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002289
2290 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002291 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002292
2293 if (cookie.error)
2294 goto nla_put_failure;
2295
2296 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002297 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002298
2299 nla_put_failure:
2300 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002301 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002302 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002303 return err;
2304}
2305
2306static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2307{
Johannes Berg4c476992010-10-04 21:36:35 +02002308 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002309 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002310 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002311 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002312
Johannes Bergb9454e82009-07-08 13:29:08 +02002313 err = nl80211_parse_key(info, &key);
2314 if (err)
2315 return err;
2316
2317 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002318 return -EINVAL;
2319
Johannes Bergb9454e82009-07-08 13:29:08 +02002320 /* only support setting default key */
2321 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002322 return -EINVAL;
2323
Johannes Bergfffd0932009-07-08 14:22:54 +02002324 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002325
2326 if (key.def) {
2327 if (!rdev->ops->set_default_key) {
2328 err = -EOPNOTSUPP;
2329 goto out;
2330 }
2331
2332 err = nl80211_key_allowed(dev->ieee80211_ptr);
2333 if (err)
2334 goto out;
2335
Hila Gonene35e4d22012-06-27 17:19:42 +03002336 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002337 key.def_uni, key.def_multi);
2338
2339 if (err)
2340 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002341
Johannes Berg3d23e342009-09-29 23:27:28 +02002342#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002343 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002344#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002345 } else {
2346 if (key.def_uni || !key.def_multi) {
2347 err = -EINVAL;
2348 goto out;
2349 }
2350
2351 if (!rdev->ops->set_default_mgmt_key) {
2352 err = -EOPNOTSUPP;
2353 goto out;
2354 }
2355
2356 err = nl80211_key_allowed(dev->ieee80211_ptr);
2357 if (err)
2358 goto out;
2359
Hila Gonene35e4d22012-06-27 17:19:42 +03002360 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002361 if (err)
2362 goto out;
2363
2364#ifdef CONFIG_CFG80211_WEXT
2365 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2366#endif
2367 }
2368
2369 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002370 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002371
Johannes Berg41ade002007-12-19 02:03:29 +01002372 return err;
2373}
2374
2375static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2376{
Johannes Berg4c476992010-10-04 21:36:35 +02002377 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002378 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002379 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002380 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002381 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002382
Johannes Bergb9454e82009-07-08 13:29:08 +02002383 err = nl80211_parse_key(info, &key);
2384 if (err)
2385 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002386
Johannes Bergb9454e82009-07-08 13:29:08 +02002387 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002388 return -EINVAL;
2389
Johannes Berg41ade002007-12-19 02:03:29 +01002390 if (info->attrs[NL80211_ATTR_MAC])
2391 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2392
Johannes Berge31b8212010-10-05 19:39:30 +02002393 if (key.type == -1) {
2394 if (mac_addr)
2395 key.type = NL80211_KEYTYPE_PAIRWISE;
2396 else
2397 key.type = NL80211_KEYTYPE_GROUP;
2398 }
2399
2400 /* for now */
2401 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2402 key.type != NL80211_KEYTYPE_GROUP)
2403 return -EINVAL;
2404
Johannes Berg4c476992010-10-04 21:36:35 +02002405 if (!rdev->ops->add_key)
2406 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002407
Johannes Berge31b8212010-10-05 19:39:30 +02002408 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2409 key.type == NL80211_KEYTYPE_PAIRWISE,
2410 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002411 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002412
2413 wdev_lock(dev->ieee80211_ptr);
2414 err = nl80211_key_allowed(dev->ieee80211_ptr);
2415 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002416 err = rdev_add_key(rdev, dev, key.idx,
2417 key.type == NL80211_KEYTYPE_PAIRWISE,
2418 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002419 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002420
Johannes Berg41ade002007-12-19 02:03:29 +01002421 return err;
2422}
2423
2424static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2425{
Johannes Berg4c476992010-10-04 21:36:35 +02002426 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002427 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002428 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002429 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002430 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002431
Johannes Bergb9454e82009-07-08 13:29:08 +02002432 err = nl80211_parse_key(info, &key);
2433 if (err)
2434 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002435
2436 if (info->attrs[NL80211_ATTR_MAC])
2437 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2438
Johannes Berge31b8212010-10-05 19:39:30 +02002439 if (key.type == -1) {
2440 if (mac_addr)
2441 key.type = NL80211_KEYTYPE_PAIRWISE;
2442 else
2443 key.type = NL80211_KEYTYPE_GROUP;
2444 }
2445
2446 /* for now */
2447 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2448 key.type != NL80211_KEYTYPE_GROUP)
2449 return -EINVAL;
2450
Johannes Berg4c476992010-10-04 21:36:35 +02002451 if (!rdev->ops->del_key)
2452 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002453
Johannes Bergfffd0932009-07-08 14:22:54 +02002454 wdev_lock(dev->ieee80211_ptr);
2455 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002456
2457 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2458 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2459 err = -ENOENT;
2460
Johannes Bergfffd0932009-07-08 14:22:54 +02002461 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002462 err = rdev_del_key(rdev, dev, key.idx,
2463 key.type == NL80211_KEYTYPE_PAIRWISE,
2464 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002465
Johannes Berg3d23e342009-09-29 23:27:28 +02002466#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002467 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002468 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002469 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002470 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002471 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2472 }
2473#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002474 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002475
Johannes Berg41ade002007-12-19 02:03:29 +01002476 return err;
2477}
2478
Johannes Berg88600202012-02-13 15:17:18 +01002479static int nl80211_parse_beacon(struct genl_info *info,
2480 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002481{
Johannes Berg88600202012-02-13 15:17:18 +01002482 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002483
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002484 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2485 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2486 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2487 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002488 return -EINVAL;
2489
Johannes Berg88600202012-02-13 15:17:18 +01002490 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002491
Johannes Berged1b6cc2007-12-19 02:03:32 +01002492 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002493 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2494 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2495 if (!bcn->head_len)
2496 return -EINVAL;
2497 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002498 }
2499
2500 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002501 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2502 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002503 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002504 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002505 }
2506
Johannes Berg4c476992010-10-04 21:36:35 +02002507 if (!haveinfo)
2508 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002509
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002510 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002511 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2512 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002513 }
2514
2515 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002516 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002517 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002518 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002519 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2520 }
2521
2522 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002523 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002524 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002525 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002526 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2527 }
2528
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002529 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002530 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002531 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002532 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002533 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2534 }
2535
Johannes Berg88600202012-02-13 15:17:18 +01002536 return 0;
2537}
2538
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002539static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2540 struct cfg80211_ap_settings *params)
2541{
2542 struct wireless_dev *wdev;
2543 bool ret = false;
2544
2545 mutex_lock(&rdev->devlist_mtx);
2546
Johannes Berg89a54e42012-06-15 14:33:17 +02002547 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002548 if (wdev->iftype != NL80211_IFTYPE_AP &&
2549 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2550 continue;
2551
Johannes Berg683b6d32012-11-08 21:25:48 +01002552 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002553 continue;
2554
Johannes Berg683b6d32012-11-08 21:25:48 +01002555 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002556 ret = true;
2557 break;
2558 }
2559
2560 mutex_unlock(&rdev->devlist_mtx);
2561
2562 return ret;
2563}
2564
Jouni Malinene39e5b52012-09-30 19:29:39 +03002565static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2566 enum nl80211_auth_type auth_type,
2567 enum nl80211_commands cmd)
2568{
2569 if (auth_type > NL80211_AUTHTYPE_MAX)
2570 return false;
2571
2572 switch (cmd) {
2573 case NL80211_CMD_AUTHENTICATE:
2574 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2575 auth_type == NL80211_AUTHTYPE_SAE)
2576 return false;
2577 return true;
2578 case NL80211_CMD_CONNECT:
2579 case NL80211_CMD_START_AP:
2580 /* SAE not supported yet */
2581 if (auth_type == NL80211_AUTHTYPE_SAE)
2582 return false;
2583 return true;
2584 default:
2585 return false;
2586 }
2587}
2588
Johannes Berg88600202012-02-13 15:17:18 +01002589static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2590{
2591 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2592 struct net_device *dev = info->user_ptr[1];
2593 struct wireless_dev *wdev = dev->ieee80211_ptr;
2594 struct cfg80211_ap_settings params;
2595 int err;
2596
2597 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2598 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2599 return -EOPNOTSUPP;
2600
2601 if (!rdev->ops->start_ap)
2602 return -EOPNOTSUPP;
2603
2604 if (wdev->beacon_interval)
2605 return -EALREADY;
2606
2607 memset(&params, 0, sizeof(params));
2608
2609 /* these are required for START_AP */
2610 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2611 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2612 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2613 return -EINVAL;
2614
2615 err = nl80211_parse_beacon(info, &params.beacon);
2616 if (err)
2617 return err;
2618
2619 params.beacon_interval =
2620 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2621 params.dtim_period =
2622 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2623
2624 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2625 if (err)
2626 return err;
2627
2628 /*
2629 * In theory, some of these attributes should be required here
2630 * but since they were not used when the command was originally
2631 * added, keep them optional for old user space programs to let
2632 * them continue to work with drivers that do not need the
2633 * additional information -- drivers must check!
2634 */
2635 if (info->attrs[NL80211_ATTR_SSID]) {
2636 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2637 params.ssid_len =
2638 nla_len(info->attrs[NL80211_ATTR_SSID]);
2639 if (params.ssid_len == 0 ||
2640 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2641 return -EINVAL;
2642 }
2643
2644 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2645 params.hidden_ssid = nla_get_u32(
2646 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2647 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2648 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2649 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2650 return -EINVAL;
2651 }
2652
2653 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2654
2655 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2656 params.auth_type = nla_get_u32(
2657 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002658 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2659 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002660 return -EINVAL;
2661 } else
2662 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2663
2664 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2665 NL80211_MAX_NR_CIPHER_SUITES);
2666 if (err)
2667 return err;
2668
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302669 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2670 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2671 return -EOPNOTSUPP;
2672 params.inactivity_timeout = nla_get_u16(
2673 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2674 }
2675
Johannes Bergaa430da2012-05-16 23:50:18 +02002676 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002677 err = nl80211_parse_chandef(rdev, info, &params.chandef);
2678 if (err)
2679 return err;
2680 } else if (wdev->preset_chandef.chan) {
2681 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002682 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002683 return -EINVAL;
2684
Johannes Berg683b6d32012-11-08 21:25:48 +01002685 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02002686 return -EINVAL;
2687
Michal Kaziore4e32452012-06-29 12:47:08 +02002688 mutex_lock(&rdev->devlist_mtx);
Johannes Berg683b6d32012-11-08 21:25:48 +01002689 err = cfg80211_can_use_chan(rdev, wdev, params.chandef.chan,
Michal Kaziore4e32452012-06-29 12:47:08 +02002690 CHAN_MODE_SHARED);
2691 mutex_unlock(&rdev->devlist_mtx);
2692
2693 if (err)
2694 return err;
2695
Hila Gonene35e4d22012-06-27 17:19:42 +03002696 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002697 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002698 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01002699 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01002700 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01002701 wdev->ssid_len = params.ssid_len;
2702 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002703 }
Johannes Berg56d18932011-05-09 18:41:15 +02002704 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002705}
2706
Johannes Berg88600202012-02-13 15:17:18 +01002707static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2708{
2709 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2710 struct net_device *dev = info->user_ptr[1];
2711 struct wireless_dev *wdev = dev->ieee80211_ptr;
2712 struct cfg80211_beacon_data params;
2713 int err;
2714
2715 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2716 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2717 return -EOPNOTSUPP;
2718
2719 if (!rdev->ops->change_beacon)
2720 return -EOPNOTSUPP;
2721
2722 if (!wdev->beacon_interval)
2723 return -EINVAL;
2724
2725 err = nl80211_parse_beacon(info, &params);
2726 if (err)
2727 return err;
2728
Hila Gonene35e4d22012-06-27 17:19:42 +03002729 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002730}
2731
2732static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002733{
Johannes Berg4c476992010-10-04 21:36:35 +02002734 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2735 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002736
Michal Kazior60771782012-06-29 12:46:56 +02002737 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002738}
2739
Johannes Berg5727ef12007-12-19 02:03:34 +01002740static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2741 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2742 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2743 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002744 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002745 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002746 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002747};
2748
Johannes Bergeccb8e82009-05-11 21:57:56 +03002749static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002750 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002751 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002752{
2753 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002754 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002755 int flag;
2756
Johannes Bergeccb8e82009-05-11 21:57:56 +03002757 /*
2758 * Try parsing the new attribute first so userspace
2759 * can specify both for older kernels.
2760 */
2761 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2762 if (nla) {
2763 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002764
Johannes Bergeccb8e82009-05-11 21:57:56 +03002765 sta_flags = nla_data(nla);
2766 params->sta_flags_mask = sta_flags->mask;
2767 params->sta_flags_set = sta_flags->set;
2768 if ((params->sta_flags_mask |
2769 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2770 return -EINVAL;
2771 return 0;
2772 }
2773
2774 /* if present, parse the old attribute */
2775
2776 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002777 if (!nla)
2778 return 0;
2779
2780 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2781 nla, sta_flags_policy))
2782 return -EINVAL;
2783
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002784 /*
2785 * Only allow certain flags for interface types so that
2786 * other attributes are silently ignored. Remember that
2787 * this is backward compatibility code with old userspace
2788 * and shouldn't be hit in other cases anyway.
2789 */
2790 switch (iftype) {
2791 case NL80211_IFTYPE_AP:
2792 case NL80211_IFTYPE_AP_VLAN:
2793 case NL80211_IFTYPE_P2P_GO:
2794 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2795 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2796 BIT(NL80211_STA_FLAG_WME) |
2797 BIT(NL80211_STA_FLAG_MFP);
2798 break;
2799 case NL80211_IFTYPE_P2P_CLIENT:
2800 case NL80211_IFTYPE_STATION:
2801 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2802 BIT(NL80211_STA_FLAG_TDLS_PEER);
2803 break;
2804 case NL80211_IFTYPE_MESH_POINT:
2805 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2806 BIT(NL80211_STA_FLAG_MFP) |
2807 BIT(NL80211_STA_FLAG_AUTHORIZED);
2808 default:
2809 return -EINVAL;
2810 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002811
Johannes Berg3383b5a2012-05-10 20:14:43 +02002812 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2813 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002814 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002815
Johannes Berg3383b5a2012-05-10 20:14:43 +02002816 /* no longer support new API additions in old API */
2817 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2818 return -EINVAL;
2819 }
2820 }
2821
Johannes Berg5727ef12007-12-19 02:03:34 +01002822 return 0;
2823}
2824
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002825static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2826 int attr)
2827{
2828 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002829 u32 bitrate;
2830 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002831
2832 rate = nla_nest_start(msg, attr);
2833 if (!rate)
2834 goto nla_put_failure;
2835
2836 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2837 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002838 /* report 16-bit bitrate only if we can */
2839 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002840 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002841 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2842 (bitrate_compat > 0 &&
2843 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002844 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2845 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2846 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2847 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2848 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2849 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2850 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002851
2852 nla_nest_end(msg, rate);
2853 return true;
2854
2855nla_put_failure:
2856 return false;
2857}
2858
Eric W. Biederman15e47302012-09-07 20:12:54 +00002859static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002860 int flags,
2861 struct cfg80211_registered_device *rdev,
2862 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002863 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002864{
2865 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002866 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002867
Eric W. Biederman15e47302012-09-07 20:12:54 +00002868 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002869 if (!hdr)
2870 return -1;
2871
David S. Miller9360ffd2012-03-29 04:41:26 -04002872 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2873 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2874 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2875 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002876
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002877 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2878 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002879 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002880 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2881 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2882 sinfo->connected_time))
2883 goto nla_put_failure;
2884 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2885 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2886 sinfo->inactive_time))
2887 goto nla_put_failure;
2888 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2889 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2890 sinfo->rx_bytes))
2891 goto nla_put_failure;
2892 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2893 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2894 sinfo->tx_bytes))
2895 goto nla_put_failure;
2896 if ((sinfo->filled & STATION_INFO_LLID) &&
2897 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2898 goto nla_put_failure;
2899 if ((sinfo->filled & STATION_INFO_PLID) &&
2900 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2901 goto nla_put_failure;
2902 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2903 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2904 sinfo->plink_state))
2905 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002906 switch (rdev->wiphy.signal_type) {
2907 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002908 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2909 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2910 sinfo->signal))
2911 goto nla_put_failure;
2912 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2913 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2914 sinfo->signal_avg))
2915 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002916 break;
2917 default:
2918 break;
2919 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002920 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002921 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2922 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002923 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002924 }
2925 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2926 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2927 NL80211_STA_INFO_RX_BITRATE))
2928 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002929 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002930 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2931 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2932 sinfo->rx_packets))
2933 goto nla_put_failure;
2934 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2935 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2936 sinfo->tx_packets))
2937 goto nla_put_failure;
2938 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2939 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2940 sinfo->tx_retries))
2941 goto nla_put_failure;
2942 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2943 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2944 sinfo->tx_failed))
2945 goto nla_put_failure;
2946 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2947 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2948 sinfo->beacon_loss_count))
2949 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002950 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2951 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2952 if (!bss_param)
2953 goto nla_put_failure;
2954
David S. Miller9360ffd2012-03-29 04:41:26 -04002955 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2956 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2957 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2958 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2959 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2960 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2961 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2962 sinfo->bss_param.dtim_period) ||
2963 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2964 sinfo->bss_param.beacon_interval))
2965 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002966
2967 nla_nest_end(msg, bss_param);
2968 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002969 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2970 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2971 sizeof(struct nl80211_sta_flag_update),
2972 &sinfo->sta_flags))
2973 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002974 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2975 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2976 sinfo->t_offset))
2977 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002978 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002979
David S. Miller9360ffd2012-03-29 04:41:26 -04002980 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2981 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2982 sinfo->assoc_req_ies))
2983 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002984
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002985 return genlmsg_end(msg, hdr);
2986
2987 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002988 genlmsg_cancel(msg, hdr);
2989 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002990}
2991
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002992static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002993 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002994{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002995 struct station_info sinfo;
2996 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002997 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002998 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002999 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003000 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003001
Johannes Berg67748892010-10-04 21:14:06 +02003002 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3003 if (err)
3004 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003005
3006 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003007 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003008 goto out_err;
3009 }
3010
Johannes Bergbba95fe2008-07-29 13:22:51 +02003011 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003012 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03003013 err = rdev_dump_station(dev, netdev, sta_idx,
3014 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003015 if (err == -ENOENT)
3016 break;
3017 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003018 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003019
3020 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003021 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003022 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04003023 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003024 &sinfo) < 0)
3025 goto out;
3026
3027 sta_idx++;
3028 }
3029
3030
3031 out:
3032 cb->args[1] = sta_idx;
3033 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003034 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003035 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003036
3037 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003038}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003039
Johannes Berg5727ef12007-12-19 02:03:34 +01003040static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3041{
Johannes Berg4c476992010-10-04 21:36:35 +02003042 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3043 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003044 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003045 struct sk_buff *msg;
3046 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003047 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003048
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003049 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003050
3051 if (!info->attrs[NL80211_ATTR_MAC])
3052 return -EINVAL;
3053
3054 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3055
Johannes Berg4c476992010-10-04 21:36:35 +02003056 if (!rdev->ops->get_station)
3057 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003058
Hila Gonene35e4d22012-06-27 17:19:42 +03003059 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003060 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003061 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003062
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003063 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003064 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003065 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003066
Eric W. Biederman15e47302012-09-07 20:12:54 +00003067 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003068 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003069 nlmsg_free(msg);
3070 return -ENOBUFS;
3071 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003072
Johannes Berg4c476992010-10-04 21:36:35 +02003073 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003074}
3075
3076/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003077 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003078 */
Johannes Berg80b99892011-11-18 16:23:01 +01003079static struct net_device *get_vlan(struct genl_info *info,
3080 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003081{
Johannes Berg463d0182009-07-14 00:33:35 +02003082 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003083 struct net_device *v;
3084 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003085
Johannes Berg80b99892011-11-18 16:23:01 +01003086 if (!vlanattr)
3087 return NULL;
3088
3089 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3090 if (!v)
3091 return ERR_PTR(-ENODEV);
3092
3093 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3094 ret = -EINVAL;
3095 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003096 }
Johannes Berg80b99892011-11-18 16:23:01 +01003097
3098 if (!netif_running(v)) {
3099 ret = -ENETDOWN;
3100 goto error;
3101 }
3102
3103 return v;
3104 error:
3105 dev_put(v);
3106 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003107}
3108
3109static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3110{
Johannes Berg4c476992010-10-04 21:36:35 +02003111 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003112 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003113 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003114 struct station_parameters params;
3115 u8 *mac_addr = NULL;
3116
3117 memset(&params, 0, sizeof(params));
3118
3119 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003120 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003121
3122 if (info->attrs[NL80211_ATTR_STA_AID])
3123 return -EINVAL;
3124
3125 if (!info->attrs[NL80211_ATTR_MAC])
3126 return -EINVAL;
3127
3128 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3129
3130 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3131 params.supported_rates =
3132 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3133 params.supported_rates_len =
3134 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3135 }
3136
3137 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3138 params.listen_interval =
3139 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3140
Jouni Malinen36aedc92008-08-25 11:58:58 +03003141 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3142 params.ht_capa =
3143 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3144
Johannes Bergbdd90d52011-12-14 12:20:27 +01003145 if (!rdev->ops->change_station)
3146 return -EOPNOTSUPP;
3147
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003148 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003149 return -EINVAL;
3150
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003151 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3152 params.plink_action =
3153 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3154
Javier Cardona9c3990a2011-05-03 16:57:11 -07003155 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3156 params.plink_state =
3157 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3158
Johannes Berga97f4422009-06-18 17:23:43 +02003159 switch (dev->ieee80211_ptr->iftype) {
3160 case NL80211_IFTYPE_AP:
3161 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003162 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003163 /* disallow mesh-specific things */
3164 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003165 return -EINVAL;
3166
3167 /* TDLS can't be set, ... */
3168 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3169 return -EINVAL;
3170 /*
3171 * ... but don't bother the driver with it. This works around
3172 * a hostapd/wpa_supplicant issue -- it always includes the
3173 * TLDS_PEER flag in the mask even for AP mode.
3174 */
3175 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3176
3177 /* accept only the listed bits */
3178 if (params.sta_flags_mask &
3179 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3180 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3181 BIT(NL80211_STA_FLAG_WME) |
3182 BIT(NL80211_STA_FLAG_MFP)))
3183 return -EINVAL;
3184
3185 /* must be last in here for error handling */
3186 params.vlan = get_vlan(info, rdev);
3187 if (IS_ERR(params.vlan))
3188 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003189 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003190 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003191 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003192 /*
3193 * Don't allow userspace to change the TDLS_PEER flag,
3194 * but silently ignore attempts to change it since we
3195 * don't have state here to verify that it doesn't try
3196 * to change the flag.
3197 */
3198 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003199 /* fall through */
3200 case NL80211_IFTYPE_ADHOC:
3201 /* disallow things sta doesn't support */
3202 if (params.plink_action)
3203 return -EINVAL;
3204 if (params.ht_capa)
3205 return -EINVAL;
3206 if (params.listen_interval >= 0)
3207 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003208 /* reject any changes other than AUTHORIZED */
3209 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3210 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003211 break;
3212 case NL80211_IFTYPE_MESH_POINT:
3213 /* disallow things mesh doesn't support */
3214 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003215 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003216 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003217 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003218 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003219 return -EINVAL;
3220 /*
3221 * No special handling for TDLS here -- the userspace
3222 * mesh code doesn't have this bug.
3223 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003224 if (params.sta_flags_mask &
3225 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003226 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003227 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003228 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003229 break;
3230 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003231 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003232 }
3233
Johannes Bergbdd90d52011-12-14 12:20:27 +01003234 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003235
Hila Gonene35e4d22012-06-27 17:19:42 +03003236 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003237
Johannes Berg5727ef12007-12-19 02:03:34 +01003238 if (params.vlan)
3239 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003240
Johannes Berg5727ef12007-12-19 02:03:34 +01003241 return err;
3242}
3243
Eliad Pellerc75786c2011-08-23 14:37:46 +03003244static struct nla_policy
3245nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3246 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3247 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3248};
3249
Johannes Berg5727ef12007-12-19 02:03:34 +01003250static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3251{
Johannes Berg4c476992010-10-04 21:36:35 +02003252 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003253 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003254 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003255 struct station_parameters params;
3256 u8 *mac_addr = NULL;
3257
3258 memset(&params, 0, sizeof(params));
3259
3260 if (!info->attrs[NL80211_ATTR_MAC])
3261 return -EINVAL;
3262
Johannes Berg5727ef12007-12-19 02:03:34 +01003263 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3264 return -EINVAL;
3265
3266 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3267 return -EINVAL;
3268
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003269 if (!info->attrs[NL80211_ATTR_STA_AID])
3270 return -EINVAL;
3271
Johannes Berg5727ef12007-12-19 02:03:34 +01003272 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3273 params.supported_rates =
3274 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3275 params.supported_rates_len =
3276 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3277 params.listen_interval =
3278 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003279
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003280 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3281 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3282 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003283
Jouni Malinen36aedc92008-08-25 11:58:58 +03003284 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3285 params.ht_capa =
3286 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003287
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003288 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3289 params.vht_capa =
3290 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3291
Javier Cardona96b78df2011-04-07 15:08:33 -07003292 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3293 params.plink_action =
3294 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3295
Johannes Bergbdd90d52011-12-14 12:20:27 +01003296 if (!rdev->ops->add_station)
3297 return -EOPNOTSUPP;
3298
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003299 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003300 return -EINVAL;
3301
Johannes Bergbdd90d52011-12-14 12:20:27 +01003302 switch (dev->ieee80211_ptr->iftype) {
3303 case NL80211_IFTYPE_AP:
3304 case NL80211_IFTYPE_AP_VLAN:
3305 case NL80211_IFTYPE_P2P_GO:
3306 /* parse WME attributes if sta is WME capable */
3307 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3308 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3309 info->attrs[NL80211_ATTR_STA_WME]) {
3310 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3311 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003312
Johannes Bergbdd90d52011-12-14 12:20:27 +01003313 nla = info->attrs[NL80211_ATTR_STA_WME];
3314 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3315 nl80211_sta_wme_policy);
3316 if (err)
3317 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003318
Johannes Bergbdd90d52011-12-14 12:20:27 +01003319 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3320 params.uapsd_queues =
3321 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3322 if (params.uapsd_queues &
3323 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3324 return -EINVAL;
3325
3326 if (tb[NL80211_STA_WME_MAX_SP])
3327 params.max_sp =
3328 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3329
3330 if (params.max_sp &
3331 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3332 return -EINVAL;
3333
3334 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3335 }
3336 /* TDLS peers cannot be added */
3337 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003338 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003339 /* but don't bother the driver with it */
3340 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003341
Johannes Bergbdd90d52011-12-14 12:20:27 +01003342 /* must be last in here for error handling */
3343 params.vlan = get_vlan(info, rdev);
3344 if (IS_ERR(params.vlan))
3345 return PTR_ERR(params.vlan);
3346 break;
3347 case NL80211_IFTYPE_MESH_POINT:
3348 /* TDLS peers cannot be added */
3349 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003350 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003351 break;
3352 case NL80211_IFTYPE_STATION:
3353 /* Only TDLS peers can be added */
3354 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3355 return -EINVAL;
3356 /* Can only add if TDLS ... */
3357 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3358 return -EOPNOTSUPP;
3359 /* ... with external setup is supported */
3360 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3361 return -EOPNOTSUPP;
3362 break;
3363 default:
3364 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003365 }
3366
Johannes Bergbdd90d52011-12-14 12:20:27 +01003367 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003368
Hila Gonene35e4d22012-06-27 17:19:42 +03003369 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003370
Johannes Berg5727ef12007-12-19 02:03:34 +01003371 if (params.vlan)
3372 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003373 return err;
3374}
3375
3376static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3377{
Johannes Berg4c476992010-10-04 21:36:35 +02003378 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3379 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003380 u8 *mac_addr = NULL;
3381
3382 if (info->attrs[NL80211_ATTR_MAC])
3383 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3384
Johannes Berge80cf852009-05-11 14:43:13 +02003385 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003386 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003387 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003388 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3389 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003390
Johannes Berg4c476992010-10-04 21:36:35 +02003391 if (!rdev->ops->del_station)
3392 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003393
Hila Gonene35e4d22012-06-27 17:19:42 +03003394 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003395}
3396
Eric W. Biederman15e47302012-09-07 20:12:54 +00003397static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003398 int flags, struct net_device *dev,
3399 u8 *dst, u8 *next_hop,
3400 struct mpath_info *pinfo)
3401{
3402 void *hdr;
3403 struct nlattr *pinfoattr;
3404
Eric W. Biederman15e47302012-09-07 20:12:54 +00003405 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003406 if (!hdr)
3407 return -1;
3408
David S. Miller9360ffd2012-03-29 04:41:26 -04003409 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3410 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3411 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3412 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3413 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003414
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003415 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3416 if (!pinfoattr)
3417 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003418 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3419 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3420 pinfo->frame_qlen))
3421 goto nla_put_failure;
3422 if (((pinfo->filled & MPATH_INFO_SN) &&
3423 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3424 ((pinfo->filled & MPATH_INFO_METRIC) &&
3425 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3426 pinfo->metric)) ||
3427 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3428 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3429 pinfo->exptime)) ||
3430 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3431 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3432 pinfo->flags)) ||
3433 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3434 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3435 pinfo->discovery_timeout)) ||
3436 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3437 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3438 pinfo->discovery_retries)))
3439 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003440
3441 nla_nest_end(msg, pinfoattr);
3442
3443 return genlmsg_end(msg, hdr);
3444
3445 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003446 genlmsg_cancel(msg, hdr);
3447 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003448}
3449
3450static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003451 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003452{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003453 struct mpath_info pinfo;
3454 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003455 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003456 u8 dst[ETH_ALEN];
3457 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003458 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003459 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003460
Johannes Berg67748892010-10-04 21:14:06 +02003461 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3462 if (err)
3463 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003464
3465 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003466 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003467 goto out_err;
3468 }
3469
Jouni Malineneec60b02009-03-20 21:21:19 +02003470 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3471 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003472 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003473 }
3474
Johannes Bergbba95fe2008-07-29 13:22:51 +02003475 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003476 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3477 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003478 if (err == -ENOENT)
3479 break;
3480 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003481 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003482
Eric W. Biederman15e47302012-09-07 20:12:54 +00003483 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003484 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3485 netdev, dst, next_hop,
3486 &pinfo) < 0)
3487 goto out;
3488
3489 path_idx++;
3490 }
3491
3492
3493 out:
3494 cb->args[1] = path_idx;
3495 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003496 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003497 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003498 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003499}
3500
3501static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3502{
Johannes Berg4c476992010-10-04 21:36:35 +02003503 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003504 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003505 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003506 struct mpath_info pinfo;
3507 struct sk_buff *msg;
3508 u8 *dst = NULL;
3509 u8 next_hop[ETH_ALEN];
3510
3511 memset(&pinfo, 0, sizeof(pinfo));
3512
3513 if (!info->attrs[NL80211_ATTR_MAC])
3514 return -EINVAL;
3515
3516 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3517
Johannes Berg4c476992010-10-04 21:36:35 +02003518 if (!rdev->ops->get_mpath)
3519 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003520
Johannes Berg4c476992010-10-04 21:36:35 +02003521 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3522 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003523
Hila Gonene35e4d22012-06-27 17:19:42 +03003524 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003525 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003526 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003527
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003528 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003529 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003530 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003531
Eric W. Biederman15e47302012-09-07 20:12:54 +00003532 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003533 dev, dst, next_hop, &pinfo) < 0) {
3534 nlmsg_free(msg);
3535 return -ENOBUFS;
3536 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003537
Johannes Berg4c476992010-10-04 21:36:35 +02003538 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003539}
3540
3541static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3542{
Johannes Berg4c476992010-10-04 21:36:35 +02003543 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3544 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003545 u8 *dst = NULL;
3546 u8 *next_hop = NULL;
3547
3548 if (!info->attrs[NL80211_ATTR_MAC])
3549 return -EINVAL;
3550
3551 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3552 return -EINVAL;
3553
3554 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3555 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3556
Johannes Berg4c476992010-10-04 21:36:35 +02003557 if (!rdev->ops->change_mpath)
3558 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003559
Johannes Berg4c476992010-10-04 21:36:35 +02003560 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3561 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003562
Hila Gonene35e4d22012-06-27 17:19:42 +03003563 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003564}
Johannes Berg4c476992010-10-04 21:36:35 +02003565
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003566static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3567{
Johannes Berg4c476992010-10-04 21:36:35 +02003568 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3569 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003570 u8 *dst = NULL;
3571 u8 *next_hop = NULL;
3572
3573 if (!info->attrs[NL80211_ATTR_MAC])
3574 return -EINVAL;
3575
3576 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3577 return -EINVAL;
3578
3579 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3580 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3581
Johannes Berg4c476992010-10-04 21:36:35 +02003582 if (!rdev->ops->add_mpath)
3583 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003584
Johannes Berg4c476992010-10-04 21:36:35 +02003585 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3586 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003587
Hila Gonene35e4d22012-06-27 17:19:42 +03003588 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003589}
3590
3591static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3592{
Johannes Berg4c476992010-10-04 21:36:35 +02003593 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3594 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003595 u8 *dst = NULL;
3596
3597 if (info->attrs[NL80211_ATTR_MAC])
3598 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3599
Johannes Berg4c476992010-10-04 21:36:35 +02003600 if (!rdev->ops->del_mpath)
3601 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003602
Hila Gonene35e4d22012-06-27 17:19:42 +03003603 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003604}
3605
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003606static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3607{
Johannes Berg4c476992010-10-04 21:36:35 +02003608 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3609 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003610 struct bss_parameters params;
3611
3612 memset(&params, 0, sizeof(params));
3613 /* default to not changing parameters */
3614 params.use_cts_prot = -1;
3615 params.use_short_preamble = -1;
3616 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003617 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003618 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003619
3620 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3621 params.use_cts_prot =
3622 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3623 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3624 params.use_short_preamble =
3625 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3626 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3627 params.use_short_slot_time =
3628 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003629 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3630 params.basic_rates =
3631 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3632 params.basic_rates_len =
3633 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3634 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003635 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3636 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003637 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3638 params.ht_opmode =
3639 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003640
Johannes Berg4c476992010-10-04 21:36:35 +02003641 if (!rdev->ops->change_bss)
3642 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003643
Johannes Berg074ac8d2010-09-16 14:58:22 +02003644 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003645 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3646 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003647
Hila Gonene35e4d22012-06-27 17:19:42 +03003648 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003649}
3650
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003651static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003652 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3653 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3654 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3655 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3656 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3657 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3658};
3659
3660static int parse_reg_rule(struct nlattr *tb[],
3661 struct ieee80211_reg_rule *reg_rule)
3662{
3663 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3664 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3665
3666 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3667 return -EINVAL;
3668 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3669 return -EINVAL;
3670 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3671 return -EINVAL;
3672 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3673 return -EINVAL;
3674 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3675 return -EINVAL;
3676
3677 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3678
3679 freq_range->start_freq_khz =
3680 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3681 freq_range->end_freq_khz =
3682 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3683 freq_range->max_bandwidth_khz =
3684 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3685
3686 power_rule->max_eirp =
3687 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3688
3689 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3690 power_rule->max_antenna_gain =
3691 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3692
3693 return 0;
3694}
3695
3696static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3697{
3698 int r;
3699 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003700 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003701
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003702 /*
3703 * You should only get this when cfg80211 hasn't yet initialized
3704 * completely when built-in to the kernel right between the time
3705 * window between nl80211_init() and regulatory_init(), if that is
3706 * even possible.
3707 */
3708 mutex_lock(&cfg80211_mutex);
3709 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003710 mutex_unlock(&cfg80211_mutex);
3711 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003712 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003713 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003714
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003715 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3716 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003717
3718 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3719
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003720 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3721 user_reg_hint_type =
3722 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3723 else
3724 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3725
3726 switch (user_reg_hint_type) {
3727 case NL80211_USER_REG_HINT_USER:
3728 case NL80211_USER_REG_HINT_CELL_BASE:
3729 break;
3730 default:
3731 return -EINVAL;
3732 }
3733
3734 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003735
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003736 return r;
3737}
3738
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003739static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003740 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003741{
Johannes Berg4c476992010-10-04 21:36:35 +02003742 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003743 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003744 struct wireless_dev *wdev = dev->ieee80211_ptr;
3745 struct mesh_config cur_params;
3746 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003747 void *hdr;
3748 struct nlattr *pinfoattr;
3749 struct sk_buff *msg;
3750
Johannes Berg29cbe682010-12-03 09:20:44 +01003751 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3752 return -EOPNOTSUPP;
3753
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003754 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003755 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003756
Johannes Berg29cbe682010-12-03 09:20:44 +01003757 wdev_lock(wdev);
3758 /* If not connected, get default parameters */
3759 if (!wdev->mesh_id_len)
3760 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3761 else
Hila Gonene35e4d22012-06-27 17:19:42 +03003762 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01003763 wdev_unlock(wdev);
3764
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003765 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003766 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003767
3768 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003769 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003770 if (!msg)
3771 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003772 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003773 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003774 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003775 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003776 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003777 if (!pinfoattr)
3778 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003779 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3780 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3781 cur_params.dot11MeshRetryTimeout) ||
3782 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3783 cur_params.dot11MeshConfirmTimeout) ||
3784 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3785 cur_params.dot11MeshHoldingTimeout) ||
3786 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3787 cur_params.dot11MeshMaxPeerLinks) ||
3788 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3789 cur_params.dot11MeshMaxRetries) ||
3790 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3791 cur_params.dot11MeshTTL) ||
3792 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3793 cur_params.element_ttl) ||
3794 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3795 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003796 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3797 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003798 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3799 cur_params.dot11MeshHWMPmaxPREQretries) ||
3800 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3801 cur_params.path_refresh_time) ||
3802 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3803 cur_params.min_discovery_timeout) ||
3804 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3805 cur_params.dot11MeshHWMPactivePathTimeout) ||
3806 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3807 cur_params.dot11MeshHWMPpreqMinInterval) ||
3808 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3809 cur_params.dot11MeshHWMPperrMinInterval) ||
3810 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3811 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3812 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3813 cur_params.dot11MeshHWMPRootMode) ||
3814 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3815 cur_params.dot11MeshHWMPRannInterval) ||
3816 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3817 cur_params.dot11MeshGateAnnouncementProtocol) ||
3818 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3819 cur_params.dot11MeshForwarding) ||
3820 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003821 cur_params.rssi_threshold) ||
3822 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003823 cur_params.ht_opmode) ||
3824 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3825 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3826 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003827 cur_params.dot11MeshHWMProotInterval) ||
3828 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3829 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003830 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003831 nla_nest_end(msg, pinfoattr);
3832 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003833 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003834
Johannes Berg3b858752009-03-12 09:55:09 +01003835 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003836 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003837 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003838 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003839 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003840}
3841
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003842static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003843 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3844 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3845 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3846 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3847 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3848 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003849 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003850 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003851 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003852 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3853 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3854 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3855 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3856 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003857 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003858 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003859 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003860 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003861 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003862 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003863 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3864 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003865 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3866 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003867 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003868};
3869
Javier Cardonac80d5452010-12-16 17:37:49 -08003870static const struct nla_policy
3871 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003872 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003873 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3874 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003875 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003876 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003877 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003878 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003879};
3880
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003881static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003882 struct mesh_config *cfg,
3883 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003884{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003885 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003886 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003887
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003888#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3889do {\
3890 if (table[attr_num]) {\
3891 cfg->param = nla_fn(table[attr_num]); \
3892 mask |= (1 << (attr_num - 1)); \
3893 } \
3894} while (0);\
3895
3896
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003897 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003898 return -EINVAL;
3899 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003900 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003901 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003902 return -EINVAL;
3903
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003904 /* This makes sure that there aren't more than 32 mesh config
3905 * parameters (otherwise our bitfield scheme would not work.) */
3906 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3907
3908 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003909 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003910 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3911 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003912 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003913 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3914 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003915 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003916 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3917 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003918 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003919 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3920 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003921 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003922 mask, NL80211_MESHCONF_MAX_RETRIES,
3923 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003924 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003925 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003926 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003927 mask, NL80211_MESHCONF_ELEMENT_TTL,
3928 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003929 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003930 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3931 nla_get_u8);
3932 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3933 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3934 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003935 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003936 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3937 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003938 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003939 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3940 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003941 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003942 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3943 nla_get_u16);
3944 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3945 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3946 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003947 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003948 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3949 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003950 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003951 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3952 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003953 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003954 dot11MeshHWMPnetDiameterTraversalTime, mask,
3955 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3956 nla_get_u16);
3957 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3958 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3959 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3960 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3961 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003962 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003963 dot11MeshGateAnnouncementProtocol, mask,
3964 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3965 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003966 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003967 mask, NL80211_MESHCONF_FORWARDING,
3968 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003969 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003970 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3971 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003972 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003973 mask, NL80211_MESHCONF_HT_OPMODE,
3974 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003975 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3976 mask,
3977 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3978 nla_get_u32);
3979 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3980 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3981 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003982 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3983 dot11MeshHWMPconfirmationInterval, mask,
3984 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3985 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003986 if (mask_out)
3987 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003988
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003989 return 0;
3990
3991#undef FILL_IN_MESH_PARAM_IF_SET
3992}
3993
Javier Cardonac80d5452010-12-16 17:37:49 -08003994static int nl80211_parse_mesh_setup(struct genl_info *info,
3995 struct mesh_setup *setup)
3996{
3997 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3998
3999 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4000 return -EINVAL;
4001 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4002 info->attrs[NL80211_ATTR_MESH_SETUP],
4003 nl80211_mesh_setup_params_policy))
4004 return -EINVAL;
4005
Javier Cardonad299a1f2012-03-31 11:31:33 -07004006 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4007 setup->sync_method =
4008 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4009 IEEE80211_SYNC_METHOD_VENDOR :
4010 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4011
Javier Cardonac80d5452010-12-16 17:37:49 -08004012 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4013 setup->path_sel_proto =
4014 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4015 IEEE80211_PATH_PROTOCOL_VENDOR :
4016 IEEE80211_PATH_PROTOCOL_HWMP;
4017
4018 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4019 setup->path_metric =
4020 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4021 IEEE80211_PATH_METRIC_VENDOR :
4022 IEEE80211_PATH_METRIC_AIRTIME;
4023
Javier Cardona581a8b02011-04-07 15:08:27 -07004024
4025 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004026 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004027 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004028 if (!is_valid_ie_attr(ieattr))
4029 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004030 setup->ie = nla_data(ieattr);
4031 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004032 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07004033 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4034 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08004035
4036 return 0;
4037}
4038
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004039static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004040 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004041{
4042 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4043 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004044 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004045 struct mesh_config cfg;
4046 u32 mask;
4047 int err;
4048
Johannes Berg29cbe682010-12-03 09:20:44 +01004049 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4050 return -EOPNOTSUPP;
4051
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004052 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004053 return -EOPNOTSUPP;
4054
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004055 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004056 if (err)
4057 return err;
4058
Johannes Berg29cbe682010-12-03 09:20:44 +01004059 wdev_lock(wdev);
4060 if (!wdev->mesh_id_len)
4061 err = -ENOLINK;
4062
4063 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004064 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004065
4066 wdev_unlock(wdev);
4067
4068 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004069}
4070
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004071static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4072{
4073 struct sk_buff *msg;
4074 void *hdr = NULL;
4075 struct nlattr *nl_reg_rules;
4076 unsigned int i;
4077 int err = -EINVAL;
4078
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004079 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004080
4081 if (!cfg80211_regdomain)
4082 goto out;
4083
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004084 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004085 if (!msg) {
4086 err = -ENOBUFS;
4087 goto out;
4088 }
4089
Eric W. Biederman15e47302012-09-07 20:12:54 +00004090 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004091 NL80211_CMD_GET_REG);
4092 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004093 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004094
David S. Miller9360ffd2012-03-29 04:41:26 -04004095 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
4096 cfg80211_regdomain->alpha2) ||
4097 (cfg80211_regdomain->dfs_region &&
4098 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
4099 cfg80211_regdomain->dfs_region)))
4100 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004101
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004102 if (reg_last_request_cell_base() &&
4103 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4104 NL80211_USER_REG_HINT_CELL_BASE))
4105 goto nla_put_failure;
4106
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004107 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4108 if (!nl_reg_rules)
4109 goto nla_put_failure;
4110
4111 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
4112 struct nlattr *nl_reg_rule;
4113 const struct ieee80211_reg_rule *reg_rule;
4114 const struct ieee80211_freq_range *freq_range;
4115 const struct ieee80211_power_rule *power_rule;
4116
4117 reg_rule = &cfg80211_regdomain->reg_rules[i];
4118 freq_range = &reg_rule->freq_range;
4119 power_rule = &reg_rule->power_rule;
4120
4121 nl_reg_rule = nla_nest_start(msg, i);
4122 if (!nl_reg_rule)
4123 goto nla_put_failure;
4124
David S. Miller9360ffd2012-03-29 04:41:26 -04004125 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4126 reg_rule->flags) ||
4127 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4128 freq_range->start_freq_khz) ||
4129 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4130 freq_range->end_freq_khz) ||
4131 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4132 freq_range->max_bandwidth_khz) ||
4133 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4134 power_rule->max_antenna_gain) ||
4135 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4136 power_rule->max_eirp))
4137 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004138
4139 nla_nest_end(msg, nl_reg_rule);
4140 }
4141
4142 nla_nest_end(msg, nl_reg_rules);
4143
4144 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004145 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004146 goto out;
4147
4148nla_put_failure:
4149 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004150put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004151 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004152 err = -EMSGSIZE;
4153out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004154 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004155 return err;
4156}
4157
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004158static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4159{
4160 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4161 struct nlattr *nl_reg_rule;
4162 char *alpha2 = NULL;
4163 int rem_reg_rules = 0, r = 0;
4164 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004165 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004166 struct ieee80211_regdomain *rd = NULL;
4167
4168 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4169 return -EINVAL;
4170
4171 if (!info->attrs[NL80211_ATTR_REG_RULES])
4172 return -EINVAL;
4173
4174 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4175
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004176 if (info->attrs[NL80211_ATTR_DFS_REGION])
4177 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4178
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004179 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4180 rem_reg_rules) {
4181 num_rules++;
4182 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004183 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004184 }
4185
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004186 mutex_lock(&cfg80211_mutex);
4187
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004188 if (!reg_is_valid_request(alpha2)) {
4189 r = -EINVAL;
4190 goto bad_reg;
4191 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004192
4193 size_of_regd = sizeof(struct ieee80211_regdomain) +
4194 (num_rules * sizeof(struct ieee80211_reg_rule));
4195
4196 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004197 if (!rd) {
4198 r = -ENOMEM;
4199 goto bad_reg;
4200 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004201
4202 rd->n_reg_rules = num_rules;
4203 rd->alpha2[0] = alpha2[0];
4204 rd->alpha2[1] = alpha2[1];
4205
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004206 /*
4207 * Disable DFS master mode if the DFS region was
4208 * not supported or known on this kernel.
4209 */
4210 if (reg_supported_dfs_region(dfs_region))
4211 rd->dfs_region = dfs_region;
4212
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004213 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4214 rem_reg_rules) {
4215 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4216 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4217 reg_rule_policy);
4218 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4219 if (r)
4220 goto bad_reg;
4221
4222 rule_idx++;
4223
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004224 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4225 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004226 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004227 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004228 }
4229
4230 BUG_ON(rule_idx != num_rules);
4231
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004232 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004233
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004234 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004235
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004236 return r;
4237
Johannes Bergd2372b32008-10-24 20:32:20 +02004238 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004239 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004240 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004241 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004242}
4243
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004244static int validate_scan_freqs(struct nlattr *freqs)
4245{
4246 struct nlattr *attr1, *attr2;
4247 int n_channels = 0, tmp1, tmp2;
4248
4249 nla_for_each_nested(attr1, freqs, tmp1) {
4250 n_channels++;
4251 /*
4252 * Some hardware has a limited channel list for
4253 * scanning, and it is pretty much nonsensical
4254 * to scan for a channel twice, so disallow that
4255 * and don't require drivers to check that the
4256 * channel list they get isn't longer than what
4257 * they can scan, as long as they can scan all
4258 * the channels they registered at once.
4259 */
4260 nla_for_each_nested(attr2, freqs, tmp2)
4261 if (attr1 != attr2 &&
4262 nla_get_u32(attr1) == nla_get_u32(attr2))
4263 return 0;
4264 }
4265
4266 return n_channels;
4267}
4268
Johannes Berg2a519312009-02-10 21:25:55 +01004269static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4270{
Johannes Berg4c476992010-10-04 21:36:35 +02004271 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004272 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004273 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004274 struct nlattr *attr;
4275 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004276 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004277 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004278
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004279 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4280 return -EINVAL;
4281
Johannes Berg79c97e92009-07-07 03:56:12 +02004282 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004283
Johannes Berg4c476992010-10-04 21:36:35 +02004284 if (!rdev->ops->scan)
4285 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004286
Johannes Berg4c476992010-10-04 21:36:35 +02004287 if (rdev->scan_req)
4288 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004289
4290 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004291 n_channels = validate_scan_freqs(
4292 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004293 if (!n_channels)
4294 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004295 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004296 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004297 n_channels = 0;
4298
Johannes Berg2a519312009-02-10 21:25:55 +01004299 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4300 if (wiphy->bands[band])
4301 n_channels += wiphy->bands[band]->n_channels;
4302 }
4303
4304 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4305 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4306 n_ssids++;
4307
Johannes Berg4c476992010-10-04 21:36:35 +02004308 if (n_ssids > wiphy->max_scan_ssids)
4309 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004310
Jouni Malinen70692ad2009-02-16 19:39:13 +02004311 if (info->attrs[NL80211_ATTR_IE])
4312 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4313 else
4314 ie_len = 0;
4315
Johannes Berg4c476992010-10-04 21:36:35 +02004316 if (ie_len > wiphy->max_scan_ie_len)
4317 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004318
Johannes Berg2a519312009-02-10 21:25:55 +01004319 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004320 + sizeof(*request->ssids) * n_ssids
4321 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004322 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004323 if (!request)
4324 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004325
Johannes Berg2a519312009-02-10 21:25:55 +01004326 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004327 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004328 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004329 if (ie_len) {
4330 if (request->ssids)
4331 request->ie = (void *)(request->ssids + n_ssids);
4332 else
4333 request->ie = (void *)(request->channels + n_channels);
4334 }
Johannes Berg2a519312009-02-10 21:25:55 +01004335
Johannes Berg584991d2009-11-02 13:32:03 +01004336 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004337 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4338 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004339 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004340 struct ieee80211_channel *chan;
4341
4342 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4343
4344 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004345 err = -EINVAL;
4346 goto out_free;
4347 }
Johannes Berg584991d2009-11-02 13:32:03 +01004348
4349 /* ignore disabled channels */
4350 if (chan->flags & IEEE80211_CHAN_DISABLED)
4351 continue;
4352
4353 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004354 i++;
4355 }
4356 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004357 enum ieee80211_band band;
4358
Johannes Berg2a519312009-02-10 21:25:55 +01004359 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004360 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4361 int j;
4362 if (!wiphy->bands[band])
4363 continue;
4364 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004365 struct ieee80211_channel *chan;
4366
4367 chan = &wiphy->bands[band]->channels[j];
4368
4369 if (chan->flags & IEEE80211_CHAN_DISABLED)
4370 continue;
4371
4372 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004373 i++;
4374 }
4375 }
4376 }
4377
Johannes Berg584991d2009-11-02 13:32:03 +01004378 if (!i) {
4379 err = -EINVAL;
4380 goto out_free;
4381 }
4382
4383 request->n_channels = i;
4384
Johannes Berg2a519312009-02-10 21:25:55 +01004385 i = 0;
4386 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4387 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004388 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004389 err = -EINVAL;
4390 goto out_free;
4391 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004392 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004393 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004394 i++;
4395 }
4396 }
4397
Jouni Malinen70692ad2009-02-16 19:39:13 +02004398 if (info->attrs[NL80211_ATTR_IE]) {
4399 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004400 memcpy((void *)request->ie,
4401 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004402 request->ie_len);
4403 }
4404
Johannes Berg34850ab2011-07-18 18:08:35 +02004405 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004406 if (wiphy->bands[i])
4407 request->rates[i] =
4408 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004409
4410 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4411 nla_for_each_nested(attr,
4412 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4413 tmp) {
4414 enum ieee80211_band band = nla_type(attr);
4415
Dan Carpenter84404622011-07-29 11:52:18 +03004416 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004417 err = -EINVAL;
4418 goto out_free;
4419 }
4420 err = ieee80211_get_ratemask(wiphy->bands[band],
4421 nla_data(attr),
4422 nla_len(attr),
4423 &request->rates[band]);
4424 if (err)
4425 goto out_free;
4426 }
4427 }
4428
Sam Leffler46856bb2012-10-11 21:03:32 -07004429 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004430 request->flags = nla_get_u32(
4431 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004432 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4433 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4434 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4435 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004436 err = -EOPNOTSUPP;
4437 goto out_free;
4438 }
4439 }
Sam Lefflered4737712012-10-11 21:03:31 -07004440
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304441 request->no_cck =
4442 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4443
Johannes Bergfd014282012-06-18 19:17:03 +02004444 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004445 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004446 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004447
Johannes Berg79c97e92009-07-07 03:56:12 +02004448 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004449 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004450
Johannes Berg463d0182009-07-14 00:33:35 +02004451 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004452 nl80211_send_scan_start(rdev, wdev);
4453 if (wdev->netdev)
4454 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004455 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004456 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004457 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004458 kfree(request);
4459 }
Johannes Berg3b858752009-03-12 09:55:09 +01004460
Johannes Berg2a519312009-02-10 21:25:55 +01004461 return err;
4462}
4463
Luciano Coelho807f8a82011-05-11 17:09:35 +03004464static int nl80211_start_sched_scan(struct sk_buff *skb,
4465 struct genl_info *info)
4466{
4467 struct cfg80211_sched_scan_request *request;
4468 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4469 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004470 struct nlattr *attr;
4471 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004472 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004473 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004474 enum ieee80211_band band;
4475 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004476 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004477
4478 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4479 !rdev->ops->sched_scan_start)
4480 return -EOPNOTSUPP;
4481
4482 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4483 return -EINVAL;
4484
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004485 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4486 return -EINVAL;
4487
4488 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4489 if (interval == 0)
4490 return -EINVAL;
4491
Luciano Coelho807f8a82011-05-11 17:09:35 +03004492 wiphy = &rdev->wiphy;
4493
4494 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4495 n_channels = validate_scan_freqs(
4496 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4497 if (!n_channels)
4498 return -EINVAL;
4499 } else {
4500 n_channels = 0;
4501
4502 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4503 if (wiphy->bands[band])
4504 n_channels += wiphy->bands[band]->n_channels;
4505 }
4506
4507 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4508 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4509 tmp)
4510 n_ssids++;
4511
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004512 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004513 return -EINVAL;
4514
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004515 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4516 nla_for_each_nested(attr,
4517 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4518 tmp)
4519 n_match_sets++;
4520
4521 if (n_match_sets > wiphy->max_match_sets)
4522 return -EINVAL;
4523
Luciano Coelho807f8a82011-05-11 17:09:35 +03004524 if (info->attrs[NL80211_ATTR_IE])
4525 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4526 else
4527 ie_len = 0;
4528
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004529 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004530 return -EINVAL;
4531
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004532 mutex_lock(&rdev->sched_scan_mtx);
4533
4534 if (rdev->sched_scan_req) {
4535 err = -EINPROGRESS;
4536 goto out;
4537 }
4538
Luciano Coelho807f8a82011-05-11 17:09:35 +03004539 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004540 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004541 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004542 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004543 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004544 if (!request) {
4545 err = -ENOMEM;
4546 goto out;
4547 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004548
4549 if (n_ssids)
4550 request->ssids = (void *)&request->channels[n_channels];
4551 request->n_ssids = n_ssids;
4552 if (ie_len) {
4553 if (request->ssids)
4554 request->ie = (void *)(request->ssids + n_ssids);
4555 else
4556 request->ie = (void *)(request->channels + n_channels);
4557 }
4558
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004559 if (n_match_sets) {
4560 if (request->ie)
4561 request->match_sets = (void *)(request->ie + ie_len);
4562 else if (request->ssids)
4563 request->match_sets =
4564 (void *)(request->ssids + n_ssids);
4565 else
4566 request->match_sets =
4567 (void *)(request->channels + n_channels);
4568 }
4569 request->n_match_sets = n_match_sets;
4570
Luciano Coelho807f8a82011-05-11 17:09:35 +03004571 i = 0;
4572 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4573 /* user specified, bail out if channel not found */
4574 nla_for_each_nested(attr,
4575 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4576 tmp) {
4577 struct ieee80211_channel *chan;
4578
4579 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4580
4581 if (!chan) {
4582 err = -EINVAL;
4583 goto out_free;
4584 }
4585
4586 /* ignore disabled channels */
4587 if (chan->flags & IEEE80211_CHAN_DISABLED)
4588 continue;
4589
4590 request->channels[i] = chan;
4591 i++;
4592 }
4593 } else {
4594 /* all channels */
4595 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4596 int j;
4597 if (!wiphy->bands[band])
4598 continue;
4599 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4600 struct ieee80211_channel *chan;
4601
4602 chan = &wiphy->bands[band]->channels[j];
4603
4604 if (chan->flags & IEEE80211_CHAN_DISABLED)
4605 continue;
4606
4607 request->channels[i] = chan;
4608 i++;
4609 }
4610 }
4611 }
4612
4613 if (!i) {
4614 err = -EINVAL;
4615 goto out_free;
4616 }
4617
4618 request->n_channels = i;
4619
4620 i = 0;
4621 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4622 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4623 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004624 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004625 err = -EINVAL;
4626 goto out_free;
4627 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004628 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004629 memcpy(request->ssids[i].ssid, nla_data(attr),
4630 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004631 i++;
4632 }
4633 }
4634
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004635 i = 0;
4636 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4637 nla_for_each_nested(attr,
4638 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4639 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004640 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004641
4642 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4643 nla_data(attr), nla_len(attr),
4644 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004645 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004646 if (ssid) {
4647 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4648 err = -EINVAL;
4649 goto out_free;
4650 }
4651 memcpy(request->match_sets[i].ssid.ssid,
4652 nla_data(ssid), nla_len(ssid));
4653 request->match_sets[i].ssid.ssid_len =
4654 nla_len(ssid);
4655 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004656 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4657 if (rssi)
4658 request->rssi_thold = nla_get_u32(rssi);
4659 else
4660 request->rssi_thold =
4661 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004662 i++;
4663 }
4664 }
4665
Luciano Coelho807f8a82011-05-11 17:09:35 +03004666 if (info->attrs[NL80211_ATTR_IE]) {
4667 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4668 memcpy((void *)request->ie,
4669 nla_data(info->attrs[NL80211_ATTR_IE]),
4670 request->ie_len);
4671 }
4672
Sam Leffler46856bb2012-10-11 21:03:32 -07004673 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004674 request->flags = nla_get_u32(
4675 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004676 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4677 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4678 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4679 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004680 err = -EOPNOTSUPP;
4681 goto out_free;
4682 }
4683 }
Sam Lefflered4737712012-10-11 21:03:31 -07004684
Luciano Coelho807f8a82011-05-11 17:09:35 +03004685 request->dev = dev;
4686 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004687 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004688 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004689
Hila Gonene35e4d22012-06-27 17:19:42 +03004690 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004691 if (!err) {
4692 rdev->sched_scan_req = request;
4693 nl80211_send_sched_scan(rdev, dev,
4694 NL80211_CMD_START_SCHED_SCAN);
4695 goto out;
4696 }
4697
4698out_free:
4699 kfree(request);
4700out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004701 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004702 return err;
4703}
4704
4705static int nl80211_stop_sched_scan(struct sk_buff *skb,
4706 struct genl_info *info)
4707{
4708 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004709 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004710
4711 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4712 !rdev->ops->sched_scan_stop)
4713 return -EOPNOTSUPP;
4714
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004715 mutex_lock(&rdev->sched_scan_mtx);
4716 err = __cfg80211_stop_sched_scan(rdev, false);
4717 mutex_unlock(&rdev->sched_scan_mtx);
4718
4719 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004720}
4721
Johannes Berg9720bb32011-06-21 09:45:33 +02004722static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4723 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004724 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004725 struct wireless_dev *wdev,
4726 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004727{
Johannes Berg48ab9052009-07-10 18:42:31 +02004728 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004729 void *hdr;
4730 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004731
4732 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004733
Eric W. Biederman15e47302012-09-07 20:12:54 +00004734 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004735 NL80211_CMD_NEW_SCAN_RESULTS);
4736 if (!hdr)
4737 return -1;
4738
Johannes Berg9720bb32011-06-21 09:45:33 +02004739 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4740
David S. Miller9360ffd2012-03-29 04:41:26 -04004741 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4742 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4743 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004744
4745 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4746 if (!bss)
4747 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004748 if ((!is_zero_ether_addr(res->bssid) &&
4749 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4750 (res->information_elements && res->len_information_elements &&
4751 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4752 res->len_information_elements,
4753 res->information_elements)) ||
4754 (res->beacon_ies && res->len_beacon_ies &&
4755 res->beacon_ies != res->information_elements &&
4756 nla_put(msg, NL80211_BSS_BEACON_IES,
4757 res->len_beacon_ies, res->beacon_ies)))
4758 goto nla_put_failure;
4759 if (res->tsf &&
4760 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4761 goto nla_put_failure;
4762 if (res->beacon_interval &&
4763 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4764 goto nla_put_failure;
4765 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4766 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4767 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4768 jiffies_to_msecs(jiffies - intbss->ts)))
4769 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004770
Johannes Berg77965c92009-02-18 18:45:06 +01004771 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004772 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004773 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4774 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004775 break;
4776 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004777 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4778 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004779 break;
4780 default:
4781 break;
4782 }
4783
Johannes Berg48ab9052009-07-10 18:42:31 +02004784 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004785 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004786 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004787 if (intbss == wdev->current_bss &&
4788 nla_put_u32(msg, NL80211_BSS_STATUS,
4789 NL80211_BSS_STATUS_ASSOCIATED))
4790 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004791 break;
4792 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004793 if (intbss == wdev->current_bss &&
4794 nla_put_u32(msg, NL80211_BSS_STATUS,
4795 NL80211_BSS_STATUS_IBSS_JOINED))
4796 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004797 break;
4798 default:
4799 break;
4800 }
4801
Johannes Berg2a519312009-02-10 21:25:55 +01004802 nla_nest_end(msg, bss);
4803
4804 return genlmsg_end(msg, hdr);
4805
4806 nla_put_failure:
4807 genlmsg_cancel(msg, hdr);
4808 return -EMSGSIZE;
4809}
4810
4811static int nl80211_dump_scan(struct sk_buff *skb,
4812 struct netlink_callback *cb)
4813{
Johannes Berg48ab9052009-07-10 18:42:31 +02004814 struct cfg80211_registered_device *rdev;
4815 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004816 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004817 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004818 int start = cb->args[1], idx = 0;
4819 int err;
4820
Johannes Berg67748892010-10-04 21:14:06 +02004821 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4822 if (err)
4823 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004824
Johannes Berg48ab9052009-07-10 18:42:31 +02004825 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004826
Johannes Berg48ab9052009-07-10 18:42:31 +02004827 wdev_lock(wdev);
4828 spin_lock_bh(&rdev->bss_lock);
4829 cfg80211_bss_expire(rdev);
4830
Johannes Berg9720bb32011-06-21 09:45:33 +02004831 cb->seq = rdev->bss_generation;
4832
Johannes Berg48ab9052009-07-10 18:42:31 +02004833 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004834 if (++idx <= start)
4835 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004836 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004837 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004838 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004839 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004840 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004841 }
4842 }
4843
Johannes Berg48ab9052009-07-10 18:42:31 +02004844 spin_unlock_bh(&rdev->bss_lock);
4845 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004846
4847 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004848 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004849
Johannes Berg67748892010-10-04 21:14:06 +02004850 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004851}
4852
Eric W. Biederman15e47302012-09-07 20:12:54 +00004853static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004854 int flags, struct net_device *dev,
4855 struct survey_info *survey)
4856{
4857 void *hdr;
4858 struct nlattr *infoattr;
4859
Eric W. Biederman15e47302012-09-07 20:12:54 +00004860 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004861 NL80211_CMD_NEW_SURVEY_RESULTS);
4862 if (!hdr)
4863 return -ENOMEM;
4864
David S. Miller9360ffd2012-03-29 04:41:26 -04004865 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4866 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004867
4868 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4869 if (!infoattr)
4870 goto nla_put_failure;
4871
David S. Miller9360ffd2012-03-29 04:41:26 -04004872 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4873 survey->channel->center_freq))
4874 goto nla_put_failure;
4875
4876 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4877 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4878 goto nla_put_failure;
4879 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4880 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4881 goto nla_put_failure;
4882 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4883 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4884 survey->channel_time))
4885 goto nla_put_failure;
4886 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4887 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4888 survey->channel_time_busy))
4889 goto nla_put_failure;
4890 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4891 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4892 survey->channel_time_ext_busy))
4893 goto nla_put_failure;
4894 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4895 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4896 survey->channel_time_rx))
4897 goto nla_put_failure;
4898 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4899 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4900 survey->channel_time_tx))
4901 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004902
4903 nla_nest_end(msg, infoattr);
4904
4905 return genlmsg_end(msg, hdr);
4906
4907 nla_put_failure:
4908 genlmsg_cancel(msg, hdr);
4909 return -EMSGSIZE;
4910}
4911
4912static int nl80211_dump_survey(struct sk_buff *skb,
4913 struct netlink_callback *cb)
4914{
4915 struct survey_info survey;
4916 struct cfg80211_registered_device *dev;
4917 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004918 int survey_idx = cb->args[1];
4919 int res;
4920
Johannes Berg67748892010-10-04 21:14:06 +02004921 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4922 if (res)
4923 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004924
4925 if (!dev->ops->dump_survey) {
4926 res = -EOPNOTSUPP;
4927 goto out_err;
4928 }
4929
4930 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004931 struct ieee80211_channel *chan;
4932
Hila Gonene35e4d22012-06-27 17:19:42 +03004933 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01004934 if (res == -ENOENT)
4935 break;
4936 if (res)
4937 goto out_err;
4938
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004939 /* Survey without a channel doesn't make sense */
4940 if (!survey.channel) {
4941 res = -EINVAL;
4942 goto out;
4943 }
4944
4945 chan = ieee80211_get_channel(&dev->wiphy,
4946 survey.channel->center_freq);
4947 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4948 survey_idx++;
4949 continue;
4950 }
4951
Holger Schurig61fa7132009-11-11 12:25:40 +01004952 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004953 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01004954 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4955 netdev,
4956 &survey) < 0)
4957 goto out;
4958 survey_idx++;
4959 }
4960
4961 out:
4962 cb->args[1] = survey_idx;
4963 res = skb->len;
4964 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004965 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004966 return res;
4967}
4968
Samuel Ortizb23aa672009-07-01 21:26:54 +02004969static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4970{
4971 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4972 NL80211_WPA_VERSION_2));
4973}
4974
Jouni Malinen636a5d32009-03-19 13:39:22 +02004975static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4976{
Johannes Berg4c476992010-10-04 21:36:35 +02004977 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4978 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004979 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03004980 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
4981 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004982 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004983 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004984 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004985
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004986 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4987 return -EINVAL;
4988
4989 if (!info->attrs[NL80211_ATTR_MAC])
4990 return -EINVAL;
4991
Jouni Malinen17780922009-03-27 20:52:47 +02004992 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4993 return -EINVAL;
4994
Johannes Berg19957bb2009-07-02 17:20:43 +02004995 if (!info->attrs[NL80211_ATTR_SSID])
4996 return -EINVAL;
4997
4998 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4999 return -EINVAL;
5000
Johannes Bergfffd0932009-07-08 14:22:54 +02005001 err = nl80211_parse_key(info, &key);
5002 if (err)
5003 return err;
5004
5005 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005006 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5007 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005008 if (!key.p.key || !key.p.key_len)
5009 return -EINVAL;
5010 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5011 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5012 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5013 key.p.key_len != WLAN_KEY_LEN_WEP104))
5014 return -EINVAL;
5015 if (key.idx > 4)
5016 return -EINVAL;
5017 } else {
5018 key.p.key_len = 0;
5019 key.p.key = NULL;
5020 }
5021
Johannes Bergafea0b72010-08-10 09:46:42 +02005022 if (key.idx >= 0) {
5023 int i;
5024 bool ok = false;
5025 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5026 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5027 ok = true;
5028 break;
5029 }
5030 }
Johannes Berg4c476992010-10-04 21:36:35 +02005031 if (!ok)
5032 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005033 }
5034
Johannes Berg4c476992010-10-04 21:36:35 +02005035 if (!rdev->ops->auth)
5036 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005037
Johannes Berg074ac8d2010-09-16 14:58:22 +02005038 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005039 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5040 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005041
Johannes Berg19957bb2009-07-02 17:20:43 +02005042 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005043 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005044 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005045 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5046 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005047
Johannes Berg19957bb2009-07-02 17:20:43 +02005048 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5049 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5050
5051 if (info->attrs[NL80211_ATTR_IE]) {
5052 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5053 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5054 }
5055
5056 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005057 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005058 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005059
Jouni Malinene39e5b52012-09-30 19:29:39 +03005060 if (auth_type == NL80211_AUTHTYPE_SAE &&
5061 !info->attrs[NL80211_ATTR_SAE_DATA])
5062 return -EINVAL;
5063
5064 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5065 if (auth_type != NL80211_AUTHTYPE_SAE)
5066 return -EINVAL;
5067 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5068 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5069 /* need to include at least Auth Transaction and Status Code */
5070 if (sae_data_len < 4)
5071 return -EINVAL;
5072 }
5073
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005074 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5075
Johannes Berg95de8172012-01-20 13:55:25 +01005076 /*
5077 * Since we no longer track auth state, ignore
5078 * requests to only change local state.
5079 */
5080 if (local_state_change)
5081 return 0;
5082
Johannes Berg4c476992010-10-04 21:36:35 +02005083 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5084 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005085 key.p.key, key.p.key_len, key.idx,
5086 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005087}
5088
Johannes Bergc0692b82010-08-27 14:26:53 +03005089static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5090 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005091 struct cfg80211_crypto_settings *settings,
5092 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005093{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005094 memset(settings, 0, sizeof(*settings));
5095
Samuel Ortizb23aa672009-07-01 21:26:54 +02005096 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5097
Johannes Bergc0692b82010-08-27 14:26:53 +03005098 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5099 u16 proto;
5100 proto = nla_get_u16(
5101 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5102 settings->control_port_ethertype = cpu_to_be16(proto);
5103 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5104 proto != ETH_P_PAE)
5105 return -EINVAL;
5106 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5107 settings->control_port_no_encrypt = true;
5108 } else
5109 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5110
Samuel Ortizb23aa672009-07-01 21:26:54 +02005111 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5112 void *data;
5113 int len, i;
5114
5115 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5116 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5117 settings->n_ciphers_pairwise = len / sizeof(u32);
5118
5119 if (len % sizeof(u32))
5120 return -EINVAL;
5121
Johannes Berg3dc27d22009-07-02 21:36:37 +02005122 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005123 return -EINVAL;
5124
5125 memcpy(settings->ciphers_pairwise, data, len);
5126
5127 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005128 if (!cfg80211_supported_cipher_suite(
5129 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005130 settings->ciphers_pairwise[i]))
5131 return -EINVAL;
5132 }
5133
5134 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5135 settings->cipher_group =
5136 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005137 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5138 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005139 return -EINVAL;
5140 }
5141
5142 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5143 settings->wpa_versions =
5144 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5145 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5146 return -EINVAL;
5147 }
5148
5149 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5150 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005151 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005152
5153 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5154 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5155 settings->n_akm_suites = len / sizeof(u32);
5156
5157 if (len % sizeof(u32))
5158 return -EINVAL;
5159
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005160 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5161 return -EINVAL;
5162
Samuel Ortizb23aa672009-07-01 21:26:54 +02005163 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005164 }
5165
5166 return 0;
5167}
5168
Jouni Malinen636a5d32009-03-19 13:39:22 +02005169static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5170{
Johannes Berg4c476992010-10-04 21:36:35 +02005171 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5172 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005173 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005174 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005175 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005176 int err, ssid_len, ie_len = 0;
5177 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005178 u32 flags = 0;
5179 struct ieee80211_ht_cap *ht_capa = NULL;
5180 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005181
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005182 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5183 return -EINVAL;
5184
5185 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005186 !info->attrs[NL80211_ATTR_SSID] ||
5187 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005188 return -EINVAL;
5189
Johannes Berg4c476992010-10-04 21:36:35 +02005190 if (!rdev->ops->assoc)
5191 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005192
Johannes Berg074ac8d2010-09-16 14:58:22 +02005193 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005194 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5195 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005196
Johannes Berg19957bb2009-07-02 17:20:43 +02005197 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005198
Johannes Berg19957bb2009-07-02 17:20:43 +02005199 chan = ieee80211_get_channel(&rdev->wiphy,
5200 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005201 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5202 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005203
Johannes Berg19957bb2009-07-02 17:20:43 +02005204 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5205 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005206
5207 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005208 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5209 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005210 }
5211
Jouni Malinendc6382c2009-05-06 22:09:37 +03005212 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005213 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005214 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005215 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005216 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005217 else if (mfp != NL80211_MFP_NO)
5218 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005219 }
5220
Johannes Berg3e5d7642009-07-07 14:37:26 +02005221 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5222 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5223
Ben Greear7e7c8922011-11-18 11:31:59 -08005224 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5225 flags |= ASSOC_REQ_DISABLE_HT;
5226
5227 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5228 ht_capa_mask =
5229 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5230
5231 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5232 if (!ht_capa_mask)
5233 return -EINVAL;
5234 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5235 }
5236
Johannes Bergc0692b82010-08-27 14:26:53 +03005237 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005238 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005239 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5240 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005241 &crypto, flags, ht_capa,
5242 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005243
Jouni Malinen636a5d32009-03-19 13:39:22 +02005244 return err;
5245}
5246
5247static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5248{
Johannes Berg4c476992010-10-04 21:36:35 +02005249 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5250 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005251 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005252 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005253 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005254 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005255
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005256 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5257 return -EINVAL;
5258
5259 if (!info->attrs[NL80211_ATTR_MAC])
5260 return -EINVAL;
5261
5262 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5263 return -EINVAL;
5264
Johannes Berg4c476992010-10-04 21:36:35 +02005265 if (!rdev->ops->deauth)
5266 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005267
Johannes Berg074ac8d2010-09-16 14:58:22 +02005268 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005269 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5270 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005271
Johannes Berg19957bb2009-07-02 17:20:43 +02005272 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005273
Johannes Berg19957bb2009-07-02 17:20:43 +02005274 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5275 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005276 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005277 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005278 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005279
5280 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005281 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5282 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005283 }
5284
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005285 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5286
Johannes Berg4c476992010-10-04 21:36:35 +02005287 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5288 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005289}
5290
5291static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5292{
Johannes Berg4c476992010-10-04 21:36:35 +02005293 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5294 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005295 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005296 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005297 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005298 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005299
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005300 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5301 return -EINVAL;
5302
5303 if (!info->attrs[NL80211_ATTR_MAC])
5304 return -EINVAL;
5305
5306 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5307 return -EINVAL;
5308
Johannes Berg4c476992010-10-04 21:36:35 +02005309 if (!rdev->ops->disassoc)
5310 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005311
Johannes Berg074ac8d2010-09-16 14:58:22 +02005312 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005313 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5314 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005315
Johannes Berg19957bb2009-07-02 17:20:43 +02005316 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005317
Johannes Berg19957bb2009-07-02 17:20:43 +02005318 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5319 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005320 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005321 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005322 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005323
5324 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005325 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5326 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005327 }
5328
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005329 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5330
Johannes Berg4c476992010-10-04 21:36:35 +02005331 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5332 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005333}
5334
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005335static bool
5336nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5337 int mcast_rate[IEEE80211_NUM_BANDS],
5338 int rateval)
5339{
5340 struct wiphy *wiphy = &rdev->wiphy;
5341 bool found = false;
5342 int band, i;
5343
5344 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5345 struct ieee80211_supported_band *sband;
5346
5347 sband = wiphy->bands[band];
5348 if (!sband)
5349 continue;
5350
5351 for (i = 0; i < sband->n_bitrates; i++) {
5352 if (sband->bitrates[i].bitrate == rateval) {
5353 mcast_rate[band] = i + 1;
5354 found = true;
5355 break;
5356 }
5357 }
5358 }
5359
5360 return found;
5361}
5362
Johannes Berg04a773a2009-04-19 21:24:32 +02005363static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5364{
Johannes Berg4c476992010-10-04 21:36:35 +02005365 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5366 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005367 struct cfg80211_ibss_params ibss;
5368 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005369 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005370 int err;
5371
Johannes Berg8e30bc52009-04-22 17:45:38 +02005372 memset(&ibss, 0, sizeof(ibss));
5373
Johannes Berg04a773a2009-04-19 21:24:32 +02005374 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5375 return -EINVAL;
5376
Johannes Berg683b6d32012-11-08 21:25:48 +01005377 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02005378 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5379 return -EINVAL;
5380
Johannes Berg8e30bc52009-04-22 17:45:38 +02005381 ibss.beacon_interval = 100;
5382
5383 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5384 ibss.beacon_interval =
5385 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5386 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5387 return -EINVAL;
5388 }
5389
Johannes Berg4c476992010-10-04 21:36:35 +02005390 if (!rdev->ops->join_ibss)
5391 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005392
Johannes Berg4c476992010-10-04 21:36:35 +02005393 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5394 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005395
Johannes Berg79c97e92009-07-07 03:56:12 +02005396 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005397
Johannes Berg39193492011-09-16 13:45:25 +02005398 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005399 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005400
5401 if (!is_valid_ether_addr(ibss.bssid))
5402 return -EINVAL;
5403 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005404 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5405 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5406
5407 if (info->attrs[NL80211_ATTR_IE]) {
5408 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5409 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5410 }
5411
Johannes Berg683b6d32012-11-08 21:25:48 +01005412 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
5413 if (err)
5414 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005415
Johannes Berg683b6d32012-11-08 21:25:48 +01005416 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005417 return -EINVAL;
5418
Johannes Berg04a773a2009-04-19 21:24:32 +02005419 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005420 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005421
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005422 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5423 u8 *rates =
5424 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5425 int n_rates =
5426 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5427 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01005428 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005429
Johannes Berg34850ab2011-07-18 18:08:35 +02005430 err = ieee80211_get_ratemask(sband, rates, n_rates,
5431 &ibss.basic_rates);
5432 if (err)
5433 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005434 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005435
5436 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5437 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5438 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5439 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005440
Johannes Berg4c476992010-10-04 21:36:35 +02005441 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305442 bool no_ht = false;
5443
Johannes Berg4c476992010-10-04 21:36:35 +02005444 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305445 info->attrs[NL80211_ATTR_KEYS],
5446 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005447 if (IS_ERR(connkeys))
5448 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305449
Johannes Berg683b6d32012-11-08 21:25:48 +01005450 if ((ibss.chandef._type != NL80211_CHAN_NO_HT) && no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305451 kfree(connkeys);
5452 return -EINVAL;
5453 }
Johannes Berg4c476992010-10-04 21:36:35 +02005454 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005455
Antonio Quartulli267335d2012-01-31 20:25:47 +01005456 ibss.control_port =
5457 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5458
Johannes Berg4c476992010-10-04 21:36:35 +02005459 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005460 if (err)
5461 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005462 return err;
5463}
5464
5465static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5466{
Johannes Berg4c476992010-10-04 21:36:35 +02005467 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5468 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005469
Johannes Berg4c476992010-10-04 21:36:35 +02005470 if (!rdev->ops->leave_ibss)
5471 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005472
Johannes Berg4c476992010-10-04 21:36:35 +02005473 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5474 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005475
Johannes Berg4c476992010-10-04 21:36:35 +02005476 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005477}
5478
Antonio Quartullif4e583c2012-11-02 13:27:48 +01005479static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
5480{
5481 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5482 struct net_device *dev = info->user_ptr[1];
5483 int mcast_rate[IEEE80211_NUM_BANDS];
5484 u32 nla_rate;
5485 int err;
5486
5487 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5488 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
5489 return -EOPNOTSUPP;
5490
5491 if (!rdev->ops->set_mcast_rate)
5492 return -EOPNOTSUPP;
5493
5494 memset(mcast_rate, 0, sizeof(mcast_rate));
5495
5496 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
5497 return -EINVAL;
5498
5499 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
5500 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
5501 return -EINVAL;
5502
5503 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
5504
5505 return err;
5506}
5507
5508
Johannes Bergaff89a92009-07-01 21:26:51 +02005509#ifdef CONFIG_NL80211_TESTMODE
5510static struct genl_multicast_group nl80211_testmode_mcgrp = {
5511 .name = "testmode",
5512};
5513
5514static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5515{
Johannes Berg4c476992010-10-04 21:36:35 +02005516 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005517 int err;
5518
5519 if (!info->attrs[NL80211_ATTR_TESTDATA])
5520 return -EINVAL;
5521
Johannes Bergaff89a92009-07-01 21:26:51 +02005522 err = -EOPNOTSUPP;
5523 if (rdev->ops->testmode_cmd) {
5524 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005525 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005526 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5527 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5528 rdev->testmode_info = NULL;
5529 }
5530
Johannes Bergaff89a92009-07-01 21:26:51 +02005531 return err;
5532}
5533
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005534static int nl80211_testmode_dump(struct sk_buff *skb,
5535 struct netlink_callback *cb)
5536{
Johannes Berg00918d32011-12-13 17:22:05 +01005537 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005538 int err;
5539 long phy_idx;
5540 void *data = NULL;
5541 int data_len = 0;
5542
5543 if (cb->args[0]) {
5544 /*
5545 * 0 is a valid index, but not valid for args[0],
5546 * so we need to offset by 1.
5547 */
5548 phy_idx = cb->args[0] - 1;
5549 } else {
5550 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5551 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5552 nl80211_policy);
5553 if (err)
5554 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005555
Johannes Berg2bd7e352012-06-15 14:23:16 +02005556 mutex_lock(&cfg80211_mutex);
5557 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5558 nl80211_fam.attrbuf);
5559 if (IS_ERR(rdev)) {
5560 mutex_unlock(&cfg80211_mutex);
5561 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005562 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005563 phy_idx = rdev->wiphy_idx;
5564 rdev = NULL;
5565 mutex_unlock(&cfg80211_mutex);
5566
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005567 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5568 cb->args[1] =
5569 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5570 }
5571
5572 if (cb->args[1]) {
5573 data = nla_data((void *)cb->args[1]);
5574 data_len = nla_len((void *)cb->args[1]);
5575 }
5576
5577 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005578 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5579 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005580 mutex_unlock(&cfg80211_mutex);
5581 return -ENOENT;
5582 }
Johannes Berg00918d32011-12-13 17:22:05 +01005583 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005584 mutex_unlock(&cfg80211_mutex);
5585
Johannes Berg00918d32011-12-13 17:22:05 +01005586 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005587 err = -EOPNOTSUPP;
5588 goto out_err;
5589 }
5590
5591 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005592 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005593 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5594 NL80211_CMD_TESTMODE);
5595 struct nlattr *tmdata;
5596
David S. Miller9360ffd2012-03-29 04:41:26 -04005597 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005598 genlmsg_cancel(skb, hdr);
5599 break;
5600 }
5601
5602 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5603 if (!tmdata) {
5604 genlmsg_cancel(skb, hdr);
5605 break;
5606 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005607 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005608 nla_nest_end(skb, tmdata);
5609
5610 if (err == -ENOBUFS || err == -ENOENT) {
5611 genlmsg_cancel(skb, hdr);
5612 break;
5613 } else if (err) {
5614 genlmsg_cancel(skb, hdr);
5615 goto out_err;
5616 }
5617
5618 genlmsg_end(skb, hdr);
5619 }
5620
5621 err = skb->len;
5622 /* see above */
5623 cb->args[0] = phy_idx + 1;
5624 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005625 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005626 return err;
5627}
5628
Johannes Bergaff89a92009-07-01 21:26:51 +02005629static struct sk_buff *
5630__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005631 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005632{
5633 struct sk_buff *skb;
5634 void *hdr;
5635 struct nlattr *data;
5636
5637 skb = nlmsg_new(approxlen + 100, gfp);
5638 if (!skb)
5639 return NULL;
5640
Eric W. Biederman15e47302012-09-07 20:12:54 +00005641 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005642 if (!hdr) {
5643 kfree_skb(skb);
5644 return NULL;
5645 }
5646
David S. Miller9360ffd2012-03-29 04:41:26 -04005647 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5648 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005649 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5650
5651 ((void **)skb->cb)[0] = rdev;
5652 ((void **)skb->cb)[1] = hdr;
5653 ((void **)skb->cb)[2] = data;
5654
5655 return skb;
5656
5657 nla_put_failure:
5658 kfree_skb(skb);
5659 return NULL;
5660}
5661
5662struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5663 int approxlen)
5664{
5665 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5666
5667 if (WARN_ON(!rdev->testmode_info))
5668 return NULL;
5669
5670 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005671 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005672 rdev->testmode_info->snd_seq,
5673 GFP_KERNEL);
5674}
5675EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5676
5677int cfg80211_testmode_reply(struct sk_buff *skb)
5678{
5679 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5680 void *hdr = ((void **)skb->cb)[1];
5681 struct nlattr *data = ((void **)skb->cb)[2];
5682
5683 if (WARN_ON(!rdev->testmode_info)) {
5684 kfree_skb(skb);
5685 return -EINVAL;
5686 }
5687
5688 nla_nest_end(skb, data);
5689 genlmsg_end(skb, hdr);
5690 return genlmsg_reply(skb, rdev->testmode_info);
5691}
5692EXPORT_SYMBOL(cfg80211_testmode_reply);
5693
5694struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5695 int approxlen, gfp_t gfp)
5696{
5697 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5698
5699 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5700}
5701EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5702
5703void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5704{
5705 void *hdr = ((void **)skb->cb)[1];
5706 struct nlattr *data = ((void **)skb->cb)[2];
5707
5708 nla_nest_end(skb, data);
5709 genlmsg_end(skb, hdr);
5710 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5711}
5712EXPORT_SYMBOL(cfg80211_testmode_event);
5713#endif
5714
Samuel Ortizb23aa672009-07-01 21:26:54 +02005715static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5716{
Johannes Berg4c476992010-10-04 21:36:35 +02005717 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5718 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005719 struct cfg80211_connect_params connect;
5720 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005721 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005722 int err;
5723
5724 memset(&connect, 0, sizeof(connect));
5725
5726 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5727 return -EINVAL;
5728
5729 if (!info->attrs[NL80211_ATTR_SSID] ||
5730 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5731 return -EINVAL;
5732
5733 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5734 connect.auth_type =
5735 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005736 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5737 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005738 return -EINVAL;
5739 } else
5740 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5741
5742 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5743
Johannes Bergc0692b82010-08-27 14:26:53 +03005744 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005745 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005746 if (err)
5747 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005748
Johannes Berg074ac8d2010-09-16 14:58:22 +02005749 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005750 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5751 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005752
Johannes Berg79c97e92009-07-07 03:56:12 +02005753 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005754
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305755 connect.bg_scan_period = -1;
5756 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5757 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5758 connect.bg_scan_period =
5759 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5760 }
5761
Samuel Ortizb23aa672009-07-01 21:26:54 +02005762 if (info->attrs[NL80211_ATTR_MAC])
5763 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5764 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5765 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5766
5767 if (info->attrs[NL80211_ATTR_IE]) {
5768 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5769 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5770 }
5771
5772 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5773 connect.channel =
5774 ieee80211_get_channel(wiphy,
5775 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5776 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005777 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5778 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005779 }
5780
Johannes Bergfffd0932009-07-08 14:22:54 +02005781 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5782 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305783 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02005784 if (IS_ERR(connkeys))
5785 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005786 }
5787
Ben Greear7e7c8922011-11-18 11:31:59 -08005788 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5789 connect.flags |= ASSOC_REQ_DISABLE_HT;
5790
5791 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5792 memcpy(&connect.ht_capa_mask,
5793 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5794 sizeof(connect.ht_capa_mask));
5795
5796 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005797 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5798 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005799 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005800 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005801 memcpy(&connect.ht_capa,
5802 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5803 sizeof(connect.ht_capa));
5804 }
5805
Johannes Bergfffd0932009-07-08 14:22:54 +02005806 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005807 if (err)
5808 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005809 return err;
5810}
5811
5812static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5813{
Johannes Berg4c476992010-10-04 21:36:35 +02005814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5815 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005816 u16 reason;
5817
5818 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5819 reason = WLAN_REASON_DEAUTH_LEAVING;
5820 else
5821 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5822
5823 if (reason == 0)
5824 return -EINVAL;
5825
Johannes Berg074ac8d2010-09-16 14:58:22 +02005826 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005827 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5828 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005829
Johannes Berg4c476992010-10-04 21:36:35 +02005830 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005831}
5832
Johannes Berg463d0182009-07-14 00:33:35 +02005833static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5834{
Johannes Berg4c476992010-10-04 21:36:35 +02005835 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005836 struct net *net;
5837 int err;
5838 u32 pid;
5839
5840 if (!info->attrs[NL80211_ATTR_PID])
5841 return -EINVAL;
5842
5843 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5844
Johannes Berg463d0182009-07-14 00:33:35 +02005845 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005846 if (IS_ERR(net))
5847 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005848
5849 err = 0;
5850
5851 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005852 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5853 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005854
Johannes Berg463d0182009-07-14 00:33:35 +02005855 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005856 return err;
5857}
5858
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005859static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5860{
Johannes Berg4c476992010-10-04 21:36:35 +02005861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005862 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5863 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005864 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005865 struct cfg80211_pmksa pmksa;
5866
5867 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5868
5869 if (!info->attrs[NL80211_ATTR_MAC])
5870 return -EINVAL;
5871
5872 if (!info->attrs[NL80211_ATTR_PMKID])
5873 return -EINVAL;
5874
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005875 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5876 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5877
Johannes Berg074ac8d2010-09-16 14:58:22 +02005878 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005879 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5880 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005881
5882 switch (info->genlhdr->cmd) {
5883 case NL80211_CMD_SET_PMKSA:
5884 rdev_ops = rdev->ops->set_pmksa;
5885 break;
5886 case NL80211_CMD_DEL_PMKSA:
5887 rdev_ops = rdev->ops->del_pmksa;
5888 break;
5889 default:
5890 WARN_ON(1);
5891 break;
5892 }
5893
Johannes Berg4c476992010-10-04 21:36:35 +02005894 if (!rdev_ops)
5895 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005896
Johannes Berg4c476992010-10-04 21:36:35 +02005897 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005898}
5899
5900static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5901{
Johannes Berg4c476992010-10-04 21:36:35 +02005902 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5903 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005904
Johannes Berg074ac8d2010-09-16 14:58:22 +02005905 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005906 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5907 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005908
Johannes Berg4c476992010-10-04 21:36:35 +02005909 if (!rdev->ops->flush_pmksa)
5910 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005911
Hila Gonene35e4d22012-06-27 17:19:42 +03005912 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005913}
5914
Arik Nemtsov109086c2011-09-28 14:12:50 +03005915static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5916{
5917 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5918 struct net_device *dev = info->user_ptr[1];
5919 u8 action_code, dialog_token;
5920 u16 status_code;
5921 u8 *peer;
5922
5923 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5924 !rdev->ops->tdls_mgmt)
5925 return -EOPNOTSUPP;
5926
5927 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5928 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5929 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5930 !info->attrs[NL80211_ATTR_IE] ||
5931 !info->attrs[NL80211_ATTR_MAC])
5932 return -EINVAL;
5933
5934 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5935 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5936 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5937 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5938
Hila Gonene35e4d22012-06-27 17:19:42 +03005939 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
5940 dialog_token, status_code,
5941 nla_data(info->attrs[NL80211_ATTR_IE]),
5942 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03005943}
5944
5945static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5946{
5947 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5948 struct net_device *dev = info->user_ptr[1];
5949 enum nl80211_tdls_operation operation;
5950 u8 *peer;
5951
5952 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5953 !rdev->ops->tdls_oper)
5954 return -EOPNOTSUPP;
5955
5956 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5957 !info->attrs[NL80211_ATTR_MAC])
5958 return -EINVAL;
5959
5960 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5961 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5962
Hila Gonene35e4d22012-06-27 17:19:42 +03005963 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03005964}
5965
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005966static int nl80211_remain_on_channel(struct sk_buff *skb,
5967 struct genl_info *info)
5968{
Johannes Berg4c476992010-10-04 21:36:35 +02005969 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005970 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01005971 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005972 struct sk_buff *msg;
5973 void *hdr;
5974 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01005975 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005976 int err;
5977
5978 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5979 !info->attrs[NL80211_ATTR_DURATION])
5980 return -EINVAL;
5981
5982 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5983
Johannes Berg7c4ef712011-11-18 15:33:48 +01005984 if (!rdev->ops->remain_on_channel ||
5985 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005986 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005987
Johannes Bergebf348f2012-06-01 12:50:54 +02005988 /*
5989 * We should be on that channel for at least a minimum amount of
5990 * time (10ms) but no longer than the driver supports.
5991 */
5992 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5993 duration > rdev->wiphy.max_remain_on_channel_duration)
5994 return -EINVAL;
5995
Johannes Berg683b6d32012-11-08 21:25:48 +01005996 err = nl80211_parse_chandef(rdev, info, &chandef);
5997 if (err)
5998 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005999
6000 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006001 if (!msg)
6002 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006003
Eric W. Biederman15e47302012-09-07 20:12:54 +00006004 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006005 NL80211_CMD_REMAIN_ON_CHANNEL);
6006
6007 if (IS_ERR(hdr)) {
6008 err = PTR_ERR(hdr);
6009 goto free_msg;
6010 }
6011
Johannes Berg683b6d32012-11-08 21:25:48 +01006012 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6013 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006014
6015 if (err)
6016 goto free_msg;
6017
David S. Miller9360ffd2012-03-29 04:41:26 -04006018 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6019 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006020
6021 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006022
6023 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006024
6025 nla_put_failure:
6026 err = -ENOBUFS;
6027 free_msg:
6028 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006029 return err;
6030}
6031
6032static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6033 struct genl_info *info)
6034{
Johannes Berg4c476992010-10-04 21:36:35 +02006035 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006036 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006037 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006038
6039 if (!info->attrs[NL80211_ATTR_COOKIE])
6040 return -EINVAL;
6041
Johannes Berg4c476992010-10-04 21:36:35 +02006042 if (!rdev->ops->cancel_remain_on_channel)
6043 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006044
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006045 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6046
Hila Gonene35e4d22012-06-27 17:19:42 +03006047 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006048}
6049
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006050static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6051 u8 *rates, u8 rates_len)
6052{
6053 u8 i;
6054 u32 mask = 0;
6055
6056 for (i = 0; i < rates_len; i++) {
6057 int rate = (rates[i] & 0x7f) * 5;
6058 int ridx;
6059 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6060 struct ieee80211_rate *srate =
6061 &sband->bitrates[ridx];
6062 if (rate == srate->bitrate) {
6063 mask |= 1 << ridx;
6064 break;
6065 }
6066 }
6067 if (ridx == sband->n_bitrates)
6068 return 0; /* rate not found */
6069 }
6070
6071 return mask;
6072}
6073
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006074static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6075 u8 *rates, u8 rates_len,
6076 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6077{
6078 u8 i;
6079
6080 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6081
6082 for (i = 0; i < rates_len; i++) {
6083 int ridx, rbit;
6084
6085 ridx = rates[i] / 8;
6086 rbit = BIT(rates[i] % 8);
6087
6088 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006089 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006090 return false;
6091
6092 /* check availability */
6093 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6094 mcs[ridx] |= rbit;
6095 else
6096 return false;
6097 }
6098
6099 return true;
6100}
6101
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006102static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006103 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6104 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006105 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6106 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006107};
6108
6109static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6110 struct genl_info *info)
6111{
6112 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006113 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006114 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006115 int rem, i;
6116 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006117 struct nlattr *tx_rates;
6118 struct ieee80211_supported_band *sband;
6119
6120 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6121 return -EINVAL;
6122
Johannes Berg4c476992010-10-04 21:36:35 +02006123 if (!rdev->ops->set_bitrate_mask)
6124 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006125
6126 memset(&mask, 0, sizeof(mask));
6127 /* Default to all rates enabled */
6128 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6129 sband = rdev->wiphy.bands[i];
6130 mask.control[i].legacy =
6131 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006132 if (sband)
6133 memcpy(mask.control[i].mcs,
6134 sband->ht_cap.mcs.rx_mask,
6135 sizeof(mask.control[i].mcs));
6136 else
6137 memset(mask.control[i].mcs, 0,
6138 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006139 }
6140
6141 /*
6142 * The nested attribute uses enum nl80211_band as the index. This maps
6143 * directly to the enum ieee80211_band values used in cfg80211.
6144 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006145 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006146 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6147 {
6148 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006149 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6150 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006151 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006152 if (sband == NULL)
6153 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006154 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6155 nla_len(tx_rates), nl80211_txattr_policy);
6156 if (tb[NL80211_TXRATE_LEGACY]) {
6157 mask.control[band].legacy = rateset_to_mask(
6158 sband,
6159 nla_data(tb[NL80211_TXRATE_LEGACY]),
6160 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306161 if ((mask.control[band].legacy == 0) &&
6162 nla_len(tb[NL80211_TXRATE_LEGACY]))
6163 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006164 }
6165 if (tb[NL80211_TXRATE_MCS]) {
6166 if (!ht_rateset_to_mask(
6167 sband,
6168 nla_data(tb[NL80211_TXRATE_MCS]),
6169 nla_len(tb[NL80211_TXRATE_MCS]),
6170 mask.control[band].mcs))
6171 return -EINVAL;
6172 }
6173
6174 if (mask.control[band].legacy == 0) {
6175 /* don't allow empty legacy rates if HT
6176 * is not even supported. */
6177 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6178 return -EINVAL;
6179
6180 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6181 if (mask.control[band].mcs[i])
6182 break;
6183
6184 /* legacy and mcs rates may not be both empty */
6185 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006186 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006187 }
6188 }
6189
Hila Gonene35e4d22012-06-27 17:19:42 +03006190 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006191}
6192
Johannes Berg2e161f72010-08-12 15:38:38 +02006193static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006194{
Johannes Berg4c476992010-10-04 21:36:35 +02006195 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006196 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006197 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006198
6199 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6200 return -EINVAL;
6201
Johannes Berg2e161f72010-08-12 15:38:38 +02006202 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6203 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006204
Johannes Berg71bbc992012-06-15 15:30:18 +02006205 switch (wdev->iftype) {
6206 case NL80211_IFTYPE_STATION:
6207 case NL80211_IFTYPE_ADHOC:
6208 case NL80211_IFTYPE_P2P_CLIENT:
6209 case NL80211_IFTYPE_AP:
6210 case NL80211_IFTYPE_AP_VLAN:
6211 case NL80211_IFTYPE_MESH_POINT:
6212 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006213 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006214 break;
6215 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006216 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006217 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006218
6219 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006220 if (!rdev->ops->mgmt_tx)
6221 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006222
Eric W. Biederman15e47302012-09-07 20:12:54 +00006223 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006224 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6225 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006226}
6227
Johannes Berg2e161f72010-08-12 15:38:38 +02006228static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006229{
Johannes Berg4c476992010-10-04 21:36:35 +02006230 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006231 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006232 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02006233 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006234 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006235 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006236 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006237 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006238 bool offchan, no_cck, dont_wait_for_ack;
6239
6240 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006241
Johannes Berg683b6d32012-11-08 21:25:48 +01006242 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02006243 return -EINVAL;
6244
Johannes Berg4c476992010-10-04 21:36:35 +02006245 if (!rdev->ops->mgmt_tx)
6246 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006247
Johannes Berg71bbc992012-06-15 15:30:18 +02006248 switch (wdev->iftype) {
6249 case NL80211_IFTYPE_STATION:
6250 case NL80211_IFTYPE_ADHOC:
6251 case NL80211_IFTYPE_P2P_CLIENT:
6252 case NL80211_IFTYPE_AP:
6253 case NL80211_IFTYPE_AP_VLAN:
6254 case NL80211_IFTYPE_MESH_POINT:
6255 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006256 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006257 break;
6258 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006259 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006260 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006261
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006262 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006263 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006264 return -EINVAL;
6265 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006266
6267 /*
6268 * We should wait on the channel for at least a minimum amount
6269 * of time (10ms) but no longer than the driver supports.
6270 */
6271 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6272 wait > rdev->wiphy.max_remain_on_channel_duration)
6273 return -EINVAL;
6274
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006275 }
6276
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006277 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6278
Johannes Berg7c4ef712011-11-18 15:33:48 +01006279 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6280 return -EINVAL;
6281
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306282 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6283
Johannes Berg683b6d32012-11-08 21:25:48 +01006284 err = nl80211_parse_chandef(rdev, info, &chandef);
6285 if (err)
6286 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02006287
Johannes Berge247bd902011-11-04 11:18:21 +01006288 if (!dont_wait_for_ack) {
6289 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6290 if (!msg)
6291 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006292
Eric W. Biederman15e47302012-09-07 20:12:54 +00006293 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006294 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006295
Johannes Berge247bd902011-11-04 11:18:21 +01006296 if (IS_ERR(hdr)) {
6297 err = PTR_ERR(hdr);
6298 goto free_msg;
6299 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006300 }
Johannes Berge247bd902011-11-04 11:18:21 +01006301
Johannes Berg683b6d32012-11-08 21:25:48 +01006302 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006303 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6304 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006305 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006306 if (err)
6307 goto free_msg;
6308
Johannes Berge247bd902011-11-04 11:18:21 +01006309 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006310 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6311 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006312
Johannes Berge247bd902011-11-04 11:18:21 +01006313 genlmsg_end(msg, hdr);
6314 return genlmsg_reply(msg, info);
6315 }
6316
6317 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006318
6319 nla_put_failure:
6320 err = -ENOBUFS;
6321 free_msg:
6322 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006323 return err;
6324}
6325
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006326static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6327{
6328 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006329 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006330 u64 cookie;
6331
6332 if (!info->attrs[NL80211_ATTR_COOKIE])
6333 return -EINVAL;
6334
6335 if (!rdev->ops->mgmt_tx_cancel_wait)
6336 return -EOPNOTSUPP;
6337
Johannes Berg71bbc992012-06-15 15:30:18 +02006338 switch (wdev->iftype) {
6339 case NL80211_IFTYPE_STATION:
6340 case NL80211_IFTYPE_ADHOC:
6341 case NL80211_IFTYPE_P2P_CLIENT:
6342 case NL80211_IFTYPE_AP:
6343 case NL80211_IFTYPE_AP_VLAN:
6344 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006345 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006346 break;
6347 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006348 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006349 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006350
6351 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6352
Hila Gonene35e4d22012-06-27 17:19:42 +03006353 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006354}
6355
Kalle Valoffb9eb32010-02-17 17:58:10 +02006356static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6357{
Johannes Berg4c476992010-10-04 21:36:35 +02006358 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006359 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006360 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006361 u8 ps_state;
6362 bool state;
6363 int err;
6364
Johannes Berg4c476992010-10-04 21:36:35 +02006365 if (!info->attrs[NL80211_ATTR_PS_STATE])
6366 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006367
6368 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6369
Johannes Berg4c476992010-10-04 21:36:35 +02006370 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6371 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006372
6373 wdev = dev->ieee80211_ptr;
6374
Johannes Berg4c476992010-10-04 21:36:35 +02006375 if (!rdev->ops->set_power_mgmt)
6376 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006377
6378 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6379
6380 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006381 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006382
Hila Gonene35e4d22012-06-27 17:19:42 +03006383 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006384 if (!err)
6385 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006386 return err;
6387}
6388
6389static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6390{
Johannes Berg4c476992010-10-04 21:36:35 +02006391 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006392 enum nl80211_ps_state ps_state;
6393 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006394 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006395 struct sk_buff *msg;
6396 void *hdr;
6397 int err;
6398
Kalle Valoffb9eb32010-02-17 17:58:10 +02006399 wdev = dev->ieee80211_ptr;
6400
Johannes Berg4c476992010-10-04 21:36:35 +02006401 if (!rdev->ops->set_power_mgmt)
6402 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006403
6404 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006405 if (!msg)
6406 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006407
Eric W. Biederman15e47302012-09-07 20:12:54 +00006408 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006409 NL80211_CMD_GET_POWER_SAVE);
6410 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006411 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006412 goto free_msg;
6413 }
6414
6415 if (wdev->ps)
6416 ps_state = NL80211_PS_ENABLED;
6417 else
6418 ps_state = NL80211_PS_DISABLED;
6419
David S. Miller9360ffd2012-03-29 04:41:26 -04006420 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6421 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006422
6423 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006424 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006425
Johannes Berg4c476992010-10-04 21:36:35 +02006426 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006427 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006428 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006429 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006430 return err;
6431}
6432
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006433static struct nla_policy
6434nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6435 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6436 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6437 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006438 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6439 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6440 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006441};
6442
Thomas Pedersen84f10702012-07-12 16:17:33 -07006443static int nl80211_set_cqm_txe(struct genl_info *info,
6444 u32 rate, u32 pkts, u32 intvl)
6445{
6446 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6447 struct wireless_dev *wdev;
6448 struct net_device *dev = info->user_ptr[1];
6449
6450 if ((rate < 0 || rate > 100) ||
6451 (intvl < 0 || intvl > NL80211_CQM_TXE_MAX_INTVL))
6452 return -EINVAL;
6453
6454 wdev = dev->ieee80211_ptr;
6455
6456 if (!rdev->ops->set_cqm_txe_config)
6457 return -EOPNOTSUPP;
6458
6459 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6460 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6461 return -EOPNOTSUPP;
6462
Hila Gonene35e4d22012-06-27 17:19:42 +03006463 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006464}
6465
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006466static int nl80211_set_cqm_rssi(struct genl_info *info,
6467 s32 threshold, u32 hysteresis)
6468{
Johannes Berg4c476992010-10-04 21:36:35 +02006469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006470 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006471 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006472
6473 if (threshold > 0)
6474 return -EINVAL;
6475
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006476 wdev = dev->ieee80211_ptr;
6477
Johannes Berg4c476992010-10-04 21:36:35 +02006478 if (!rdev->ops->set_cqm_rssi_config)
6479 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006480
Johannes Berg074ac8d2010-09-16 14:58:22 +02006481 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006482 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6483 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006484
Hila Gonene35e4d22012-06-27 17:19:42 +03006485 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006486}
6487
6488static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6489{
6490 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6491 struct nlattr *cqm;
6492 int err;
6493
6494 cqm = info->attrs[NL80211_ATTR_CQM];
6495 if (!cqm) {
6496 err = -EINVAL;
6497 goto out;
6498 }
6499
6500 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6501 nl80211_attr_cqm_policy);
6502 if (err)
6503 goto out;
6504
6505 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6506 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6507 s32 threshold;
6508 u32 hysteresis;
6509 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6510 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6511 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006512 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6513 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6514 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6515 u32 rate, pkts, intvl;
6516 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6517 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6518 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6519 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006520 } else
6521 err = -EINVAL;
6522
6523out:
6524 return err;
6525}
6526
Johannes Berg29cbe682010-12-03 09:20:44 +01006527static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6528{
6529 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6530 struct net_device *dev = info->user_ptr[1];
6531 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006532 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006533 int err;
6534
6535 /* start with default */
6536 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006537 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006538
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006539 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006540 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006541 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006542 if (err)
6543 return err;
6544 }
6545
6546 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6547 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6548 return -EINVAL;
6549
Javier Cardonac80d5452010-12-16 17:37:49 -08006550 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6551 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6552
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006553 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6554 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6555 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6556 return -EINVAL;
6557
Javier Cardonac80d5452010-12-16 17:37:49 -08006558 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6559 /* parse additional setup parameters if given */
6560 err = nl80211_parse_mesh_setup(info, &setup);
6561 if (err)
6562 return err;
6563 }
6564
Johannes Bergcc1d2802012-05-16 23:50:20 +02006565 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01006566 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
6567 if (err)
6568 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006569 } else {
6570 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01006571 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02006572 }
6573
Javier Cardonac80d5452010-12-16 17:37:49 -08006574 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006575}
6576
6577static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6578{
6579 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6580 struct net_device *dev = info->user_ptr[1];
6581
6582 return cfg80211_leave_mesh(rdev, dev);
6583}
6584
Johannes Bergdfb89c52012-06-27 09:23:48 +02006585#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006586static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6587{
6588 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6589 struct sk_buff *msg;
6590 void *hdr;
6591
6592 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6593 return -EOPNOTSUPP;
6594
6595 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6596 if (!msg)
6597 return -ENOMEM;
6598
Eric W. Biederman15e47302012-09-07 20:12:54 +00006599 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006600 NL80211_CMD_GET_WOWLAN);
6601 if (!hdr)
6602 goto nla_put_failure;
6603
6604 if (rdev->wowlan) {
6605 struct nlattr *nl_wowlan;
6606
6607 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6608 if (!nl_wowlan)
6609 goto nla_put_failure;
6610
David S. Miller9360ffd2012-03-29 04:41:26 -04006611 if ((rdev->wowlan->any &&
6612 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6613 (rdev->wowlan->disconnect &&
6614 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6615 (rdev->wowlan->magic_pkt &&
6616 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6617 (rdev->wowlan->gtk_rekey_failure &&
6618 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6619 (rdev->wowlan->eap_identity_req &&
6620 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6621 (rdev->wowlan->four_way_handshake &&
6622 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6623 (rdev->wowlan->rfkill_release &&
6624 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6625 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006626 if (rdev->wowlan->n_patterns) {
6627 struct nlattr *nl_pats, *nl_pat;
6628 int i, pat_len;
6629
6630 nl_pats = nla_nest_start(msg,
6631 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6632 if (!nl_pats)
6633 goto nla_put_failure;
6634
6635 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6636 nl_pat = nla_nest_start(msg, i + 1);
6637 if (!nl_pat)
6638 goto nla_put_failure;
6639 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006640 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6641 DIV_ROUND_UP(pat_len, 8),
6642 rdev->wowlan->patterns[i].mask) ||
6643 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6644 pat_len,
6645 rdev->wowlan->patterns[i].pattern))
6646 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006647 nla_nest_end(msg, nl_pat);
6648 }
6649 nla_nest_end(msg, nl_pats);
6650 }
6651
6652 nla_nest_end(msg, nl_wowlan);
6653 }
6654
6655 genlmsg_end(msg, hdr);
6656 return genlmsg_reply(msg, info);
6657
6658nla_put_failure:
6659 nlmsg_free(msg);
6660 return -ENOBUFS;
6661}
6662
6663static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6664{
6665 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6666 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006667 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006668 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006669 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6670 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006671 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006672
6673 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6674 return -EOPNOTSUPP;
6675
Johannes Bergae33bd82012-07-12 16:25:02 +02006676 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6677 cfg80211_rdev_free_wowlan(rdev);
6678 rdev->wowlan = NULL;
6679 goto set_wakeup;
6680 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006681
6682 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6683 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6684 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6685 nl80211_wowlan_policy);
6686 if (err)
6687 return err;
6688
6689 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6690 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6691 return -EINVAL;
6692 new_triggers.any = true;
6693 }
6694
6695 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6696 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6697 return -EINVAL;
6698 new_triggers.disconnect = true;
6699 }
6700
6701 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6702 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6703 return -EINVAL;
6704 new_triggers.magic_pkt = true;
6705 }
6706
Johannes Berg77dbbb12011-07-13 10:48:55 +02006707 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6708 return -EINVAL;
6709
6710 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6711 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6712 return -EINVAL;
6713 new_triggers.gtk_rekey_failure = true;
6714 }
6715
6716 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6717 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6718 return -EINVAL;
6719 new_triggers.eap_identity_req = true;
6720 }
6721
6722 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6723 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6724 return -EINVAL;
6725 new_triggers.four_way_handshake = true;
6726 }
6727
6728 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6729 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6730 return -EINVAL;
6731 new_triggers.rfkill_release = true;
6732 }
6733
Johannes Bergff1b6e62011-05-04 15:37:28 +02006734 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6735 struct nlattr *pat;
6736 int n_patterns = 0;
6737 int rem, pat_len, mask_len;
6738 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6739
6740 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6741 rem)
6742 n_patterns++;
6743 if (n_patterns > wowlan->n_patterns)
6744 return -EINVAL;
6745
6746 new_triggers.patterns = kcalloc(n_patterns,
6747 sizeof(new_triggers.patterns[0]),
6748 GFP_KERNEL);
6749 if (!new_triggers.patterns)
6750 return -ENOMEM;
6751
6752 new_triggers.n_patterns = n_patterns;
6753 i = 0;
6754
6755 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6756 rem) {
6757 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6758 nla_data(pat), nla_len(pat), NULL);
6759 err = -EINVAL;
6760 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6761 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6762 goto error;
6763 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6764 mask_len = DIV_ROUND_UP(pat_len, 8);
6765 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6766 mask_len)
6767 goto error;
6768 if (pat_len > wowlan->pattern_max_len ||
6769 pat_len < wowlan->pattern_min_len)
6770 goto error;
6771
6772 new_triggers.patterns[i].mask =
6773 kmalloc(mask_len + pat_len, GFP_KERNEL);
6774 if (!new_triggers.patterns[i].mask) {
6775 err = -ENOMEM;
6776 goto error;
6777 }
6778 new_triggers.patterns[i].pattern =
6779 new_triggers.patterns[i].mask + mask_len;
6780 memcpy(new_triggers.patterns[i].mask,
6781 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6782 mask_len);
6783 new_triggers.patterns[i].pattern_len = pat_len;
6784 memcpy(new_triggers.patterns[i].pattern,
6785 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6786 pat_len);
6787 i++;
6788 }
6789 }
6790
Johannes Bergae33bd82012-07-12 16:25:02 +02006791 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6792 if (!ntrig) {
6793 err = -ENOMEM;
6794 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006795 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006796 cfg80211_rdev_free_wowlan(rdev);
6797 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006798
Johannes Bergae33bd82012-07-12 16:25:02 +02006799 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006800 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03006801 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02006802
Johannes Bergff1b6e62011-05-04 15:37:28 +02006803 return 0;
6804 error:
6805 for (i = 0; i < new_triggers.n_patterns; i++)
6806 kfree(new_triggers.patterns[i].mask);
6807 kfree(new_triggers.patterns);
6808 return err;
6809}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006810#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006811
Johannes Berge5497d72011-07-05 16:35:40 +02006812static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6813{
6814 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6815 struct net_device *dev = info->user_ptr[1];
6816 struct wireless_dev *wdev = dev->ieee80211_ptr;
6817 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6818 struct cfg80211_gtk_rekey_data rekey_data;
6819 int err;
6820
6821 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6822 return -EINVAL;
6823
6824 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6825 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6826 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6827 nl80211_rekey_policy);
6828 if (err)
6829 return err;
6830
6831 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6832 return -ERANGE;
6833 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6834 return -ERANGE;
6835 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6836 return -ERANGE;
6837
6838 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6839 NL80211_KEK_LEN);
6840 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6841 NL80211_KCK_LEN);
6842 memcpy(rekey_data.replay_ctr,
6843 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6844 NL80211_REPLAY_CTR_LEN);
6845
6846 wdev_lock(wdev);
6847 if (!wdev->current_bss) {
6848 err = -ENOTCONN;
6849 goto out;
6850 }
6851
6852 if (!rdev->ops->set_rekey_data) {
6853 err = -EOPNOTSUPP;
6854 goto out;
6855 }
6856
Hila Gonene35e4d22012-06-27 17:19:42 +03006857 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02006858 out:
6859 wdev_unlock(wdev);
6860 return err;
6861}
6862
Johannes Berg28946da2011-11-04 11:18:12 +01006863static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6864 struct genl_info *info)
6865{
6866 struct net_device *dev = info->user_ptr[1];
6867 struct wireless_dev *wdev = dev->ieee80211_ptr;
6868
6869 if (wdev->iftype != NL80211_IFTYPE_AP &&
6870 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6871 return -EINVAL;
6872
Eric W. Biederman15e47302012-09-07 20:12:54 +00006873 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01006874 return -EBUSY;
6875
Eric W. Biederman15e47302012-09-07 20:12:54 +00006876 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01006877 return 0;
6878}
6879
Johannes Berg7f6cf312011-11-04 11:18:15 +01006880static int nl80211_probe_client(struct sk_buff *skb,
6881 struct genl_info *info)
6882{
6883 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6884 struct net_device *dev = info->user_ptr[1];
6885 struct wireless_dev *wdev = dev->ieee80211_ptr;
6886 struct sk_buff *msg;
6887 void *hdr;
6888 const u8 *addr;
6889 u64 cookie;
6890 int err;
6891
6892 if (wdev->iftype != NL80211_IFTYPE_AP &&
6893 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6894 return -EOPNOTSUPP;
6895
6896 if (!info->attrs[NL80211_ATTR_MAC])
6897 return -EINVAL;
6898
6899 if (!rdev->ops->probe_client)
6900 return -EOPNOTSUPP;
6901
6902 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6903 if (!msg)
6904 return -ENOMEM;
6905
Eric W. Biederman15e47302012-09-07 20:12:54 +00006906 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01006907 NL80211_CMD_PROBE_CLIENT);
6908
6909 if (IS_ERR(hdr)) {
6910 err = PTR_ERR(hdr);
6911 goto free_msg;
6912 }
6913
6914 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6915
Hila Gonene35e4d22012-06-27 17:19:42 +03006916 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01006917 if (err)
6918 goto free_msg;
6919
David S. Miller9360ffd2012-03-29 04:41:26 -04006920 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6921 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006922
6923 genlmsg_end(msg, hdr);
6924
6925 return genlmsg_reply(msg, info);
6926
6927 nla_put_failure:
6928 err = -ENOBUFS;
6929 free_msg:
6930 nlmsg_free(msg);
6931 return err;
6932}
6933
Johannes Berg5e760232011-11-04 11:18:17 +01006934static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6935{
6936 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07006937 struct cfg80211_beacon_registration *reg, *nreg;
6938 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01006939
6940 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6941 return -EOPNOTSUPP;
6942
Ben Greear37c73b52012-10-26 14:49:25 -07006943 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
6944 if (!nreg)
6945 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01006946
Ben Greear37c73b52012-10-26 14:49:25 -07006947 /* First, check if already registered. */
6948 spin_lock_bh(&rdev->beacon_registrations_lock);
6949 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
6950 if (reg->nlportid == info->snd_portid) {
6951 rv = -EALREADY;
6952 goto out_err;
6953 }
6954 }
6955 /* Add it to the list */
6956 nreg->nlportid = info->snd_portid;
6957 list_add(&nreg->list, &rdev->beacon_registrations);
6958
6959 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01006960
6961 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07006962out_err:
6963 spin_unlock_bh(&rdev->beacon_registrations_lock);
6964 kfree(nreg);
6965 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01006966}
6967
Johannes Berg98104fde2012-06-16 00:19:54 +02006968static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
6969{
6970 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6971 struct wireless_dev *wdev = info->user_ptr[1];
6972 int err;
6973
6974 if (!rdev->ops->start_p2p_device)
6975 return -EOPNOTSUPP;
6976
6977 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
6978 return -EOPNOTSUPP;
6979
6980 if (wdev->p2p_started)
6981 return 0;
6982
6983 mutex_lock(&rdev->devlist_mtx);
6984 err = cfg80211_can_add_interface(rdev, wdev->iftype);
6985 mutex_unlock(&rdev->devlist_mtx);
6986 if (err)
6987 return err;
6988
Johannes Bergeeb126e2012-10-23 15:16:50 +02006989 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02006990 if (err)
6991 return err;
6992
6993 wdev->p2p_started = true;
6994 mutex_lock(&rdev->devlist_mtx);
6995 rdev->opencount++;
6996 mutex_unlock(&rdev->devlist_mtx);
6997
6998 return 0;
6999}
7000
7001static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
7002{
7003 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7004 struct wireless_dev *wdev = info->user_ptr[1];
7005
7006 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
7007 return -EOPNOTSUPP;
7008
7009 if (!rdev->ops->stop_p2p_device)
7010 return -EOPNOTSUPP;
7011
7012 if (!wdev->p2p_started)
7013 return 0;
7014
Johannes Bergeeb126e2012-10-23 15:16:50 +02007015 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007016 wdev->p2p_started = false;
7017
7018 mutex_lock(&rdev->devlist_mtx);
7019 rdev->opencount--;
7020 mutex_unlock(&rdev->devlist_mtx);
7021
7022 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
7023 rdev->scan_req->aborted = true;
7024 ___cfg80211_scan_done(rdev, true);
7025 }
7026
7027 return 0;
7028}
7029
Johannes Berg4c476992010-10-04 21:36:35 +02007030#define NL80211_FLAG_NEED_WIPHY 0x01
7031#define NL80211_FLAG_NEED_NETDEV 0x02
7032#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02007033#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
7034#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
7035 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02007036#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02007037/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02007038#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
7039 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02007040
7041static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
7042 struct genl_info *info)
7043{
7044 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007045 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007046 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007047 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7048
7049 if (rtnl)
7050 rtnl_lock();
7051
7052 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007053 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007054 if (IS_ERR(rdev)) {
7055 if (rtnl)
7056 rtnl_unlock();
7057 return PTR_ERR(rdev);
7058 }
7059 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007060 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7061 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007062 mutex_lock(&cfg80211_mutex);
7063 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7064 info->attrs);
7065 if (IS_ERR(wdev)) {
7066 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007067 if (rtnl)
7068 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007069 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007070 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007071
Johannes Berg89a54e42012-06-15 14:33:17 +02007072 dev = wdev->netdev;
7073 rdev = wiphy_to_dev(wdev->wiphy);
7074
Johannes Berg1bf614e2012-06-15 15:23:36 +02007075 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7076 if (!dev) {
7077 mutex_unlock(&cfg80211_mutex);
7078 if (rtnl)
7079 rtnl_unlock();
7080 return -EINVAL;
7081 }
7082
7083 info->user_ptr[1] = dev;
7084 } else {
7085 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007086 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007087
Johannes Berg1bf614e2012-06-15 15:23:36 +02007088 if (dev) {
7089 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7090 !netif_running(dev)) {
7091 mutex_unlock(&cfg80211_mutex);
7092 if (rtnl)
7093 rtnl_unlock();
7094 return -ENETDOWN;
7095 }
7096
7097 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007098 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7099 if (!wdev->p2p_started) {
7100 mutex_unlock(&cfg80211_mutex);
7101 if (rtnl)
7102 rtnl_unlock();
7103 return -ENETDOWN;
7104 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007105 }
7106
Johannes Berg89a54e42012-06-15 14:33:17 +02007107 cfg80211_lock_rdev(rdev);
7108
7109 mutex_unlock(&cfg80211_mutex);
7110
Johannes Berg4c476992010-10-04 21:36:35 +02007111 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007112 }
7113
7114 return 0;
7115}
7116
7117static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7118 struct genl_info *info)
7119{
7120 if (info->user_ptr[0])
7121 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007122 if (info->user_ptr[1]) {
7123 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7124 struct wireless_dev *wdev = info->user_ptr[1];
7125
7126 if (wdev->netdev)
7127 dev_put(wdev->netdev);
7128 } else {
7129 dev_put(info->user_ptr[1]);
7130 }
7131 }
Johannes Berg4c476992010-10-04 21:36:35 +02007132 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7133 rtnl_unlock();
7134}
7135
Johannes Berg55682962007-09-20 13:09:35 -04007136static struct genl_ops nl80211_ops[] = {
7137 {
7138 .cmd = NL80211_CMD_GET_WIPHY,
7139 .doit = nl80211_get_wiphy,
7140 .dumpit = nl80211_dump_wiphy,
7141 .policy = nl80211_policy,
7142 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007143 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007144 },
7145 {
7146 .cmd = NL80211_CMD_SET_WIPHY,
7147 .doit = nl80211_set_wiphy,
7148 .policy = nl80211_policy,
7149 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007150 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007151 },
7152 {
7153 .cmd = NL80211_CMD_GET_INTERFACE,
7154 .doit = nl80211_get_interface,
7155 .dumpit = nl80211_dump_interface,
7156 .policy = nl80211_policy,
7157 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007158 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007159 },
7160 {
7161 .cmd = NL80211_CMD_SET_INTERFACE,
7162 .doit = nl80211_set_interface,
7163 .policy = nl80211_policy,
7164 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007165 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7166 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007167 },
7168 {
7169 .cmd = NL80211_CMD_NEW_INTERFACE,
7170 .doit = nl80211_new_interface,
7171 .policy = nl80211_policy,
7172 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007173 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7174 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007175 },
7176 {
7177 .cmd = NL80211_CMD_DEL_INTERFACE,
7178 .doit = nl80211_del_interface,
7179 .policy = nl80211_policy,
7180 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007181 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007182 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007183 },
Johannes Berg41ade002007-12-19 02:03:29 +01007184 {
7185 .cmd = NL80211_CMD_GET_KEY,
7186 .doit = nl80211_get_key,
7187 .policy = nl80211_policy,
7188 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007189 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007190 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007191 },
7192 {
7193 .cmd = NL80211_CMD_SET_KEY,
7194 .doit = nl80211_set_key,
7195 .policy = nl80211_policy,
7196 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007197 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007198 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007199 },
7200 {
7201 .cmd = NL80211_CMD_NEW_KEY,
7202 .doit = nl80211_new_key,
7203 .policy = nl80211_policy,
7204 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007205 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007206 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007207 },
7208 {
7209 .cmd = NL80211_CMD_DEL_KEY,
7210 .doit = nl80211_del_key,
7211 .policy = nl80211_policy,
7212 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007213 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007214 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007215 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007216 {
7217 .cmd = NL80211_CMD_SET_BEACON,
7218 .policy = nl80211_policy,
7219 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007220 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007221 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007222 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007223 },
7224 {
Johannes Berg88600202012-02-13 15:17:18 +01007225 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007226 .policy = nl80211_policy,
7227 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007228 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007229 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007230 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007231 },
7232 {
Johannes Berg88600202012-02-13 15:17:18 +01007233 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007234 .policy = nl80211_policy,
7235 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007236 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007237 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007238 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007239 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007240 {
7241 .cmd = NL80211_CMD_GET_STATION,
7242 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007243 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007244 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007245 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7246 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007247 },
7248 {
7249 .cmd = NL80211_CMD_SET_STATION,
7250 .doit = nl80211_set_station,
7251 .policy = nl80211_policy,
7252 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007253 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007254 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007255 },
7256 {
7257 .cmd = NL80211_CMD_NEW_STATION,
7258 .doit = nl80211_new_station,
7259 .policy = nl80211_policy,
7260 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007261 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007262 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007263 },
7264 {
7265 .cmd = NL80211_CMD_DEL_STATION,
7266 .doit = nl80211_del_station,
7267 .policy = nl80211_policy,
7268 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007269 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007270 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007271 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007272 {
7273 .cmd = NL80211_CMD_GET_MPATH,
7274 .doit = nl80211_get_mpath,
7275 .dumpit = nl80211_dump_mpath,
7276 .policy = nl80211_policy,
7277 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007278 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007279 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007280 },
7281 {
7282 .cmd = NL80211_CMD_SET_MPATH,
7283 .doit = nl80211_set_mpath,
7284 .policy = nl80211_policy,
7285 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007286 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007287 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007288 },
7289 {
7290 .cmd = NL80211_CMD_NEW_MPATH,
7291 .doit = nl80211_new_mpath,
7292 .policy = nl80211_policy,
7293 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007294 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007295 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007296 },
7297 {
7298 .cmd = NL80211_CMD_DEL_MPATH,
7299 .doit = nl80211_del_mpath,
7300 .policy = nl80211_policy,
7301 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007302 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007303 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007304 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007305 {
7306 .cmd = NL80211_CMD_SET_BSS,
7307 .doit = nl80211_set_bss,
7308 .policy = nl80211_policy,
7309 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007310 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007311 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007312 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007313 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007314 .cmd = NL80211_CMD_GET_REG,
7315 .doit = nl80211_get_reg,
7316 .policy = nl80211_policy,
7317 /* can be retrieved by unprivileged users */
7318 },
7319 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007320 .cmd = NL80211_CMD_SET_REG,
7321 .doit = nl80211_set_reg,
7322 .policy = nl80211_policy,
7323 .flags = GENL_ADMIN_PERM,
7324 },
7325 {
7326 .cmd = NL80211_CMD_REQ_SET_REG,
7327 .doit = nl80211_req_set_reg,
7328 .policy = nl80211_policy,
7329 .flags = GENL_ADMIN_PERM,
7330 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007331 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007332 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7333 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007334 .policy = nl80211_policy,
7335 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007336 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007337 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007338 },
7339 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007340 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7341 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007342 .policy = nl80211_policy,
7343 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007344 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007345 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007346 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007347 {
Johannes Berg2a519312009-02-10 21:25:55 +01007348 .cmd = NL80211_CMD_TRIGGER_SCAN,
7349 .doit = nl80211_trigger_scan,
7350 .policy = nl80211_policy,
7351 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007352 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007353 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007354 },
7355 {
7356 .cmd = NL80211_CMD_GET_SCAN,
7357 .policy = nl80211_policy,
7358 .dumpit = nl80211_dump_scan,
7359 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007360 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007361 .cmd = NL80211_CMD_START_SCHED_SCAN,
7362 .doit = nl80211_start_sched_scan,
7363 .policy = nl80211_policy,
7364 .flags = GENL_ADMIN_PERM,
7365 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7366 NL80211_FLAG_NEED_RTNL,
7367 },
7368 {
7369 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7370 .doit = nl80211_stop_sched_scan,
7371 .policy = nl80211_policy,
7372 .flags = GENL_ADMIN_PERM,
7373 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7374 NL80211_FLAG_NEED_RTNL,
7375 },
7376 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007377 .cmd = NL80211_CMD_AUTHENTICATE,
7378 .doit = nl80211_authenticate,
7379 .policy = nl80211_policy,
7380 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007381 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007382 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007383 },
7384 {
7385 .cmd = NL80211_CMD_ASSOCIATE,
7386 .doit = nl80211_associate,
7387 .policy = nl80211_policy,
7388 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007389 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007390 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007391 },
7392 {
7393 .cmd = NL80211_CMD_DEAUTHENTICATE,
7394 .doit = nl80211_deauthenticate,
7395 .policy = nl80211_policy,
7396 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007397 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007398 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007399 },
7400 {
7401 .cmd = NL80211_CMD_DISASSOCIATE,
7402 .doit = nl80211_disassociate,
7403 .policy = nl80211_policy,
7404 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007405 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007406 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007407 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007408 {
7409 .cmd = NL80211_CMD_JOIN_IBSS,
7410 .doit = nl80211_join_ibss,
7411 .policy = nl80211_policy,
7412 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007413 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007414 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007415 },
7416 {
7417 .cmd = NL80211_CMD_LEAVE_IBSS,
7418 .doit = nl80211_leave_ibss,
7419 .policy = nl80211_policy,
7420 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007421 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007422 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007423 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007424#ifdef CONFIG_NL80211_TESTMODE
7425 {
7426 .cmd = NL80211_CMD_TESTMODE,
7427 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007428 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007429 .policy = nl80211_policy,
7430 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007431 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7432 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007433 },
7434#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007435 {
7436 .cmd = NL80211_CMD_CONNECT,
7437 .doit = nl80211_connect,
7438 .policy = nl80211_policy,
7439 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007440 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007441 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007442 },
7443 {
7444 .cmd = NL80211_CMD_DISCONNECT,
7445 .doit = nl80211_disconnect,
7446 .policy = nl80211_policy,
7447 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007448 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007449 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007450 },
Johannes Berg463d0182009-07-14 00:33:35 +02007451 {
7452 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7453 .doit = nl80211_wiphy_netns,
7454 .policy = nl80211_policy,
7455 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007456 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7457 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007458 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007459 {
7460 .cmd = NL80211_CMD_GET_SURVEY,
7461 .policy = nl80211_policy,
7462 .dumpit = nl80211_dump_survey,
7463 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007464 {
7465 .cmd = NL80211_CMD_SET_PMKSA,
7466 .doit = nl80211_setdel_pmksa,
7467 .policy = nl80211_policy,
7468 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007469 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007470 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007471 },
7472 {
7473 .cmd = NL80211_CMD_DEL_PMKSA,
7474 .doit = nl80211_setdel_pmksa,
7475 .policy = nl80211_policy,
7476 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007477 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007478 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007479 },
7480 {
7481 .cmd = NL80211_CMD_FLUSH_PMKSA,
7482 .doit = nl80211_flush_pmksa,
7483 .policy = nl80211_policy,
7484 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007485 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007486 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007487 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007488 {
7489 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7490 .doit = nl80211_remain_on_channel,
7491 .policy = nl80211_policy,
7492 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007493 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007494 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007495 },
7496 {
7497 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7498 .doit = nl80211_cancel_remain_on_channel,
7499 .policy = nl80211_policy,
7500 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007501 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007502 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007503 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007504 {
7505 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7506 .doit = nl80211_set_tx_bitrate_mask,
7507 .policy = nl80211_policy,
7508 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007509 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7510 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007511 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007512 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007513 .cmd = NL80211_CMD_REGISTER_FRAME,
7514 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007515 .policy = nl80211_policy,
7516 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007517 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007518 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007519 },
7520 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007521 .cmd = NL80211_CMD_FRAME,
7522 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007523 .policy = nl80211_policy,
7524 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007525 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007526 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007527 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007528 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007529 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7530 .doit = nl80211_tx_mgmt_cancel_wait,
7531 .policy = nl80211_policy,
7532 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007533 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007534 NL80211_FLAG_NEED_RTNL,
7535 },
7536 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007537 .cmd = NL80211_CMD_SET_POWER_SAVE,
7538 .doit = nl80211_set_power_save,
7539 .policy = nl80211_policy,
7540 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007541 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7542 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007543 },
7544 {
7545 .cmd = NL80211_CMD_GET_POWER_SAVE,
7546 .doit = nl80211_get_power_save,
7547 .policy = nl80211_policy,
7548 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007549 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7550 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007551 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007552 {
7553 .cmd = NL80211_CMD_SET_CQM,
7554 .doit = nl80211_set_cqm,
7555 .policy = nl80211_policy,
7556 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007557 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7558 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007559 },
Johannes Bergf444de02010-05-05 15:25:02 +02007560 {
7561 .cmd = NL80211_CMD_SET_CHANNEL,
7562 .doit = nl80211_set_channel,
7563 .policy = nl80211_policy,
7564 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007565 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7566 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007567 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007568 {
7569 .cmd = NL80211_CMD_SET_WDS_PEER,
7570 .doit = nl80211_set_wds_peer,
7571 .policy = nl80211_policy,
7572 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007573 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7574 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007575 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007576 {
7577 .cmd = NL80211_CMD_JOIN_MESH,
7578 .doit = nl80211_join_mesh,
7579 .policy = nl80211_policy,
7580 .flags = GENL_ADMIN_PERM,
7581 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7582 NL80211_FLAG_NEED_RTNL,
7583 },
7584 {
7585 .cmd = NL80211_CMD_LEAVE_MESH,
7586 .doit = nl80211_leave_mesh,
7587 .policy = nl80211_policy,
7588 .flags = GENL_ADMIN_PERM,
7589 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7590 NL80211_FLAG_NEED_RTNL,
7591 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007592#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007593 {
7594 .cmd = NL80211_CMD_GET_WOWLAN,
7595 .doit = nl80211_get_wowlan,
7596 .policy = nl80211_policy,
7597 /* can be retrieved by unprivileged users */
7598 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7599 NL80211_FLAG_NEED_RTNL,
7600 },
7601 {
7602 .cmd = NL80211_CMD_SET_WOWLAN,
7603 .doit = nl80211_set_wowlan,
7604 .policy = nl80211_policy,
7605 .flags = GENL_ADMIN_PERM,
7606 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7607 NL80211_FLAG_NEED_RTNL,
7608 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007609#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007610 {
7611 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7612 .doit = nl80211_set_rekey_data,
7613 .policy = nl80211_policy,
7614 .flags = GENL_ADMIN_PERM,
7615 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7616 NL80211_FLAG_NEED_RTNL,
7617 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007618 {
7619 .cmd = NL80211_CMD_TDLS_MGMT,
7620 .doit = nl80211_tdls_mgmt,
7621 .policy = nl80211_policy,
7622 .flags = GENL_ADMIN_PERM,
7623 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7624 NL80211_FLAG_NEED_RTNL,
7625 },
7626 {
7627 .cmd = NL80211_CMD_TDLS_OPER,
7628 .doit = nl80211_tdls_oper,
7629 .policy = nl80211_policy,
7630 .flags = GENL_ADMIN_PERM,
7631 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7632 NL80211_FLAG_NEED_RTNL,
7633 },
Johannes Berg28946da2011-11-04 11:18:12 +01007634 {
7635 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7636 .doit = nl80211_register_unexpected_frame,
7637 .policy = nl80211_policy,
7638 .flags = GENL_ADMIN_PERM,
7639 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7640 NL80211_FLAG_NEED_RTNL,
7641 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007642 {
7643 .cmd = NL80211_CMD_PROBE_CLIENT,
7644 .doit = nl80211_probe_client,
7645 .policy = nl80211_policy,
7646 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007647 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007648 NL80211_FLAG_NEED_RTNL,
7649 },
Johannes Berg5e760232011-11-04 11:18:17 +01007650 {
7651 .cmd = NL80211_CMD_REGISTER_BEACONS,
7652 .doit = nl80211_register_beacons,
7653 .policy = nl80211_policy,
7654 .flags = GENL_ADMIN_PERM,
7655 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7656 NL80211_FLAG_NEED_RTNL,
7657 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007658 {
7659 .cmd = NL80211_CMD_SET_NOACK_MAP,
7660 .doit = nl80211_set_noack_map,
7661 .policy = nl80211_policy,
7662 .flags = GENL_ADMIN_PERM,
7663 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7664 NL80211_FLAG_NEED_RTNL,
7665 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007666 {
7667 .cmd = NL80211_CMD_START_P2P_DEVICE,
7668 .doit = nl80211_start_p2p_device,
7669 .policy = nl80211_policy,
7670 .flags = GENL_ADMIN_PERM,
7671 .internal_flags = NL80211_FLAG_NEED_WDEV |
7672 NL80211_FLAG_NEED_RTNL,
7673 },
7674 {
7675 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7676 .doit = nl80211_stop_p2p_device,
7677 .policy = nl80211_policy,
7678 .flags = GENL_ADMIN_PERM,
7679 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7680 NL80211_FLAG_NEED_RTNL,
7681 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01007682 {
7683 .cmd = NL80211_CMD_SET_MCAST_RATE,
7684 .doit = nl80211_set_mcast_rate,
7685 .policy = nl80211_policy,
7686 .flags = GENL_ADMIN_PERM,
7687 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7688 NL80211_FLAG_NEED_RTNL,
7689 },
Johannes Berg55682962007-09-20 13:09:35 -04007690};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007691
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007692static struct genl_multicast_group nl80211_mlme_mcgrp = {
7693 .name = "mlme",
7694};
Johannes Berg55682962007-09-20 13:09:35 -04007695
7696/* multicast groups */
7697static struct genl_multicast_group nl80211_config_mcgrp = {
7698 .name = "config",
7699};
Johannes Berg2a519312009-02-10 21:25:55 +01007700static struct genl_multicast_group nl80211_scan_mcgrp = {
7701 .name = "scan",
7702};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007703static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7704 .name = "regulatory",
7705};
Johannes Berg55682962007-09-20 13:09:35 -04007706
7707/* notification functions */
7708
7709void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7710{
7711 struct sk_buff *msg;
7712
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007713 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007714 if (!msg)
7715 return;
7716
7717 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7718 nlmsg_free(msg);
7719 return;
7720 }
7721
Johannes Berg463d0182009-07-14 00:33:35 +02007722 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7723 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007724}
7725
Johannes Berg362a4152009-05-24 16:43:15 +02007726static int nl80211_add_scan_req(struct sk_buff *msg,
7727 struct cfg80211_registered_device *rdev)
7728{
7729 struct cfg80211_scan_request *req = rdev->scan_req;
7730 struct nlattr *nest;
7731 int i;
7732
Johannes Berg667503dd2009-07-07 03:56:11 +02007733 ASSERT_RDEV_LOCK(rdev);
7734
Johannes Berg362a4152009-05-24 16:43:15 +02007735 if (WARN_ON(!req))
7736 return 0;
7737
7738 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7739 if (!nest)
7740 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007741 for (i = 0; i < req->n_ssids; i++) {
7742 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7743 goto nla_put_failure;
7744 }
Johannes Berg362a4152009-05-24 16:43:15 +02007745 nla_nest_end(msg, nest);
7746
7747 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7748 if (!nest)
7749 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007750 for (i = 0; i < req->n_channels; i++) {
7751 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7752 goto nla_put_failure;
7753 }
Johannes Berg362a4152009-05-24 16:43:15 +02007754 nla_nest_end(msg, nest);
7755
David S. Miller9360ffd2012-03-29 04:41:26 -04007756 if (req->ie &&
7757 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7758 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007759
Sam Lefflered4737712012-10-11 21:03:31 -07007760 if (req->flags)
7761 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7762
Johannes Berg362a4152009-05-24 16:43:15 +02007763 return 0;
7764 nla_put_failure:
7765 return -ENOBUFS;
7766}
7767
Johannes Berga538e2d2009-06-16 19:56:42 +02007768static int nl80211_send_scan_msg(struct sk_buff *msg,
7769 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007770 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007771 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007772 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007773{
7774 void *hdr;
7775
Eric W. Biederman15e47302012-09-07 20:12:54 +00007776 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007777 if (!hdr)
7778 return -1;
7779
David S. Miller9360ffd2012-03-29 04:41:26 -04007780 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007781 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7782 wdev->netdev->ifindex)) ||
7783 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007784 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007785
Johannes Berg362a4152009-05-24 16:43:15 +02007786 /* ignore errors and send incomplete event anyway */
7787 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007788
7789 return genlmsg_end(msg, hdr);
7790
7791 nla_put_failure:
7792 genlmsg_cancel(msg, hdr);
7793 return -EMSGSIZE;
7794}
7795
Luciano Coelho807f8a82011-05-11 17:09:35 +03007796static int
7797nl80211_send_sched_scan_msg(struct sk_buff *msg,
7798 struct cfg80211_registered_device *rdev,
7799 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007800 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007801{
7802 void *hdr;
7803
Eric W. Biederman15e47302012-09-07 20:12:54 +00007804 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007805 if (!hdr)
7806 return -1;
7807
David S. Miller9360ffd2012-03-29 04:41:26 -04007808 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7809 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7810 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007811
7812 return genlmsg_end(msg, hdr);
7813
7814 nla_put_failure:
7815 genlmsg_cancel(msg, hdr);
7816 return -EMSGSIZE;
7817}
7818
Johannes Berga538e2d2009-06-16 19:56:42 +02007819void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007820 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007821{
7822 struct sk_buff *msg;
7823
Thomas Graf58050fc2012-06-28 03:57:45 +00007824 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007825 if (!msg)
7826 return;
7827
Johannes Bergfd014282012-06-18 19:17:03 +02007828 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007829 NL80211_CMD_TRIGGER_SCAN) < 0) {
7830 nlmsg_free(msg);
7831 return;
7832 }
7833
Johannes Berg463d0182009-07-14 00:33:35 +02007834 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7835 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007836}
7837
Johannes Berg2a519312009-02-10 21:25:55 +01007838void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007839 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007840{
7841 struct sk_buff *msg;
7842
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007843 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007844 if (!msg)
7845 return;
7846
Johannes Bergfd014282012-06-18 19:17:03 +02007847 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007848 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007849 nlmsg_free(msg);
7850 return;
7851 }
7852
Johannes Berg463d0182009-07-14 00:33:35 +02007853 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7854 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007855}
7856
7857void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007858 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007859{
7860 struct sk_buff *msg;
7861
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007863 if (!msg)
7864 return;
7865
Johannes Bergfd014282012-06-18 19:17:03 +02007866 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007867 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007868 nlmsg_free(msg);
7869 return;
7870 }
7871
Johannes Berg463d0182009-07-14 00:33:35 +02007872 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7873 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007874}
7875
Luciano Coelho807f8a82011-05-11 17:09:35 +03007876void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7877 struct net_device *netdev)
7878{
7879 struct sk_buff *msg;
7880
7881 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7882 if (!msg)
7883 return;
7884
7885 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7886 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7887 nlmsg_free(msg);
7888 return;
7889 }
7890
7891 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7892 nl80211_scan_mcgrp.id, GFP_KERNEL);
7893}
7894
7895void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7896 struct net_device *netdev, u32 cmd)
7897{
7898 struct sk_buff *msg;
7899
Thomas Graf58050fc2012-06-28 03:57:45 +00007900 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007901 if (!msg)
7902 return;
7903
7904 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7905 nlmsg_free(msg);
7906 return;
7907 }
7908
7909 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7910 nl80211_scan_mcgrp.id, GFP_KERNEL);
7911}
7912
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007913/*
7914 * This can happen on global regulatory changes or device specific settings
7915 * based on custom world regulatory domains.
7916 */
7917void nl80211_send_reg_change_event(struct regulatory_request *request)
7918{
7919 struct sk_buff *msg;
7920 void *hdr;
7921
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007922 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007923 if (!msg)
7924 return;
7925
7926 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7927 if (!hdr) {
7928 nlmsg_free(msg);
7929 return;
7930 }
7931
7932 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007933 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7934 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007935
David S. Miller9360ffd2012-03-29 04:41:26 -04007936 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7937 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7938 NL80211_REGDOM_TYPE_WORLD))
7939 goto nla_put_failure;
7940 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7941 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7942 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7943 goto nla_put_failure;
7944 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7945 request->intersect) {
7946 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7947 NL80211_REGDOM_TYPE_INTERSECTION))
7948 goto nla_put_failure;
7949 } else {
7950 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7951 NL80211_REGDOM_TYPE_COUNTRY) ||
7952 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7953 request->alpha2))
7954 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007955 }
7956
David S. Miller9360ffd2012-03-29 04:41:26 -04007957 if (wiphy_idx_valid(request->wiphy_idx) &&
7958 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7959 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007960
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007961 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007962
Johannes Bergbc43b282009-07-25 10:54:13 +02007963 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007964 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007965 GFP_ATOMIC);
7966 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007967
7968 return;
7969
7970nla_put_failure:
7971 genlmsg_cancel(msg, hdr);
7972 nlmsg_free(msg);
7973}
7974
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007975static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7976 struct net_device *netdev,
7977 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007978 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007979{
7980 struct sk_buff *msg;
7981 void *hdr;
7982
Johannes Berge6d6e342009-07-01 21:26:47 +02007983 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007984 if (!msg)
7985 return;
7986
7987 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7988 if (!hdr) {
7989 nlmsg_free(msg);
7990 return;
7991 }
7992
David S. Miller9360ffd2012-03-29 04:41:26 -04007993 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7994 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7995 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7996 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007997
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007998 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007999
Johannes Berg463d0182009-07-14 00:33:35 +02008000 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8001 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008002 return;
8003
8004 nla_put_failure:
8005 genlmsg_cancel(msg, hdr);
8006 nlmsg_free(msg);
8007}
8008
8009void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008010 struct net_device *netdev, const u8 *buf,
8011 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008012{
8013 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008014 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008015}
8016
8017void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
8018 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008019 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008020{
Johannes Berge6d6e342009-07-01 21:26:47 +02008021 nl80211_send_mlme_event(rdev, netdev, buf, len,
8022 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008023}
8024
Jouni Malinen53b46b82009-03-27 20:53:56 +02008025void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008026 struct net_device *netdev, const u8 *buf,
8027 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008028{
8029 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008030 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008031}
8032
Jouni Malinen53b46b82009-03-27 20:53:56 +02008033void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
8034 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02008035 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008036{
8037 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02008038 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008039}
8040
Jouni Malinencf4e5942010-12-16 00:52:40 +02008041void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
8042 struct net_device *netdev, const u8 *buf,
8043 size_t len, gfp_t gfp)
8044{
8045 nl80211_send_mlme_event(rdev, netdev, buf, len,
8046 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
8047}
8048
8049void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
8050 struct net_device *netdev, const u8 *buf,
8051 size_t len, gfp_t gfp)
8052{
8053 nl80211_send_mlme_event(rdev, netdev, buf, len,
8054 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8055}
8056
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008057static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8058 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008059 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008060{
8061 struct sk_buff *msg;
8062 void *hdr;
8063
Johannes Berge6d6e342009-07-01 21:26:47 +02008064 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008065 if (!msg)
8066 return;
8067
8068 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8069 if (!hdr) {
8070 nlmsg_free(msg);
8071 return;
8072 }
8073
David S. Miller9360ffd2012-03-29 04:41:26 -04008074 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8075 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8076 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8077 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8078 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008079
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008080 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008081
Johannes Berg463d0182009-07-14 00:33:35 +02008082 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8083 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008084 return;
8085
8086 nla_put_failure:
8087 genlmsg_cancel(msg, hdr);
8088 nlmsg_free(msg);
8089}
8090
8091void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008092 struct net_device *netdev, const u8 *addr,
8093 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008094{
8095 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008096 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008097}
8098
8099void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008100 struct net_device *netdev, const u8 *addr,
8101 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008102{
Johannes Berge6d6e342009-07-01 21:26:47 +02008103 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8104 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008105}
8106
Samuel Ortizb23aa672009-07-01 21:26:54 +02008107void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8108 struct net_device *netdev, const u8 *bssid,
8109 const u8 *req_ie, size_t req_ie_len,
8110 const u8 *resp_ie, size_t resp_ie_len,
8111 u16 status, gfp_t gfp)
8112{
8113 struct sk_buff *msg;
8114 void *hdr;
8115
Thomas Graf58050fc2012-06-28 03:57:45 +00008116 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008117 if (!msg)
8118 return;
8119
8120 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8121 if (!hdr) {
8122 nlmsg_free(msg);
8123 return;
8124 }
8125
David S. Miller9360ffd2012-03-29 04:41:26 -04008126 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8127 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8128 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8129 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8130 (req_ie &&
8131 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8132 (resp_ie &&
8133 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8134 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008135
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008136 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008137
Johannes Berg463d0182009-07-14 00:33:35 +02008138 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8139 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008140 return;
8141
8142 nla_put_failure:
8143 genlmsg_cancel(msg, hdr);
8144 nlmsg_free(msg);
8145
8146}
8147
8148void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8149 struct net_device *netdev, const u8 *bssid,
8150 const u8 *req_ie, size_t req_ie_len,
8151 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8152{
8153 struct sk_buff *msg;
8154 void *hdr;
8155
Thomas Graf58050fc2012-06-28 03:57:45 +00008156 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008157 if (!msg)
8158 return;
8159
8160 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8161 if (!hdr) {
8162 nlmsg_free(msg);
8163 return;
8164 }
8165
David S. Miller9360ffd2012-03-29 04:41:26 -04008166 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8167 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8168 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8169 (req_ie &&
8170 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8171 (resp_ie &&
8172 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8173 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008174
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008175 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008176
Johannes Berg463d0182009-07-14 00:33:35 +02008177 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8178 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008179 return;
8180
8181 nla_put_failure:
8182 genlmsg_cancel(msg, hdr);
8183 nlmsg_free(msg);
8184
8185}
8186
8187void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8188 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008189 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008190{
8191 struct sk_buff *msg;
8192 void *hdr;
8193
Thomas Graf58050fc2012-06-28 03:57:45 +00008194 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008195 if (!msg)
8196 return;
8197
8198 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8199 if (!hdr) {
8200 nlmsg_free(msg);
8201 return;
8202 }
8203
David S. Miller9360ffd2012-03-29 04:41:26 -04008204 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8205 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8206 (from_ap && reason &&
8207 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8208 (from_ap &&
8209 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8210 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8211 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008212
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008213 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008214
Johannes Berg463d0182009-07-14 00:33:35 +02008215 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8216 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008217 return;
8218
8219 nla_put_failure:
8220 genlmsg_cancel(msg, hdr);
8221 nlmsg_free(msg);
8222
8223}
8224
Johannes Berg04a773a2009-04-19 21:24:32 +02008225void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8226 struct net_device *netdev, const u8 *bssid,
8227 gfp_t gfp)
8228{
8229 struct sk_buff *msg;
8230 void *hdr;
8231
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008232 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008233 if (!msg)
8234 return;
8235
8236 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8237 if (!hdr) {
8238 nlmsg_free(msg);
8239 return;
8240 }
8241
David S. Miller9360ffd2012-03-29 04:41:26 -04008242 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8243 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8244 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8245 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008246
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008247 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008248
Johannes Berg463d0182009-07-14 00:33:35 +02008249 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8250 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008251 return;
8252
8253 nla_put_failure:
8254 genlmsg_cancel(msg, hdr);
8255 nlmsg_free(msg);
8256}
8257
Javier Cardonac93b5e72011-04-07 15:08:34 -07008258void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8259 struct net_device *netdev,
8260 const u8 *macaddr, const u8* ie, u8 ie_len,
8261 gfp_t gfp)
8262{
8263 struct sk_buff *msg;
8264 void *hdr;
8265
8266 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8267 if (!msg)
8268 return;
8269
8270 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8271 if (!hdr) {
8272 nlmsg_free(msg);
8273 return;
8274 }
8275
David S. Miller9360ffd2012-03-29 04:41:26 -04008276 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8277 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8278 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8279 (ie_len && ie &&
8280 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8281 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008282
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008283 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008284
8285 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8286 nl80211_mlme_mcgrp.id, gfp);
8287 return;
8288
8289 nla_put_failure:
8290 genlmsg_cancel(msg, hdr);
8291 nlmsg_free(msg);
8292}
8293
Jouni Malinena3b8b052009-03-27 21:59:49 +02008294void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8295 struct net_device *netdev, const u8 *addr,
8296 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008297 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008298{
8299 struct sk_buff *msg;
8300 void *hdr;
8301
Johannes Berge6d6e342009-07-01 21:26:47 +02008302 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008303 if (!msg)
8304 return;
8305
8306 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8307 if (!hdr) {
8308 nlmsg_free(msg);
8309 return;
8310 }
8311
David S. Miller9360ffd2012-03-29 04:41:26 -04008312 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8313 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8314 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8315 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8316 (key_id != -1 &&
8317 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8318 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8319 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008320
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008321 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008322
Johannes Berg463d0182009-07-14 00:33:35 +02008323 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8324 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008325 return;
8326
8327 nla_put_failure:
8328 genlmsg_cancel(msg, hdr);
8329 nlmsg_free(msg);
8330}
8331
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008332void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8333 struct ieee80211_channel *channel_before,
8334 struct ieee80211_channel *channel_after)
8335{
8336 struct sk_buff *msg;
8337 void *hdr;
8338 struct nlattr *nl_freq;
8339
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008340 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008341 if (!msg)
8342 return;
8343
8344 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8345 if (!hdr) {
8346 nlmsg_free(msg);
8347 return;
8348 }
8349
8350 /*
8351 * Since we are applying the beacon hint to a wiphy we know its
8352 * wiphy_idx is valid
8353 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008354 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8355 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008356
8357 /* Before */
8358 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8359 if (!nl_freq)
8360 goto nla_put_failure;
8361 if (nl80211_msg_put_channel(msg, channel_before))
8362 goto nla_put_failure;
8363 nla_nest_end(msg, nl_freq);
8364
8365 /* After */
8366 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8367 if (!nl_freq)
8368 goto nla_put_failure;
8369 if (nl80211_msg_put_channel(msg, channel_after))
8370 goto nla_put_failure;
8371 nla_nest_end(msg, nl_freq);
8372
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008373 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008374
Johannes Berg463d0182009-07-14 00:33:35 +02008375 rcu_read_lock();
8376 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8377 GFP_ATOMIC);
8378 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008379
8380 return;
8381
8382nla_put_failure:
8383 genlmsg_cancel(msg, hdr);
8384 nlmsg_free(msg);
8385}
8386
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008387static void nl80211_send_remain_on_chan_event(
8388 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008389 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008390 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008391 unsigned int duration, gfp_t gfp)
8392{
8393 struct sk_buff *msg;
8394 void *hdr;
8395
8396 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8397 if (!msg)
8398 return;
8399
8400 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8401 if (!hdr) {
8402 nlmsg_free(msg);
8403 return;
8404 }
8405
David S. Miller9360ffd2012-03-29 04:41:26 -04008406 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008407 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8408 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008409 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008410 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01008411 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
8412 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008413 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8414 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008415
David S. Miller9360ffd2012-03-29 04:41:26 -04008416 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8417 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8418 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008419
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008420 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008421
8422 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8423 nl80211_mlme_mcgrp.id, gfp);
8424 return;
8425
8426 nla_put_failure:
8427 genlmsg_cancel(msg, hdr);
8428 nlmsg_free(msg);
8429}
8430
8431void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008432 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008433 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008434 unsigned int duration, gfp_t gfp)
8435{
8436 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008437 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01008438 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008439}
8440
8441void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008442 struct cfg80211_registered_device *rdev,
8443 struct wireless_dev *wdev,
Johannes Berg42d97a52012-11-08 18:31:02 +01008444 u64 cookie, struct ieee80211_channel *chan, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008445{
8446 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01008447 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008448}
8449
Johannes Berg98b62182009-12-23 13:15:44 +01008450void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8451 struct net_device *dev, const u8 *mac_addr,
8452 struct station_info *sinfo, gfp_t gfp)
8453{
8454 struct sk_buff *msg;
8455
Thomas Graf58050fc2012-06-28 03:57:45 +00008456 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008457 if (!msg)
8458 return;
8459
John W. Linville66266b32012-03-15 13:25:41 -04008460 if (nl80211_send_station(msg, 0, 0, 0,
8461 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008462 nlmsg_free(msg);
8463 return;
8464 }
8465
8466 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8467 nl80211_mlme_mcgrp.id, gfp);
8468}
8469
Jouni Malinenec15e682011-03-23 15:29:52 +02008470void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8471 struct net_device *dev, const u8 *mac_addr,
8472 gfp_t gfp)
8473{
8474 struct sk_buff *msg;
8475 void *hdr;
8476
Thomas Graf58050fc2012-06-28 03:57:45 +00008477 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008478 if (!msg)
8479 return;
8480
8481 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8482 if (!hdr) {
8483 nlmsg_free(msg);
8484 return;
8485 }
8486
David S. Miller9360ffd2012-03-29 04:41:26 -04008487 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8488 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8489 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008490
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008491 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008492
8493 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8494 nl80211_mlme_mcgrp.id, gfp);
8495 return;
8496
8497 nla_put_failure:
8498 genlmsg_cancel(msg, hdr);
8499 nlmsg_free(msg);
8500}
8501
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308502void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8503 struct net_device *dev, const u8 *mac_addr,
8504 enum nl80211_connect_failed_reason reason,
8505 gfp_t gfp)
8506{
8507 struct sk_buff *msg;
8508 void *hdr;
8509
8510 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8511 if (!msg)
8512 return;
8513
8514 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8515 if (!hdr) {
8516 nlmsg_free(msg);
8517 return;
8518 }
8519
8520 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8521 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8522 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8523 goto nla_put_failure;
8524
8525 genlmsg_end(msg, hdr);
8526
8527 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8528 nl80211_mlme_mcgrp.id, gfp);
8529 return;
8530
8531 nla_put_failure:
8532 genlmsg_cancel(msg, hdr);
8533 nlmsg_free(msg);
8534}
8535
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008536static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8537 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008538{
8539 struct wireless_dev *wdev = dev->ieee80211_ptr;
8540 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8541 struct sk_buff *msg;
8542 void *hdr;
8543 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008544 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008545
Eric W. Biederman15e47302012-09-07 20:12:54 +00008546 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008547 return false;
8548
8549 msg = nlmsg_new(100, gfp);
8550 if (!msg)
8551 return true;
8552
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008553 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008554 if (!hdr) {
8555 nlmsg_free(msg);
8556 return true;
8557 }
8558
David S. Miller9360ffd2012-03-29 04:41:26 -04008559 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8560 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8561 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8562 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008563
8564 err = genlmsg_end(msg, hdr);
8565 if (err < 0) {
8566 nlmsg_free(msg);
8567 return true;
8568 }
8569
Eric W. Biederman15e47302012-09-07 20:12:54 +00008570 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008571 return true;
8572
8573 nla_put_failure:
8574 genlmsg_cancel(msg, hdr);
8575 nlmsg_free(msg);
8576 return true;
8577}
8578
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008579bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8580{
8581 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8582 addr, gfp);
8583}
8584
8585bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8586 const u8 *addr, gfp_t gfp)
8587{
8588 return __nl80211_unexpected_frame(dev,
8589 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8590 addr, gfp);
8591}
8592
Johannes Berg2e161f72010-08-12 15:38:38 +02008593int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008594 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008595 int freq, int sig_dbm,
8596 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008597{
Johannes Berg71bbc992012-06-15 15:30:18 +02008598 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008599 struct sk_buff *msg;
8600 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008601
8602 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8603 if (!msg)
8604 return -ENOMEM;
8605
Johannes Berg2e161f72010-08-12 15:38:38 +02008606 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008607 if (!hdr) {
8608 nlmsg_free(msg);
8609 return -ENOMEM;
8610 }
8611
David S. Miller9360ffd2012-03-29 04:41:26 -04008612 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008613 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8614 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008615 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8616 (sig_dbm &&
8617 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8618 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8619 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008620
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008621 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008622
Eric W. Biederman15e47302012-09-07 20:12:54 +00008623 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008624
8625 nla_put_failure:
8626 genlmsg_cancel(msg, hdr);
8627 nlmsg_free(msg);
8628 return -ENOBUFS;
8629}
8630
Johannes Berg2e161f72010-08-12 15:38:38 +02008631void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008632 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008633 const u8 *buf, size_t len, bool ack,
8634 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008635{
Johannes Berg71bbc992012-06-15 15:30:18 +02008636 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008637 struct sk_buff *msg;
8638 void *hdr;
8639
8640 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8641 if (!msg)
8642 return;
8643
Johannes Berg2e161f72010-08-12 15:38:38 +02008644 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008645 if (!hdr) {
8646 nlmsg_free(msg);
8647 return;
8648 }
8649
David S. Miller9360ffd2012-03-29 04:41:26 -04008650 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008651 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8652 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008653 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8654 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8655 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8656 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008657
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008658 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008659
8660 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8661 return;
8662
8663 nla_put_failure:
8664 genlmsg_cancel(msg, hdr);
8665 nlmsg_free(msg);
8666}
8667
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008668void
8669nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8670 struct net_device *netdev,
8671 enum nl80211_cqm_rssi_threshold_event rssi_event,
8672 gfp_t gfp)
8673{
8674 struct sk_buff *msg;
8675 struct nlattr *pinfoattr;
8676 void *hdr;
8677
Thomas Graf58050fc2012-06-28 03:57:45 +00008678 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008679 if (!msg)
8680 return;
8681
8682 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8683 if (!hdr) {
8684 nlmsg_free(msg);
8685 return;
8686 }
8687
David S. Miller9360ffd2012-03-29 04:41:26 -04008688 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8689 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8690 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008691
8692 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8693 if (!pinfoattr)
8694 goto nla_put_failure;
8695
David S. Miller9360ffd2012-03-29 04:41:26 -04008696 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8697 rssi_event))
8698 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008699
8700 nla_nest_end(msg, pinfoattr);
8701
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008702 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008703
8704 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8705 nl80211_mlme_mcgrp.id, gfp);
8706 return;
8707
8708 nla_put_failure:
8709 genlmsg_cancel(msg, hdr);
8710 nlmsg_free(msg);
8711}
8712
Johannes Berge5497d72011-07-05 16:35:40 +02008713void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8714 struct net_device *netdev, const u8 *bssid,
8715 const u8 *replay_ctr, gfp_t gfp)
8716{
8717 struct sk_buff *msg;
8718 struct nlattr *rekey_attr;
8719 void *hdr;
8720
Thomas Graf58050fc2012-06-28 03:57:45 +00008721 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008722 if (!msg)
8723 return;
8724
8725 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8726 if (!hdr) {
8727 nlmsg_free(msg);
8728 return;
8729 }
8730
David S. Miller9360ffd2012-03-29 04:41:26 -04008731 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8732 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8733 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8734 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008735
8736 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8737 if (!rekey_attr)
8738 goto nla_put_failure;
8739
David S. Miller9360ffd2012-03-29 04:41:26 -04008740 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8741 NL80211_REPLAY_CTR_LEN, replay_ctr))
8742 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008743
8744 nla_nest_end(msg, rekey_attr);
8745
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008746 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008747
8748 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8749 nl80211_mlme_mcgrp.id, gfp);
8750 return;
8751
8752 nla_put_failure:
8753 genlmsg_cancel(msg, hdr);
8754 nlmsg_free(msg);
8755}
8756
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008757void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8758 struct net_device *netdev, int index,
8759 const u8 *bssid, bool preauth, gfp_t gfp)
8760{
8761 struct sk_buff *msg;
8762 struct nlattr *attr;
8763 void *hdr;
8764
Thomas Graf58050fc2012-06-28 03:57:45 +00008765 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008766 if (!msg)
8767 return;
8768
8769 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8770 if (!hdr) {
8771 nlmsg_free(msg);
8772 return;
8773 }
8774
David S. Miller9360ffd2012-03-29 04:41:26 -04008775 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8776 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8777 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008778
8779 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8780 if (!attr)
8781 goto nla_put_failure;
8782
David S. Miller9360ffd2012-03-29 04:41:26 -04008783 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8784 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8785 (preauth &&
8786 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8787 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008788
8789 nla_nest_end(msg, attr);
8790
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008791 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008792
8793 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8794 nl80211_mlme_mcgrp.id, gfp);
8795 return;
8796
8797 nla_put_failure:
8798 genlmsg_cancel(msg, hdr);
8799 nlmsg_free(msg);
8800}
8801
Thomas Pedersen53145262012-04-06 13:35:47 -07008802void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
Johannes Berg683b6d32012-11-08 21:25:48 +01008803 struct net_device *netdev,
8804 struct cfg80211_chan_def *chandef, gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -07008805{
8806 struct sk_buff *msg;
8807 void *hdr;
8808
Thomas Graf58050fc2012-06-28 03:57:45 +00008809 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008810 if (!msg)
8811 return;
8812
8813 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8814 if (!hdr) {
8815 nlmsg_free(msg);
8816 return;
8817 }
8818
Johannes Berg683b6d32012-11-08 21:25:48 +01008819 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8820 goto nla_put_failure;
8821
8822 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -04008823 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008824
8825 genlmsg_end(msg, hdr);
8826
8827 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8828 nl80211_mlme_mcgrp.id, gfp);
8829 return;
8830
8831 nla_put_failure:
8832 genlmsg_cancel(msg, hdr);
8833 nlmsg_free(msg);
8834}
8835
Johannes Bergc063dbf2010-11-24 08:10:05 +01008836void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008837nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8838 struct net_device *netdev, const u8 *peer,
8839 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8840{
8841 struct sk_buff *msg;
8842 struct nlattr *pinfoattr;
8843 void *hdr;
8844
8845 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8846 if (!msg)
8847 return;
8848
8849 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8850 if (!hdr) {
8851 nlmsg_free(msg);
8852 return;
8853 }
8854
8855 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8856 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8857 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8858 goto nla_put_failure;
8859
8860 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8861 if (!pinfoattr)
8862 goto nla_put_failure;
8863
8864 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8865 goto nla_put_failure;
8866
8867 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8868 goto nla_put_failure;
8869
8870 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8871 goto nla_put_failure;
8872
8873 nla_nest_end(msg, pinfoattr);
8874
8875 genlmsg_end(msg, hdr);
8876
8877 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8878 nl80211_mlme_mcgrp.id, gfp);
8879 return;
8880
8881 nla_put_failure:
8882 genlmsg_cancel(msg, hdr);
8883 nlmsg_free(msg);
8884}
8885
8886void
Johannes Bergc063dbf2010-11-24 08:10:05 +01008887nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8888 struct net_device *netdev, const u8 *peer,
8889 u32 num_packets, gfp_t gfp)
8890{
8891 struct sk_buff *msg;
8892 struct nlattr *pinfoattr;
8893 void *hdr;
8894
Thomas Graf58050fc2012-06-28 03:57:45 +00008895 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008896 if (!msg)
8897 return;
8898
8899 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8900 if (!hdr) {
8901 nlmsg_free(msg);
8902 return;
8903 }
8904
David S. Miller9360ffd2012-03-29 04:41:26 -04008905 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8906 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8907 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8908 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008909
8910 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8911 if (!pinfoattr)
8912 goto nla_put_failure;
8913
David S. Miller9360ffd2012-03-29 04:41:26 -04008914 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8915 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008916
8917 nla_nest_end(msg, pinfoattr);
8918
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008919 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008920
8921 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8922 nl80211_mlme_mcgrp.id, gfp);
8923 return;
8924
8925 nla_put_failure:
8926 genlmsg_cancel(msg, hdr);
8927 nlmsg_free(msg);
8928}
8929
Johannes Berg7f6cf312011-11-04 11:18:15 +01008930void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8931 u64 cookie, bool acked, gfp_t gfp)
8932{
8933 struct wireless_dev *wdev = dev->ieee80211_ptr;
8934 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8935 struct sk_buff *msg;
8936 void *hdr;
8937 int err;
8938
Beni Lev4ee3e062012-08-27 12:49:39 +03008939 trace_cfg80211_probe_status(dev, addr, cookie, acked);
8940
Thomas Graf58050fc2012-06-28 03:57:45 +00008941 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03008942
Johannes Berg7f6cf312011-11-04 11:18:15 +01008943 if (!msg)
8944 return;
8945
8946 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8947 if (!hdr) {
8948 nlmsg_free(msg);
8949 return;
8950 }
8951
David S. Miller9360ffd2012-03-29 04:41:26 -04008952 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8953 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8954 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8955 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8956 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8957 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008958
8959 err = genlmsg_end(msg, hdr);
8960 if (err < 0) {
8961 nlmsg_free(msg);
8962 return;
8963 }
8964
8965 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8966 nl80211_mlme_mcgrp.id, gfp);
8967 return;
8968
8969 nla_put_failure:
8970 genlmsg_cancel(msg, hdr);
8971 nlmsg_free(msg);
8972}
8973EXPORT_SYMBOL(cfg80211_probe_status);
8974
Johannes Berg5e760232011-11-04 11:18:17 +01008975void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8976 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -07008977 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +01008978{
8979 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8980 struct sk_buff *msg;
8981 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -07008982 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +01008983
Beni Lev4ee3e062012-08-27 12:49:39 +03008984 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
8985
Ben Greear37c73b52012-10-26 14:49:25 -07008986 spin_lock_bh(&rdev->beacon_registrations_lock);
8987 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8988 msg = nlmsg_new(len + 100, GFP_ATOMIC);
8989 if (!msg) {
8990 spin_unlock_bh(&rdev->beacon_registrations_lock);
8991 return;
8992 }
Johannes Berg5e760232011-11-04 11:18:17 +01008993
Ben Greear37c73b52012-10-26 14:49:25 -07008994 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8995 if (!hdr)
8996 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008997
Ben Greear37c73b52012-10-26 14:49:25 -07008998 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8999 (freq &&
9000 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
9001 (sig_dbm &&
9002 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9003 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
9004 goto nla_put_failure;
9005
9006 genlmsg_end(msg, hdr);
9007
9008 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01009009 }
Ben Greear37c73b52012-10-26 14:49:25 -07009010 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009011 return;
9012
9013 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -07009014 spin_unlock_bh(&rdev->beacon_registrations_lock);
9015 if (hdr)
9016 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +01009017 nlmsg_free(msg);
9018}
9019EXPORT_SYMBOL(cfg80211_report_obss_beacon);
9020
Jouni Malinen3475b092012-11-16 22:49:57 +02009021void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
9022 enum nl80211_tdls_operation oper,
9023 u16 reason_code, gfp_t gfp)
9024{
9025 struct wireless_dev *wdev = dev->ieee80211_ptr;
9026 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9027 struct sk_buff *msg;
9028 void *hdr;
9029 int err;
9030
9031 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
9032 reason_code);
9033
9034 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9035 if (!msg)
9036 return;
9037
9038 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
9039 if (!hdr) {
9040 nlmsg_free(msg);
9041 return;
9042 }
9043
9044 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9045 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9046 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
9047 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
9048 (reason_code > 0 &&
9049 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
9050 goto nla_put_failure;
9051
9052 err = genlmsg_end(msg, hdr);
9053 if (err < 0) {
9054 nlmsg_free(msg);
9055 return;
9056 }
9057
9058 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9059 nl80211_mlme_mcgrp.id, gfp);
9060 return;
9061
9062 nla_put_failure:
9063 genlmsg_cancel(msg, hdr);
9064 nlmsg_free(msg);
9065}
9066EXPORT_SYMBOL(cfg80211_tdls_oper_request);
9067
Jouni Malinen026331c2010-02-15 12:53:10 +02009068static int nl80211_netlink_notify(struct notifier_block * nb,
9069 unsigned long state,
9070 void *_notify)
9071{
9072 struct netlink_notify *notify = _notify;
9073 struct cfg80211_registered_device *rdev;
9074 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -07009075 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +02009076
9077 if (state != NETLINK_URELEASE)
9078 return NOTIFY_DONE;
9079
9080 rcu_read_lock();
9081
Johannes Berg5e760232011-11-04 11:18:17 +01009082 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02009083 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00009084 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -07009085
9086 spin_lock_bh(&rdev->beacon_registrations_lock);
9087 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
9088 list) {
9089 if (reg->nlportid == notify->portid) {
9090 list_del(&reg->list);
9091 kfree(reg);
9092 break;
9093 }
9094 }
9095 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01009096 }
Jouni Malinen026331c2010-02-15 12:53:10 +02009097
9098 rcu_read_unlock();
9099
9100 return NOTIFY_DONE;
9101}
9102
9103static struct notifier_block nl80211_netlink_notifier = {
9104 .notifier_call = nl80211_netlink_notify,
9105};
9106
Johannes Berg55682962007-09-20 13:09:35 -04009107/* initialisation/exit functions */
9108
9109int nl80211_init(void)
9110{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009111 int err;
Johannes Berg55682962007-09-20 13:09:35 -04009112
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00009113 err = genl_register_family_with_ops(&nl80211_fam,
9114 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04009115 if (err)
9116 return err;
9117
Johannes Berg55682962007-09-20 13:09:35 -04009118 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9119 if (err)
9120 goto err_out;
9121
Johannes Berg2a519312009-02-10 21:25:55 +01009122 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9123 if (err)
9124 goto err_out;
9125
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009126 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9127 if (err)
9128 goto err_out;
9129
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009130 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9131 if (err)
9132 goto err_out;
9133
Johannes Bergaff89a92009-07-01 21:26:51 +02009134#ifdef CONFIG_NL80211_TESTMODE
9135 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9136 if (err)
9137 goto err_out;
9138#endif
9139
Jouni Malinen026331c2010-02-15 12:53:10 +02009140 err = netlink_register_notifier(&nl80211_netlink_notifier);
9141 if (err)
9142 goto err_out;
9143
Johannes Berg55682962007-09-20 13:09:35 -04009144 return 0;
9145 err_out:
9146 genl_unregister_family(&nl80211_fam);
9147 return err;
9148}
9149
9150void nl80211_exit(void)
9151{
Jouni Malinen026331c2010-02-15 12:53:10 +02009152 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009153 genl_unregister_family(&nl80211_fam);
9154}