blob: 8c0857815a900ab8aeb56791585739bb37650233 [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);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001113
Kalle Valo4745fc02011-11-17 19:06:10 +02001114#ifdef CONFIG_NL80211_TESTMODE
1115 CMD(testmode_cmd, TESTMODE);
1116#endif
1117
Johannes Berg8fdc6212009-03-14 09:34:01 +01001118#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001119
Johannes Berg6829c872009-07-02 09:13:27 +02001120 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001121 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001122 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1123 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001124 }
1125
Johannes Berg6829c872009-07-02 09:13:27 +02001126 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001127 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001128 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1129 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001130 }
1131
Johannes Berg8fdc6212009-03-14 09:34:01 +01001132 nla_nest_end(msg, nl_cmds);
1133
Johannes Berg7c4ef712011-11-18 15:33:48 +01001134 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001135 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1136 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1137 dev->wiphy.max_remain_on_channel_duration))
1138 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001139
David S. Miller9360ffd2012-03-29 04:41:26 -04001140 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1141 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1142 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001143
Johannes Berg2e161f72010-08-12 15:38:38 +02001144 if (mgmt_stypes) {
1145 u16 stypes;
1146 struct nlattr *nl_ftypes, *nl_ifs;
1147 enum nl80211_iftype ift;
1148
1149 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1150 if (!nl_ifs)
1151 goto nla_put_failure;
1152
1153 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1154 nl_ftypes = nla_nest_start(msg, ift);
1155 if (!nl_ftypes)
1156 goto nla_put_failure;
1157 i = 0;
1158 stypes = mgmt_stypes[ift].tx;
1159 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001160 if ((stypes & 1) &&
1161 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1162 (i << 4) | IEEE80211_FTYPE_MGMT))
1163 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001164 stypes >>= 1;
1165 i++;
1166 }
1167 nla_nest_end(msg, nl_ftypes);
1168 }
1169
Johannes Berg74b70a42010-08-24 12:15:53 +02001170 nla_nest_end(msg, nl_ifs);
1171
Johannes Berg2e161f72010-08-12 15:38:38 +02001172 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1173 if (!nl_ifs)
1174 goto nla_put_failure;
1175
1176 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1177 nl_ftypes = nla_nest_start(msg, ift);
1178 if (!nl_ftypes)
1179 goto nla_put_failure;
1180 i = 0;
1181 stypes = mgmt_stypes[ift].rx;
1182 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001183 if ((stypes & 1) &&
1184 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1185 (i << 4) | IEEE80211_FTYPE_MGMT))
1186 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001187 stypes >>= 1;
1188 i++;
1189 }
1190 nla_nest_end(msg, nl_ftypes);
1191 }
1192 nla_nest_end(msg, nl_ifs);
1193 }
1194
Johannes Bergdfb89c52012-06-27 09:23:48 +02001195#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001196 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1197 struct nlattr *nl_wowlan;
1198
1199 nl_wowlan = nla_nest_start(msg,
1200 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1201 if (!nl_wowlan)
1202 goto nla_put_failure;
1203
David S. Miller9360ffd2012-03-29 04:41:26 -04001204 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1205 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1206 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1207 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1208 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1209 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1210 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1211 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1212 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1213 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1214 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1215 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1216 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1217 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1218 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1219 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1220 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001221 if (dev->wiphy.wowlan.n_patterns) {
1222 struct nl80211_wowlan_pattern_support pat = {
1223 .max_patterns = dev->wiphy.wowlan.n_patterns,
1224 .min_pattern_len =
1225 dev->wiphy.wowlan.pattern_min_len,
1226 .max_pattern_len =
1227 dev->wiphy.wowlan.pattern_max_len,
1228 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001229 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1230 sizeof(pat), &pat))
1231 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001232 }
1233
1234 nla_nest_end(msg, nl_wowlan);
1235 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001236#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001237
Johannes Berg7527a782011-05-13 10:58:57 +02001238 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1239 dev->wiphy.software_iftypes))
1240 goto nla_put_failure;
1241
1242 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1243 goto nla_put_failure;
1244
David S. Miller9360ffd2012-03-29 04:41:26 -04001245 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1246 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1247 dev->wiphy.ap_sme_capa))
1248 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001249
David S. Miller9360ffd2012-03-29 04:41:26 -04001250 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1251 dev->wiphy.features))
1252 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001253
David S. Miller9360ffd2012-03-29 04:41:26 -04001254 if (dev->wiphy.ht_capa_mod_mask &&
1255 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1256 sizeof(*dev->wiphy.ht_capa_mod_mask),
1257 dev->wiphy.ht_capa_mod_mask))
1258 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001259
Johannes Berg55682962007-09-20 13:09:35 -04001260 return genlmsg_end(msg, hdr);
1261
1262 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001263 genlmsg_cancel(msg, hdr);
1264 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001265}
1266
1267static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1268{
1269 int idx = 0;
1270 int start = cb->args[0];
1271 struct cfg80211_registered_device *dev;
1272
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001273 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001274 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001275 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1276 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001277 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001278 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001279 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001280 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001281 dev) < 0) {
1282 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001283 break;
Julius Volzb4637272008-07-08 14:02:19 +02001284 }
Johannes Berg55682962007-09-20 13:09:35 -04001285 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001286 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001287
1288 cb->args[0] = idx;
1289
1290 return skb->len;
1291}
1292
1293static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1294{
1295 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001296 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001297
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001298 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001299 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001300 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001301
Eric W. Biederman15e47302012-09-07 20:12:54 +00001302 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001303 nlmsg_free(msg);
1304 return -ENOBUFS;
1305 }
Johannes Berg55682962007-09-20 13:09:35 -04001306
Johannes Berg134e6372009-07-10 09:51:34 +00001307 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001308}
1309
Jouni Malinen31888482008-10-30 16:59:24 +02001310static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1311 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1312 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1313 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1314 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1315 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1316};
1317
1318static int parse_txq_params(struct nlattr *tb[],
1319 struct ieee80211_txq_params *txq_params)
1320{
Johannes Berga3304b02012-03-28 11:04:24 +02001321 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001322 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1323 !tb[NL80211_TXQ_ATTR_AIFS])
1324 return -EINVAL;
1325
Johannes Berga3304b02012-03-28 11:04:24 +02001326 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001327 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1328 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1329 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1330 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1331
Johannes Berga3304b02012-03-28 11:04:24 +02001332 if (txq_params->ac >= NL80211_NUM_ACS)
1333 return -EINVAL;
1334
Jouni Malinen31888482008-10-30 16:59:24 +02001335 return 0;
1336}
1337
Johannes Bergf444de02010-05-05 15:25:02 +02001338static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1339{
1340 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001341 * You can only set the channel explicitly for WDS interfaces,
1342 * all others have their channel managed via their respective
1343 * "establish a connection" command (connect, join, ...)
1344 *
1345 * For AP/GO and mesh mode, the channel can be set with the
1346 * channel userspace API, but is only stored and passed to the
1347 * low-level driver when the AP starts or the mesh is joined.
1348 * This is for backward compatibility, userspace can also give
1349 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001350 *
1351 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001352 * whatever else is going on, so they have their own special
1353 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001354 */
1355 return !wdev ||
1356 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001357 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001358 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1359 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001360}
1361
Johannes Bergcd6c6592012-05-10 21:27:18 +02001362static bool nl80211_valid_channel_type(struct genl_info *info,
1363 enum nl80211_channel_type *channel_type)
1364{
1365 enum nl80211_channel_type tmp;
1366
1367 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1368 return false;
1369
1370 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1371 if (tmp != NL80211_CHAN_NO_HT &&
1372 tmp != NL80211_CHAN_HT20 &&
1373 tmp != NL80211_CHAN_HT40PLUS &&
1374 tmp != NL80211_CHAN_HT40MINUS)
1375 return false;
1376
1377 if (channel_type)
1378 *channel_type = tmp;
1379
1380 return true;
1381}
1382
Johannes Bergf444de02010-05-05 15:25:02 +02001383static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1384 struct wireless_dev *wdev,
1385 struct genl_info *info)
1386{
Johannes Bergaa430da2012-05-16 23:50:18 +02001387 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001388 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1389 u32 freq;
1390 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001391 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1392
1393 if (wdev)
1394 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001395
1396 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1397 return -EINVAL;
1398
1399 if (!nl80211_can_set_dev_channel(wdev))
1400 return -EOPNOTSUPP;
1401
Johannes Bergcd6c6592012-05-10 21:27:18 +02001402 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1403 !nl80211_valid_channel_type(info, &channel_type))
1404 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001405
1406 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1407
1408 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001409 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001410 case NL80211_IFTYPE_AP:
1411 case NL80211_IFTYPE_P2P_GO:
1412 if (wdev->beacon_interval) {
1413 result = -EBUSY;
1414 break;
1415 }
1416 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1417 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1418 channel,
1419 channel_type)) {
1420 result = -EINVAL;
1421 break;
1422 }
1423 wdev->preset_chan = channel;
1424 wdev->preset_chantype = channel_type;
1425 result = 0;
1426 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001427 case NL80211_IFTYPE_MESH_POINT:
1428 result = cfg80211_set_mesh_freq(rdev, wdev, freq, channel_type);
1429 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001430 case NL80211_IFTYPE_MONITOR:
1431 result = cfg80211_set_monitor_channel(rdev, freq, channel_type);
1432 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001433 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001434 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001435 }
1436 mutex_unlock(&rdev->devlist_mtx);
1437
1438 return result;
1439}
1440
1441static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1442{
Johannes Berg4c476992010-10-04 21:36:35 +02001443 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1444 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001445
Johannes Berg4c476992010-10-04 21:36:35 +02001446 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001447}
1448
Bill Jordane8347eb2010-10-01 13:54:28 -04001449static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1450{
Johannes Berg43b19952010-10-07 13:10:30 +02001451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1452 struct net_device *dev = info->user_ptr[1];
1453 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001454 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001455
1456 if (!info->attrs[NL80211_ATTR_MAC])
1457 return -EINVAL;
1458
Johannes Berg43b19952010-10-07 13:10:30 +02001459 if (netif_running(dev))
1460 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001461
Johannes Berg43b19952010-10-07 13:10:30 +02001462 if (!rdev->ops->set_wds_peer)
1463 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001464
Johannes Berg43b19952010-10-07 13:10:30 +02001465 if (wdev->iftype != NL80211_IFTYPE_WDS)
1466 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001467
1468 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001469 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001470}
1471
1472
Johannes Berg55682962007-09-20 13:09:35 -04001473static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1474{
1475 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001476 struct net_device *netdev = NULL;
1477 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001478 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001479 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001480 u32 changed;
1481 u8 retry_short = 0, retry_long = 0;
1482 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001483 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001484
Johannes Bergf444de02010-05-05 15:25:02 +02001485 /*
1486 * Try to find the wiphy and netdev. Normally this
1487 * function shouldn't need the netdev, but this is
1488 * done for backward compatibility -- previously
1489 * setting the channel was done per wiphy, but now
1490 * it is per netdev. Previous userland like hostapd
1491 * also passed a netdev to set_wiphy, so that it is
1492 * possible to let that go to the right netdev!
1493 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001494 mutex_lock(&cfg80211_mutex);
1495
Johannes Bergf444de02010-05-05 15:25:02 +02001496 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1497 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1498
1499 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1500 if (netdev && netdev->ieee80211_ptr) {
1501 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1502 mutex_lock(&rdev->mtx);
1503 } else
1504 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001505 }
1506
Johannes Bergf444de02010-05-05 15:25:02 +02001507 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001508 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1509 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001510 if (IS_ERR(rdev)) {
1511 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001512 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001513 }
1514 wdev = NULL;
1515 netdev = NULL;
1516 result = 0;
1517
1518 mutex_lock(&rdev->mtx);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001519 } else if (nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
Johannes Bergf444de02010-05-05 15:25:02 +02001520 wdev = netdev->ieee80211_ptr;
1521 else
1522 wdev = NULL;
1523
1524 /*
1525 * end workaround code, by now the rdev is available
1526 * and locked, and wdev may or may not be NULL.
1527 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001528
1529 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001530 result = cfg80211_dev_rename(
1531 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001532
1533 mutex_unlock(&cfg80211_mutex);
1534
1535 if (result)
1536 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001537
Jouni Malinen31888482008-10-30 16:59:24 +02001538 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1539 struct ieee80211_txq_params txq_params;
1540 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1541
1542 if (!rdev->ops->set_txq_params) {
1543 result = -EOPNOTSUPP;
1544 goto bad_res;
1545 }
1546
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001547 if (!netdev) {
1548 result = -EINVAL;
1549 goto bad_res;
1550 }
1551
Johannes Berg133a3ff2011-11-03 14:50:13 +01001552 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1553 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1554 result = -EINVAL;
1555 goto bad_res;
1556 }
1557
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001558 if (!netif_running(netdev)) {
1559 result = -ENETDOWN;
1560 goto bad_res;
1561 }
1562
Jouni Malinen31888482008-10-30 16:59:24 +02001563 nla_for_each_nested(nl_txq_params,
1564 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1565 rem_txq_params) {
1566 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1567 nla_data(nl_txq_params),
1568 nla_len(nl_txq_params),
1569 txq_params_policy);
1570 result = parse_txq_params(tb, &txq_params);
1571 if (result)
1572 goto bad_res;
1573
Hila Gonene35e4d22012-06-27 17:19:42 +03001574 result = rdev_set_txq_params(rdev, netdev,
1575 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001576 if (result)
1577 goto bad_res;
1578 }
1579 }
1580
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001581 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001582 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001583 if (result)
1584 goto bad_res;
1585 }
1586
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001587 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1588 enum nl80211_tx_power_setting type;
1589 int idx, mbm = 0;
1590
1591 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001592 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001593 goto bad_res;
1594 }
1595
1596 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1597 type = nla_get_u32(info->attrs[idx]);
1598
1599 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1600 (type != NL80211_TX_POWER_AUTOMATIC)) {
1601 result = -EINVAL;
1602 goto bad_res;
1603 }
1604
1605 if (type != NL80211_TX_POWER_AUTOMATIC) {
1606 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1607 mbm = nla_get_u32(info->attrs[idx]);
1608 }
1609
Hila Gonene35e4d22012-06-27 17:19:42 +03001610 result = rdev_set_tx_power(rdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001611 if (result)
1612 goto bad_res;
1613 }
1614
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001615 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1616 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1617 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001618 if ((!rdev->wiphy.available_antennas_tx &&
1619 !rdev->wiphy.available_antennas_rx) ||
1620 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001621 result = -EOPNOTSUPP;
1622 goto bad_res;
1623 }
1624
1625 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1626 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1627
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001628 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001629 * available antenna masks, except for the "all" mask */
1630 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1631 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001632 result = -EINVAL;
1633 goto bad_res;
1634 }
1635
Bruno Randolf7f531e02010-12-16 11:30:22 +09001636 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1637 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001638
Hila Gonene35e4d22012-06-27 17:19:42 +03001639 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001640 if (result)
1641 goto bad_res;
1642 }
1643
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001644 changed = 0;
1645
1646 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1647 retry_short = nla_get_u8(
1648 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1649 if (retry_short == 0) {
1650 result = -EINVAL;
1651 goto bad_res;
1652 }
1653 changed |= WIPHY_PARAM_RETRY_SHORT;
1654 }
1655
1656 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1657 retry_long = nla_get_u8(
1658 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1659 if (retry_long == 0) {
1660 result = -EINVAL;
1661 goto bad_res;
1662 }
1663 changed |= WIPHY_PARAM_RETRY_LONG;
1664 }
1665
1666 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1667 frag_threshold = nla_get_u32(
1668 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1669 if (frag_threshold < 256) {
1670 result = -EINVAL;
1671 goto bad_res;
1672 }
1673 if (frag_threshold != (u32) -1) {
1674 /*
1675 * Fragments (apart from the last one) are required to
1676 * have even length. Make the fragmentation code
1677 * simpler by stripping LSB should someone try to use
1678 * odd threshold value.
1679 */
1680 frag_threshold &= ~0x1;
1681 }
1682 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1683 }
1684
1685 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1686 rts_threshold = nla_get_u32(
1687 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1688 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1689 }
1690
Lukáš Turek81077e82009-12-21 22:50:47 +01001691 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1692 coverage_class = nla_get_u8(
1693 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1694 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1695 }
1696
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001697 if (changed) {
1698 u8 old_retry_short, old_retry_long;
1699 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001700 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001701
1702 if (!rdev->ops->set_wiphy_params) {
1703 result = -EOPNOTSUPP;
1704 goto bad_res;
1705 }
1706
1707 old_retry_short = rdev->wiphy.retry_short;
1708 old_retry_long = rdev->wiphy.retry_long;
1709 old_frag_threshold = rdev->wiphy.frag_threshold;
1710 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001711 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001712
1713 if (changed & WIPHY_PARAM_RETRY_SHORT)
1714 rdev->wiphy.retry_short = retry_short;
1715 if (changed & WIPHY_PARAM_RETRY_LONG)
1716 rdev->wiphy.retry_long = retry_long;
1717 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1718 rdev->wiphy.frag_threshold = frag_threshold;
1719 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1720 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001721 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1722 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001723
Hila Gonene35e4d22012-06-27 17:19:42 +03001724 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001725 if (result) {
1726 rdev->wiphy.retry_short = old_retry_short;
1727 rdev->wiphy.retry_long = old_retry_long;
1728 rdev->wiphy.frag_threshold = old_frag_threshold;
1729 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001730 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001731 }
1732 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001733
Johannes Berg306d6112008-12-08 12:39:04 +01001734 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001735 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001736 if (netdev)
1737 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001738 return result;
1739}
1740
Johannes Berg71bbc992012-06-15 15:30:18 +02001741static inline u64 wdev_id(struct wireless_dev *wdev)
1742{
1743 return (u64)wdev->identifier |
1744 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1745}
Johannes Berg55682962007-09-20 13:09:35 -04001746
Eric W. Biederman15e47302012-09-07 20:12:54 +00001747static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001748 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001749 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001750{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001751 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001752 void *hdr;
1753
Eric W. Biederman15e47302012-09-07 20:12:54 +00001754 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001755 if (!hdr)
1756 return -1;
1757
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001758 if (dev &&
1759 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001760 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001761 goto nla_put_failure;
1762
1763 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1764 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001765 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001766 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001767 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1768 rdev->devlist_generation ^
1769 (cfg80211_rdev_list_generation << 2)))
1770 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001771
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001772 if (rdev->ops->get_channel) {
1773 struct ieee80211_channel *chan;
1774 enum nl80211_channel_type channel_type;
1775
Hila Gonene35e4d22012-06-27 17:19:42 +03001776 chan = rdev_get_channel(rdev, wdev, &channel_type);
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001777 if (chan &&
1778 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1779 chan->center_freq) ||
1780 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1781 channel_type)))
John W. Linville59ef43e2012-04-18 14:17:13 -04001782 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001783 }
1784
Johannes Berg55682962007-09-20 13:09:35 -04001785 return genlmsg_end(msg, hdr);
1786
1787 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001788 genlmsg_cancel(msg, hdr);
1789 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001790}
1791
1792static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1793{
1794 int wp_idx = 0;
1795 int if_idx = 0;
1796 int wp_start = cb->args[0];
1797 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001798 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001799 struct wireless_dev *wdev;
1800
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001801 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001802 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1803 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001804 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001805 if (wp_idx < wp_start) {
1806 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001807 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001808 }
Johannes Berg55682962007-09-20 13:09:35 -04001809 if_idx = 0;
1810
Johannes Bergf5ea9122009-08-07 16:17:38 +02001811 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001812 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001813 if (if_idx < if_start) {
1814 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001815 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001816 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001817 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001818 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001819 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001820 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001821 goto out;
1822 }
1823 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001824 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001825 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001826
1827 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001828 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001829 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001830 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001831
1832 cb->args[0] = wp_idx;
1833 cb->args[1] = if_idx;
1834
1835 return skb->len;
1836}
1837
1838static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1839{
1840 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001841 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001842 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001843
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001844 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001845 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001846 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001847
Eric W. Biederman15e47302012-09-07 20:12:54 +00001848 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001849 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001850 nlmsg_free(msg);
1851 return -ENOBUFS;
1852 }
Johannes Berg55682962007-09-20 13:09:35 -04001853
Johannes Berg134e6372009-07-10 09:51:34 +00001854 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001855}
1856
Michael Wu66f7ac52008-01-31 19:48:22 +01001857static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1858 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1859 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1860 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1861 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1862 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1863};
1864
1865static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1866{
1867 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1868 int flag;
1869
1870 *mntrflags = 0;
1871
1872 if (!nla)
1873 return -EINVAL;
1874
1875 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1876 nla, mntr_flags_policy))
1877 return -EINVAL;
1878
1879 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1880 if (flags[flag])
1881 *mntrflags |= (1<<flag);
1882
1883 return 0;
1884}
1885
Johannes Berg9bc383d2009-11-19 11:55:19 +01001886static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001887 struct net_device *netdev, u8 use_4addr,
1888 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001889{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001890 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001891 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001892 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001893 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001894 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001895
1896 switch (iftype) {
1897 case NL80211_IFTYPE_AP_VLAN:
1898 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1899 return 0;
1900 break;
1901 case NL80211_IFTYPE_STATION:
1902 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1903 return 0;
1904 break;
1905 default:
1906 break;
1907 }
1908
1909 return -EOPNOTSUPP;
1910}
1911
Johannes Berg55682962007-09-20 13:09:35 -04001912static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1913{
Johannes Berg4c476992010-10-04 21:36:35 +02001914 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001915 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001916 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001917 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001918 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001919 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001920 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001921
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001922 memset(&params, 0, sizeof(params));
1923
Johannes Berg04a773a2009-04-19 21:24:32 +02001924 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001925
Johannes Berg723b0382008-09-16 20:22:09 +02001926 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001927 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001928 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001929 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001930 if (ntype > NL80211_IFTYPE_MAX)
1931 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001932 }
1933
Johannes Berg92ffe052008-09-16 20:39:36 +02001934 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001935 struct wireless_dev *wdev = dev->ieee80211_ptr;
1936
Johannes Berg4c476992010-10-04 21:36:35 +02001937 if (ntype != NL80211_IFTYPE_MESH_POINT)
1938 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001939 if (netif_running(dev))
1940 return -EBUSY;
1941
1942 wdev_lock(wdev);
1943 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1944 IEEE80211_MAX_MESH_ID_LEN);
1945 wdev->mesh_id_up_len =
1946 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1947 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1948 wdev->mesh_id_up_len);
1949 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001950 }
1951
Felix Fietkau8b787642009-11-10 18:53:10 +01001952 if (info->attrs[NL80211_ATTR_4ADDR]) {
1953 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1954 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001955 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001956 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001957 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001958 } else {
1959 params.use_4addr = -1;
1960 }
1961
Johannes Berg92ffe052008-09-16 20:39:36 +02001962 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001963 if (ntype != NL80211_IFTYPE_MONITOR)
1964 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001965 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1966 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001967 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001968 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001969
1970 flags = &_flags;
1971 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001972 }
Johannes Berg3b858752009-03-12 09:55:09 +01001973
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001974 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001975 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001976 else
1977 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001978
Johannes Berg9bc383d2009-11-19 11:55:19 +01001979 if (!err && params.use_4addr != -1)
1980 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1981
Johannes Berg55682962007-09-20 13:09:35 -04001982 return err;
1983}
1984
1985static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1986{
Johannes Berg4c476992010-10-04 21:36:35 +02001987 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001988 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02001989 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02001990 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04001991 int err;
1992 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001993 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001994
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001995 memset(&params, 0, sizeof(params));
1996
Johannes Berg55682962007-09-20 13:09:35 -04001997 if (!info->attrs[NL80211_ATTR_IFNAME])
1998 return -EINVAL;
1999
2000 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2001 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2002 if (type > NL80211_IFTYPE_MAX)
2003 return -EINVAL;
2004 }
2005
Johannes Berg79c97e92009-07-07 03:56:12 +02002006 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002007 !(rdev->wiphy.interface_modes & (1 << type)))
2008 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002009
Johannes Berg9bc383d2009-11-19 11:55:19 +01002010 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002011 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002012 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002013 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002014 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002015 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002016
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002017 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2018 if (!msg)
2019 return -ENOMEM;
2020
Michael Wu66f7ac52008-01-31 19:48:22 +01002021 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2022 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2023 &flags);
Hila Gonene35e4d22012-06-27 17:19:42 +03002024 wdev = rdev_add_virtual_intf(rdev,
2025 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2026 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002027 if (IS_ERR(wdev)) {
2028 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002029 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002030 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002031
Johannes Berg98104fde2012-06-16 00:19:54 +02002032 switch (type) {
2033 case NL80211_IFTYPE_MESH_POINT:
2034 if (!info->attrs[NL80211_ATTR_MESH_ID])
2035 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002036 wdev_lock(wdev);
2037 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2038 IEEE80211_MAX_MESH_ID_LEN);
2039 wdev->mesh_id_up_len =
2040 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2041 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2042 wdev->mesh_id_up_len);
2043 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002044 break;
2045 case NL80211_IFTYPE_P2P_DEVICE:
2046 /*
2047 * P2P Device doesn't have a netdev, so doesn't go
2048 * through the netdev notifier and must be added here
2049 */
2050 mutex_init(&wdev->mtx);
2051 INIT_LIST_HEAD(&wdev->event_list);
2052 spin_lock_init(&wdev->event_lock);
2053 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2054 spin_lock_init(&wdev->mgmt_registrations_lock);
2055
2056 mutex_lock(&rdev->devlist_mtx);
2057 wdev->identifier = ++rdev->wdev_id;
2058 list_add_rcu(&wdev->list, &rdev->wdev_list);
2059 rdev->devlist_generation++;
2060 mutex_unlock(&rdev->devlist_mtx);
2061 break;
2062 default:
2063 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002064 }
2065
Eric W. Biederman15e47302012-09-07 20:12:54 +00002066 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002067 rdev, wdev) < 0) {
2068 nlmsg_free(msg);
2069 return -ENOBUFS;
2070 }
2071
2072 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002073}
2074
2075static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2076{
Johannes Berg4c476992010-10-04 21:36:35 +02002077 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002078 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002079
Johannes Berg4c476992010-10-04 21:36:35 +02002080 if (!rdev->ops->del_virtual_intf)
2081 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002082
Johannes Berg84efbb82012-06-16 00:00:26 +02002083 /*
2084 * If we remove a wireless device without a netdev then clear
2085 * user_ptr[1] so that nl80211_post_doit won't dereference it
2086 * to check if it needs to do dev_put(). Otherwise it crashes
2087 * since the wdev has been freed, unlike with a netdev where
2088 * we need the dev_put() for the netdev to really be freed.
2089 */
2090 if (!wdev->netdev)
2091 info->user_ptr[1] = NULL;
2092
Hila Gonene35e4d22012-06-27 17:19:42 +03002093 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002094}
2095
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002096static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2097{
2098 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2099 struct net_device *dev = info->user_ptr[1];
2100 u16 noack_map;
2101
2102 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2103 return -EINVAL;
2104
2105 if (!rdev->ops->set_noack_map)
2106 return -EOPNOTSUPP;
2107
2108 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2109
Hila Gonene35e4d22012-06-27 17:19:42 +03002110 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002111}
2112
Johannes Berg41ade002007-12-19 02:03:29 +01002113struct get_key_cookie {
2114 struct sk_buff *msg;
2115 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002116 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002117};
2118
2119static void get_key_callback(void *c, struct key_params *params)
2120{
Johannes Bergb9454e82009-07-08 13:29:08 +02002121 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002122 struct get_key_cookie *cookie = c;
2123
David S. Miller9360ffd2012-03-29 04:41:26 -04002124 if ((params->key &&
2125 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2126 params->key_len, params->key)) ||
2127 (params->seq &&
2128 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2129 params->seq_len, params->seq)) ||
2130 (params->cipher &&
2131 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2132 params->cipher)))
2133 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002134
Johannes Bergb9454e82009-07-08 13:29:08 +02002135 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2136 if (!key)
2137 goto nla_put_failure;
2138
David S. Miller9360ffd2012-03-29 04:41:26 -04002139 if ((params->key &&
2140 nla_put(cookie->msg, NL80211_KEY_DATA,
2141 params->key_len, params->key)) ||
2142 (params->seq &&
2143 nla_put(cookie->msg, NL80211_KEY_SEQ,
2144 params->seq_len, params->seq)) ||
2145 (params->cipher &&
2146 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2147 params->cipher)))
2148 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002149
David S. Miller9360ffd2012-03-29 04:41:26 -04002150 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2151 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002152
2153 nla_nest_end(cookie->msg, key);
2154
Johannes Berg41ade002007-12-19 02:03:29 +01002155 return;
2156 nla_put_failure:
2157 cookie->error = 1;
2158}
2159
2160static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2161{
Johannes Berg4c476992010-10-04 21:36:35 +02002162 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002163 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002164 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002165 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002166 const u8 *mac_addr = NULL;
2167 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002168 struct get_key_cookie cookie = {
2169 .error = 0,
2170 };
2171 void *hdr;
2172 struct sk_buff *msg;
2173
2174 if (info->attrs[NL80211_ATTR_KEY_IDX])
2175 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2176
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002177 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002178 return -EINVAL;
2179
2180 if (info->attrs[NL80211_ATTR_MAC])
2181 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2182
Johannes Berge31b8212010-10-05 19:39:30 +02002183 pairwise = !!mac_addr;
2184 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2185 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2186 if (kt >= NUM_NL80211_KEYTYPES)
2187 return -EINVAL;
2188 if (kt != NL80211_KEYTYPE_GROUP &&
2189 kt != NL80211_KEYTYPE_PAIRWISE)
2190 return -EINVAL;
2191 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2192 }
2193
Johannes Berg4c476992010-10-04 21:36:35 +02002194 if (!rdev->ops->get_key)
2195 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002196
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002197 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002198 if (!msg)
2199 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002200
Eric W. Biederman15e47302012-09-07 20:12:54 +00002201 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002202 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002203 if (IS_ERR(hdr))
2204 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002205
2206 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002207 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002208
David S. Miller9360ffd2012-03-29 04:41:26 -04002209 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2210 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2211 goto nla_put_failure;
2212 if (mac_addr &&
2213 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2214 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002215
Johannes Berge31b8212010-10-05 19:39:30 +02002216 if (pairwise && mac_addr &&
2217 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2218 return -ENOENT;
2219
Hila Gonene35e4d22012-06-27 17:19:42 +03002220 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2221 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002222
2223 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002224 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002225
2226 if (cookie.error)
2227 goto nla_put_failure;
2228
2229 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002230 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002231
2232 nla_put_failure:
2233 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002234 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002235 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002236 return err;
2237}
2238
2239static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2240{
Johannes Berg4c476992010-10-04 21:36:35 +02002241 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002242 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002243 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002244 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002245
Johannes Bergb9454e82009-07-08 13:29:08 +02002246 err = nl80211_parse_key(info, &key);
2247 if (err)
2248 return err;
2249
2250 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002251 return -EINVAL;
2252
Johannes Bergb9454e82009-07-08 13:29:08 +02002253 /* only support setting default key */
2254 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002255 return -EINVAL;
2256
Johannes Bergfffd0932009-07-08 14:22:54 +02002257 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002258
2259 if (key.def) {
2260 if (!rdev->ops->set_default_key) {
2261 err = -EOPNOTSUPP;
2262 goto out;
2263 }
2264
2265 err = nl80211_key_allowed(dev->ieee80211_ptr);
2266 if (err)
2267 goto out;
2268
Hila Gonene35e4d22012-06-27 17:19:42 +03002269 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002270 key.def_uni, key.def_multi);
2271
2272 if (err)
2273 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002274
Johannes Berg3d23e342009-09-29 23:27:28 +02002275#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002276 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002277#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002278 } else {
2279 if (key.def_uni || !key.def_multi) {
2280 err = -EINVAL;
2281 goto out;
2282 }
2283
2284 if (!rdev->ops->set_default_mgmt_key) {
2285 err = -EOPNOTSUPP;
2286 goto out;
2287 }
2288
2289 err = nl80211_key_allowed(dev->ieee80211_ptr);
2290 if (err)
2291 goto out;
2292
Hila Gonene35e4d22012-06-27 17:19:42 +03002293 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002294 if (err)
2295 goto out;
2296
2297#ifdef CONFIG_CFG80211_WEXT
2298 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2299#endif
2300 }
2301
2302 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002303 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002304
Johannes Berg41ade002007-12-19 02:03:29 +01002305 return err;
2306}
2307
2308static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2309{
Johannes Berg4c476992010-10-04 21:36:35 +02002310 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002311 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002312 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002313 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002314 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002315
Johannes Bergb9454e82009-07-08 13:29:08 +02002316 err = nl80211_parse_key(info, &key);
2317 if (err)
2318 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002319
Johannes Bergb9454e82009-07-08 13:29:08 +02002320 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002321 return -EINVAL;
2322
Johannes Berg41ade002007-12-19 02:03:29 +01002323 if (info->attrs[NL80211_ATTR_MAC])
2324 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2325
Johannes Berge31b8212010-10-05 19:39:30 +02002326 if (key.type == -1) {
2327 if (mac_addr)
2328 key.type = NL80211_KEYTYPE_PAIRWISE;
2329 else
2330 key.type = NL80211_KEYTYPE_GROUP;
2331 }
2332
2333 /* for now */
2334 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2335 key.type != NL80211_KEYTYPE_GROUP)
2336 return -EINVAL;
2337
Johannes Berg4c476992010-10-04 21:36:35 +02002338 if (!rdev->ops->add_key)
2339 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002340
Johannes Berge31b8212010-10-05 19:39:30 +02002341 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2342 key.type == NL80211_KEYTYPE_PAIRWISE,
2343 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002344 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002345
2346 wdev_lock(dev->ieee80211_ptr);
2347 err = nl80211_key_allowed(dev->ieee80211_ptr);
2348 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002349 err = rdev_add_key(rdev, dev, key.idx,
2350 key.type == NL80211_KEYTYPE_PAIRWISE,
2351 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002352 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002353
Johannes Berg41ade002007-12-19 02:03:29 +01002354 return err;
2355}
2356
2357static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2358{
Johannes Berg4c476992010-10-04 21:36:35 +02002359 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002360 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002361 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002362 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002363 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002364
Johannes Bergb9454e82009-07-08 13:29:08 +02002365 err = nl80211_parse_key(info, &key);
2366 if (err)
2367 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002368
2369 if (info->attrs[NL80211_ATTR_MAC])
2370 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2371
Johannes Berge31b8212010-10-05 19:39:30 +02002372 if (key.type == -1) {
2373 if (mac_addr)
2374 key.type = NL80211_KEYTYPE_PAIRWISE;
2375 else
2376 key.type = NL80211_KEYTYPE_GROUP;
2377 }
2378
2379 /* for now */
2380 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2381 key.type != NL80211_KEYTYPE_GROUP)
2382 return -EINVAL;
2383
Johannes Berg4c476992010-10-04 21:36:35 +02002384 if (!rdev->ops->del_key)
2385 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002386
Johannes Bergfffd0932009-07-08 14:22:54 +02002387 wdev_lock(dev->ieee80211_ptr);
2388 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002389
2390 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2391 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2392 err = -ENOENT;
2393
Johannes Bergfffd0932009-07-08 14:22:54 +02002394 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002395 err = rdev_del_key(rdev, dev, key.idx,
2396 key.type == NL80211_KEYTYPE_PAIRWISE,
2397 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002398
Johannes Berg3d23e342009-09-29 23:27:28 +02002399#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002400 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002401 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002402 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002403 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002404 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2405 }
2406#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002407 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002408
Johannes Berg41ade002007-12-19 02:03:29 +01002409 return err;
2410}
2411
Johannes Berg88600202012-02-13 15:17:18 +01002412static int nl80211_parse_beacon(struct genl_info *info,
2413 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002414{
Johannes Berg88600202012-02-13 15:17:18 +01002415 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002416
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002417 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2418 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2419 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2420 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002421 return -EINVAL;
2422
Johannes Berg88600202012-02-13 15:17:18 +01002423 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002424
Johannes Berged1b6cc2007-12-19 02:03:32 +01002425 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002426 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2427 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2428 if (!bcn->head_len)
2429 return -EINVAL;
2430 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002431 }
2432
2433 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002434 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2435 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002436 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002437 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002438 }
2439
Johannes Berg4c476992010-10-04 21:36:35 +02002440 if (!haveinfo)
2441 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002442
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002443 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002444 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2445 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002446 }
2447
2448 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002449 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002450 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002451 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002452 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2453 }
2454
2455 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002456 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002457 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002458 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002459 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2460 }
2461
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002462 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002463 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002464 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002465 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002466 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2467 }
2468
Johannes Berg88600202012-02-13 15:17:18 +01002469 return 0;
2470}
2471
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002472static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2473 struct cfg80211_ap_settings *params)
2474{
2475 struct wireless_dev *wdev;
2476 bool ret = false;
2477
2478 mutex_lock(&rdev->devlist_mtx);
2479
Johannes Berg89a54e42012-06-15 14:33:17 +02002480 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002481 if (wdev->iftype != NL80211_IFTYPE_AP &&
2482 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2483 continue;
2484
2485 if (!wdev->preset_chan)
2486 continue;
2487
2488 params->channel = wdev->preset_chan;
2489 params->channel_type = wdev->preset_chantype;
2490 ret = true;
2491 break;
2492 }
2493
2494 mutex_unlock(&rdev->devlist_mtx);
2495
2496 return ret;
2497}
2498
Jouni Malinene39e5b52012-09-30 19:29:39 +03002499static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2500 enum nl80211_auth_type auth_type,
2501 enum nl80211_commands cmd)
2502{
2503 if (auth_type > NL80211_AUTHTYPE_MAX)
2504 return false;
2505
2506 switch (cmd) {
2507 case NL80211_CMD_AUTHENTICATE:
2508 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2509 auth_type == NL80211_AUTHTYPE_SAE)
2510 return false;
2511 return true;
2512 case NL80211_CMD_CONNECT:
2513 case NL80211_CMD_START_AP:
2514 /* SAE not supported yet */
2515 if (auth_type == NL80211_AUTHTYPE_SAE)
2516 return false;
2517 return true;
2518 default:
2519 return false;
2520 }
2521}
2522
Johannes Berg88600202012-02-13 15:17:18 +01002523static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2524{
2525 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2526 struct net_device *dev = info->user_ptr[1];
2527 struct wireless_dev *wdev = dev->ieee80211_ptr;
2528 struct cfg80211_ap_settings params;
2529 int err;
2530
2531 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2532 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2533 return -EOPNOTSUPP;
2534
2535 if (!rdev->ops->start_ap)
2536 return -EOPNOTSUPP;
2537
2538 if (wdev->beacon_interval)
2539 return -EALREADY;
2540
2541 memset(&params, 0, sizeof(params));
2542
2543 /* these are required for START_AP */
2544 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2545 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2546 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2547 return -EINVAL;
2548
2549 err = nl80211_parse_beacon(info, &params.beacon);
2550 if (err)
2551 return err;
2552
2553 params.beacon_interval =
2554 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2555 params.dtim_period =
2556 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2557
2558 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2559 if (err)
2560 return err;
2561
2562 /*
2563 * In theory, some of these attributes should be required here
2564 * but since they were not used when the command was originally
2565 * added, keep them optional for old user space programs to let
2566 * them continue to work with drivers that do not need the
2567 * additional information -- drivers must check!
2568 */
2569 if (info->attrs[NL80211_ATTR_SSID]) {
2570 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2571 params.ssid_len =
2572 nla_len(info->attrs[NL80211_ATTR_SSID]);
2573 if (params.ssid_len == 0 ||
2574 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2575 return -EINVAL;
2576 }
2577
2578 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2579 params.hidden_ssid = nla_get_u32(
2580 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2581 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2582 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2583 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2584 return -EINVAL;
2585 }
2586
2587 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2588
2589 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2590 params.auth_type = nla_get_u32(
2591 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002592 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2593 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002594 return -EINVAL;
2595 } else
2596 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2597
2598 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2599 NL80211_MAX_NR_CIPHER_SUITES);
2600 if (err)
2601 return err;
2602
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302603 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2604 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2605 return -EOPNOTSUPP;
2606 params.inactivity_timeout = nla_get_u16(
2607 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2608 }
2609
Johannes Bergaa430da2012-05-16 23:50:18 +02002610 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2611 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2612
2613 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2614 !nl80211_valid_channel_type(info, &channel_type))
2615 return -EINVAL;
2616
2617 params.channel = rdev_freq_to_chan(rdev,
2618 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2619 channel_type);
2620 if (!params.channel)
2621 return -EINVAL;
2622 params.channel_type = channel_type;
2623 } else if (wdev->preset_chan) {
2624 params.channel = wdev->preset_chan;
2625 params.channel_type = wdev->preset_chantype;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002626 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002627 return -EINVAL;
2628
2629 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2630 params.channel_type))
2631 return -EINVAL;
2632
Michal Kaziore4e32452012-06-29 12:47:08 +02002633 mutex_lock(&rdev->devlist_mtx);
2634 err = cfg80211_can_use_chan(rdev, wdev, params.channel,
2635 CHAN_MODE_SHARED);
2636 mutex_unlock(&rdev->devlist_mtx);
2637
2638 if (err)
2639 return err;
2640
Hila Gonene35e4d22012-06-27 17:19:42 +03002641 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002642 if (!err) {
2643 wdev->preset_chan = params.channel;
2644 wdev->preset_chantype = params.channel_type;
Johannes Berg88600202012-02-13 15:17:18 +01002645 wdev->beacon_interval = params.beacon_interval;
Michal Kaziorf4489eb2012-06-29 12:46:58 +02002646 wdev->channel = params.channel;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002647 }
Johannes Berg56d18932011-05-09 18:41:15 +02002648 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002649}
2650
Johannes Berg88600202012-02-13 15:17:18 +01002651static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2652{
2653 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2654 struct net_device *dev = info->user_ptr[1];
2655 struct wireless_dev *wdev = dev->ieee80211_ptr;
2656 struct cfg80211_beacon_data params;
2657 int err;
2658
2659 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2660 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2661 return -EOPNOTSUPP;
2662
2663 if (!rdev->ops->change_beacon)
2664 return -EOPNOTSUPP;
2665
2666 if (!wdev->beacon_interval)
2667 return -EINVAL;
2668
2669 err = nl80211_parse_beacon(info, &params);
2670 if (err)
2671 return err;
2672
Hila Gonene35e4d22012-06-27 17:19:42 +03002673 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01002674}
2675
2676static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002677{
Johannes Berg4c476992010-10-04 21:36:35 +02002678 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2679 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002680
Michal Kazior60771782012-06-29 12:46:56 +02002681 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002682}
2683
Johannes Berg5727ef12007-12-19 02:03:34 +01002684static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2685 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2686 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2687 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002688 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002689 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002690 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002691};
2692
Johannes Bergeccb8e82009-05-11 21:57:56 +03002693static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002694 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002695 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002696{
2697 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002698 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002699 int flag;
2700
Johannes Bergeccb8e82009-05-11 21:57:56 +03002701 /*
2702 * Try parsing the new attribute first so userspace
2703 * can specify both for older kernels.
2704 */
2705 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2706 if (nla) {
2707 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002708
Johannes Bergeccb8e82009-05-11 21:57:56 +03002709 sta_flags = nla_data(nla);
2710 params->sta_flags_mask = sta_flags->mask;
2711 params->sta_flags_set = sta_flags->set;
2712 if ((params->sta_flags_mask |
2713 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2714 return -EINVAL;
2715 return 0;
2716 }
2717
2718 /* if present, parse the old attribute */
2719
2720 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002721 if (!nla)
2722 return 0;
2723
2724 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2725 nla, sta_flags_policy))
2726 return -EINVAL;
2727
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002728 /*
2729 * Only allow certain flags for interface types so that
2730 * other attributes are silently ignored. Remember that
2731 * this is backward compatibility code with old userspace
2732 * and shouldn't be hit in other cases anyway.
2733 */
2734 switch (iftype) {
2735 case NL80211_IFTYPE_AP:
2736 case NL80211_IFTYPE_AP_VLAN:
2737 case NL80211_IFTYPE_P2P_GO:
2738 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2739 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2740 BIT(NL80211_STA_FLAG_WME) |
2741 BIT(NL80211_STA_FLAG_MFP);
2742 break;
2743 case NL80211_IFTYPE_P2P_CLIENT:
2744 case NL80211_IFTYPE_STATION:
2745 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2746 BIT(NL80211_STA_FLAG_TDLS_PEER);
2747 break;
2748 case NL80211_IFTYPE_MESH_POINT:
2749 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2750 BIT(NL80211_STA_FLAG_MFP) |
2751 BIT(NL80211_STA_FLAG_AUTHORIZED);
2752 default:
2753 return -EINVAL;
2754 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002755
Johannes Berg3383b5a2012-05-10 20:14:43 +02002756 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2757 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002758 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002759
Johannes Berg3383b5a2012-05-10 20:14:43 +02002760 /* no longer support new API additions in old API */
2761 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2762 return -EINVAL;
2763 }
2764 }
2765
Johannes Berg5727ef12007-12-19 02:03:34 +01002766 return 0;
2767}
2768
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002769static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2770 int attr)
2771{
2772 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002773 u32 bitrate;
2774 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002775
2776 rate = nla_nest_start(msg, attr);
2777 if (!rate)
2778 goto nla_put_failure;
2779
2780 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2781 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002782 /* report 16-bit bitrate only if we can */
2783 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002784 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002785 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2786 (bitrate_compat > 0 &&
2787 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002788 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2789 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2790 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2791 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2792 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2793 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2794 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002795
2796 nla_nest_end(msg, rate);
2797 return true;
2798
2799nla_put_failure:
2800 return false;
2801}
2802
Eric W. Biederman15e47302012-09-07 20:12:54 +00002803static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002804 int flags,
2805 struct cfg80211_registered_device *rdev,
2806 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002807 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002808{
2809 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002810 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002811
Eric W. Biederman15e47302012-09-07 20:12:54 +00002812 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002813 if (!hdr)
2814 return -1;
2815
David S. Miller9360ffd2012-03-29 04:41:26 -04002816 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2817 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2818 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2819 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002820
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002821 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2822 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002823 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002824 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2825 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2826 sinfo->connected_time))
2827 goto nla_put_failure;
2828 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2829 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2830 sinfo->inactive_time))
2831 goto nla_put_failure;
2832 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2833 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2834 sinfo->rx_bytes))
2835 goto nla_put_failure;
2836 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2837 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2838 sinfo->tx_bytes))
2839 goto nla_put_failure;
2840 if ((sinfo->filled & STATION_INFO_LLID) &&
2841 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2842 goto nla_put_failure;
2843 if ((sinfo->filled & STATION_INFO_PLID) &&
2844 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2845 goto nla_put_failure;
2846 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2847 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2848 sinfo->plink_state))
2849 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002850 switch (rdev->wiphy.signal_type) {
2851 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002852 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2853 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2854 sinfo->signal))
2855 goto nla_put_failure;
2856 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2857 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2858 sinfo->signal_avg))
2859 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002860 break;
2861 default:
2862 break;
2863 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002864 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002865 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2866 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002867 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002868 }
2869 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2870 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2871 NL80211_STA_INFO_RX_BITRATE))
2872 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002873 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002874 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2875 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2876 sinfo->rx_packets))
2877 goto nla_put_failure;
2878 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2879 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2880 sinfo->tx_packets))
2881 goto nla_put_failure;
2882 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2883 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2884 sinfo->tx_retries))
2885 goto nla_put_failure;
2886 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2887 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2888 sinfo->tx_failed))
2889 goto nla_put_failure;
2890 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2891 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2892 sinfo->beacon_loss_count))
2893 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002894 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2895 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2896 if (!bss_param)
2897 goto nla_put_failure;
2898
David S. Miller9360ffd2012-03-29 04:41:26 -04002899 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2900 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2901 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2902 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2903 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2904 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2905 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2906 sinfo->bss_param.dtim_period) ||
2907 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2908 sinfo->bss_param.beacon_interval))
2909 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002910
2911 nla_nest_end(msg, bss_param);
2912 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002913 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2914 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2915 sizeof(struct nl80211_sta_flag_update),
2916 &sinfo->sta_flags))
2917 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002918 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2919 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2920 sinfo->t_offset))
2921 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002922 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002923
David S. Miller9360ffd2012-03-29 04:41:26 -04002924 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2925 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2926 sinfo->assoc_req_ies))
2927 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002928
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002929 return genlmsg_end(msg, hdr);
2930
2931 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002932 genlmsg_cancel(msg, hdr);
2933 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002934}
2935
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002936static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002937 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002938{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002939 struct station_info sinfo;
2940 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002941 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002942 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002943 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002944 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002945
Johannes Berg67748892010-10-04 21:14:06 +02002946 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2947 if (err)
2948 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002949
2950 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002951 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002952 goto out_err;
2953 }
2954
Johannes Bergbba95fe2008-07-29 13:22:51 +02002955 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002956 memset(&sinfo, 0, sizeof(sinfo));
Hila Gonene35e4d22012-06-27 17:19:42 +03002957 err = rdev_dump_station(dev, netdev, sta_idx,
2958 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002959 if (err == -ENOENT)
2960 break;
2961 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002962 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002963
2964 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002965 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002966 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002967 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002968 &sinfo) < 0)
2969 goto out;
2970
2971 sta_idx++;
2972 }
2973
2974
2975 out:
2976 cb->args[1] = sta_idx;
2977 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002978 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002979 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002980
2981 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002982}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002983
Johannes Berg5727ef12007-12-19 02:03:34 +01002984static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2985{
Johannes Berg4c476992010-10-04 21:36:35 +02002986 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2987 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002988 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002989 struct sk_buff *msg;
2990 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002991 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002992
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002993 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002994
2995 if (!info->attrs[NL80211_ATTR_MAC])
2996 return -EINVAL;
2997
2998 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2999
Johannes Berg4c476992010-10-04 21:36:35 +02003000 if (!rdev->ops->get_station)
3001 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003002
Hila Gonene35e4d22012-06-27 17:19:42 +03003003 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003004 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003005 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003006
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003007 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003008 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003009 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003010
Eric W. Biederman15e47302012-09-07 20:12:54 +00003011 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003012 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003013 nlmsg_free(msg);
3014 return -ENOBUFS;
3015 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003016
Johannes Berg4c476992010-10-04 21:36:35 +02003017 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003018}
3019
3020/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003021 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003022 */
Johannes Berg80b99892011-11-18 16:23:01 +01003023static struct net_device *get_vlan(struct genl_info *info,
3024 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003025{
Johannes Berg463d0182009-07-14 00:33:35 +02003026 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003027 struct net_device *v;
3028 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003029
Johannes Berg80b99892011-11-18 16:23:01 +01003030 if (!vlanattr)
3031 return NULL;
3032
3033 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3034 if (!v)
3035 return ERR_PTR(-ENODEV);
3036
3037 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3038 ret = -EINVAL;
3039 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003040 }
Johannes Berg80b99892011-11-18 16:23:01 +01003041
3042 if (!netif_running(v)) {
3043 ret = -ENETDOWN;
3044 goto error;
3045 }
3046
3047 return v;
3048 error:
3049 dev_put(v);
3050 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003051}
3052
3053static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3054{
Johannes Berg4c476992010-10-04 21:36:35 +02003055 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003056 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003057 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003058 struct station_parameters params;
3059 u8 *mac_addr = NULL;
3060
3061 memset(&params, 0, sizeof(params));
3062
3063 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003064 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003065
3066 if (info->attrs[NL80211_ATTR_STA_AID])
3067 return -EINVAL;
3068
3069 if (!info->attrs[NL80211_ATTR_MAC])
3070 return -EINVAL;
3071
3072 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3073
3074 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3075 params.supported_rates =
3076 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3077 params.supported_rates_len =
3078 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3079 }
3080
3081 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3082 params.listen_interval =
3083 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3084
Jouni Malinen36aedc92008-08-25 11:58:58 +03003085 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3086 params.ht_capa =
3087 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3088
Johannes Bergbdd90d52011-12-14 12:20:27 +01003089 if (!rdev->ops->change_station)
3090 return -EOPNOTSUPP;
3091
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003092 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003093 return -EINVAL;
3094
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003095 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3096 params.plink_action =
3097 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3098
Javier Cardona9c3990a2011-05-03 16:57:11 -07003099 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3100 params.plink_state =
3101 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3102
Johannes Berga97f4422009-06-18 17:23:43 +02003103 switch (dev->ieee80211_ptr->iftype) {
3104 case NL80211_IFTYPE_AP:
3105 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003106 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003107 /* disallow mesh-specific things */
3108 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003109 return -EINVAL;
3110
3111 /* TDLS can't be set, ... */
3112 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3113 return -EINVAL;
3114 /*
3115 * ... but don't bother the driver with it. This works around
3116 * a hostapd/wpa_supplicant issue -- it always includes the
3117 * TLDS_PEER flag in the mask even for AP mode.
3118 */
3119 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3120
3121 /* accept only the listed bits */
3122 if (params.sta_flags_mask &
3123 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3124 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3125 BIT(NL80211_STA_FLAG_WME) |
3126 BIT(NL80211_STA_FLAG_MFP)))
3127 return -EINVAL;
3128
3129 /* must be last in here for error handling */
3130 params.vlan = get_vlan(info, rdev);
3131 if (IS_ERR(params.vlan))
3132 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003133 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003134 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003135 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003136 /*
3137 * Don't allow userspace to change the TDLS_PEER flag,
3138 * but silently ignore attempts to change it since we
3139 * don't have state here to verify that it doesn't try
3140 * to change the flag.
3141 */
3142 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003143 /* fall through */
3144 case NL80211_IFTYPE_ADHOC:
3145 /* disallow things sta doesn't support */
3146 if (params.plink_action)
3147 return -EINVAL;
3148 if (params.ht_capa)
3149 return -EINVAL;
3150 if (params.listen_interval >= 0)
3151 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003152 /* reject any changes other than AUTHORIZED */
3153 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3154 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003155 break;
3156 case NL80211_IFTYPE_MESH_POINT:
3157 /* disallow things mesh doesn't support */
3158 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003159 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003160 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003161 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003162 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003163 return -EINVAL;
3164 /*
3165 * No special handling for TDLS here -- the userspace
3166 * mesh code doesn't have this bug.
3167 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003168 if (params.sta_flags_mask &
3169 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003170 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003171 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003172 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003173 break;
3174 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003175 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003176 }
3177
Johannes Bergbdd90d52011-12-14 12:20:27 +01003178 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003179
Hila Gonene35e4d22012-06-27 17:19:42 +03003180 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003181
Johannes Berg5727ef12007-12-19 02:03:34 +01003182 if (params.vlan)
3183 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003184
Johannes Berg5727ef12007-12-19 02:03:34 +01003185 return err;
3186}
3187
Eliad Pellerc75786c2011-08-23 14:37:46 +03003188static struct nla_policy
3189nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3190 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3191 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3192};
3193
Johannes Berg5727ef12007-12-19 02:03:34 +01003194static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3195{
Johannes Berg4c476992010-10-04 21:36:35 +02003196 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003197 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003198 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003199 struct station_parameters params;
3200 u8 *mac_addr = NULL;
3201
3202 memset(&params, 0, sizeof(params));
3203
3204 if (!info->attrs[NL80211_ATTR_MAC])
3205 return -EINVAL;
3206
Johannes Berg5727ef12007-12-19 02:03:34 +01003207 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3208 return -EINVAL;
3209
3210 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3211 return -EINVAL;
3212
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003213 if (!info->attrs[NL80211_ATTR_STA_AID])
3214 return -EINVAL;
3215
Johannes Berg5727ef12007-12-19 02:03:34 +01003216 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3217 params.supported_rates =
3218 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3219 params.supported_rates_len =
3220 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3221 params.listen_interval =
3222 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003223
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003224 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3225 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3226 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003227
Jouni Malinen36aedc92008-08-25 11:58:58 +03003228 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3229 params.ht_capa =
3230 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003231
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003232 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3233 params.vht_capa =
3234 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3235
Javier Cardona96b78df2011-04-07 15:08:33 -07003236 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3237 params.plink_action =
3238 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3239
Johannes Bergbdd90d52011-12-14 12:20:27 +01003240 if (!rdev->ops->add_station)
3241 return -EOPNOTSUPP;
3242
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003243 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003244 return -EINVAL;
3245
Johannes Bergbdd90d52011-12-14 12:20:27 +01003246 switch (dev->ieee80211_ptr->iftype) {
3247 case NL80211_IFTYPE_AP:
3248 case NL80211_IFTYPE_AP_VLAN:
3249 case NL80211_IFTYPE_P2P_GO:
3250 /* parse WME attributes if sta is WME capable */
3251 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3252 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3253 info->attrs[NL80211_ATTR_STA_WME]) {
3254 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3255 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003256
Johannes Bergbdd90d52011-12-14 12:20:27 +01003257 nla = info->attrs[NL80211_ATTR_STA_WME];
3258 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3259 nl80211_sta_wme_policy);
3260 if (err)
3261 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003262
Johannes Bergbdd90d52011-12-14 12:20:27 +01003263 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3264 params.uapsd_queues =
3265 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3266 if (params.uapsd_queues &
3267 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3268 return -EINVAL;
3269
3270 if (tb[NL80211_STA_WME_MAX_SP])
3271 params.max_sp =
3272 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3273
3274 if (params.max_sp &
3275 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3276 return -EINVAL;
3277
3278 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3279 }
3280 /* TDLS peers cannot be added */
3281 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003282 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003283 /* but don't bother the driver with it */
3284 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003285
Johannes Bergbdd90d52011-12-14 12:20:27 +01003286 /* must be last in here for error handling */
3287 params.vlan = get_vlan(info, rdev);
3288 if (IS_ERR(params.vlan))
3289 return PTR_ERR(params.vlan);
3290 break;
3291 case NL80211_IFTYPE_MESH_POINT:
3292 /* TDLS peers cannot be added */
3293 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003294 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003295 break;
3296 case NL80211_IFTYPE_STATION:
3297 /* Only TDLS peers can be added */
3298 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3299 return -EINVAL;
3300 /* Can only add if TDLS ... */
3301 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3302 return -EOPNOTSUPP;
3303 /* ... with external setup is supported */
3304 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3305 return -EOPNOTSUPP;
3306 break;
3307 default:
3308 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003309 }
3310
Johannes Bergbdd90d52011-12-14 12:20:27 +01003311 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003312
Hila Gonene35e4d22012-06-27 17:19:42 +03003313 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003314
Johannes Berg5727ef12007-12-19 02:03:34 +01003315 if (params.vlan)
3316 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003317 return err;
3318}
3319
3320static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3321{
Johannes Berg4c476992010-10-04 21:36:35 +02003322 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3323 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003324 u8 *mac_addr = NULL;
3325
3326 if (info->attrs[NL80211_ATTR_MAC])
3327 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3328
Johannes Berge80cf852009-05-11 14:43:13 +02003329 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003330 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003331 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003332 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3333 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003334
Johannes Berg4c476992010-10-04 21:36:35 +02003335 if (!rdev->ops->del_station)
3336 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003337
Hila Gonene35e4d22012-06-27 17:19:42 +03003338 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003339}
3340
Eric W. Biederman15e47302012-09-07 20:12:54 +00003341static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003342 int flags, struct net_device *dev,
3343 u8 *dst, u8 *next_hop,
3344 struct mpath_info *pinfo)
3345{
3346 void *hdr;
3347 struct nlattr *pinfoattr;
3348
Eric W. Biederman15e47302012-09-07 20:12:54 +00003349 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003350 if (!hdr)
3351 return -1;
3352
David S. Miller9360ffd2012-03-29 04:41:26 -04003353 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3354 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3355 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3356 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3357 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003358
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003359 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3360 if (!pinfoattr)
3361 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003362 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3363 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3364 pinfo->frame_qlen))
3365 goto nla_put_failure;
3366 if (((pinfo->filled & MPATH_INFO_SN) &&
3367 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3368 ((pinfo->filled & MPATH_INFO_METRIC) &&
3369 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3370 pinfo->metric)) ||
3371 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3372 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3373 pinfo->exptime)) ||
3374 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3375 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3376 pinfo->flags)) ||
3377 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3378 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3379 pinfo->discovery_timeout)) ||
3380 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3381 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3382 pinfo->discovery_retries)))
3383 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003384
3385 nla_nest_end(msg, pinfoattr);
3386
3387 return genlmsg_end(msg, hdr);
3388
3389 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003390 genlmsg_cancel(msg, hdr);
3391 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003392}
3393
3394static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003395 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003396{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003397 struct mpath_info pinfo;
3398 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003399 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003400 u8 dst[ETH_ALEN];
3401 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003402 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003403 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003404
Johannes Berg67748892010-10-04 21:14:06 +02003405 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3406 if (err)
3407 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003408
3409 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003410 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003411 goto out_err;
3412 }
3413
Jouni Malineneec60b02009-03-20 21:21:19 +02003414 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3415 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003416 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003417 }
3418
Johannes Bergbba95fe2008-07-29 13:22:51 +02003419 while (1) {
Hila Gonene35e4d22012-06-27 17:19:42 +03003420 err = rdev_dump_mpath(dev, netdev, path_idx, dst, next_hop,
3421 &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003422 if (err == -ENOENT)
3423 break;
3424 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003425 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003426
Eric W. Biederman15e47302012-09-07 20:12:54 +00003427 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003428 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3429 netdev, dst, next_hop,
3430 &pinfo) < 0)
3431 goto out;
3432
3433 path_idx++;
3434 }
3435
3436
3437 out:
3438 cb->args[1] = path_idx;
3439 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003440 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003441 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003442 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003443}
3444
3445static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3446{
Johannes Berg4c476992010-10-04 21:36:35 +02003447 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003448 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003449 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003450 struct mpath_info pinfo;
3451 struct sk_buff *msg;
3452 u8 *dst = NULL;
3453 u8 next_hop[ETH_ALEN];
3454
3455 memset(&pinfo, 0, sizeof(pinfo));
3456
3457 if (!info->attrs[NL80211_ATTR_MAC])
3458 return -EINVAL;
3459
3460 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3461
Johannes Berg4c476992010-10-04 21:36:35 +02003462 if (!rdev->ops->get_mpath)
3463 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003464
Johannes Berg4c476992010-10-04 21:36:35 +02003465 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3466 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003467
Hila Gonene35e4d22012-06-27 17:19:42 +03003468 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003469 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003470 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003471
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003472 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003473 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003474 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003475
Eric W. Biederman15e47302012-09-07 20:12:54 +00003476 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003477 dev, dst, next_hop, &pinfo) < 0) {
3478 nlmsg_free(msg);
3479 return -ENOBUFS;
3480 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003481
Johannes Berg4c476992010-10-04 21:36:35 +02003482 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003483}
3484
3485static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3486{
Johannes Berg4c476992010-10-04 21:36:35 +02003487 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3488 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003489 u8 *dst = NULL;
3490 u8 *next_hop = NULL;
3491
3492 if (!info->attrs[NL80211_ATTR_MAC])
3493 return -EINVAL;
3494
3495 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3496 return -EINVAL;
3497
3498 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3499 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3500
Johannes Berg4c476992010-10-04 21:36:35 +02003501 if (!rdev->ops->change_mpath)
3502 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003503
Johannes Berg4c476992010-10-04 21:36:35 +02003504 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3505 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003506
Hila Gonene35e4d22012-06-27 17:19:42 +03003507 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003508}
Johannes Berg4c476992010-10-04 21:36:35 +02003509
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003510static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3511{
Johannes Berg4c476992010-10-04 21:36:35 +02003512 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3513 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003514 u8 *dst = NULL;
3515 u8 *next_hop = NULL;
3516
3517 if (!info->attrs[NL80211_ATTR_MAC])
3518 return -EINVAL;
3519
3520 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3521 return -EINVAL;
3522
3523 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3524 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3525
Johannes Berg4c476992010-10-04 21:36:35 +02003526 if (!rdev->ops->add_mpath)
3527 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003528
Johannes Berg4c476992010-10-04 21:36:35 +02003529 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3530 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003531
Hila Gonene35e4d22012-06-27 17:19:42 +03003532 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003533}
3534
3535static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3536{
Johannes Berg4c476992010-10-04 21:36:35 +02003537 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3538 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003539 u8 *dst = NULL;
3540
3541 if (info->attrs[NL80211_ATTR_MAC])
3542 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3543
Johannes Berg4c476992010-10-04 21:36:35 +02003544 if (!rdev->ops->del_mpath)
3545 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003546
Hila Gonene35e4d22012-06-27 17:19:42 +03003547 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003548}
3549
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003550static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3551{
Johannes Berg4c476992010-10-04 21:36:35 +02003552 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3553 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003554 struct bss_parameters params;
3555
3556 memset(&params, 0, sizeof(params));
3557 /* default to not changing parameters */
3558 params.use_cts_prot = -1;
3559 params.use_short_preamble = -1;
3560 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003561 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003562 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003563
3564 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3565 params.use_cts_prot =
3566 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3567 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3568 params.use_short_preamble =
3569 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3570 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3571 params.use_short_slot_time =
3572 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003573 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3574 params.basic_rates =
3575 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3576 params.basic_rates_len =
3577 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3578 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003579 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3580 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003581 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3582 params.ht_opmode =
3583 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003584
Johannes Berg4c476992010-10-04 21:36:35 +02003585 if (!rdev->ops->change_bss)
3586 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003587
Johannes Berg074ac8d2010-09-16 14:58:22 +02003588 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003589 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3590 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003591
Hila Gonene35e4d22012-06-27 17:19:42 +03003592 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003593}
3594
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003595static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003596 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3597 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3598 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3599 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3600 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3601 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3602};
3603
3604static int parse_reg_rule(struct nlattr *tb[],
3605 struct ieee80211_reg_rule *reg_rule)
3606{
3607 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3608 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3609
3610 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3611 return -EINVAL;
3612 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3613 return -EINVAL;
3614 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3615 return -EINVAL;
3616 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3617 return -EINVAL;
3618 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3619 return -EINVAL;
3620
3621 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3622
3623 freq_range->start_freq_khz =
3624 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3625 freq_range->end_freq_khz =
3626 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3627 freq_range->max_bandwidth_khz =
3628 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3629
3630 power_rule->max_eirp =
3631 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3632
3633 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3634 power_rule->max_antenna_gain =
3635 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3636
3637 return 0;
3638}
3639
3640static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3641{
3642 int r;
3643 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003644 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003645
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003646 /*
3647 * You should only get this when cfg80211 hasn't yet initialized
3648 * completely when built-in to the kernel right between the time
3649 * window between nl80211_init() and regulatory_init(), if that is
3650 * even possible.
3651 */
3652 mutex_lock(&cfg80211_mutex);
3653 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003654 mutex_unlock(&cfg80211_mutex);
3655 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003656 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003657 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003658
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003659 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3660 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003661
3662 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3663
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003664 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3665 user_reg_hint_type =
3666 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3667 else
3668 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3669
3670 switch (user_reg_hint_type) {
3671 case NL80211_USER_REG_HINT_USER:
3672 case NL80211_USER_REG_HINT_CELL_BASE:
3673 break;
3674 default:
3675 return -EINVAL;
3676 }
3677
3678 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003679
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003680 return r;
3681}
3682
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003683static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003684 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003685{
Johannes Berg4c476992010-10-04 21:36:35 +02003686 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003687 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003688 struct wireless_dev *wdev = dev->ieee80211_ptr;
3689 struct mesh_config cur_params;
3690 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003691 void *hdr;
3692 struct nlattr *pinfoattr;
3693 struct sk_buff *msg;
3694
Johannes Berg29cbe682010-12-03 09:20:44 +01003695 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3696 return -EOPNOTSUPP;
3697
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003698 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003699 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003700
Johannes Berg29cbe682010-12-03 09:20:44 +01003701 wdev_lock(wdev);
3702 /* If not connected, get default parameters */
3703 if (!wdev->mesh_id_len)
3704 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3705 else
Hila Gonene35e4d22012-06-27 17:19:42 +03003706 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01003707 wdev_unlock(wdev);
3708
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003709 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003710 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003711
3712 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003713 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003714 if (!msg)
3715 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003716 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003717 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003718 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003719 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003720 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003721 if (!pinfoattr)
3722 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003723 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3724 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3725 cur_params.dot11MeshRetryTimeout) ||
3726 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3727 cur_params.dot11MeshConfirmTimeout) ||
3728 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3729 cur_params.dot11MeshHoldingTimeout) ||
3730 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3731 cur_params.dot11MeshMaxPeerLinks) ||
3732 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3733 cur_params.dot11MeshMaxRetries) ||
3734 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3735 cur_params.dot11MeshTTL) ||
3736 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3737 cur_params.element_ttl) ||
3738 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3739 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003740 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3741 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003742 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3743 cur_params.dot11MeshHWMPmaxPREQretries) ||
3744 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3745 cur_params.path_refresh_time) ||
3746 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3747 cur_params.min_discovery_timeout) ||
3748 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3749 cur_params.dot11MeshHWMPactivePathTimeout) ||
3750 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3751 cur_params.dot11MeshHWMPpreqMinInterval) ||
3752 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3753 cur_params.dot11MeshHWMPperrMinInterval) ||
3754 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3755 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3756 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3757 cur_params.dot11MeshHWMPRootMode) ||
3758 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3759 cur_params.dot11MeshHWMPRannInterval) ||
3760 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3761 cur_params.dot11MeshGateAnnouncementProtocol) ||
3762 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3763 cur_params.dot11MeshForwarding) ||
3764 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003765 cur_params.rssi_threshold) ||
3766 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003767 cur_params.ht_opmode) ||
3768 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3769 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3770 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003771 cur_params.dot11MeshHWMProotInterval) ||
3772 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3773 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003774 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003775 nla_nest_end(msg, pinfoattr);
3776 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003777 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003778
Johannes Berg3b858752009-03-12 09:55:09 +01003779 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003780 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003781 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003782 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003783 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003784}
3785
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003786static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003787 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3788 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3789 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3790 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3791 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3792 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003793 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003794 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003795 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003796 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3797 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3798 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3799 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3800 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003801 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003802 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003803 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003804 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003805 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003806 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003807 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3808 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003809 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3810 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003811 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003812};
3813
Javier Cardonac80d5452010-12-16 17:37:49 -08003814static const struct nla_policy
3815 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003816 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003817 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3818 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003819 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003820 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003821 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003822 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003823};
3824
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003825static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003826 struct mesh_config *cfg,
3827 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003828{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003829 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003830 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003831
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003832#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3833do {\
3834 if (table[attr_num]) {\
3835 cfg->param = nla_fn(table[attr_num]); \
3836 mask |= (1 << (attr_num - 1)); \
3837 } \
3838} while (0);\
3839
3840
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003841 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003842 return -EINVAL;
3843 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003844 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003845 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003846 return -EINVAL;
3847
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003848 /* This makes sure that there aren't more than 32 mesh config
3849 * parameters (otherwise our bitfield scheme would not work.) */
3850 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3851
3852 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003853 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003854 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3855 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003856 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003857 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3858 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003859 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003860 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3861 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003862 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003863 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3864 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003865 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003866 mask, NL80211_MESHCONF_MAX_RETRIES,
3867 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003868 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003869 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003870 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003871 mask, NL80211_MESHCONF_ELEMENT_TTL,
3872 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003873 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003874 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3875 nla_get_u8);
3876 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3877 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3878 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003879 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003880 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3881 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003882 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003883 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3884 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003885 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003886 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3887 nla_get_u16);
3888 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3889 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3890 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003891 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003892 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3893 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003894 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003895 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3896 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003897 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003898 dot11MeshHWMPnetDiameterTraversalTime, mask,
3899 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3900 nla_get_u16);
3901 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3902 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3903 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3904 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3905 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003906 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003907 dot11MeshGateAnnouncementProtocol, mask,
3908 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3909 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003910 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003911 mask, NL80211_MESHCONF_FORWARDING,
3912 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003914 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3915 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003917 mask, NL80211_MESHCONF_HT_OPMODE,
3918 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3920 mask,
3921 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3922 nla_get_u32);
3923 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3924 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3925 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003926 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3927 dot11MeshHWMPconfirmationInterval, mask,
3928 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3929 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003930 if (mask_out)
3931 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003932
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003933 return 0;
3934
3935#undef FILL_IN_MESH_PARAM_IF_SET
3936}
3937
Javier Cardonac80d5452010-12-16 17:37:49 -08003938static int nl80211_parse_mesh_setup(struct genl_info *info,
3939 struct mesh_setup *setup)
3940{
3941 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3942
3943 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3944 return -EINVAL;
3945 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3946 info->attrs[NL80211_ATTR_MESH_SETUP],
3947 nl80211_mesh_setup_params_policy))
3948 return -EINVAL;
3949
Javier Cardonad299a1f2012-03-31 11:31:33 -07003950 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3951 setup->sync_method =
3952 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3953 IEEE80211_SYNC_METHOD_VENDOR :
3954 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3955
Javier Cardonac80d5452010-12-16 17:37:49 -08003956 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3957 setup->path_sel_proto =
3958 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3959 IEEE80211_PATH_PROTOCOL_VENDOR :
3960 IEEE80211_PATH_PROTOCOL_HWMP;
3961
3962 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3963 setup->path_metric =
3964 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3965 IEEE80211_PATH_METRIC_VENDOR :
3966 IEEE80211_PATH_METRIC_AIRTIME;
3967
Javier Cardona581a8b02011-04-07 15:08:27 -07003968
3969 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003970 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003971 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003972 if (!is_valid_ie_attr(ieattr))
3973 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003974 setup->ie = nla_data(ieattr);
3975 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003976 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003977 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3978 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003979
3980 return 0;
3981}
3982
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003983static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003984 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003985{
3986 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3987 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003988 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003989 struct mesh_config cfg;
3990 u32 mask;
3991 int err;
3992
Johannes Berg29cbe682010-12-03 09:20:44 +01003993 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3994 return -EOPNOTSUPP;
3995
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003996 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003997 return -EOPNOTSUPP;
3998
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003999 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004000 if (err)
4001 return err;
4002
Johannes Berg29cbe682010-12-03 09:20:44 +01004003 wdev_lock(wdev);
4004 if (!wdev->mesh_id_len)
4005 err = -ENOLINK;
4006
4007 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004008 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004009
4010 wdev_unlock(wdev);
4011
4012 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004013}
4014
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004015static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4016{
4017 struct sk_buff *msg;
4018 void *hdr = NULL;
4019 struct nlattr *nl_reg_rules;
4020 unsigned int i;
4021 int err = -EINVAL;
4022
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004023 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004024
4025 if (!cfg80211_regdomain)
4026 goto out;
4027
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004028 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004029 if (!msg) {
4030 err = -ENOBUFS;
4031 goto out;
4032 }
4033
Eric W. Biederman15e47302012-09-07 20:12:54 +00004034 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004035 NL80211_CMD_GET_REG);
4036 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004037 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004038
David S. Miller9360ffd2012-03-29 04:41:26 -04004039 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
4040 cfg80211_regdomain->alpha2) ||
4041 (cfg80211_regdomain->dfs_region &&
4042 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
4043 cfg80211_regdomain->dfs_region)))
4044 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004045
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004046 if (reg_last_request_cell_base() &&
4047 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4048 NL80211_USER_REG_HINT_CELL_BASE))
4049 goto nla_put_failure;
4050
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004051 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4052 if (!nl_reg_rules)
4053 goto nla_put_failure;
4054
4055 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
4056 struct nlattr *nl_reg_rule;
4057 const struct ieee80211_reg_rule *reg_rule;
4058 const struct ieee80211_freq_range *freq_range;
4059 const struct ieee80211_power_rule *power_rule;
4060
4061 reg_rule = &cfg80211_regdomain->reg_rules[i];
4062 freq_range = &reg_rule->freq_range;
4063 power_rule = &reg_rule->power_rule;
4064
4065 nl_reg_rule = nla_nest_start(msg, i);
4066 if (!nl_reg_rule)
4067 goto nla_put_failure;
4068
David S. Miller9360ffd2012-03-29 04:41:26 -04004069 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4070 reg_rule->flags) ||
4071 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4072 freq_range->start_freq_khz) ||
4073 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4074 freq_range->end_freq_khz) ||
4075 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4076 freq_range->max_bandwidth_khz) ||
4077 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4078 power_rule->max_antenna_gain) ||
4079 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4080 power_rule->max_eirp))
4081 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004082
4083 nla_nest_end(msg, nl_reg_rule);
4084 }
4085
4086 nla_nest_end(msg, nl_reg_rules);
4087
4088 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004089 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004090 goto out;
4091
4092nla_put_failure:
4093 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004094put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004095 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004096 err = -EMSGSIZE;
4097out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004098 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004099 return err;
4100}
4101
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004102static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4103{
4104 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4105 struct nlattr *nl_reg_rule;
4106 char *alpha2 = NULL;
4107 int rem_reg_rules = 0, r = 0;
4108 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004109 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004110 struct ieee80211_regdomain *rd = NULL;
4111
4112 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4113 return -EINVAL;
4114
4115 if (!info->attrs[NL80211_ATTR_REG_RULES])
4116 return -EINVAL;
4117
4118 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4119
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004120 if (info->attrs[NL80211_ATTR_DFS_REGION])
4121 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4122
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004123 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4124 rem_reg_rules) {
4125 num_rules++;
4126 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004127 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004128 }
4129
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004130 mutex_lock(&cfg80211_mutex);
4131
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004132 if (!reg_is_valid_request(alpha2)) {
4133 r = -EINVAL;
4134 goto bad_reg;
4135 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004136
4137 size_of_regd = sizeof(struct ieee80211_regdomain) +
4138 (num_rules * sizeof(struct ieee80211_reg_rule));
4139
4140 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004141 if (!rd) {
4142 r = -ENOMEM;
4143 goto bad_reg;
4144 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004145
4146 rd->n_reg_rules = num_rules;
4147 rd->alpha2[0] = alpha2[0];
4148 rd->alpha2[1] = alpha2[1];
4149
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004150 /*
4151 * Disable DFS master mode if the DFS region was
4152 * not supported or known on this kernel.
4153 */
4154 if (reg_supported_dfs_region(dfs_region))
4155 rd->dfs_region = dfs_region;
4156
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004157 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4158 rem_reg_rules) {
4159 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4160 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4161 reg_rule_policy);
4162 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4163 if (r)
4164 goto bad_reg;
4165
4166 rule_idx++;
4167
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004168 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4169 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004170 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004171 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004172 }
4173
4174 BUG_ON(rule_idx != num_rules);
4175
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004176 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004177
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004178 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004179
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004180 return r;
4181
Johannes Bergd2372b32008-10-24 20:32:20 +02004182 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004183 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004184 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004185 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004186}
4187
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004188static int validate_scan_freqs(struct nlattr *freqs)
4189{
4190 struct nlattr *attr1, *attr2;
4191 int n_channels = 0, tmp1, tmp2;
4192
4193 nla_for_each_nested(attr1, freqs, tmp1) {
4194 n_channels++;
4195 /*
4196 * Some hardware has a limited channel list for
4197 * scanning, and it is pretty much nonsensical
4198 * to scan for a channel twice, so disallow that
4199 * and don't require drivers to check that the
4200 * channel list they get isn't longer than what
4201 * they can scan, as long as they can scan all
4202 * the channels they registered at once.
4203 */
4204 nla_for_each_nested(attr2, freqs, tmp2)
4205 if (attr1 != attr2 &&
4206 nla_get_u32(attr1) == nla_get_u32(attr2))
4207 return 0;
4208 }
4209
4210 return n_channels;
4211}
4212
Johannes Berg2a519312009-02-10 21:25:55 +01004213static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4214{
Johannes Berg4c476992010-10-04 21:36:35 +02004215 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004216 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004217 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004218 struct nlattr *attr;
4219 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004220 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004221 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004222
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004223 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4224 return -EINVAL;
4225
Johannes Berg79c97e92009-07-07 03:56:12 +02004226 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004227
Johannes Berg4c476992010-10-04 21:36:35 +02004228 if (!rdev->ops->scan)
4229 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004230
Johannes Berg4c476992010-10-04 21:36:35 +02004231 if (rdev->scan_req)
4232 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004233
4234 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004235 n_channels = validate_scan_freqs(
4236 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004237 if (!n_channels)
4238 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004239 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004240 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004241 n_channels = 0;
4242
Johannes Berg2a519312009-02-10 21:25:55 +01004243 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4244 if (wiphy->bands[band])
4245 n_channels += wiphy->bands[band]->n_channels;
4246 }
4247
4248 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4249 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4250 n_ssids++;
4251
Johannes Berg4c476992010-10-04 21:36:35 +02004252 if (n_ssids > wiphy->max_scan_ssids)
4253 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004254
Jouni Malinen70692ad2009-02-16 19:39:13 +02004255 if (info->attrs[NL80211_ATTR_IE])
4256 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4257 else
4258 ie_len = 0;
4259
Johannes Berg4c476992010-10-04 21:36:35 +02004260 if (ie_len > wiphy->max_scan_ie_len)
4261 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004262
Johannes Berg2a519312009-02-10 21:25:55 +01004263 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004264 + sizeof(*request->ssids) * n_ssids
4265 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004266 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004267 if (!request)
4268 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004269
Johannes Berg2a519312009-02-10 21:25:55 +01004270 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004271 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004272 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004273 if (ie_len) {
4274 if (request->ssids)
4275 request->ie = (void *)(request->ssids + n_ssids);
4276 else
4277 request->ie = (void *)(request->channels + n_channels);
4278 }
Johannes Berg2a519312009-02-10 21:25:55 +01004279
Johannes Berg584991d2009-11-02 13:32:03 +01004280 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004281 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4282 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004283 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004284 struct ieee80211_channel *chan;
4285
4286 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4287
4288 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004289 err = -EINVAL;
4290 goto out_free;
4291 }
Johannes Berg584991d2009-11-02 13:32:03 +01004292
4293 /* ignore disabled channels */
4294 if (chan->flags & IEEE80211_CHAN_DISABLED)
4295 continue;
4296
4297 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004298 i++;
4299 }
4300 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004301 enum ieee80211_band band;
4302
Johannes Berg2a519312009-02-10 21:25:55 +01004303 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004304 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4305 int j;
4306 if (!wiphy->bands[band])
4307 continue;
4308 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004309 struct ieee80211_channel *chan;
4310
4311 chan = &wiphy->bands[band]->channels[j];
4312
4313 if (chan->flags & IEEE80211_CHAN_DISABLED)
4314 continue;
4315
4316 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004317 i++;
4318 }
4319 }
4320 }
4321
Johannes Berg584991d2009-11-02 13:32:03 +01004322 if (!i) {
4323 err = -EINVAL;
4324 goto out_free;
4325 }
4326
4327 request->n_channels = i;
4328
Johannes Berg2a519312009-02-10 21:25:55 +01004329 i = 0;
4330 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4331 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004332 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004333 err = -EINVAL;
4334 goto out_free;
4335 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004336 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004337 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004338 i++;
4339 }
4340 }
4341
Jouni Malinen70692ad2009-02-16 19:39:13 +02004342 if (info->attrs[NL80211_ATTR_IE]) {
4343 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004344 memcpy((void *)request->ie,
4345 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004346 request->ie_len);
4347 }
4348
Johannes Berg34850ab2011-07-18 18:08:35 +02004349 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004350 if (wiphy->bands[i])
4351 request->rates[i] =
4352 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004353
4354 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4355 nla_for_each_nested(attr,
4356 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4357 tmp) {
4358 enum ieee80211_band band = nla_type(attr);
4359
Dan Carpenter84404622011-07-29 11:52:18 +03004360 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004361 err = -EINVAL;
4362 goto out_free;
4363 }
4364 err = ieee80211_get_ratemask(wiphy->bands[band],
4365 nla_data(attr),
4366 nla_len(attr),
4367 &request->rates[band]);
4368 if (err)
4369 goto out_free;
4370 }
4371 }
4372
Sam Leffler46856bb2012-10-11 21:03:32 -07004373 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004374 request->flags = nla_get_u32(
4375 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004376 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4377 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4378 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4379 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004380 err = -EOPNOTSUPP;
4381 goto out_free;
4382 }
4383 }
Sam Lefflered4737712012-10-11 21:03:31 -07004384
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304385 request->no_cck =
4386 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4387
Johannes Bergfd014282012-06-18 19:17:03 +02004388 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004389 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004390 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004391
Johannes Berg79c97e92009-07-07 03:56:12 +02004392 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03004393 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004394
Johannes Berg463d0182009-07-14 00:33:35 +02004395 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004396 nl80211_send_scan_start(rdev, wdev);
4397 if (wdev->netdev)
4398 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004399 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004400 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004401 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004402 kfree(request);
4403 }
Johannes Berg3b858752009-03-12 09:55:09 +01004404
Johannes Berg2a519312009-02-10 21:25:55 +01004405 return err;
4406}
4407
Luciano Coelho807f8a82011-05-11 17:09:35 +03004408static int nl80211_start_sched_scan(struct sk_buff *skb,
4409 struct genl_info *info)
4410{
4411 struct cfg80211_sched_scan_request *request;
4412 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4413 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004414 struct nlattr *attr;
4415 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004416 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004417 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004418 enum ieee80211_band band;
4419 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004420 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004421
4422 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4423 !rdev->ops->sched_scan_start)
4424 return -EOPNOTSUPP;
4425
4426 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4427 return -EINVAL;
4428
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004429 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4430 return -EINVAL;
4431
4432 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4433 if (interval == 0)
4434 return -EINVAL;
4435
Luciano Coelho807f8a82011-05-11 17:09:35 +03004436 wiphy = &rdev->wiphy;
4437
4438 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4439 n_channels = validate_scan_freqs(
4440 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4441 if (!n_channels)
4442 return -EINVAL;
4443 } else {
4444 n_channels = 0;
4445
4446 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4447 if (wiphy->bands[band])
4448 n_channels += wiphy->bands[band]->n_channels;
4449 }
4450
4451 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4452 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4453 tmp)
4454 n_ssids++;
4455
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004456 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004457 return -EINVAL;
4458
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004459 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4460 nla_for_each_nested(attr,
4461 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4462 tmp)
4463 n_match_sets++;
4464
4465 if (n_match_sets > wiphy->max_match_sets)
4466 return -EINVAL;
4467
Luciano Coelho807f8a82011-05-11 17:09:35 +03004468 if (info->attrs[NL80211_ATTR_IE])
4469 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4470 else
4471 ie_len = 0;
4472
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004473 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004474 return -EINVAL;
4475
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004476 mutex_lock(&rdev->sched_scan_mtx);
4477
4478 if (rdev->sched_scan_req) {
4479 err = -EINPROGRESS;
4480 goto out;
4481 }
4482
Luciano Coelho807f8a82011-05-11 17:09:35 +03004483 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004484 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004485 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004486 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004487 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004488 if (!request) {
4489 err = -ENOMEM;
4490 goto out;
4491 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004492
4493 if (n_ssids)
4494 request->ssids = (void *)&request->channels[n_channels];
4495 request->n_ssids = n_ssids;
4496 if (ie_len) {
4497 if (request->ssids)
4498 request->ie = (void *)(request->ssids + n_ssids);
4499 else
4500 request->ie = (void *)(request->channels + n_channels);
4501 }
4502
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004503 if (n_match_sets) {
4504 if (request->ie)
4505 request->match_sets = (void *)(request->ie + ie_len);
4506 else if (request->ssids)
4507 request->match_sets =
4508 (void *)(request->ssids + n_ssids);
4509 else
4510 request->match_sets =
4511 (void *)(request->channels + n_channels);
4512 }
4513 request->n_match_sets = n_match_sets;
4514
Luciano Coelho807f8a82011-05-11 17:09:35 +03004515 i = 0;
4516 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4517 /* user specified, bail out if channel not found */
4518 nla_for_each_nested(attr,
4519 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4520 tmp) {
4521 struct ieee80211_channel *chan;
4522
4523 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4524
4525 if (!chan) {
4526 err = -EINVAL;
4527 goto out_free;
4528 }
4529
4530 /* ignore disabled channels */
4531 if (chan->flags & IEEE80211_CHAN_DISABLED)
4532 continue;
4533
4534 request->channels[i] = chan;
4535 i++;
4536 }
4537 } else {
4538 /* all channels */
4539 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4540 int j;
4541 if (!wiphy->bands[band])
4542 continue;
4543 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4544 struct ieee80211_channel *chan;
4545
4546 chan = &wiphy->bands[band]->channels[j];
4547
4548 if (chan->flags & IEEE80211_CHAN_DISABLED)
4549 continue;
4550
4551 request->channels[i] = chan;
4552 i++;
4553 }
4554 }
4555 }
4556
4557 if (!i) {
4558 err = -EINVAL;
4559 goto out_free;
4560 }
4561
4562 request->n_channels = i;
4563
4564 i = 0;
4565 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4566 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4567 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004568 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004569 err = -EINVAL;
4570 goto out_free;
4571 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004572 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004573 memcpy(request->ssids[i].ssid, nla_data(attr),
4574 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004575 i++;
4576 }
4577 }
4578
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004579 i = 0;
4580 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4581 nla_for_each_nested(attr,
4582 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4583 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004584 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004585
4586 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4587 nla_data(attr), nla_len(attr),
4588 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004589 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004590 if (ssid) {
4591 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4592 err = -EINVAL;
4593 goto out_free;
4594 }
4595 memcpy(request->match_sets[i].ssid.ssid,
4596 nla_data(ssid), nla_len(ssid));
4597 request->match_sets[i].ssid.ssid_len =
4598 nla_len(ssid);
4599 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004600 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4601 if (rssi)
4602 request->rssi_thold = nla_get_u32(rssi);
4603 else
4604 request->rssi_thold =
4605 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004606 i++;
4607 }
4608 }
4609
Luciano Coelho807f8a82011-05-11 17:09:35 +03004610 if (info->attrs[NL80211_ATTR_IE]) {
4611 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4612 memcpy((void *)request->ie,
4613 nla_data(info->attrs[NL80211_ATTR_IE]),
4614 request->ie_len);
4615 }
4616
Sam Leffler46856bb2012-10-11 21:03:32 -07004617 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004618 request->flags = nla_get_u32(
4619 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004620 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4621 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4622 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4623 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004624 err = -EOPNOTSUPP;
4625 goto out_free;
4626 }
4627 }
Sam Lefflered4737712012-10-11 21:03:31 -07004628
Luciano Coelho807f8a82011-05-11 17:09:35 +03004629 request->dev = dev;
4630 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004631 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004632 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004633
Hila Gonene35e4d22012-06-27 17:19:42 +03004634 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004635 if (!err) {
4636 rdev->sched_scan_req = request;
4637 nl80211_send_sched_scan(rdev, dev,
4638 NL80211_CMD_START_SCHED_SCAN);
4639 goto out;
4640 }
4641
4642out_free:
4643 kfree(request);
4644out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004645 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004646 return err;
4647}
4648
4649static int nl80211_stop_sched_scan(struct sk_buff *skb,
4650 struct genl_info *info)
4651{
4652 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004653 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004654
4655 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4656 !rdev->ops->sched_scan_stop)
4657 return -EOPNOTSUPP;
4658
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004659 mutex_lock(&rdev->sched_scan_mtx);
4660 err = __cfg80211_stop_sched_scan(rdev, false);
4661 mutex_unlock(&rdev->sched_scan_mtx);
4662
4663 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004664}
4665
Johannes Berg9720bb32011-06-21 09:45:33 +02004666static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4667 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004668 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004669 struct wireless_dev *wdev,
4670 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004671{
Johannes Berg48ab9052009-07-10 18:42:31 +02004672 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004673 void *hdr;
4674 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004675
4676 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004677
Eric W. Biederman15e47302012-09-07 20:12:54 +00004678 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004679 NL80211_CMD_NEW_SCAN_RESULTS);
4680 if (!hdr)
4681 return -1;
4682
Johannes Berg9720bb32011-06-21 09:45:33 +02004683 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4684
David S. Miller9360ffd2012-03-29 04:41:26 -04004685 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4686 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4687 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004688
4689 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4690 if (!bss)
4691 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004692 if ((!is_zero_ether_addr(res->bssid) &&
4693 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4694 (res->information_elements && res->len_information_elements &&
4695 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4696 res->len_information_elements,
4697 res->information_elements)) ||
4698 (res->beacon_ies && res->len_beacon_ies &&
4699 res->beacon_ies != res->information_elements &&
4700 nla_put(msg, NL80211_BSS_BEACON_IES,
4701 res->len_beacon_ies, res->beacon_ies)))
4702 goto nla_put_failure;
4703 if (res->tsf &&
4704 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4705 goto nla_put_failure;
4706 if (res->beacon_interval &&
4707 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4708 goto nla_put_failure;
4709 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4710 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4711 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4712 jiffies_to_msecs(jiffies - intbss->ts)))
4713 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004714
Johannes Berg77965c92009-02-18 18:45:06 +01004715 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004716 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004717 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4718 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004719 break;
4720 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004721 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4722 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004723 break;
4724 default:
4725 break;
4726 }
4727
Johannes Berg48ab9052009-07-10 18:42:31 +02004728 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004729 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004730 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004731 if (intbss == wdev->current_bss &&
4732 nla_put_u32(msg, NL80211_BSS_STATUS,
4733 NL80211_BSS_STATUS_ASSOCIATED))
4734 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004735 break;
4736 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004737 if (intbss == wdev->current_bss &&
4738 nla_put_u32(msg, NL80211_BSS_STATUS,
4739 NL80211_BSS_STATUS_IBSS_JOINED))
4740 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004741 break;
4742 default:
4743 break;
4744 }
4745
Johannes Berg2a519312009-02-10 21:25:55 +01004746 nla_nest_end(msg, bss);
4747
4748 return genlmsg_end(msg, hdr);
4749
4750 nla_put_failure:
4751 genlmsg_cancel(msg, hdr);
4752 return -EMSGSIZE;
4753}
4754
4755static int nl80211_dump_scan(struct sk_buff *skb,
4756 struct netlink_callback *cb)
4757{
Johannes Berg48ab9052009-07-10 18:42:31 +02004758 struct cfg80211_registered_device *rdev;
4759 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004760 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004761 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004762 int start = cb->args[1], idx = 0;
4763 int err;
4764
Johannes Berg67748892010-10-04 21:14:06 +02004765 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4766 if (err)
4767 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004768
Johannes Berg48ab9052009-07-10 18:42:31 +02004769 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004770
Johannes Berg48ab9052009-07-10 18:42:31 +02004771 wdev_lock(wdev);
4772 spin_lock_bh(&rdev->bss_lock);
4773 cfg80211_bss_expire(rdev);
4774
Johannes Berg9720bb32011-06-21 09:45:33 +02004775 cb->seq = rdev->bss_generation;
4776
Johannes Berg48ab9052009-07-10 18:42:31 +02004777 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004778 if (++idx <= start)
4779 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004780 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004781 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004782 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004783 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004784 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004785 }
4786 }
4787
Johannes Berg48ab9052009-07-10 18:42:31 +02004788 spin_unlock_bh(&rdev->bss_lock);
4789 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004790
4791 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004792 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004793
Johannes Berg67748892010-10-04 21:14:06 +02004794 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004795}
4796
Eric W. Biederman15e47302012-09-07 20:12:54 +00004797static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004798 int flags, struct net_device *dev,
4799 struct survey_info *survey)
4800{
4801 void *hdr;
4802 struct nlattr *infoattr;
4803
Eric W. Biederman15e47302012-09-07 20:12:54 +00004804 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004805 NL80211_CMD_NEW_SURVEY_RESULTS);
4806 if (!hdr)
4807 return -ENOMEM;
4808
David S. Miller9360ffd2012-03-29 04:41:26 -04004809 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4810 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004811
4812 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4813 if (!infoattr)
4814 goto nla_put_failure;
4815
David S. Miller9360ffd2012-03-29 04:41:26 -04004816 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4817 survey->channel->center_freq))
4818 goto nla_put_failure;
4819
4820 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4821 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4822 goto nla_put_failure;
4823 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4824 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4825 goto nla_put_failure;
4826 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4827 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4828 survey->channel_time))
4829 goto nla_put_failure;
4830 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4831 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4832 survey->channel_time_busy))
4833 goto nla_put_failure;
4834 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4835 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4836 survey->channel_time_ext_busy))
4837 goto nla_put_failure;
4838 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4839 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4840 survey->channel_time_rx))
4841 goto nla_put_failure;
4842 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4843 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4844 survey->channel_time_tx))
4845 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004846
4847 nla_nest_end(msg, infoattr);
4848
4849 return genlmsg_end(msg, hdr);
4850
4851 nla_put_failure:
4852 genlmsg_cancel(msg, hdr);
4853 return -EMSGSIZE;
4854}
4855
4856static int nl80211_dump_survey(struct sk_buff *skb,
4857 struct netlink_callback *cb)
4858{
4859 struct survey_info survey;
4860 struct cfg80211_registered_device *dev;
4861 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004862 int survey_idx = cb->args[1];
4863 int res;
4864
Johannes Berg67748892010-10-04 21:14:06 +02004865 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4866 if (res)
4867 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004868
4869 if (!dev->ops->dump_survey) {
4870 res = -EOPNOTSUPP;
4871 goto out_err;
4872 }
4873
4874 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004875 struct ieee80211_channel *chan;
4876
Hila Gonene35e4d22012-06-27 17:19:42 +03004877 res = rdev_dump_survey(dev, netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01004878 if (res == -ENOENT)
4879 break;
4880 if (res)
4881 goto out_err;
4882
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004883 /* Survey without a channel doesn't make sense */
4884 if (!survey.channel) {
4885 res = -EINVAL;
4886 goto out;
4887 }
4888
4889 chan = ieee80211_get_channel(&dev->wiphy,
4890 survey.channel->center_freq);
4891 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4892 survey_idx++;
4893 continue;
4894 }
4895
Holger Schurig61fa7132009-11-11 12:25:40 +01004896 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004897 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01004898 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4899 netdev,
4900 &survey) < 0)
4901 goto out;
4902 survey_idx++;
4903 }
4904
4905 out:
4906 cb->args[1] = survey_idx;
4907 res = skb->len;
4908 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004909 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004910 return res;
4911}
4912
Samuel Ortizb23aa672009-07-01 21:26:54 +02004913static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4914{
4915 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4916 NL80211_WPA_VERSION_2));
4917}
4918
Jouni Malinen636a5d32009-03-19 13:39:22 +02004919static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4920{
Johannes Berg4c476992010-10-04 21:36:35 +02004921 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4922 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004923 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03004924 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
4925 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004926 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004927 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004928 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004929
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004930 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4931 return -EINVAL;
4932
4933 if (!info->attrs[NL80211_ATTR_MAC])
4934 return -EINVAL;
4935
Jouni Malinen17780922009-03-27 20:52:47 +02004936 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4937 return -EINVAL;
4938
Johannes Berg19957bb2009-07-02 17:20:43 +02004939 if (!info->attrs[NL80211_ATTR_SSID])
4940 return -EINVAL;
4941
4942 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4943 return -EINVAL;
4944
Johannes Bergfffd0932009-07-08 14:22:54 +02004945 err = nl80211_parse_key(info, &key);
4946 if (err)
4947 return err;
4948
4949 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004950 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4951 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004952 if (!key.p.key || !key.p.key_len)
4953 return -EINVAL;
4954 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4955 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4956 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4957 key.p.key_len != WLAN_KEY_LEN_WEP104))
4958 return -EINVAL;
4959 if (key.idx > 4)
4960 return -EINVAL;
4961 } else {
4962 key.p.key_len = 0;
4963 key.p.key = NULL;
4964 }
4965
Johannes Bergafea0b72010-08-10 09:46:42 +02004966 if (key.idx >= 0) {
4967 int i;
4968 bool ok = false;
4969 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4970 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4971 ok = true;
4972 break;
4973 }
4974 }
Johannes Berg4c476992010-10-04 21:36:35 +02004975 if (!ok)
4976 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004977 }
4978
Johannes Berg4c476992010-10-04 21:36:35 +02004979 if (!rdev->ops->auth)
4980 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004981
Johannes Berg074ac8d2010-09-16 14:58:22 +02004982 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004983 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4984 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004985
Johannes Berg19957bb2009-07-02 17:20:43 +02004986 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004987 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004988 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004989 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4990 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004991
Johannes Berg19957bb2009-07-02 17:20:43 +02004992 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4993 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4994
4995 if (info->attrs[NL80211_ATTR_IE]) {
4996 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4997 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4998 }
4999
5000 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005001 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005002 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005003
Jouni Malinene39e5b52012-09-30 19:29:39 +03005004 if (auth_type == NL80211_AUTHTYPE_SAE &&
5005 !info->attrs[NL80211_ATTR_SAE_DATA])
5006 return -EINVAL;
5007
5008 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5009 if (auth_type != NL80211_AUTHTYPE_SAE)
5010 return -EINVAL;
5011 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5012 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5013 /* need to include at least Auth Transaction and Status Code */
5014 if (sae_data_len < 4)
5015 return -EINVAL;
5016 }
5017
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005018 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5019
Johannes Berg95de8172012-01-20 13:55:25 +01005020 /*
5021 * Since we no longer track auth state, ignore
5022 * requests to only change local state.
5023 */
5024 if (local_state_change)
5025 return 0;
5026
Johannes Berg4c476992010-10-04 21:36:35 +02005027 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5028 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005029 key.p.key, key.p.key_len, key.idx,
5030 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005031}
5032
Johannes Bergc0692b82010-08-27 14:26:53 +03005033static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5034 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005035 struct cfg80211_crypto_settings *settings,
5036 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005037{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005038 memset(settings, 0, sizeof(*settings));
5039
Samuel Ortizb23aa672009-07-01 21:26:54 +02005040 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5041
Johannes Bergc0692b82010-08-27 14:26:53 +03005042 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5043 u16 proto;
5044 proto = nla_get_u16(
5045 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5046 settings->control_port_ethertype = cpu_to_be16(proto);
5047 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5048 proto != ETH_P_PAE)
5049 return -EINVAL;
5050 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5051 settings->control_port_no_encrypt = true;
5052 } else
5053 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5054
Samuel Ortizb23aa672009-07-01 21:26:54 +02005055 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5056 void *data;
5057 int len, i;
5058
5059 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5060 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5061 settings->n_ciphers_pairwise = len / sizeof(u32);
5062
5063 if (len % sizeof(u32))
5064 return -EINVAL;
5065
Johannes Berg3dc27d22009-07-02 21:36:37 +02005066 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005067 return -EINVAL;
5068
5069 memcpy(settings->ciphers_pairwise, data, len);
5070
5071 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005072 if (!cfg80211_supported_cipher_suite(
5073 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005074 settings->ciphers_pairwise[i]))
5075 return -EINVAL;
5076 }
5077
5078 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5079 settings->cipher_group =
5080 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005081 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5082 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005083 return -EINVAL;
5084 }
5085
5086 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5087 settings->wpa_versions =
5088 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5089 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5090 return -EINVAL;
5091 }
5092
5093 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5094 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005095 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005096
5097 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5098 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5099 settings->n_akm_suites = len / sizeof(u32);
5100
5101 if (len % sizeof(u32))
5102 return -EINVAL;
5103
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005104 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5105 return -EINVAL;
5106
Samuel Ortizb23aa672009-07-01 21:26:54 +02005107 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005108 }
5109
5110 return 0;
5111}
5112
Jouni Malinen636a5d32009-03-19 13:39:22 +02005113static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5114{
Johannes Berg4c476992010-10-04 21:36:35 +02005115 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5116 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005117 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005118 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005119 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005120 int err, ssid_len, ie_len = 0;
5121 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005122 u32 flags = 0;
5123 struct ieee80211_ht_cap *ht_capa = NULL;
5124 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005125
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005126 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5127 return -EINVAL;
5128
5129 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005130 !info->attrs[NL80211_ATTR_SSID] ||
5131 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005132 return -EINVAL;
5133
Johannes Berg4c476992010-10-04 21:36:35 +02005134 if (!rdev->ops->assoc)
5135 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005136
Johannes Berg074ac8d2010-09-16 14:58:22 +02005137 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005138 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5139 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005140
Johannes Berg19957bb2009-07-02 17:20:43 +02005141 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005142
Johannes Berg19957bb2009-07-02 17:20:43 +02005143 chan = ieee80211_get_channel(&rdev->wiphy,
5144 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005145 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5146 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005147
Johannes Berg19957bb2009-07-02 17:20:43 +02005148 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5149 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005150
5151 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005152 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5153 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005154 }
5155
Jouni Malinendc6382c2009-05-06 22:09:37 +03005156 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005157 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005158 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005159 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005160 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005161 else if (mfp != NL80211_MFP_NO)
5162 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005163 }
5164
Johannes Berg3e5d7642009-07-07 14:37:26 +02005165 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5166 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5167
Ben Greear7e7c8922011-11-18 11:31:59 -08005168 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5169 flags |= ASSOC_REQ_DISABLE_HT;
5170
5171 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5172 ht_capa_mask =
5173 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5174
5175 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5176 if (!ht_capa_mask)
5177 return -EINVAL;
5178 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5179 }
5180
Johannes Bergc0692b82010-08-27 14:26:53 +03005181 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005182 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005183 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5184 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005185 &crypto, flags, ht_capa,
5186 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005187
Jouni Malinen636a5d32009-03-19 13:39:22 +02005188 return err;
5189}
5190
5191static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5192{
Johannes Berg4c476992010-10-04 21:36:35 +02005193 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5194 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005195 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005196 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005197 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005198 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005199
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005200 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5201 return -EINVAL;
5202
5203 if (!info->attrs[NL80211_ATTR_MAC])
5204 return -EINVAL;
5205
5206 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5207 return -EINVAL;
5208
Johannes Berg4c476992010-10-04 21:36:35 +02005209 if (!rdev->ops->deauth)
5210 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005211
Johannes Berg074ac8d2010-09-16 14:58:22 +02005212 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005213 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5214 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005215
Johannes Berg19957bb2009-07-02 17:20:43 +02005216 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005217
Johannes Berg19957bb2009-07-02 17:20:43 +02005218 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5219 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005220 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005221 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005222 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005223
5224 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005225 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5226 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005227 }
5228
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005229 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5230
Johannes Berg4c476992010-10-04 21:36:35 +02005231 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5232 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005233}
5234
5235static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5236{
Johannes Berg4c476992010-10-04 21:36:35 +02005237 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5238 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005239 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005240 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005241 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005242 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005243
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005244 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5245 return -EINVAL;
5246
5247 if (!info->attrs[NL80211_ATTR_MAC])
5248 return -EINVAL;
5249
5250 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5251 return -EINVAL;
5252
Johannes Berg4c476992010-10-04 21:36:35 +02005253 if (!rdev->ops->disassoc)
5254 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005255
Johannes Berg074ac8d2010-09-16 14:58:22 +02005256 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005257 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5258 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005259
Johannes Berg19957bb2009-07-02 17:20:43 +02005260 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005261
Johannes Berg19957bb2009-07-02 17:20:43 +02005262 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5263 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005264 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005265 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005266 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005267
5268 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005269 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5270 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005271 }
5272
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005273 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5274
Johannes Berg4c476992010-10-04 21:36:35 +02005275 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5276 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005277}
5278
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005279static bool
5280nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5281 int mcast_rate[IEEE80211_NUM_BANDS],
5282 int rateval)
5283{
5284 struct wiphy *wiphy = &rdev->wiphy;
5285 bool found = false;
5286 int band, i;
5287
5288 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5289 struct ieee80211_supported_band *sband;
5290
5291 sband = wiphy->bands[band];
5292 if (!sband)
5293 continue;
5294
5295 for (i = 0; i < sband->n_bitrates; i++) {
5296 if (sband->bitrates[i].bitrate == rateval) {
5297 mcast_rate[band] = i + 1;
5298 found = true;
5299 break;
5300 }
5301 }
5302 }
5303
5304 return found;
5305}
5306
Johannes Berg04a773a2009-04-19 21:24:32 +02005307static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5308{
Johannes Berg4c476992010-10-04 21:36:35 +02005309 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5310 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005311 struct cfg80211_ibss_params ibss;
5312 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005313 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005314 int err;
5315
Johannes Berg8e30bc52009-04-22 17:45:38 +02005316 memset(&ibss, 0, sizeof(ibss));
5317
Johannes Berg04a773a2009-04-19 21:24:32 +02005318 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5319 return -EINVAL;
5320
5321 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5322 !info->attrs[NL80211_ATTR_SSID] ||
5323 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5324 return -EINVAL;
5325
Johannes Berg8e30bc52009-04-22 17:45:38 +02005326 ibss.beacon_interval = 100;
5327
5328 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5329 ibss.beacon_interval =
5330 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5331 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5332 return -EINVAL;
5333 }
5334
Johannes Berg4c476992010-10-04 21:36:35 +02005335 if (!rdev->ops->join_ibss)
5336 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005337
Johannes Berg4c476992010-10-04 21:36:35 +02005338 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5339 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005340
Johannes Berg79c97e92009-07-07 03:56:12 +02005341 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005342
Johannes Berg39193492011-09-16 13:45:25 +02005343 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005344 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005345
5346 if (!is_valid_ether_addr(ibss.bssid))
5347 return -EINVAL;
5348 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005349 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5350 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5351
5352 if (info->attrs[NL80211_ATTR_IE]) {
5353 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5354 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5355 }
5356
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005357 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5358 enum nl80211_channel_type channel_type;
5359
Johannes Bergcd6c6592012-05-10 21:27:18 +02005360 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005361 return -EINVAL;
5362
5363 if (channel_type != NL80211_CHAN_NO_HT &&
5364 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5365 return -EINVAL;
5366
5367 ibss.channel_type = channel_type;
5368 } else {
5369 ibss.channel_type = NL80211_CHAN_NO_HT;
5370 }
5371
5372 ibss.channel = rdev_freq_to_chan(rdev,
5373 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5374 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005375 if (!ibss.channel ||
5376 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005377 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5378 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005379
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005380 /* Both channels should be able to initiate communication */
5381 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5382 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5383 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5384 ibss.channel_type))
5385 return -EINVAL;
5386
Johannes Berg04a773a2009-04-19 21:24:32 +02005387 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005388 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005389
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005390 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5391 u8 *rates =
5392 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5393 int n_rates =
5394 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5395 struct ieee80211_supported_band *sband =
5396 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005397
Johannes Berg34850ab2011-07-18 18:08:35 +02005398 err = ieee80211_get_ratemask(sband, rates, n_rates,
5399 &ibss.basic_rates);
5400 if (err)
5401 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005402 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005403
5404 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5405 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5406 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5407 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005408
Johannes Berg4c476992010-10-04 21:36:35 +02005409 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305410 bool no_ht = false;
5411
Johannes Berg4c476992010-10-04 21:36:35 +02005412 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305413 info->attrs[NL80211_ATTR_KEYS],
5414 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005415 if (IS_ERR(connkeys))
5416 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305417
5418 if ((ibss.channel_type != NL80211_CHAN_NO_HT) && no_ht) {
5419 kfree(connkeys);
5420 return -EINVAL;
5421 }
Johannes Berg4c476992010-10-04 21:36:35 +02005422 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005423
Antonio Quartulli267335d2012-01-31 20:25:47 +01005424 ibss.control_port =
5425 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5426
Johannes Berg4c476992010-10-04 21:36:35 +02005427 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005428 if (err)
5429 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005430 return err;
5431}
5432
5433static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5434{
Johannes Berg4c476992010-10-04 21:36:35 +02005435 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5436 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005437
Johannes Berg4c476992010-10-04 21:36:35 +02005438 if (!rdev->ops->leave_ibss)
5439 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005440
Johannes Berg4c476992010-10-04 21:36:35 +02005441 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5442 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005443
Johannes Berg4c476992010-10-04 21:36:35 +02005444 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005445}
5446
Johannes Bergaff89a92009-07-01 21:26:51 +02005447#ifdef CONFIG_NL80211_TESTMODE
5448static struct genl_multicast_group nl80211_testmode_mcgrp = {
5449 .name = "testmode",
5450};
5451
5452static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5453{
Johannes Berg4c476992010-10-04 21:36:35 +02005454 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005455 int err;
5456
5457 if (!info->attrs[NL80211_ATTR_TESTDATA])
5458 return -EINVAL;
5459
Johannes Bergaff89a92009-07-01 21:26:51 +02005460 err = -EOPNOTSUPP;
5461 if (rdev->ops->testmode_cmd) {
5462 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03005463 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02005464 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5465 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5466 rdev->testmode_info = NULL;
5467 }
5468
Johannes Bergaff89a92009-07-01 21:26:51 +02005469 return err;
5470}
5471
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005472static int nl80211_testmode_dump(struct sk_buff *skb,
5473 struct netlink_callback *cb)
5474{
Johannes Berg00918d32011-12-13 17:22:05 +01005475 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005476 int err;
5477 long phy_idx;
5478 void *data = NULL;
5479 int data_len = 0;
5480
5481 if (cb->args[0]) {
5482 /*
5483 * 0 is a valid index, but not valid for args[0],
5484 * so we need to offset by 1.
5485 */
5486 phy_idx = cb->args[0] - 1;
5487 } else {
5488 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5489 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5490 nl80211_policy);
5491 if (err)
5492 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005493
Johannes Berg2bd7e352012-06-15 14:23:16 +02005494 mutex_lock(&cfg80211_mutex);
5495 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5496 nl80211_fam.attrbuf);
5497 if (IS_ERR(rdev)) {
5498 mutex_unlock(&cfg80211_mutex);
5499 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005500 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005501 phy_idx = rdev->wiphy_idx;
5502 rdev = NULL;
5503 mutex_unlock(&cfg80211_mutex);
5504
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005505 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5506 cb->args[1] =
5507 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5508 }
5509
5510 if (cb->args[1]) {
5511 data = nla_data((void *)cb->args[1]);
5512 data_len = nla_len((void *)cb->args[1]);
5513 }
5514
5515 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005516 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5517 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005518 mutex_unlock(&cfg80211_mutex);
5519 return -ENOENT;
5520 }
Johannes Berg00918d32011-12-13 17:22:05 +01005521 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005522 mutex_unlock(&cfg80211_mutex);
5523
Johannes Berg00918d32011-12-13 17:22:05 +01005524 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005525 err = -EOPNOTSUPP;
5526 goto out_err;
5527 }
5528
5529 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005530 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005531 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5532 NL80211_CMD_TESTMODE);
5533 struct nlattr *tmdata;
5534
David S. Miller9360ffd2012-03-29 04:41:26 -04005535 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005536 genlmsg_cancel(skb, hdr);
5537 break;
5538 }
5539
5540 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5541 if (!tmdata) {
5542 genlmsg_cancel(skb, hdr);
5543 break;
5544 }
Hila Gonene35e4d22012-06-27 17:19:42 +03005545 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005546 nla_nest_end(skb, tmdata);
5547
5548 if (err == -ENOBUFS || err == -ENOENT) {
5549 genlmsg_cancel(skb, hdr);
5550 break;
5551 } else if (err) {
5552 genlmsg_cancel(skb, hdr);
5553 goto out_err;
5554 }
5555
5556 genlmsg_end(skb, hdr);
5557 }
5558
5559 err = skb->len;
5560 /* see above */
5561 cb->args[0] = phy_idx + 1;
5562 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005563 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005564 return err;
5565}
5566
Johannes Bergaff89a92009-07-01 21:26:51 +02005567static struct sk_buff *
5568__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005569 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005570{
5571 struct sk_buff *skb;
5572 void *hdr;
5573 struct nlattr *data;
5574
5575 skb = nlmsg_new(approxlen + 100, gfp);
5576 if (!skb)
5577 return NULL;
5578
Eric W. Biederman15e47302012-09-07 20:12:54 +00005579 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005580 if (!hdr) {
5581 kfree_skb(skb);
5582 return NULL;
5583 }
5584
David S. Miller9360ffd2012-03-29 04:41:26 -04005585 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5586 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005587 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5588
5589 ((void **)skb->cb)[0] = rdev;
5590 ((void **)skb->cb)[1] = hdr;
5591 ((void **)skb->cb)[2] = data;
5592
5593 return skb;
5594
5595 nla_put_failure:
5596 kfree_skb(skb);
5597 return NULL;
5598}
5599
5600struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5601 int approxlen)
5602{
5603 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5604
5605 if (WARN_ON(!rdev->testmode_info))
5606 return NULL;
5607
5608 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005609 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005610 rdev->testmode_info->snd_seq,
5611 GFP_KERNEL);
5612}
5613EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5614
5615int cfg80211_testmode_reply(struct sk_buff *skb)
5616{
5617 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5618 void *hdr = ((void **)skb->cb)[1];
5619 struct nlattr *data = ((void **)skb->cb)[2];
5620
5621 if (WARN_ON(!rdev->testmode_info)) {
5622 kfree_skb(skb);
5623 return -EINVAL;
5624 }
5625
5626 nla_nest_end(skb, data);
5627 genlmsg_end(skb, hdr);
5628 return genlmsg_reply(skb, rdev->testmode_info);
5629}
5630EXPORT_SYMBOL(cfg80211_testmode_reply);
5631
5632struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5633 int approxlen, gfp_t gfp)
5634{
5635 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5636
5637 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5638}
5639EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5640
5641void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5642{
5643 void *hdr = ((void **)skb->cb)[1];
5644 struct nlattr *data = ((void **)skb->cb)[2];
5645
5646 nla_nest_end(skb, data);
5647 genlmsg_end(skb, hdr);
5648 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5649}
5650EXPORT_SYMBOL(cfg80211_testmode_event);
5651#endif
5652
Samuel Ortizb23aa672009-07-01 21:26:54 +02005653static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5654{
Johannes Berg4c476992010-10-04 21:36:35 +02005655 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5656 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005657 struct cfg80211_connect_params connect;
5658 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005659 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005660 int err;
5661
5662 memset(&connect, 0, sizeof(connect));
5663
5664 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5665 return -EINVAL;
5666
5667 if (!info->attrs[NL80211_ATTR_SSID] ||
5668 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5669 return -EINVAL;
5670
5671 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5672 connect.auth_type =
5673 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005674 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5675 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005676 return -EINVAL;
5677 } else
5678 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5679
5680 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5681
Johannes Bergc0692b82010-08-27 14:26:53 +03005682 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005683 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005684 if (err)
5685 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005686
Johannes Berg074ac8d2010-09-16 14:58:22 +02005687 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005688 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5689 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005690
Johannes Berg79c97e92009-07-07 03:56:12 +02005691 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005692
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305693 connect.bg_scan_period = -1;
5694 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5695 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5696 connect.bg_scan_period =
5697 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5698 }
5699
Samuel Ortizb23aa672009-07-01 21:26:54 +02005700 if (info->attrs[NL80211_ATTR_MAC])
5701 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5702 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5703 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5704
5705 if (info->attrs[NL80211_ATTR_IE]) {
5706 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5707 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5708 }
5709
5710 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5711 connect.channel =
5712 ieee80211_get_channel(wiphy,
5713 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5714 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005715 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5716 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005717 }
5718
Johannes Bergfffd0932009-07-08 14:22:54 +02005719 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5720 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305721 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02005722 if (IS_ERR(connkeys))
5723 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005724 }
5725
Ben Greear7e7c8922011-11-18 11:31:59 -08005726 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5727 connect.flags |= ASSOC_REQ_DISABLE_HT;
5728
5729 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5730 memcpy(&connect.ht_capa_mask,
5731 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5732 sizeof(connect.ht_capa_mask));
5733
5734 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005735 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5736 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005737 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005738 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005739 memcpy(&connect.ht_capa,
5740 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5741 sizeof(connect.ht_capa));
5742 }
5743
Johannes Bergfffd0932009-07-08 14:22:54 +02005744 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005745 if (err)
5746 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005747 return err;
5748}
5749
5750static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5751{
Johannes Berg4c476992010-10-04 21:36:35 +02005752 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5753 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005754 u16 reason;
5755
5756 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5757 reason = WLAN_REASON_DEAUTH_LEAVING;
5758 else
5759 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5760
5761 if (reason == 0)
5762 return -EINVAL;
5763
Johannes Berg074ac8d2010-09-16 14:58:22 +02005764 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005765 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5766 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005767
Johannes Berg4c476992010-10-04 21:36:35 +02005768 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005769}
5770
Johannes Berg463d0182009-07-14 00:33:35 +02005771static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5772{
Johannes Berg4c476992010-10-04 21:36:35 +02005773 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005774 struct net *net;
5775 int err;
5776 u32 pid;
5777
5778 if (!info->attrs[NL80211_ATTR_PID])
5779 return -EINVAL;
5780
5781 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5782
Johannes Berg463d0182009-07-14 00:33:35 +02005783 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005784 if (IS_ERR(net))
5785 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005786
5787 err = 0;
5788
5789 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005790 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5791 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005792
Johannes Berg463d0182009-07-14 00:33:35 +02005793 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005794 return err;
5795}
5796
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005797static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5798{
Johannes Berg4c476992010-10-04 21:36:35 +02005799 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005800 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5801 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005802 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005803 struct cfg80211_pmksa pmksa;
5804
5805 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5806
5807 if (!info->attrs[NL80211_ATTR_MAC])
5808 return -EINVAL;
5809
5810 if (!info->attrs[NL80211_ATTR_PMKID])
5811 return -EINVAL;
5812
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005813 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5814 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5815
Johannes Berg074ac8d2010-09-16 14:58:22 +02005816 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005817 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5818 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005819
5820 switch (info->genlhdr->cmd) {
5821 case NL80211_CMD_SET_PMKSA:
5822 rdev_ops = rdev->ops->set_pmksa;
5823 break;
5824 case NL80211_CMD_DEL_PMKSA:
5825 rdev_ops = rdev->ops->del_pmksa;
5826 break;
5827 default:
5828 WARN_ON(1);
5829 break;
5830 }
5831
Johannes Berg4c476992010-10-04 21:36:35 +02005832 if (!rdev_ops)
5833 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005834
Johannes Berg4c476992010-10-04 21:36:35 +02005835 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005836}
5837
5838static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5839{
Johannes Berg4c476992010-10-04 21:36:35 +02005840 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5841 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005842
Johannes Berg074ac8d2010-09-16 14:58:22 +02005843 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005844 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5845 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005846
Johannes Berg4c476992010-10-04 21:36:35 +02005847 if (!rdev->ops->flush_pmksa)
5848 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005849
Hila Gonene35e4d22012-06-27 17:19:42 +03005850 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005851}
5852
Arik Nemtsov109086c2011-09-28 14:12:50 +03005853static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5854{
5855 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5856 struct net_device *dev = info->user_ptr[1];
5857 u8 action_code, dialog_token;
5858 u16 status_code;
5859 u8 *peer;
5860
5861 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5862 !rdev->ops->tdls_mgmt)
5863 return -EOPNOTSUPP;
5864
5865 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5866 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5867 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5868 !info->attrs[NL80211_ATTR_IE] ||
5869 !info->attrs[NL80211_ATTR_MAC])
5870 return -EINVAL;
5871
5872 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5873 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5874 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5875 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5876
Hila Gonene35e4d22012-06-27 17:19:42 +03005877 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
5878 dialog_token, status_code,
5879 nla_data(info->attrs[NL80211_ATTR_IE]),
5880 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03005881}
5882
5883static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5884{
5885 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5886 struct net_device *dev = info->user_ptr[1];
5887 enum nl80211_tdls_operation operation;
5888 u8 *peer;
5889
5890 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5891 !rdev->ops->tdls_oper)
5892 return -EOPNOTSUPP;
5893
5894 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5895 !info->attrs[NL80211_ATTR_MAC])
5896 return -EINVAL;
5897
5898 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5899 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5900
Hila Gonene35e4d22012-06-27 17:19:42 +03005901 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03005902}
5903
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005904static int nl80211_remain_on_channel(struct sk_buff *skb,
5905 struct genl_info *info)
5906{
Johannes Berg4c476992010-10-04 21:36:35 +02005907 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005908 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005909 struct ieee80211_channel *chan;
5910 struct sk_buff *msg;
5911 void *hdr;
5912 u64 cookie;
5913 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5914 u32 freq, duration;
5915 int err;
5916
5917 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5918 !info->attrs[NL80211_ATTR_DURATION])
5919 return -EINVAL;
5920
5921 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5922
Johannes Berg7c4ef712011-11-18 15:33:48 +01005923 if (!rdev->ops->remain_on_channel ||
5924 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005925 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005926
Johannes Bergebf348f2012-06-01 12:50:54 +02005927 /*
5928 * We should be on that channel for at least a minimum amount of
5929 * time (10ms) but no longer than the driver supports.
5930 */
5931 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5932 duration > rdev->wiphy.max_remain_on_channel_duration)
5933 return -EINVAL;
5934
Johannes Bergcd6c6592012-05-10 21:27:18 +02005935 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5936 !nl80211_valid_channel_type(info, &channel_type))
5937 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005938
5939 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5940 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005941 if (chan == NULL)
5942 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005943
5944 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005945 if (!msg)
5946 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005947
Eric W. Biederman15e47302012-09-07 20:12:54 +00005948 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005949 NL80211_CMD_REMAIN_ON_CHANNEL);
5950
5951 if (IS_ERR(hdr)) {
5952 err = PTR_ERR(hdr);
5953 goto free_msg;
5954 }
5955
Hila Gonene35e4d22012-06-27 17:19:42 +03005956 err = rdev_remain_on_channel(rdev, wdev, chan, channel_type, duration,
5957 &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005958
5959 if (err)
5960 goto free_msg;
5961
David S. Miller9360ffd2012-03-29 04:41:26 -04005962 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5963 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005964
5965 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005966
5967 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005968
5969 nla_put_failure:
5970 err = -ENOBUFS;
5971 free_msg:
5972 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005973 return err;
5974}
5975
5976static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5977 struct genl_info *info)
5978{
Johannes Berg4c476992010-10-04 21:36:35 +02005979 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005980 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005981 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005982
5983 if (!info->attrs[NL80211_ATTR_COOKIE])
5984 return -EINVAL;
5985
Johannes Berg4c476992010-10-04 21:36:35 +02005986 if (!rdev->ops->cancel_remain_on_channel)
5987 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005988
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005989 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5990
Hila Gonene35e4d22012-06-27 17:19:42 +03005991 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005992}
5993
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005994static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5995 u8 *rates, u8 rates_len)
5996{
5997 u8 i;
5998 u32 mask = 0;
5999
6000 for (i = 0; i < rates_len; i++) {
6001 int rate = (rates[i] & 0x7f) * 5;
6002 int ridx;
6003 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6004 struct ieee80211_rate *srate =
6005 &sband->bitrates[ridx];
6006 if (rate == srate->bitrate) {
6007 mask |= 1 << ridx;
6008 break;
6009 }
6010 }
6011 if (ridx == sband->n_bitrates)
6012 return 0; /* rate not found */
6013 }
6014
6015 return mask;
6016}
6017
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006018static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6019 u8 *rates, u8 rates_len,
6020 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6021{
6022 u8 i;
6023
6024 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6025
6026 for (i = 0; i < rates_len; i++) {
6027 int ridx, rbit;
6028
6029 ridx = rates[i] / 8;
6030 rbit = BIT(rates[i] % 8);
6031
6032 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006033 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006034 return false;
6035
6036 /* check availability */
6037 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6038 mcs[ridx] |= rbit;
6039 else
6040 return false;
6041 }
6042
6043 return true;
6044}
6045
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006046static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006047 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6048 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006049 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6050 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006051};
6052
6053static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6054 struct genl_info *info)
6055{
6056 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006057 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006058 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006059 int rem, i;
6060 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006061 struct nlattr *tx_rates;
6062 struct ieee80211_supported_band *sband;
6063
6064 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6065 return -EINVAL;
6066
Johannes Berg4c476992010-10-04 21:36:35 +02006067 if (!rdev->ops->set_bitrate_mask)
6068 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006069
6070 memset(&mask, 0, sizeof(mask));
6071 /* Default to all rates enabled */
6072 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6073 sband = rdev->wiphy.bands[i];
6074 mask.control[i].legacy =
6075 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006076 if (sband)
6077 memcpy(mask.control[i].mcs,
6078 sband->ht_cap.mcs.rx_mask,
6079 sizeof(mask.control[i].mcs));
6080 else
6081 memset(mask.control[i].mcs, 0,
6082 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006083 }
6084
6085 /*
6086 * The nested attribute uses enum nl80211_band as the index. This maps
6087 * directly to the enum ieee80211_band values used in cfg80211.
6088 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006089 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006090 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6091 {
6092 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006093 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6094 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006095 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006096 if (sband == NULL)
6097 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006098 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6099 nla_len(tx_rates), nl80211_txattr_policy);
6100 if (tb[NL80211_TXRATE_LEGACY]) {
6101 mask.control[band].legacy = rateset_to_mask(
6102 sband,
6103 nla_data(tb[NL80211_TXRATE_LEGACY]),
6104 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306105 if ((mask.control[band].legacy == 0) &&
6106 nla_len(tb[NL80211_TXRATE_LEGACY]))
6107 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006108 }
6109 if (tb[NL80211_TXRATE_MCS]) {
6110 if (!ht_rateset_to_mask(
6111 sband,
6112 nla_data(tb[NL80211_TXRATE_MCS]),
6113 nla_len(tb[NL80211_TXRATE_MCS]),
6114 mask.control[band].mcs))
6115 return -EINVAL;
6116 }
6117
6118 if (mask.control[band].legacy == 0) {
6119 /* don't allow empty legacy rates if HT
6120 * is not even supported. */
6121 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6122 return -EINVAL;
6123
6124 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6125 if (mask.control[band].mcs[i])
6126 break;
6127
6128 /* legacy and mcs rates may not be both empty */
6129 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006130 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006131 }
6132 }
6133
Hila Gonene35e4d22012-06-27 17:19:42 +03006134 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006135}
6136
Johannes Berg2e161f72010-08-12 15:38:38 +02006137static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006138{
Johannes Berg4c476992010-10-04 21:36:35 +02006139 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006140 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006141 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006142
6143 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6144 return -EINVAL;
6145
Johannes Berg2e161f72010-08-12 15:38:38 +02006146 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6147 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006148
Johannes Berg71bbc992012-06-15 15:30:18 +02006149 switch (wdev->iftype) {
6150 case NL80211_IFTYPE_STATION:
6151 case NL80211_IFTYPE_ADHOC:
6152 case NL80211_IFTYPE_P2P_CLIENT:
6153 case NL80211_IFTYPE_AP:
6154 case NL80211_IFTYPE_AP_VLAN:
6155 case NL80211_IFTYPE_MESH_POINT:
6156 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006157 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006158 break;
6159 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006160 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006161 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006162
6163 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006164 if (!rdev->ops->mgmt_tx)
6165 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006166
Eric W. Biederman15e47302012-09-07 20:12:54 +00006167 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006168 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6169 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006170}
6171
Johannes Berg2e161f72010-08-12 15:38:38 +02006172static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006173{
Johannes Berg4c476992010-10-04 21:36:35 +02006174 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006175 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02006176 struct ieee80211_channel *chan;
6177 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02006178 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02006179 u32 freq;
6180 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006181 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006182 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006183 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006184 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006185 bool offchan, no_cck, dont_wait_for_ack;
6186
6187 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006188
6189 if (!info->attrs[NL80211_ATTR_FRAME] ||
6190 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6191 return -EINVAL;
6192
Johannes Berg4c476992010-10-04 21:36:35 +02006193 if (!rdev->ops->mgmt_tx)
6194 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006195
Johannes Berg71bbc992012-06-15 15:30:18 +02006196 switch (wdev->iftype) {
6197 case NL80211_IFTYPE_STATION:
6198 case NL80211_IFTYPE_ADHOC:
6199 case NL80211_IFTYPE_P2P_CLIENT:
6200 case NL80211_IFTYPE_AP:
6201 case NL80211_IFTYPE_AP_VLAN:
6202 case NL80211_IFTYPE_MESH_POINT:
6203 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006204 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006205 break;
6206 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006207 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006208 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006209
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006210 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006211 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006212 return -EINVAL;
6213 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006214
6215 /*
6216 * We should wait on the channel for at least a minimum amount
6217 * of time (10ms) but no longer than the driver supports.
6218 */
6219 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6220 wait > rdev->wiphy.max_remain_on_channel_duration)
6221 return -EINVAL;
6222
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006223 }
6224
Jouni Malinen026331c2010-02-15 12:53:10 +02006225 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02006226 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02006227 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02006228 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02006229 }
6230
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006231 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6232
Johannes Berg7c4ef712011-11-18 15:33:48 +01006233 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6234 return -EINVAL;
6235
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306236 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6237
Jouni Malinen026331c2010-02-15 12:53:10 +02006238 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6239 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02006240 if (chan == NULL)
6241 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006242
Johannes Berge247bd902011-11-04 11:18:21 +01006243 if (!dont_wait_for_ack) {
6244 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6245 if (!msg)
6246 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006247
Eric W. Biederman15e47302012-09-07 20:12:54 +00006248 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006249 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006250
Johannes Berge247bd902011-11-04 11:18:21 +01006251 if (IS_ERR(hdr)) {
6252 err = PTR_ERR(hdr);
6253 goto free_msg;
6254 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006255 }
Johannes Berge247bd902011-11-04 11:18:21 +01006256
Johannes Berg71bbc992012-06-15 15:30:18 +02006257 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chan, offchan, channel_type,
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006258 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006259 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6260 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006261 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006262 if (err)
6263 goto free_msg;
6264
Johannes Berge247bd902011-11-04 11:18:21 +01006265 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006266 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6267 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006268
Johannes Berge247bd902011-11-04 11:18:21 +01006269 genlmsg_end(msg, hdr);
6270 return genlmsg_reply(msg, info);
6271 }
6272
6273 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006274
6275 nla_put_failure:
6276 err = -ENOBUFS;
6277 free_msg:
6278 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006279 return err;
6280}
6281
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006282static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6283{
6284 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006285 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006286 u64 cookie;
6287
6288 if (!info->attrs[NL80211_ATTR_COOKIE])
6289 return -EINVAL;
6290
6291 if (!rdev->ops->mgmt_tx_cancel_wait)
6292 return -EOPNOTSUPP;
6293
Johannes Berg71bbc992012-06-15 15:30:18 +02006294 switch (wdev->iftype) {
6295 case NL80211_IFTYPE_STATION:
6296 case NL80211_IFTYPE_ADHOC:
6297 case NL80211_IFTYPE_P2P_CLIENT:
6298 case NL80211_IFTYPE_AP:
6299 case NL80211_IFTYPE_AP_VLAN:
6300 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006301 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006302 break;
6303 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006304 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006305 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006306
6307 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6308
Hila Gonene35e4d22012-06-27 17:19:42 +03006309 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006310}
6311
Kalle Valoffb9eb32010-02-17 17:58:10 +02006312static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6313{
Johannes Berg4c476992010-10-04 21:36:35 +02006314 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006315 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006316 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006317 u8 ps_state;
6318 bool state;
6319 int err;
6320
Johannes Berg4c476992010-10-04 21:36:35 +02006321 if (!info->attrs[NL80211_ATTR_PS_STATE])
6322 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006323
6324 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6325
Johannes Berg4c476992010-10-04 21:36:35 +02006326 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6327 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006328
6329 wdev = dev->ieee80211_ptr;
6330
Johannes Berg4c476992010-10-04 21:36:35 +02006331 if (!rdev->ops->set_power_mgmt)
6332 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006333
6334 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6335
6336 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006337 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006338
Hila Gonene35e4d22012-06-27 17:19:42 +03006339 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02006340 if (!err)
6341 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006342 return err;
6343}
6344
6345static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6346{
Johannes Berg4c476992010-10-04 21:36:35 +02006347 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006348 enum nl80211_ps_state ps_state;
6349 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006350 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006351 struct sk_buff *msg;
6352 void *hdr;
6353 int err;
6354
Kalle Valoffb9eb32010-02-17 17:58:10 +02006355 wdev = dev->ieee80211_ptr;
6356
Johannes Berg4c476992010-10-04 21:36:35 +02006357 if (!rdev->ops->set_power_mgmt)
6358 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006359
6360 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006361 if (!msg)
6362 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006363
Eric W. Biederman15e47302012-09-07 20:12:54 +00006364 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006365 NL80211_CMD_GET_POWER_SAVE);
6366 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006367 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006368 goto free_msg;
6369 }
6370
6371 if (wdev->ps)
6372 ps_state = NL80211_PS_ENABLED;
6373 else
6374 ps_state = NL80211_PS_DISABLED;
6375
David S. Miller9360ffd2012-03-29 04:41:26 -04006376 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6377 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006378
6379 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006380 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006381
Johannes Berg4c476992010-10-04 21:36:35 +02006382 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006383 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006384 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006385 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006386 return err;
6387}
6388
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006389static struct nla_policy
6390nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6391 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6392 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6393 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006394 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6395 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6396 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006397};
6398
Thomas Pedersen84f10702012-07-12 16:17:33 -07006399static int nl80211_set_cqm_txe(struct genl_info *info,
6400 u32 rate, u32 pkts, u32 intvl)
6401{
6402 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6403 struct wireless_dev *wdev;
6404 struct net_device *dev = info->user_ptr[1];
6405
6406 if ((rate < 0 || rate > 100) ||
6407 (intvl < 0 || intvl > NL80211_CQM_TXE_MAX_INTVL))
6408 return -EINVAL;
6409
6410 wdev = dev->ieee80211_ptr;
6411
6412 if (!rdev->ops->set_cqm_txe_config)
6413 return -EOPNOTSUPP;
6414
6415 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6416 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6417 return -EOPNOTSUPP;
6418
Hila Gonene35e4d22012-06-27 17:19:42 +03006419 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006420}
6421
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006422static int nl80211_set_cqm_rssi(struct genl_info *info,
6423 s32 threshold, u32 hysteresis)
6424{
Johannes Berg4c476992010-10-04 21:36:35 +02006425 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006426 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006427 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006428
6429 if (threshold > 0)
6430 return -EINVAL;
6431
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006432 wdev = dev->ieee80211_ptr;
6433
Johannes Berg4c476992010-10-04 21:36:35 +02006434 if (!rdev->ops->set_cqm_rssi_config)
6435 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006436
Johannes Berg074ac8d2010-09-16 14:58:22 +02006437 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006438 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6439 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006440
Hila Gonene35e4d22012-06-27 17:19:42 +03006441 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006442}
6443
6444static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6445{
6446 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6447 struct nlattr *cqm;
6448 int err;
6449
6450 cqm = info->attrs[NL80211_ATTR_CQM];
6451 if (!cqm) {
6452 err = -EINVAL;
6453 goto out;
6454 }
6455
6456 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6457 nl80211_attr_cqm_policy);
6458 if (err)
6459 goto out;
6460
6461 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6462 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6463 s32 threshold;
6464 u32 hysteresis;
6465 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6466 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6467 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006468 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6469 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6470 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6471 u32 rate, pkts, intvl;
6472 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6473 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6474 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6475 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006476 } else
6477 err = -EINVAL;
6478
6479out:
6480 return err;
6481}
6482
Johannes Berg29cbe682010-12-03 09:20:44 +01006483static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6484{
6485 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6486 struct net_device *dev = info->user_ptr[1];
6487 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006488 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006489 int err;
6490
6491 /* start with default */
6492 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006493 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006494
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006495 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006496 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006497 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006498 if (err)
6499 return err;
6500 }
6501
6502 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6503 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6504 return -EINVAL;
6505
Javier Cardonac80d5452010-12-16 17:37:49 -08006506 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6507 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6508
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006509 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6510 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6511 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6512 return -EINVAL;
6513
Javier Cardonac80d5452010-12-16 17:37:49 -08006514 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6515 /* parse additional setup parameters if given */
6516 err = nl80211_parse_mesh_setup(info, &setup);
6517 if (err)
6518 return err;
6519 }
6520
Johannes Bergcc1d2802012-05-16 23:50:20 +02006521 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6522 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6523
6524 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6525 !nl80211_valid_channel_type(info, &channel_type))
6526 return -EINVAL;
6527
6528 setup.channel = rdev_freq_to_chan(rdev,
6529 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6530 channel_type);
6531 if (!setup.channel)
6532 return -EINVAL;
6533 setup.channel_type = channel_type;
6534 } else {
6535 /* cfg80211_join_mesh() will sort it out */
6536 setup.channel = NULL;
6537 }
6538
Javier Cardonac80d5452010-12-16 17:37:49 -08006539 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006540}
6541
6542static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6543{
6544 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6545 struct net_device *dev = info->user_ptr[1];
6546
6547 return cfg80211_leave_mesh(rdev, dev);
6548}
6549
Johannes Bergdfb89c52012-06-27 09:23:48 +02006550#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006551static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6552{
6553 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6554 struct sk_buff *msg;
6555 void *hdr;
6556
6557 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6558 return -EOPNOTSUPP;
6559
6560 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6561 if (!msg)
6562 return -ENOMEM;
6563
Eric W. Biederman15e47302012-09-07 20:12:54 +00006564 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006565 NL80211_CMD_GET_WOWLAN);
6566 if (!hdr)
6567 goto nla_put_failure;
6568
6569 if (rdev->wowlan) {
6570 struct nlattr *nl_wowlan;
6571
6572 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6573 if (!nl_wowlan)
6574 goto nla_put_failure;
6575
David S. Miller9360ffd2012-03-29 04:41:26 -04006576 if ((rdev->wowlan->any &&
6577 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6578 (rdev->wowlan->disconnect &&
6579 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6580 (rdev->wowlan->magic_pkt &&
6581 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6582 (rdev->wowlan->gtk_rekey_failure &&
6583 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6584 (rdev->wowlan->eap_identity_req &&
6585 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6586 (rdev->wowlan->four_way_handshake &&
6587 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6588 (rdev->wowlan->rfkill_release &&
6589 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6590 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006591 if (rdev->wowlan->n_patterns) {
6592 struct nlattr *nl_pats, *nl_pat;
6593 int i, pat_len;
6594
6595 nl_pats = nla_nest_start(msg,
6596 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6597 if (!nl_pats)
6598 goto nla_put_failure;
6599
6600 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6601 nl_pat = nla_nest_start(msg, i + 1);
6602 if (!nl_pat)
6603 goto nla_put_failure;
6604 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006605 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6606 DIV_ROUND_UP(pat_len, 8),
6607 rdev->wowlan->patterns[i].mask) ||
6608 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6609 pat_len,
6610 rdev->wowlan->patterns[i].pattern))
6611 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006612 nla_nest_end(msg, nl_pat);
6613 }
6614 nla_nest_end(msg, nl_pats);
6615 }
6616
6617 nla_nest_end(msg, nl_wowlan);
6618 }
6619
6620 genlmsg_end(msg, hdr);
6621 return genlmsg_reply(msg, info);
6622
6623nla_put_failure:
6624 nlmsg_free(msg);
6625 return -ENOBUFS;
6626}
6627
6628static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6629{
6630 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6631 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006632 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006633 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006634 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6635 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006636 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006637
6638 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6639 return -EOPNOTSUPP;
6640
Johannes Bergae33bd82012-07-12 16:25:02 +02006641 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6642 cfg80211_rdev_free_wowlan(rdev);
6643 rdev->wowlan = NULL;
6644 goto set_wakeup;
6645 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006646
6647 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6648 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6649 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6650 nl80211_wowlan_policy);
6651 if (err)
6652 return err;
6653
6654 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6655 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6656 return -EINVAL;
6657 new_triggers.any = true;
6658 }
6659
6660 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6661 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6662 return -EINVAL;
6663 new_triggers.disconnect = true;
6664 }
6665
6666 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6667 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6668 return -EINVAL;
6669 new_triggers.magic_pkt = true;
6670 }
6671
Johannes Berg77dbbb12011-07-13 10:48:55 +02006672 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6673 return -EINVAL;
6674
6675 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6676 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6677 return -EINVAL;
6678 new_triggers.gtk_rekey_failure = true;
6679 }
6680
6681 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6682 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6683 return -EINVAL;
6684 new_triggers.eap_identity_req = true;
6685 }
6686
6687 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6688 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6689 return -EINVAL;
6690 new_triggers.four_way_handshake = true;
6691 }
6692
6693 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6694 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6695 return -EINVAL;
6696 new_triggers.rfkill_release = true;
6697 }
6698
Johannes Bergff1b6e62011-05-04 15:37:28 +02006699 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6700 struct nlattr *pat;
6701 int n_patterns = 0;
6702 int rem, pat_len, mask_len;
6703 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6704
6705 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6706 rem)
6707 n_patterns++;
6708 if (n_patterns > wowlan->n_patterns)
6709 return -EINVAL;
6710
6711 new_triggers.patterns = kcalloc(n_patterns,
6712 sizeof(new_triggers.patterns[0]),
6713 GFP_KERNEL);
6714 if (!new_triggers.patterns)
6715 return -ENOMEM;
6716
6717 new_triggers.n_patterns = n_patterns;
6718 i = 0;
6719
6720 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6721 rem) {
6722 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6723 nla_data(pat), nla_len(pat), NULL);
6724 err = -EINVAL;
6725 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6726 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6727 goto error;
6728 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6729 mask_len = DIV_ROUND_UP(pat_len, 8);
6730 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6731 mask_len)
6732 goto error;
6733 if (pat_len > wowlan->pattern_max_len ||
6734 pat_len < wowlan->pattern_min_len)
6735 goto error;
6736
6737 new_triggers.patterns[i].mask =
6738 kmalloc(mask_len + pat_len, GFP_KERNEL);
6739 if (!new_triggers.patterns[i].mask) {
6740 err = -ENOMEM;
6741 goto error;
6742 }
6743 new_triggers.patterns[i].pattern =
6744 new_triggers.patterns[i].mask + mask_len;
6745 memcpy(new_triggers.patterns[i].mask,
6746 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6747 mask_len);
6748 new_triggers.patterns[i].pattern_len = pat_len;
6749 memcpy(new_triggers.patterns[i].pattern,
6750 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6751 pat_len);
6752 i++;
6753 }
6754 }
6755
Johannes Bergae33bd82012-07-12 16:25:02 +02006756 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6757 if (!ntrig) {
6758 err = -ENOMEM;
6759 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006760 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006761 cfg80211_rdev_free_wowlan(rdev);
6762 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006763
Johannes Bergae33bd82012-07-12 16:25:02 +02006764 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006765 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
Hila Gonene35e4d22012-06-27 17:19:42 +03006766 rdev_set_wakeup(rdev, rdev->wowlan);
Johannes Berg6d525632012-04-04 15:05:25 +02006767
Johannes Bergff1b6e62011-05-04 15:37:28 +02006768 return 0;
6769 error:
6770 for (i = 0; i < new_triggers.n_patterns; i++)
6771 kfree(new_triggers.patterns[i].mask);
6772 kfree(new_triggers.patterns);
6773 return err;
6774}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006775#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006776
Johannes Berge5497d72011-07-05 16:35:40 +02006777static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6778{
6779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6780 struct net_device *dev = info->user_ptr[1];
6781 struct wireless_dev *wdev = dev->ieee80211_ptr;
6782 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6783 struct cfg80211_gtk_rekey_data rekey_data;
6784 int err;
6785
6786 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6787 return -EINVAL;
6788
6789 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6790 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6791 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6792 nl80211_rekey_policy);
6793 if (err)
6794 return err;
6795
6796 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6797 return -ERANGE;
6798 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6799 return -ERANGE;
6800 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6801 return -ERANGE;
6802
6803 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6804 NL80211_KEK_LEN);
6805 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6806 NL80211_KCK_LEN);
6807 memcpy(rekey_data.replay_ctr,
6808 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6809 NL80211_REPLAY_CTR_LEN);
6810
6811 wdev_lock(wdev);
6812 if (!wdev->current_bss) {
6813 err = -ENOTCONN;
6814 goto out;
6815 }
6816
6817 if (!rdev->ops->set_rekey_data) {
6818 err = -EOPNOTSUPP;
6819 goto out;
6820 }
6821
Hila Gonene35e4d22012-06-27 17:19:42 +03006822 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02006823 out:
6824 wdev_unlock(wdev);
6825 return err;
6826}
6827
Johannes Berg28946da2011-11-04 11:18:12 +01006828static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6829 struct genl_info *info)
6830{
6831 struct net_device *dev = info->user_ptr[1];
6832 struct wireless_dev *wdev = dev->ieee80211_ptr;
6833
6834 if (wdev->iftype != NL80211_IFTYPE_AP &&
6835 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6836 return -EINVAL;
6837
Eric W. Biederman15e47302012-09-07 20:12:54 +00006838 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01006839 return -EBUSY;
6840
Eric W. Biederman15e47302012-09-07 20:12:54 +00006841 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01006842 return 0;
6843}
6844
Johannes Berg7f6cf312011-11-04 11:18:15 +01006845static int nl80211_probe_client(struct sk_buff *skb,
6846 struct genl_info *info)
6847{
6848 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6849 struct net_device *dev = info->user_ptr[1];
6850 struct wireless_dev *wdev = dev->ieee80211_ptr;
6851 struct sk_buff *msg;
6852 void *hdr;
6853 const u8 *addr;
6854 u64 cookie;
6855 int err;
6856
6857 if (wdev->iftype != NL80211_IFTYPE_AP &&
6858 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6859 return -EOPNOTSUPP;
6860
6861 if (!info->attrs[NL80211_ATTR_MAC])
6862 return -EINVAL;
6863
6864 if (!rdev->ops->probe_client)
6865 return -EOPNOTSUPP;
6866
6867 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6868 if (!msg)
6869 return -ENOMEM;
6870
Eric W. Biederman15e47302012-09-07 20:12:54 +00006871 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01006872 NL80211_CMD_PROBE_CLIENT);
6873
6874 if (IS_ERR(hdr)) {
6875 err = PTR_ERR(hdr);
6876 goto free_msg;
6877 }
6878
6879 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6880
Hila Gonene35e4d22012-06-27 17:19:42 +03006881 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01006882 if (err)
6883 goto free_msg;
6884
David S. Miller9360ffd2012-03-29 04:41:26 -04006885 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6886 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006887
6888 genlmsg_end(msg, hdr);
6889
6890 return genlmsg_reply(msg, info);
6891
6892 nla_put_failure:
6893 err = -ENOBUFS;
6894 free_msg:
6895 nlmsg_free(msg);
6896 return err;
6897}
6898
Johannes Berg5e760232011-11-04 11:18:17 +01006899static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6900{
6901 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6902
6903 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6904 return -EOPNOTSUPP;
6905
Eric W. Biederman15e47302012-09-07 20:12:54 +00006906 if (rdev->ap_beacons_nlportid)
Johannes Berg5e760232011-11-04 11:18:17 +01006907 return -EBUSY;
6908
Eric W. Biederman15e47302012-09-07 20:12:54 +00006909 rdev->ap_beacons_nlportid = info->snd_portid;
Johannes Berg5e760232011-11-04 11:18:17 +01006910
6911 return 0;
6912}
6913
Johannes Berg98104fde2012-06-16 00:19:54 +02006914static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
6915{
6916 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6917 struct wireless_dev *wdev = info->user_ptr[1];
6918 int err;
6919
6920 if (!rdev->ops->start_p2p_device)
6921 return -EOPNOTSUPP;
6922
6923 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
6924 return -EOPNOTSUPP;
6925
6926 if (wdev->p2p_started)
6927 return 0;
6928
6929 mutex_lock(&rdev->devlist_mtx);
6930 err = cfg80211_can_add_interface(rdev, wdev->iftype);
6931 mutex_unlock(&rdev->devlist_mtx);
6932 if (err)
6933 return err;
6934
Johannes Bergeeb126e2012-10-23 15:16:50 +02006935 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02006936 if (err)
6937 return err;
6938
6939 wdev->p2p_started = true;
6940 mutex_lock(&rdev->devlist_mtx);
6941 rdev->opencount++;
6942 mutex_unlock(&rdev->devlist_mtx);
6943
6944 return 0;
6945}
6946
6947static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
6948{
6949 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6950 struct wireless_dev *wdev = info->user_ptr[1];
6951
6952 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
6953 return -EOPNOTSUPP;
6954
6955 if (!rdev->ops->stop_p2p_device)
6956 return -EOPNOTSUPP;
6957
6958 if (!wdev->p2p_started)
6959 return 0;
6960
Johannes Bergeeb126e2012-10-23 15:16:50 +02006961 rdev_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02006962 wdev->p2p_started = false;
6963
6964 mutex_lock(&rdev->devlist_mtx);
6965 rdev->opencount--;
6966 mutex_unlock(&rdev->devlist_mtx);
6967
6968 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
6969 rdev->scan_req->aborted = true;
6970 ___cfg80211_scan_done(rdev, true);
6971 }
6972
6973 return 0;
6974}
6975
Johannes Berg4c476992010-10-04 21:36:35 +02006976#define NL80211_FLAG_NEED_WIPHY 0x01
6977#define NL80211_FLAG_NEED_NETDEV 0x02
6978#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006979#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6980#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6981 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02006982#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02006983/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02006984#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
6985 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006986
6987static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6988 struct genl_info *info)
6989{
6990 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02006991 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006992 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02006993 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6994
6995 if (rtnl)
6996 rtnl_lock();
6997
6998 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02006999 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007000 if (IS_ERR(rdev)) {
7001 if (rtnl)
7002 rtnl_unlock();
7003 return PTR_ERR(rdev);
7004 }
7005 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007006 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7007 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007008 mutex_lock(&cfg80211_mutex);
7009 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7010 info->attrs);
7011 if (IS_ERR(wdev)) {
7012 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007013 if (rtnl)
7014 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007015 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007016 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007017
Johannes Berg89a54e42012-06-15 14:33:17 +02007018 dev = wdev->netdev;
7019 rdev = wiphy_to_dev(wdev->wiphy);
7020
Johannes Berg1bf614e2012-06-15 15:23:36 +02007021 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7022 if (!dev) {
7023 mutex_unlock(&cfg80211_mutex);
7024 if (rtnl)
7025 rtnl_unlock();
7026 return -EINVAL;
7027 }
7028
7029 info->user_ptr[1] = dev;
7030 } else {
7031 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007032 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007033
Johannes Berg1bf614e2012-06-15 15:23:36 +02007034 if (dev) {
7035 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7036 !netif_running(dev)) {
7037 mutex_unlock(&cfg80211_mutex);
7038 if (rtnl)
7039 rtnl_unlock();
7040 return -ENETDOWN;
7041 }
7042
7043 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007044 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7045 if (!wdev->p2p_started) {
7046 mutex_unlock(&cfg80211_mutex);
7047 if (rtnl)
7048 rtnl_unlock();
7049 return -ENETDOWN;
7050 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007051 }
7052
Johannes Berg89a54e42012-06-15 14:33:17 +02007053 cfg80211_lock_rdev(rdev);
7054
7055 mutex_unlock(&cfg80211_mutex);
7056
Johannes Berg4c476992010-10-04 21:36:35 +02007057 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007058 }
7059
7060 return 0;
7061}
7062
7063static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7064 struct genl_info *info)
7065{
7066 if (info->user_ptr[0])
7067 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007068 if (info->user_ptr[1]) {
7069 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7070 struct wireless_dev *wdev = info->user_ptr[1];
7071
7072 if (wdev->netdev)
7073 dev_put(wdev->netdev);
7074 } else {
7075 dev_put(info->user_ptr[1]);
7076 }
7077 }
Johannes Berg4c476992010-10-04 21:36:35 +02007078 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7079 rtnl_unlock();
7080}
7081
Johannes Berg55682962007-09-20 13:09:35 -04007082static struct genl_ops nl80211_ops[] = {
7083 {
7084 .cmd = NL80211_CMD_GET_WIPHY,
7085 .doit = nl80211_get_wiphy,
7086 .dumpit = nl80211_dump_wiphy,
7087 .policy = nl80211_policy,
7088 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007089 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007090 },
7091 {
7092 .cmd = NL80211_CMD_SET_WIPHY,
7093 .doit = nl80211_set_wiphy,
7094 .policy = nl80211_policy,
7095 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007096 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007097 },
7098 {
7099 .cmd = NL80211_CMD_GET_INTERFACE,
7100 .doit = nl80211_get_interface,
7101 .dumpit = nl80211_dump_interface,
7102 .policy = nl80211_policy,
7103 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007104 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007105 },
7106 {
7107 .cmd = NL80211_CMD_SET_INTERFACE,
7108 .doit = nl80211_set_interface,
7109 .policy = nl80211_policy,
7110 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007111 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7112 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007113 },
7114 {
7115 .cmd = NL80211_CMD_NEW_INTERFACE,
7116 .doit = nl80211_new_interface,
7117 .policy = nl80211_policy,
7118 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007119 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7120 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007121 },
7122 {
7123 .cmd = NL80211_CMD_DEL_INTERFACE,
7124 .doit = nl80211_del_interface,
7125 .policy = nl80211_policy,
7126 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007127 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007128 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007129 },
Johannes Berg41ade002007-12-19 02:03:29 +01007130 {
7131 .cmd = NL80211_CMD_GET_KEY,
7132 .doit = nl80211_get_key,
7133 .policy = nl80211_policy,
7134 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007135 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007136 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007137 },
7138 {
7139 .cmd = NL80211_CMD_SET_KEY,
7140 .doit = nl80211_set_key,
7141 .policy = nl80211_policy,
7142 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007143 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007144 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007145 },
7146 {
7147 .cmd = NL80211_CMD_NEW_KEY,
7148 .doit = nl80211_new_key,
7149 .policy = nl80211_policy,
7150 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007151 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007152 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007153 },
7154 {
7155 .cmd = NL80211_CMD_DEL_KEY,
7156 .doit = nl80211_del_key,
7157 .policy = nl80211_policy,
7158 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007159 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007160 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007161 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007162 {
7163 .cmd = NL80211_CMD_SET_BEACON,
7164 .policy = nl80211_policy,
7165 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007166 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007167 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007168 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007169 },
7170 {
Johannes Berg88600202012-02-13 15:17:18 +01007171 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007172 .policy = nl80211_policy,
7173 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007174 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007175 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007176 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007177 },
7178 {
Johannes Berg88600202012-02-13 15:17:18 +01007179 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007180 .policy = nl80211_policy,
7181 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007182 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007183 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007184 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007185 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007186 {
7187 .cmd = NL80211_CMD_GET_STATION,
7188 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007189 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007190 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007191 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7192 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007193 },
7194 {
7195 .cmd = NL80211_CMD_SET_STATION,
7196 .doit = nl80211_set_station,
7197 .policy = nl80211_policy,
7198 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007199 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007200 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007201 },
7202 {
7203 .cmd = NL80211_CMD_NEW_STATION,
7204 .doit = nl80211_new_station,
7205 .policy = nl80211_policy,
7206 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007207 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007208 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007209 },
7210 {
7211 .cmd = NL80211_CMD_DEL_STATION,
7212 .doit = nl80211_del_station,
7213 .policy = nl80211_policy,
7214 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007215 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007216 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007217 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007218 {
7219 .cmd = NL80211_CMD_GET_MPATH,
7220 .doit = nl80211_get_mpath,
7221 .dumpit = nl80211_dump_mpath,
7222 .policy = nl80211_policy,
7223 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007224 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007225 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007226 },
7227 {
7228 .cmd = NL80211_CMD_SET_MPATH,
7229 .doit = nl80211_set_mpath,
7230 .policy = nl80211_policy,
7231 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007232 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007233 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007234 },
7235 {
7236 .cmd = NL80211_CMD_NEW_MPATH,
7237 .doit = nl80211_new_mpath,
7238 .policy = nl80211_policy,
7239 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007240 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007241 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007242 },
7243 {
7244 .cmd = NL80211_CMD_DEL_MPATH,
7245 .doit = nl80211_del_mpath,
7246 .policy = nl80211_policy,
7247 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007248 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007249 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007250 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007251 {
7252 .cmd = NL80211_CMD_SET_BSS,
7253 .doit = nl80211_set_bss,
7254 .policy = nl80211_policy,
7255 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007256 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007257 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007258 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007259 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007260 .cmd = NL80211_CMD_GET_REG,
7261 .doit = nl80211_get_reg,
7262 .policy = nl80211_policy,
7263 /* can be retrieved by unprivileged users */
7264 },
7265 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007266 .cmd = NL80211_CMD_SET_REG,
7267 .doit = nl80211_set_reg,
7268 .policy = nl80211_policy,
7269 .flags = GENL_ADMIN_PERM,
7270 },
7271 {
7272 .cmd = NL80211_CMD_REQ_SET_REG,
7273 .doit = nl80211_req_set_reg,
7274 .policy = nl80211_policy,
7275 .flags = GENL_ADMIN_PERM,
7276 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007277 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007278 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7279 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007280 .policy = nl80211_policy,
7281 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007282 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007283 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007284 },
7285 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007286 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7287 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007288 .policy = nl80211_policy,
7289 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007290 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007291 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007292 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007293 {
Johannes Berg2a519312009-02-10 21:25:55 +01007294 .cmd = NL80211_CMD_TRIGGER_SCAN,
7295 .doit = nl80211_trigger_scan,
7296 .policy = nl80211_policy,
7297 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007298 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007299 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007300 },
7301 {
7302 .cmd = NL80211_CMD_GET_SCAN,
7303 .policy = nl80211_policy,
7304 .dumpit = nl80211_dump_scan,
7305 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007306 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007307 .cmd = NL80211_CMD_START_SCHED_SCAN,
7308 .doit = nl80211_start_sched_scan,
7309 .policy = nl80211_policy,
7310 .flags = GENL_ADMIN_PERM,
7311 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7312 NL80211_FLAG_NEED_RTNL,
7313 },
7314 {
7315 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7316 .doit = nl80211_stop_sched_scan,
7317 .policy = nl80211_policy,
7318 .flags = GENL_ADMIN_PERM,
7319 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7320 NL80211_FLAG_NEED_RTNL,
7321 },
7322 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007323 .cmd = NL80211_CMD_AUTHENTICATE,
7324 .doit = nl80211_authenticate,
7325 .policy = nl80211_policy,
7326 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007327 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007328 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007329 },
7330 {
7331 .cmd = NL80211_CMD_ASSOCIATE,
7332 .doit = nl80211_associate,
7333 .policy = nl80211_policy,
7334 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007335 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007336 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007337 },
7338 {
7339 .cmd = NL80211_CMD_DEAUTHENTICATE,
7340 .doit = nl80211_deauthenticate,
7341 .policy = nl80211_policy,
7342 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007343 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007344 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007345 },
7346 {
7347 .cmd = NL80211_CMD_DISASSOCIATE,
7348 .doit = nl80211_disassociate,
7349 .policy = nl80211_policy,
7350 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007351 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007352 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007353 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007354 {
7355 .cmd = NL80211_CMD_JOIN_IBSS,
7356 .doit = nl80211_join_ibss,
7357 .policy = nl80211_policy,
7358 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007359 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007360 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007361 },
7362 {
7363 .cmd = NL80211_CMD_LEAVE_IBSS,
7364 .doit = nl80211_leave_ibss,
7365 .policy = nl80211_policy,
7366 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007367 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007368 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007369 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007370#ifdef CONFIG_NL80211_TESTMODE
7371 {
7372 .cmd = NL80211_CMD_TESTMODE,
7373 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007374 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007375 .policy = nl80211_policy,
7376 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007377 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7378 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007379 },
7380#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007381 {
7382 .cmd = NL80211_CMD_CONNECT,
7383 .doit = nl80211_connect,
7384 .policy = nl80211_policy,
7385 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007386 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007387 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007388 },
7389 {
7390 .cmd = NL80211_CMD_DISCONNECT,
7391 .doit = nl80211_disconnect,
7392 .policy = nl80211_policy,
7393 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007394 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007395 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007396 },
Johannes Berg463d0182009-07-14 00:33:35 +02007397 {
7398 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7399 .doit = nl80211_wiphy_netns,
7400 .policy = nl80211_policy,
7401 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007402 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7403 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007404 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007405 {
7406 .cmd = NL80211_CMD_GET_SURVEY,
7407 .policy = nl80211_policy,
7408 .dumpit = nl80211_dump_survey,
7409 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007410 {
7411 .cmd = NL80211_CMD_SET_PMKSA,
7412 .doit = nl80211_setdel_pmksa,
7413 .policy = nl80211_policy,
7414 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007415 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007416 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007417 },
7418 {
7419 .cmd = NL80211_CMD_DEL_PMKSA,
7420 .doit = nl80211_setdel_pmksa,
7421 .policy = nl80211_policy,
7422 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007423 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007424 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007425 },
7426 {
7427 .cmd = NL80211_CMD_FLUSH_PMKSA,
7428 .doit = nl80211_flush_pmksa,
7429 .policy = nl80211_policy,
7430 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007431 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007432 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007433 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007434 {
7435 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7436 .doit = nl80211_remain_on_channel,
7437 .policy = nl80211_policy,
7438 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007439 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007440 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007441 },
7442 {
7443 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7444 .doit = nl80211_cancel_remain_on_channel,
7445 .policy = nl80211_policy,
7446 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007447 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007448 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007449 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007450 {
7451 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7452 .doit = nl80211_set_tx_bitrate_mask,
7453 .policy = nl80211_policy,
7454 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007455 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7456 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007457 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007458 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007459 .cmd = NL80211_CMD_REGISTER_FRAME,
7460 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007461 .policy = nl80211_policy,
7462 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007463 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007464 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007465 },
7466 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007467 .cmd = NL80211_CMD_FRAME,
7468 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007469 .policy = nl80211_policy,
7470 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007471 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007472 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007473 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007474 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007475 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7476 .doit = nl80211_tx_mgmt_cancel_wait,
7477 .policy = nl80211_policy,
7478 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007479 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007480 NL80211_FLAG_NEED_RTNL,
7481 },
7482 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007483 .cmd = NL80211_CMD_SET_POWER_SAVE,
7484 .doit = nl80211_set_power_save,
7485 .policy = nl80211_policy,
7486 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007487 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7488 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007489 },
7490 {
7491 .cmd = NL80211_CMD_GET_POWER_SAVE,
7492 .doit = nl80211_get_power_save,
7493 .policy = nl80211_policy,
7494 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007495 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7496 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007497 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007498 {
7499 .cmd = NL80211_CMD_SET_CQM,
7500 .doit = nl80211_set_cqm,
7501 .policy = nl80211_policy,
7502 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007503 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7504 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007505 },
Johannes Bergf444de02010-05-05 15:25:02 +02007506 {
7507 .cmd = NL80211_CMD_SET_CHANNEL,
7508 .doit = nl80211_set_channel,
7509 .policy = nl80211_policy,
7510 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007511 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7512 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007513 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007514 {
7515 .cmd = NL80211_CMD_SET_WDS_PEER,
7516 .doit = nl80211_set_wds_peer,
7517 .policy = nl80211_policy,
7518 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007519 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7520 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007521 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007522 {
7523 .cmd = NL80211_CMD_JOIN_MESH,
7524 .doit = nl80211_join_mesh,
7525 .policy = nl80211_policy,
7526 .flags = GENL_ADMIN_PERM,
7527 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7528 NL80211_FLAG_NEED_RTNL,
7529 },
7530 {
7531 .cmd = NL80211_CMD_LEAVE_MESH,
7532 .doit = nl80211_leave_mesh,
7533 .policy = nl80211_policy,
7534 .flags = GENL_ADMIN_PERM,
7535 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7536 NL80211_FLAG_NEED_RTNL,
7537 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007538#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007539 {
7540 .cmd = NL80211_CMD_GET_WOWLAN,
7541 .doit = nl80211_get_wowlan,
7542 .policy = nl80211_policy,
7543 /* can be retrieved by unprivileged users */
7544 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7545 NL80211_FLAG_NEED_RTNL,
7546 },
7547 {
7548 .cmd = NL80211_CMD_SET_WOWLAN,
7549 .doit = nl80211_set_wowlan,
7550 .policy = nl80211_policy,
7551 .flags = GENL_ADMIN_PERM,
7552 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7553 NL80211_FLAG_NEED_RTNL,
7554 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007555#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007556 {
7557 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7558 .doit = nl80211_set_rekey_data,
7559 .policy = nl80211_policy,
7560 .flags = GENL_ADMIN_PERM,
7561 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7562 NL80211_FLAG_NEED_RTNL,
7563 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007564 {
7565 .cmd = NL80211_CMD_TDLS_MGMT,
7566 .doit = nl80211_tdls_mgmt,
7567 .policy = nl80211_policy,
7568 .flags = GENL_ADMIN_PERM,
7569 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7570 NL80211_FLAG_NEED_RTNL,
7571 },
7572 {
7573 .cmd = NL80211_CMD_TDLS_OPER,
7574 .doit = nl80211_tdls_oper,
7575 .policy = nl80211_policy,
7576 .flags = GENL_ADMIN_PERM,
7577 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7578 NL80211_FLAG_NEED_RTNL,
7579 },
Johannes Berg28946da2011-11-04 11:18:12 +01007580 {
7581 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7582 .doit = nl80211_register_unexpected_frame,
7583 .policy = nl80211_policy,
7584 .flags = GENL_ADMIN_PERM,
7585 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7586 NL80211_FLAG_NEED_RTNL,
7587 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007588 {
7589 .cmd = NL80211_CMD_PROBE_CLIENT,
7590 .doit = nl80211_probe_client,
7591 .policy = nl80211_policy,
7592 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007593 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007594 NL80211_FLAG_NEED_RTNL,
7595 },
Johannes Berg5e760232011-11-04 11:18:17 +01007596 {
7597 .cmd = NL80211_CMD_REGISTER_BEACONS,
7598 .doit = nl80211_register_beacons,
7599 .policy = nl80211_policy,
7600 .flags = GENL_ADMIN_PERM,
7601 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7602 NL80211_FLAG_NEED_RTNL,
7603 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007604 {
7605 .cmd = NL80211_CMD_SET_NOACK_MAP,
7606 .doit = nl80211_set_noack_map,
7607 .policy = nl80211_policy,
7608 .flags = GENL_ADMIN_PERM,
7609 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7610 NL80211_FLAG_NEED_RTNL,
7611 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007612 {
7613 .cmd = NL80211_CMD_START_P2P_DEVICE,
7614 .doit = nl80211_start_p2p_device,
7615 .policy = nl80211_policy,
7616 .flags = GENL_ADMIN_PERM,
7617 .internal_flags = NL80211_FLAG_NEED_WDEV |
7618 NL80211_FLAG_NEED_RTNL,
7619 },
7620 {
7621 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7622 .doit = nl80211_stop_p2p_device,
7623 .policy = nl80211_policy,
7624 .flags = GENL_ADMIN_PERM,
7625 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7626 NL80211_FLAG_NEED_RTNL,
7627 },
Johannes Berg55682962007-09-20 13:09:35 -04007628};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007629
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007630static struct genl_multicast_group nl80211_mlme_mcgrp = {
7631 .name = "mlme",
7632};
Johannes Berg55682962007-09-20 13:09:35 -04007633
7634/* multicast groups */
7635static struct genl_multicast_group nl80211_config_mcgrp = {
7636 .name = "config",
7637};
Johannes Berg2a519312009-02-10 21:25:55 +01007638static struct genl_multicast_group nl80211_scan_mcgrp = {
7639 .name = "scan",
7640};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007641static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7642 .name = "regulatory",
7643};
Johannes Berg55682962007-09-20 13:09:35 -04007644
7645/* notification functions */
7646
7647void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7648{
7649 struct sk_buff *msg;
7650
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007651 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007652 if (!msg)
7653 return;
7654
7655 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7656 nlmsg_free(msg);
7657 return;
7658 }
7659
Johannes Berg463d0182009-07-14 00:33:35 +02007660 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7661 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007662}
7663
Johannes Berg362a4152009-05-24 16:43:15 +02007664static int nl80211_add_scan_req(struct sk_buff *msg,
7665 struct cfg80211_registered_device *rdev)
7666{
7667 struct cfg80211_scan_request *req = rdev->scan_req;
7668 struct nlattr *nest;
7669 int i;
7670
Johannes Berg667503dd2009-07-07 03:56:11 +02007671 ASSERT_RDEV_LOCK(rdev);
7672
Johannes Berg362a4152009-05-24 16:43:15 +02007673 if (WARN_ON(!req))
7674 return 0;
7675
7676 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7677 if (!nest)
7678 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007679 for (i = 0; i < req->n_ssids; i++) {
7680 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7681 goto nla_put_failure;
7682 }
Johannes Berg362a4152009-05-24 16:43:15 +02007683 nla_nest_end(msg, nest);
7684
7685 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7686 if (!nest)
7687 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007688 for (i = 0; i < req->n_channels; i++) {
7689 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7690 goto nla_put_failure;
7691 }
Johannes Berg362a4152009-05-24 16:43:15 +02007692 nla_nest_end(msg, nest);
7693
David S. Miller9360ffd2012-03-29 04:41:26 -04007694 if (req->ie &&
7695 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7696 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007697
Sam Lefflered4737712012-10-11 21:03:31 -07007698 if (req->flags)
7699 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7700
Johannes Berg362a4152009-05-24 16:43:15 +02007701 return 0;
7702 nla_put_failure:
7703 return -ENOBUFS;
7704}
7705
Johannes Berga538e2d2009-06-16 19:56:42 +02007706static int nl80211_send_scan_msg(struct sk_buff *msg,
7707 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007708 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007709 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007710 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007711{
7712 void *hdr;
7713
Eric W. Biederman15e47302012-09-07 20:12:54 +00007714 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007715 if (!hdr)
7716 return -1;
7717
David S. Miller9360ffd2012-03-29 04:41:26 -04007718 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007719 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7720 wdev->netdev->ifindex)) ||
7721 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007722 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007723
Johannes Berg362a4152009-05-24 16:43:15 +02007724 /* ignore errors and send incomplete event anyway */
7725 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007726
7727 return genlmsg_end(msg, hdr);
7728
7729 nla_put_failure:
7730 genlmsg_cancel(msg, hdr);
7731 return -EMSGSIZE;
7732}
7733
Luciano Coelho807f8a82011-05-11 17:09:35 +03007734static int
7735nl80211_send_sched_scan_msg(struct sk_buff *msg,
7736 struct cfg80211_registered_device *rdev,
7737 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007738 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007739{
7740 void *hdr;
7741
Eric W. Biederman15e47302012-09-07 20:12:54 +00007742 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007743 if (!hdr)
7744 return -1;
7745
David S. Miller9360ffd2012-03-29 04:41:26 -04007746 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7747 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7748 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007749
7750 return genlmsg_end(msg, hdr);
7751
7752 nla_put_failure:
7753 genlmsg_cancel(msg, hdr);
7754 return -EMSGSIZE;
7755}
7756
Johannes Berga538e2d2009-06-16 19:56:42 +02007757void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007758 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007759{
7760 struct sk_buff *msg;
7761
Thomas Graf58050fc2012-06-28 03:57:45 +00007762 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007763 if (!msg)
7764 return;
7765
Johannes Bergfd014282012-06-18 19:17:03 +02007766 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007767 NL80211_CMD_TRIGGER_SCAN) < 0) {
7768 nlmsg_free(msg);
7769 return;
7770 }
7771
Johannes Berg463d0182009-07-14 00:33:35 +02007772 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7773 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007774}
7775
Johannes Berg2a519312009-02-10 21:25:55 +01007776void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007777 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007778{
7779 struct sk_buff *msg;
7780
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007781 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007782 if (!msg)
7783 return;
7784
Johannes Bergfd014282012-06-18 19:17:03 +02007785 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007786 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007787 nlmsg_free(msg);
7788 return;
7789 }
7790
Johannes Berg463d0182009-07-14 00:33:35 +02007791 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7792 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007793}
7794
7795void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007796 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007797{
7798 struct sk_buff *msg;
7799
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007800 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007801 if (!msg)
7802 return;
7803
Johannes Bergfd014282012-06-18 19:17:03 +02007804 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007805 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007806 nlmsg_free(msg);
7807 return;
7808 }
7809
Johannes Berg463d0182009-07-14 00:33:35 +02007810 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7811 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007812}
7813
Luciano Coelho807f8a82011-05-11 17:09:35 +03007814void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7815 struct net_device *netdev)
7816{
7817 struct sk_buff *msg;
7818
7819 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7820 if (!msg)
7821 return;
7822
7823 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7824 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7825 nlmsg_free(msg);
7826 return;
7827 }
7828
7829 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7830 nl80211_scan_mcgrp.id, GFP_KERNEL);
7831}
7832
7833void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7834 struct net_device *netdev, u32 cmd)
7835{
7836 struct sk_buff *msg;
7837
Thomas Graf58050fc2012-06-28 03:57:45 +00007838 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007839 if (!msg)
7840 return;
7841
7842 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7843 nlmsg_free(msg);
7844 return;
7845 }
7846
7847 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7848 nl80211_scan_mcgrp.id, GFP_KERNEL);
7849}
7850
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007851/*
7852 * This can happen on global regulatory changes or device specific settings
7853 * based on custom world regulatory domains.
7854 */
7855void nl80211_send_reg_change_event(struct regulatory_request *request)
7856{
7857 struct sk_buff *msg;
7858 void *hdr;
7859
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007860 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007861 if (!msg)
7862 return;
7863
7864 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7865 if (!hdr) {
7866 nlmsg_free(msg);
7867 return;
7868 }
7869
7870 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007871 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7872 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007873
David S. Miller9360ffd2012-03-29 04:41:26 -04007874 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7875 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7876 NL80211_REGDOM_TYPE_WORLD))
7877 goto nla_put_failure;
7878 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7879 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7880 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7881 goto nla_put_failure;
7882 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7883 request->intersect) {
7884 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7885 NL80211_REGDOM_TYPE_INTERSECTION))
7886 goto nla_put_failure;
7887 } else {
7888 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7889 NL80211_REGDOM_TYPE_COUNTRY) ||
7890 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7891 request->alpha2))
7892 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007893 }
7894
David S. Miller9360ffd2012-03-29 04:41:26 -04007895 if (wiphy_idx_valid(request->wiphy_idx) &&
7896 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7897 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007898
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007899 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007900
Johannes Bergbc43b282009-07-25 10:54:13 +02007901 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007902 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007903 GFP_ATOMIC);
7904 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007905
7906 return;
7907
7908nla_put_failure:
7909 genlmsg_cancel(msg, hdr);
7910 nlmsg_free(msg);
7911}
7912
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007913static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7914 struct net_device *netdev,
7915 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007916 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007917{
7918 struct sk_buff *msg;
7919 void *hdr;
7920
Johannes Berge6d6e342009-07-01 21:26:47 +02007921 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007922 if (!msg)
7923 return;
7924
7925 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7926 if (!hdr) {
7927 nlmsg_free(msg);
7928 return;
7929 }
7930
David S. Miller9360ffd2012-03-29 04:41:26 -04007931 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7932 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7933 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7934 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007935
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007936 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007937
Johannes Berg463d0182009-07-14 00:33:35 +02007938 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7939 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007940 return;
7941
7942 nla_put_failure:
7943 genlmsg_cancel(msg, hdr);
7944 nlmsg_free(msg);
7945}
7946
7947void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007948 struct net_device *netdev, const u8 *buf,
7949 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007950{
7951 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007952 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007953}
7954
7955void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7956 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007957 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007958{
Johannes Berge6d6e342009-07-01 21:26:47 +02007959 nl80211_send_mlme_event(rdev, netdev, buf, len,
7960 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007961}
7962
Jouni Malinen53b46b82009-03-27 20:53:56 +02007963void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007964 struct net_device *netdev, const u8 *buf,
7965 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007966{
7967 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007968 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007969}
7970
Jouni Malinen53b46b82009-03-27 20:53:56 +02007971void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7972 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007973 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007974{
7975 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007976 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007977}
7978
Jouni Malinencf4e5942010-12-16 00:52:40 +02007979void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7980 struct net_device *netdev, const u8 *buf,
7981 size_t len, gfp_t gfp)
7982{
7983 nl80211_send_mlme_event(rdev, netdev, buf, len,
7984 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7985}
7986
7987void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7988 struct net_device *netdev, const u8 *buf,
7989 size_t len, gfp_t gfp)
7990{
7991 nl80211_send_mlme_event(rdev, netdev, buf, len,
7992 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7993}
7994
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007995static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7996 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007997 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007998{
7999 struct sk_buff *msg;
8000 void *hdr;
8001
Johannes Berge6d6e342009-07-01 21:26:47 +02008002 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008003 if (!msg)
8004 return;
8005
8006 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8007 if (!hdr) {
8008 nlmsg_free(msg);
8009 return;
8010 }
8011
David S. Miller9360ffd2012-03-29 04:41:26 -04008012 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8013 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8014 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8015 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8016 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008017
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008018 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008019
Johannes Berg463d0182009-07-14 00:33:35 +02008020 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8021 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008022 return;
8023
8024 nla_put_failure:
8025 genlmsg_cancel(msg, hdr);
8026 nlmsg_free(msg);
8027}
8028
8029void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008030 struct net_device *netdev, const u8 *addr,
8031 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008032{
8033 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008034 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008035}
8036
8037void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008038 struct net_device *netdev, const u8 *addr,
8039 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008040{
Johannes Berge6d6e342009-07-01 21:26:47 +02008041 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8042 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008043}
8044
Samuel Ortizb23aa672009-07-01 21:26:54 +02008045void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8046 struct net_device *netdev, const u8 *bssid,
8047 const u8 *req_ie, size_t req_ie_len,
8048 const u8 *resp_ie, size_t resp_ie_len,
8049 u16 status, gfp_t gfp)
8050{
8051 struct sk_buff *msg;
8052 void *hdr;
8053
Thomas Graf58050fc2012-06-28 03:57:45 +00008054 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008055 if (!msg)
8056 return;
8057
8058 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8059 if (!hdr) {
8060 nlmsg_free(msg);
8061 return;
8062 }
8063
David S. Miller9360ffd2012-03-29 04:41:26 -04008064 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8065 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8066 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8067 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8068 (req_ie &&
8069 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8070 (resp_ie &&
8071 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8072 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008073
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008074 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008075
Johannes Berg463d0182009-07-14 00:33:35 +02008076 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8077 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008078 return;
8079
8080 nla_put_failure:
8081 genlmsg_cancel(msg, hdr);
8082 nlmsg_free(msg);
8083
8084}
8085
8086void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8087 struct net_device *netdev, const u8 *bssid,
8088 const u8 *req_ie, size_t req_ie_len,
8089 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8090{
8091 struct sk_buff *msg;
8092 void *hdr;
8093
Thomas Graf58050fc2012-06-28 03:57:45 +00008094 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008095 if (!msg)
8096 return;
8097
8098 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8099 if (!hdr) {
8100 nlmsg_free(msg);
8101 return;
8102 }
8103
David S. Miller9360ffd2012-03-29 04:41:26 -04008104 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8105 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8106 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8107 (req_ie &&
8108 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8109 (resp_ie &&
8110 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8111 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008112
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008113 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008114
Johannes Berg463d0182009-07-14 00:33:35 +02008115 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8116 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008117 return;
8118
8119 nla_put_failure:
8120 genlmsg_cancel(msg, hdr);
8121 nlmsg_free(msg);
8122
8123}
8124
8125void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8126 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02008127 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008128{
8129 struct sk_buff *msg;
8130 void *hdr;
8131
Thomas Graf58050fc2012-06-28 03:57:45 +00008132 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008133 if (!msg)
8134 return;
8135
8136 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8137 if (!hdr) {
8138 nlmsg_free(msg);
8139 return;
8140 }
8141
David S. Miller9360ffd2012-03-29 04:41:26 -04008142 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8143 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8144 (from_ap && reason &&
8145 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8146 (from_ap &&
8147 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8148 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8149 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008150
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008151 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008152
Johannes Berg463d0182009-07-14 00:33:35 +02008153 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8154 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008155 return;
8156
8157 nla_put_failure:
8158 genlmsg_cancel(msg, hdr);
8159 nlmsg_free(msg);
8160
8161}
8162
Johannes Berg04a773a2009-04-19 21:24:32 +02008163void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8164 struct net_device *netdev, const u8 *bssid,
8165 gfp_t gfp)
8166{
8167 struct sk_buff *msg;
8168 void *hdr;
8169
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008170 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008171 if (!msg)
8172 return;
8173
8174 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8175 if (!hdr) {
8176 nlmsg_free(msg);
8177 return;
8178 }
8179
David S. Miller9360ffd2012-03-29 04:41:26 -04008180 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8181 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8182 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8183 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008184
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008185 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008186
Johannes Berg463d0182009-07-14 00:33:35 +02008187 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8188 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008189 return;
8190
8191 nla_put_failure:
8192 genlmsg_cancel(msg, hdr);
8193 nlmsg_free(msg);
8194}
8195
Javier Cardonac93b5e72011-04-07 15:08:34 -07008196void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8197 struct net_device *netdev,
8198 const u8 *macaddr, const u8* ie, u8 ie_len,
8199 gfp_t gfp)
8200{
8201 struct sk_buff *msg;
8202 void *hdr;
8203
8204 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8205 if (!msg)
8206 return;
8207
8208 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8209 if (!hdr) {
8210 nlmsg_free(msg);
8211 return;
8212 }
8213
David S. Miller9360ffd2012-03-29 04:41:26 -04008214 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8215 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8216 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8217 (ie_len && ie &&
8218 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8219 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008220
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008221 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008222
8223 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8224 nl80211_mlme_mcgrp.id, gfp);
8225 return;
8226
8227 nla_put_failure:
8228 genlmsg_cancel(msg, hdr);
8229 nlmsg_free(msg);
8230}
8231
Jouni Malinena3b8b052009-03-27 21:59:49 +02008232void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8233 struct net_device *netdev, const u8 *addr,
8234 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008235 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008236{
8237 struct sk_buff *msg;
8238 void *hdr;
8239
Johannes Berge6d6e342009-07-01 21:26:47 +02008240 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008241 if (!msg)
8242 return;
8243
8244 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8245 if (!hdr) {
8246 nlmsg_free(msg);
8247 return;
8248 }
8249
David S. Miller9360ffd2012-03-29 04:41:26 -04008250 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8251 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8252 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8253 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8254 (key_id != -1 &&
8255 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8256 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8257 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008258
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008259 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008260
Johannes Berg463d0182009-07-14 00:33:35 +02008261 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8262 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008263 return;
8264
8265 nla_put_failure:
8266 genlmsg_cancel(msg, hdr);
8267 nlmsg_free(msg);
8268}
8269
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008270void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8271 struct ieee80211_channel *channel_before,
8272 struct ieee80211_channel *channel_after)
8273{
8274 struct sk_buff *msg;
8275 void *hdr;
8276 struct nlattr *nl_freq;
8277
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008278 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008279 if (!msg)
8280 return;
8281
8282 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8283 if (!hdr) {
8284 nlmsg_free(msg);
8285 return;
8286 }
8287
8288 /*
8289 * Since we are applying the beacon hint to a wiphy we know its
8290 * wiphy_idx is valid
8291 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008292 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8293 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008294
8295 /* Before */
8296 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8297 if (!nl_freq)
8298 goto nla_put_failure;
8299 if (nl80211_msg_put_channel(msg, channel_before))
8300 goto nla_put_failure;
8301 nla_nest_end(msg, nl_freq);
8302
8303 /* After */
8304 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8305 if (!nl_freq)
8306 goto nla_put_failure;
8307 if (nl80211_msg_put_channel(msg, channel_after))
8308 goto nla_put_failure;
8309 nla_nest_end(msg, nl_freq);
8310
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008311 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008312
Johannes Berg463d0182009-07-14 00:33:35 +02008313 rcu_read_lock();
8314 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8315 GFP_ATOMIC);
8316 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008317
8318 return;
8319
8320nla_put_failure:
8321 genlmsg_cancel(msg, hdr);
8322 nlmsg_free(msg);
8323}
8324
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008325static void nl80211_send_remain_on_chan_event(
8326 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008327 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008328 struct ieee80211_channel *chan,
8329 enum nl80211_channel_type channel_type,
8330 unsigned int duration, gfp_t gfp)
8331{
8332 struct sk_buff *msg;
8333 void *hdr;
8334
8335 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8336 if (!msg)
8337 return;
8338
8339 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8340 if (!hdr) {
8341 nlmsg_free(msg);
8342 return;
8343 }
8344
David S. Miller9360ffd2012-03-29 04:41:26 -04008345 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008346 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8347 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008348 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008349 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
8350 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
8351 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8352 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008353
David S. Miller9360ffd2012-03-29 04:41:26 -04008354 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8355 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8356 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008357
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008358 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008359
8360 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8361 nl80211_mlme_mcgrp.id, gfp);
8362 return;
8363
8364 nla_put_failure:
8365 genlmsg_cancel(msg, hdr);
8366 nlmsg_free(msg);
8367}
8368
8369void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008370 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008371 struct ieee80211_channel *chan,
8372 enum nl80211_channel_type channel_type,
8373 unsigned int duration, gfp_t gfp)
8374{
8375 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008376 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008377 channel_type, duration, gfp);
8378}
8379
8380void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008381 struct cfg80211_registered_device *rdev,
8382 struct wireless_dev *wdev,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008383 u64 cookie, struct ieee80211_channel *chan,
8384 enum nl80211_channel_type channel_type, gfp_t gfp)
8385{
8386 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008387 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008388 channel_type, 0, gfp);
8389}
8390
Johannes Berg98b62182009-12-23 13:15:44 +01008391void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8392 struct net_device *dev, const u8 *mac_addr,
8393 struct station_info *sinfo, gfp_t gfp)
8394{
8395 struct sk_buff *msg;
8396
Thomas Graf58050fc2012-06-28 03:57:45 +00008397 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008398 if (!msg)
8399 return;
8400
John W. Linville66266b32012-03-15 13:25:41 -04008401 if (nl80211_send_station(msg, 0, 0, 0,
8402 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008403 nlmsg_free(msg);
8404 return;
8405 }
8406
8407 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8408 nl80211_mlme_mcgrp.id, gfp);
8409}
8410
Jouni Malinenec15e682011-03-23 15:29:52 +02008411void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8412 struct net_device *dev, const u8 *mac_addr,
8413 gfp_t gfp)
8414{
8415 struct sk_buff *msg;
8416 void *hdr;
8417
Thomas Graf58050fc2012-06-28 03:57:45 +00008418 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008419 if (!msg)
8420 return;
8421
8422 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8423 if (!hdr) {
8424 nlmsg_free(msg);
8425 return;
8426 }
8427
David S. Miller9360ffd2012-03-29 04:41:26 -04008428 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8429 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8430 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008431
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008432 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008433
8434 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8435 nl80211_mlme_mcgrp.id, gfp);
8436 return;
8437
8438 nla_put_failure:
8439 genlmsg_cancel(msg, hdr);
8440 nlmsg_free(msg);
8441}
8442
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308443void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8444 struct net_device *dev, const u8 *mac_addr,
8445 enum nl80211_connect_failed_reason reason,
8446 gfp_t gfp)
8447{
8448 struct sk_buff *msg;
8449 void *hdr;
8450
8451 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8452 if (!msg)
8453 return;
8454
8455 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8456 if (!hdr) {
8457 nlmsg_free(msg);
8458 return;
8459 }
8460
8461 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8462 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8463 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8464 goto nla_put_failure;
8465
8466 genlmsg_end(msg, hdr);
8467
8468 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8469 nl80211_mlme_mcgrp.id, gfp);
8470 return;
8471
8472 nla_put_failure:
8473 genlmsg_cancel(msg, hdr);
8474 nlmsg_free(msg);
8475}
8476
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008477static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8478 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008479{
8480 struct wireless_dev *wdev = dev->ieee80211_ptr;
8481 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8482 struct sk_buff *msg;
8483 void *hdr;
8484 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008485 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008486
Eric W. Biederman15e47302012-09-07 20:12:54 +00008487 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008488 return false;
8489
8490 msg = nlmsg_new(100, gfp);
8491 if (!msg)
8492 return true;
8493
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008494 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008495 if (!hdr) {
8496 nlmsg_free(msg);
8497 return true;
8498 }
8499
David S. Miller9360ffd2012-03-29 04:41:26 -04008500 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8501 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8502 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8503 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008504
8505 err = genlmsg_end(msg, hdr);
8506 if (err < 0) {
8507 nlmsg_free(msg);
8508 return true;
8509 }
8510
Eric W. Biederman15e47302012-09-07 20:12:54 +00008511 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008512 return true;
8513
8514 nla_put_failure:
8515 genlmsg_cancel(msg, hdr);
8516 nlmsg_free(msg);
8517 return true;
8518}
8519
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008520bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8521{
8522 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8523 addr, gfp);
8524}
8525
8526bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8527 const u8 *addr, gfp_t gfp)
8528{
8529 return __nl80211_unexpected_frame(dev,
8530 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8531 addr, gfp);
8532}
8533
Johannes Berg2e161f72010-08-12 15:38:38 +02008534int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008535 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008536 int freq, int sig_dbm,
8537 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008538{
Johannes Berg71bbc992012-06-15 15:30:18 +02008539 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008540 struct sk_buff *msg;
8541 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008542
8543 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8544 if (!msg)
8545 return -ENOMEM;
8546
Johannes Berg2e161f72010-08-12 15:38:38 +02008547 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008548 if (!hdr) {
8549 nlmsg_free(msg);
8550 return -ENOMEM;
8551 }
8552
David S. Miller9360ffd2012-03-29 04:41:26 -04008553 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008554 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8555 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008556 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8557 (sig_dbm &&
8558 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8559 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8560 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008561
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008562 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008563
Eric W. Biederman15e47302012-09-07 20:12:54 +00008564 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008565
8566 nla_put_failure:
8567 genlmsg_cancel(msg, hdr);
8568 nlmsg_free(msg);
8569 return -ENOBUFS;
8570}
8571
Johannes Berg2e161f72010-08-12 15:38:38 +02008572void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008573 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008574 const u8 *buf, size_t len, bool ack,
8575 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008576{
Johannes Berg71bbc992012-06-15 15:30:18 +02008577 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008578 struct sk_buff *msg;
8579 void *hdr;
8580
8581 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8582 if (!msg)
8583 return;
8584
Johannes Berg2e161f72010-08-12 15:38:38 +02008585 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008586 if (!hdr) {
8587 nlmsg_free(msg);
8588 return;
8589 }
8590
David S. Miller9360ffd2012-03-29 04:41:26 -04008591 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008592 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8593 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008594 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8595 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8596 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8597 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008598
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008599 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008600
8601 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8602 return;
8603
8604 nla_put_failure:
8605 genlmsg_cancel(msg, hdr);
8606 nlmsg_free(msg);
8607}
8608
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008609void
8610nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8611 struct net_device *netdev,
8612 enum nl80211_cqm_rssi_threshold_event rssi_event,
8613 gfp_t gfp)
8614{
8615 struct sk_buff *msg;
8616 struct nlattr *pinfoattr;
8617 void *hdr;
8618
Thomas Graf58050fc2012-06-28 03:57:45 +00008619 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008620 if (!msg)
8621 return;
8622
8623 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8624 if (!hdr) {
8625 nlmsg_free(msg);
8626 return;
8627 }
8628
David S. Miller9360ffd2012-03-29 04:41:26 -04008629 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8630 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8631 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008632
8633 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8634 if (!pinfoattr)
8635 goto nla_put_failure;
8636
David S. Miller9360ffd2012-03-29 04:41:26 -04008637 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8638 rssi_event))
8639 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008640
8641 nla_nest_end(msg, pinfoattr);
8642
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008643 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008644
8645 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8646 nl80211_mlme_mcgrp.id, gfp);
8647 return;
8648
8649 nla_put_failure:
8650 genlmsg_cancel(msg, hdr);
8651 nlmsg_free(msg);
8652}
8653
Johannes Berge5497d72011-07-05 16:35:40 +02008654void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8655 struct net_device *netdev, const u8 *bssid,
8656 const u8 *replay_ctr, gfp_t gfp)
8657{
8658 struct sk_buff *msg;
8659 struct nlattr *rekey_attr;
8660 void *hdr;
8661
Thomas Graf58050fc2012-06-28 03:57:45 +00008662 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008663 if (!msg)
8664 return;
8665
8666 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8667 if (!hdr) {
8668 nlmsg_free(msg);
8669 return;
8670 }
8671
David S. Miller9360ffd2012-03-29 04:41:26 -04008672 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8673 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8674 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8675 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008676
8677 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8678 if (!rekey_attr)
8679 goto nla_put_failure;
8680
David S. Miller9360ffd2012-03-29 04:41:26 -04008681 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8682 NL80211_REPLAY_CTR_LEN, replay_ctr))
8683 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008684
8685 nla_nest_end(msg, rekey_attr);
8686
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008687 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008688
8689 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8690 nl80211_mlme_mcgrp.id, gfp);
8691 return;
8692
8693 nla_put_failure:
8694 genlmsg_cancel(msg, hdr);
8695 nlmsg_free(msg);
8696}
8697
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008698void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8699 struct net_device *netdev, int index,
8700 const u8 *bssid, bool preauth, gfp_t gfp)
8701{
8702 struct sk_buff *msg;
8703 struct nlattr *attr;
8704 void *hdr;
8705
Thomas Graf58050fc2012-06-28 03:57:45 +00008706 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008707 if (!msg)
8708 return;
8709
8710 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8711 if (!hdr) {
8712 nlmsg_free(msg);
8713 return;
8714 }
8715
David S. Miller9360ffd2012-03-29 04:41:26 -04008716 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8717 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8718 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008719
8720 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8721 if (!attr)
8722 goto nla_put_failure;
8723
David S. Miller9360ffd2012-03-29 04:41:26 -04008724 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8725 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8726 (preauth &&
8727 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8728 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008729
8730 nla_nest_end(msg, attr);
8731
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008732 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008733
8734 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8735 nl80211_mlme_mcgrp.id, gfp);
8736 return;
8737
8738 nla_put_failure:
8739 genlmsg_cancel(msg, hdr);
8740 nlmsg_free(msg);
8741}
8742
Thomas Pedersen53145262012-04-06 13:35:47 -07008743void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8744 struct net_device *netdev, int freq,
8745 enum nl80211_channel_type type, gfp_t gfp)
8746{
8747 struct sk_buff *msg;
8748 void *hdr;
8749
Thomas Graf58050fc2012-06-28 03:57:45 +00008750 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008751 if (!msg)
8752 return;
8753
8754 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8755 if (!hdr) {
8756 nlmsg_free(msg);
8757 return;
8758 }
8759
John W. Linville7eab0f62012-04-12 14:25:14 -04008760 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8761 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8762 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8763 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008764
8765 genlmsg_end(msg, hdr);
8766
8767 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8768 nl80211_mlme_mcgrp.id, gfp);
8769 return;
8770
8771 nla_put_failure:
8772 genlmsg_cancel(msg, hdr);
8773 nlmsg_free(msg);
8774}
8775
Johannes Bergc063dbf2010-11-24 08:10:05 +01008776void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008777nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8778 struct net_device *netdev, const u8 *peer,
8779 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8780{
8781 struct sk_buff *msg;
8782 struct nlattr *pinfoattr;
8783 void *hdr;
8784
8785 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8786 if (!msg)
8787 return;
8788
8789 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8790 if (!hdr) {
8791 nlmsg_free(msg);
8792 return;
8793 }
8794
8795 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8796 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8797 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8798 goto nla_put_failure;
8799
8800 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8801 if (!pinfoattr)
8802 goto nla_put_failure;
8803
8804 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8805 goto nla_put_failure;
8806
8807 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8808 goto nla_put_failure;
8809
8810 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8811 goto nla_put_failure;
8812
8813 nla_nest_end(msg, pinfoattr);
8814
8815 genlmsg_end(msg, hdr);
8816
8817 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8818 nl80211_mlme_mcgrp.id, gfp);
8819 return;
8820
8821 nla_put_failure:
8822 genlmsg_cancel(msg, hdr);
8823 nlmsg_free(msg);
8824}
8825
8826void
Johannes Bergc063dbf2010-11-24 08:10:05 +01008827nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8828 struct net_device *netdev, const u8 *peer,
8829 u32 num_packets, gfp_t gfp)
8830{
8831 struct sk_buff *msg;
8832 struct nlattr *pinfoattr;
8833 void *hdr;
8834
Thomas Graf58050fc2012-06-28 03:57:45 +00008835 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008836 if (!msg)
8837 return;
8838
8839 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8840 if (!hdr) {
8841 nlmsg_free(msg);
8842 return;
8843 }
8844
David S. Miller9360ffd2012-03-29 04:41:26 -04008845 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8846 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8847 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8848 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008849
8850 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8851 if (!pinfoattr)
8852 goto nla_put_failure;
8853
David S. Miller9360ffd2012-03-29 04:41:26 -04008854 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8855 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008856
8857 nla_nest_end(msg, pinfoattr);
8858
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008859 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008860
8861 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8862 nl80211_mlme_mcgrp.id, gfp);
8863 return;
8864
8865 nla_put_failure:
8866 genlmsg_cancel(msg, hdr);
8867 nlmsg_free(msg);
8868}
8869
Johannes Berg7f6cf312011-11-04 11:18:15 +01008870void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8871 u64 cookie, bool acked, gfp_t gfp)
8872{
8873 struct wireless_dev *wdev = dev->ieee80211_ptr;
8874 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8875 struct sk_buff *msg;
8876 void *hdr;
8877 int err;
8878
Beni Lev4ee3e062012-08-27 12:49:39 +03008879 trace_cfg80211_probe_status(dev, addr, cookie, acked);
8880
Thomas Graf58050fc2012-06-28 03:57:45 +00008881 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +03008882
Johannes Berg7f6cf312011-11-04 11:18:15 +01008883 if (!msg)
8884 return;
8885
8886 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8887 if (!hdr) {
8888 nlmsg_free(msg);
8889 return;
8890 }
8891
David S. Miller9360ffd2012-03-29 04:41:26 -04008892 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8893 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8894 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8895 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8896 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8897 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008898
8899 err = genlmsg_end(msg, hdr);
8900 if (err < 0) {
8901 nlmsg_free(msg);
8902 return;
8903 }
8904
8905 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8906 nl80211_mlme_mcgrp.id, gfp);
8907 return;
8908
8909 nla_put_failure:
8910 genlmsg_cancel(msg, hdr);
8911 nlmsg_free(msg);
8912}
8913EXPORT_SYMBOL(cfg80211_probe_status);
8914
Johannes Berg5e760232011-11-04 11:18:17 +01008915void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8916 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008917 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008918{
8919 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8920 struct sk_buff *msg;
8921 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008922 u32 nlportid = ACCESS_ONCE(rdev->ap_beacons_nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01008923
Beni Lev4ee3e062012-08-27 12:49:39 +03008924 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
8925
Eric W. Biederman15e47302012-09-07 20:12:54 +00008926 if (!nlportid)
Johannes Berg5e760232011-11-04 11:18:17 +01008927 return;
8928
8929 msg = nlmsg_new(len + 100, gfp);
8930 if (!msg)
8931 return;
8932
8933 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8934 if (!hdr) {
8935 nlmsg_free(msg);
8936 return;
8937 }
8938
David S. Miller9360ffd2012-03-29 04:41:26 -04008939 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8940 (freq &&
8941 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8942 (sig_dbm &&
8943 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8944 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8945 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008946
8947 genlmsg_end(msg, hdr);
8948
Eric W. Biederman15e47302012-09-07 20:12:54 +00008949 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01008950 return;
8951
8952 nla_put_failure:
8953 genlmsg_cancel(msg, hdr);
8954 nlmsg_free(msg);
8955}
8956EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8957
Jouni Malinen026331c2010-02-15 12:53:10 +02008958static int nl80211_netlink_notify(struct notifier_block * nb,
8959 unsigned long state,
8960 void *_notify)
8961{
8962 struct netlink_notify *notify = _notify;
8963 struct cfg80211_registered_device *rdev;
8964 struct wireless_dev *wdev;
8965
8966 if (state != NETLINK_URELEASE)
8967 return NOTIFY_DONE;
8968
8969 rcu_read_lock();
8970
Johannes Berg5e760232011-11-04 11:18:17 +01008971 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008972 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00008973 cfg80211_mlme_unregister_socket(wdev, notify->portid);
8974 if (rdev->ap_beacons_nlportid == notify->portid)
8975 rdev->ap_beacons_nlportid = 0;
Johannes Berg5e760232011-11-04 11:18:17 +01008976 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008977
8978 rcu_read_unlock();
8979
8980 return NOTIFY_DONE;
8981}
8982
8983static struct notifier_block nl80211_netlink_notifier = {
8984 .notifier_call = nl80211_netlink_notify,
8985};
8986
Johannes Berg55682962007-09-20 13:09:35 -04008987/* initialisation/exit functions */
8988
8989int nl80211_init(void)
8990{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008991 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008992
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008993 err = genl_register_family_with_ops(&nl80211_fam,
8994 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008995 if (err)
8996 return err;
8997
Johannes Berg55682962007-09-20 13:09:35 -04008998 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8999 if (err)
9000 goto err_out;
9001
Johannes Berg2a519312009-02-10 21:25:55 +01009002 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9003 if (err)
9004 goto err_out;
9005
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009006 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9007 if (err)
9008 goto err_out;
9009
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009010 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9011 if (err)
9012 goto err_out;
9013
Johannes Bergaff89a92009-07-01 21:26:51 +02009014#ifdef CONFIG_NL80211_TESTMODE
9015 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9016 if (err)
9017 goto err_out;
9018#endif
9019
Jouni Malinen026331c2010-02-15 12:53:10 +02009020 err = netlink_register_notifier(&nl80211_netlink_notifier);
9021 if (err)
9022 goto err_out;
9023
Johannes Berg55682962007-09-20 13:09:35 -04009024 return 0;
9025 err_out:
9026 genl_unregister_family(&nl80211_fam);
9027 return err;
9028}
9029
9030void nl80211_exit(void)
9031{
Jouni Malinen026331c2010-02-15 12:53:10 +02009032 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009033 genl_unregister_family(&nl80211_fam);
9034}