blob: 0dc3356eea4030b54146d3f72776ef12fc907df6 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg89a54e42012-06-15 14:33:17 +020049/* returns ERR_PTR values */
50static struct wireless_dev *
51__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040052{
Johannes Berg89a54e42012-06-15 14:33:17 +020053 struct cfg80211_registered_device *rdev;
54 struct wireless_dev *result = NULL;
55 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
56 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
57 u64 wdev_id;
58 int wiphy_idx = -1;
59 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040060
Johannes Berg89a54e42012-06-15 14:33:17 +020061 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040062
Johannes Berg89a54e42012-06-15 14:33:17 +020063 if (!have_ifidx && !have_wdev_id)
64 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040065
Johannes Berg89a54e42012-06-15 14:33:17 +020066 if (have_ifidx)
67 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
68 if (have_wdev_id) {
69 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
70 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040071 }
72
Johannes Berg89a54e42012-06-15 14:33:17 +020073 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
74 struct wireless_dev *wdev;
75
76 if (wiphy_net(&rdev->wiphy) != netns)
77 continue;
78
79 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
80 continue;
81
82 mutex_lock(&rdev->devlist_mtx);
83 list_for_each_entry(wdev, &rdev->wdev_list, list) {
84 if (have_ifidx && wdev->netdev &&
85 wdev->netdev->ifindex == ifidx) {
86 result = wdev;
87 break;
88 }
89 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
90 result = wdev;
91 break;
92 }
93 }
94 mutex_unlock(&rdev->devlist_mtx);
95
96 if (result)
97 break;
98 }
99
100 if (result)
101 return result;
102 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400103}
104
Johannes Berga9455402012-06-15 13:32:49 +0200105static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200106__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200107{
Johannes Berg7fee4772012-06-15 14:09:58 +0200108 struct cfg80211_registered_device *rdev = NULL, *tmp;
109 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200110
111 assert_cfg80211_lock();
112
Johannes Berg878d9ec2012-06-15 14:18:32 +0200113 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200114 !attrs[NL80211_ATTR_IFINDEX] &&
115 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200116 return ERR_PTR(-EINVAL);
117
Johannes Berg878d9ec2012-06-15 14:18:32 +0200118 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200119 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200120 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200121
Johannes Berg89a54e42012-06-15 14:33:17 +0200122 if (attrs[NL80211_ATTR_WDEV]) {
123 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
124 struct wireless_dev *wdev;
125 bool found = false;
126
127 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
128 if (tmp) {
129 /* make sure wdev exists */
130 mutex_lock(&tmp->devlist_mtx);
131 list_for_each_entry(wdev, &tmp->wdev_list, list) {
132 if (wdev->identifier != (u32)wdev_id)
133 continue;
134 found = true;
135 break;
136 }
137 mutex_unlock(&tmp->devlist_mtx);
138
139 if (!found)
140 tmp = NULL;
141
142 if (rdev && tmp != rdev)
143 return ERR_PTR(-EINVAL);
144 rdev = tmp;
145 }
146 }
147
Johannes Berg878d9ec2012-06-15 14:18:32 +0200148 if (attrs[NL80211_ATTR_IFINDEX]) {
149 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200150 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200151 if (netdev) {
152 if (netdev->ieee80211_ptr)
153 tmp = wiphy_to_dev(
154 netdev->ieee80211_ptr->wiphy);
155 else
156 tmp = NULL;
157
158 dev_put(netdev);
159
160 /* not wireless device -- return error */
161 if (!tmp)
162 return ERR_PTR(-EINVAL);
163
164 /* mismatch -- return error */
165 if (rdev && tmp != rdev)
166 return ERR_PTR(-EINVAL);
167
168 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200169 }
Johannes Berga9455402012-06-15 13:32:49 +0200170 }
171
Johannes Berg4f7eff12012-06-15 14:14:22 +0200172 if (!rdev)
173 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200174
Johannes Berg4f7eff12012-06-15 14:14:22 +0200175 if (netns != wiphy_net(&rdev->wiphy))
176 return ERR_PTR(-ENODEV);
177
178 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200179}
180
181/*
182 * This function returns a pointer to the driver
183 * that the genl_info item that is passed refers to.
184 * If successful, it returns non-NULL and also locks
185 * the driver's mutex!
186 *
187 * This means that you need to call cfg80211_unlock_rdev()
188 * before being allowed to acquire &cfg80211_mutex!
189 *
190 * This is necessary because we need to lock the global
191 * mutex to get an item off the list safely, and then
192 * we lock the rdev mutex so it doesn't go away under us.
193 *
194 * We don't want to keep cfg80211_mutex locked
195 * for all the time in order to allow requests on
196 * other interfaces to go through at the same time.
197 *
198 * The result of this can be a PTR_ERR and hence must
199 * be checked with IS_ERR() for errors.
200 */
201static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200202cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200203{
204 struct cfg80211_registered_device *rdev;
205
206 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200207 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200208
209 /* if it is not an error we grab the lock on
210 * it to assure it won't be going away while
211 * we operate on it */
212 if (!IS_ERR(rdev))
213 mutex_lock(&rdev->mtx);
214
215 mutex_unlock(&cfg80211_mutex);
216
217 return rdev;
218}
219
Johannes Berg55682962007-09-20 13:09:35 -0400220/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000221static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400222 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
223 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700224 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200225 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200226 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530227 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200228 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
229 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
230 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
231 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100232 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400233
234 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
235 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
236 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100237
Eliad Pellere007b852011-11-24 18:13:56 +0200238 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
239 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100240
Johannes Bergb9454e82009-07-08 13:29:08 +0200241 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100242 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
243 .len = WLAN_MAX_KEY_LEN },
244 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
245 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
246 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200247 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200248 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100249
250 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
251 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
252 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
253 .len = IEEE80211_MAX_DATA_LEN },
254 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
255 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100256 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
257 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
258 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
259 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
260 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100261 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100262 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200263 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100264 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800265 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100266 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300267
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700268 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
269 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
270
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300271 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
272 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
273 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200274 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
275 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100276 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc902008-08-25 11:58:58 +0300277
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800278 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700279 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700280
Johannes Berg6c739412011-11-03 09:27:01 +0100281 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200282
283 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
284 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
285 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100286 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
287 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200288
289 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
290 .len = IEEE80211_MAX_SSID_LEN },
291 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
292 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200293 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300294 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300295 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300296 [NL80211_ATTR_STA_FLAGS2] = {
297 .len = sizeof(struct nl80211_sta_flag_update),
298 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300299 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300300 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
301 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200302 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
303 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
304 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200305 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100306 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100307 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
308 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100309 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
310 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200311 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200312 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
313 .len = IEEE80211_MAX_DATA_LEN },
314 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200315 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200316 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300317 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200318 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300319 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
320 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200321 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900322 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
323 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100324 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100325 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100326 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200327 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700328 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300329 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200330 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200331 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300332 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300333 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
334 .len = IEEE80211_MAX_DATA_LEN },
335 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
336 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530337 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300338 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530339 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300340 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
341 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
342 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
343 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
344 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100345 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200346 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
347 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700348 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800349 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
350 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
351 .len = NL80211_HT_CAPABILITY_LEN
352 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100353 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530354 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530355 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200356 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Johannes Berg55682962007-09-20 13:09:35 -0400357};
358
Johannes Berge31b8212010-10-05 19:39:30 +0200359/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000360static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200361 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200362 [NL80211_KEY_IDX] = { .type = NLA_U8 },
363 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200364 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200365 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
366 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200367 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100368 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
369};
370
371/* policy for the key default flags */
372static const struct nla_policy
373nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
374 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
375 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200376};
377
Johannes Bergff1b6e62011-05-04 15:37:28 +0200378/* policy for WoWLAN attributes */
379static const struct nla_policy
380nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
381 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
382 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
383 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
384 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200385 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
386 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
387 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
388 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200389};
390
Johannes Berge5497d72011-07-05 16:35:40 +0200391/* policy for GTK rekey offload attributes */
392static const struct nla_policy
393nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
394 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
395 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
396 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
397};
398
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300399static const struct nla_policy
400nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200401 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300402 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700403 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300404};
405
Holger Schuriga0438972009-11-11 11:30:02 +0100406/* ifidx get helper */
407static int nl80211_get_ifidx(struct netlink_callback *cb)
408{
409 int res;
410
411 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
412 nl80211_fam.attrbuf, nl80211_fam.maxattr,
413 nl80211_policy);
414 if (res)
415 return res;
416
417 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
418 return -EINVAL;
419
420 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
421 if (!res)
422 return -EINVAL;
423 return res;
424}
425
Johannes Berg67748892010-10-04 21:14:06 +0200426static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
427 struct netlink_callback *cb,
428 struct cfg80211_registered_device **rdev,
429 struct net_device **dev)
430{
431 int ifidx = cb->args[0];
432 int err;
433
434 if (!ifidx)
435 ifidx = nl80211_get_ifidx(cb);
436 if (ifidx < 0)
437 return ifidx;
438
439 cb->args[0] = ifidx;
440
441 rtnl_lock();
442
443 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
444 if (!*dev) {
445 err = -ENODEV;
446 goto out_rtnl;
447 }
448
449 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100450 if (IS_ERR(*rdev)) {
451 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200452 goto out_rtnl;
453 }
454
455 return 0;
456 out_rtnl:
457 rtnl_unlock();
458 return err;
459}
460
461static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
462{
463 cfg80211_unlock_rdev(rdev);
464 rtnl_unlock();
465}
466
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100467/* IE validation */
468static bool is_valid_ie_attr(const struct nlattr *attr)
469{
470 const u8 *pos;
471 int len;
472
473 if (!attr)
474 return true;
475
476 pos = nla_data(attr);
477 len = nla_len(attr);
478
479 while (len) {
480 u8 elemlen;
481
482 if (len < 2)
483 return false;
484 len -= 2;
485
486 elemlen = pos[1];
487 if (elemlen > len)
488 return false;
489
490 len -= elemlen;
491 pos += 2 + elemlen;
492 }
493
494 return true;
495}
496
Johannes Berg55682962007-09-20 13:09:35 -0400497/* message building helper */
498static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
499 int flags, u8 cmd)
500{
501 /* since there is no private header just add the generic one */
502 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
503}
504
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400505static int nl80211_msg_put_channel(struct sk_buff *msg,
506 struct ieee80211_channel *chan)
507{
David S. Miller9360ffd2012-03-29 04:41:26 -0400508 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
509 chan->center_freq))
510 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400511
David S. Miller9360ffd2012-03-29 04:41:26 -0400512 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
513 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
514 goto nla_put_failure;
515 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
516 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
517 goto nla_put_failure;
518 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
519 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
520 goto nla_put_failure;
521 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
522 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
523 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400524
David S. Miller9360ffd2012-03-29 04:41:26 -0400525 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
526 DBM_TO_MBM(chan->max_power)))
527 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400528
529 return 0;
530
531 nla_put_failure:
532 return -ENOBUFS;
533}
534
Johannes Berg55682962007-09-20 13:09:35 -0400535/* netlink command implementations */
536
Johannes Bergb9454e82009-07-08 13:29:08 +0200537struct key_parse {
538 struct key_params p;
539 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200540 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200541 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100542 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200543};
544
545static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
546{
547 struct nlattr *tb[NL80211_KEY_MAX + 1];
548 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
549 nl80211_key_policy);
550 if (err)
551 return err;
552
553 k->def = !!tb[NL80211_KEY_DEFAULT];
554 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
555
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100556 if (k->def) {
557 k->def_uni = true;
558 k->def_multi = true;
559 }
560 if (k->defmgmt)
561 k->def_multi = true;
562
Johannes Bergb9454e82009-07-08 13:29:08 +0200563 if (tb[NL80211_KEY_IDX])
564 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
565
566 if (tb[NL80211_KEY_DATA]) {
567 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
568 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
569 }
570
571 if (tb[NL80211_KEY_SEQ]) {
572 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
573 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
574 }
575
576 if (tb[NL80211_KEY_CIPHER])
577 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
578
Johannes Berge31b8212010-10-05 19:39:30 +0200579 if (tb[NL80211_KEY_TYPE]) {
580 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
581 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
582 return -EINVAL;
583 }
584
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100585 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
586 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100587 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
588 tb[NL80211_KEY_DEFAULT_TYPES],
589 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100590 if (err)
591 return err;
592
593 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
594 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
595 }
596
Johannes Bergb9454e82009-07-08 13:29:08 +0200597 return 0;
598}
599
600static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
601{
602 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
603 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
604 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
605 }
606
607 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
608 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
609 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
610 }
611
612 if (info->attrs[NL80211_ATTR_KEY_IDX])
613 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
614
615 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
616 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
617
618 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
619 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
620
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100621 if (k->def) {
622 k->def_uni = true;
623 k->def_multi = true;
624 }
625 if (k->defmgmt)
626 k->def_multi = true;
627
Johannes Berge31b8212010-10-05 19:39:30 +0200628 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
629 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
630 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
631 return -EINVAL;
632 }
633
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100634 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
635 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
636 int err = nla_parse_nested(
637 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
638 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
639 nl80211_key_default_policy);
640 if (err)
641 return err;
642
643 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
644 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
645 }
646
Johannes Bergb9454e82009-07-08 13:29:08 +0200647 return 0;
648}
649
650static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
651{
652 int err;
653
654 memset(k, 0, sizeof(*k));
655 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200656 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200657
658 if (info->attrs[NL80211_ATTR_KEY])
659 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
660 else
661 err = nl80211_parse_key_old(info, k);
662
663 if (err)
664 return err;
665
666 if (k->def && k->defmgmt)
667 return -EINVAL;
668
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100669 if (k->defmgmt) {
670 if (k->def_uni || !k->def_multi)
671 return -EINVAL;
672 }
673
Johannes Bergb9454e82009-07-08 13:29:08 +0200674 if (k->idx != -1) {
675 if (k->defmgmt) {
676 if (k->idx < 4 || k->idx > 5)
677 return -EINVAL;
678 } else if (k->def) {
679 if (k->idx < 0 || k->idx > 3)
680 return -EINVAL;
681 } else {
682 if (k->idx < 0 || k->idx > 5)
683 return -EINVAL;
684 }
685 }
686
687 return 0;
688}
689
Johannes Bergfffd0932009-07-08 14:22:54 +0200690static struct cfg80211_cached_keys *
691nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
692 struct nlattr *keys)
693{
694 struct key_parse parse;
695 struct nlattr *key;
696 struct cfg80211_cached_keys *result;
697 int rem, err, def = 0;
698
699 result = kzalloc(sizeof(*result), GFP_KERNEL);
700 if (!result)
701 return ERR_PTR(-ENOMEM);
702
703 result->def = -1;
704 result->defmgmt = -1;
705
706 nla_for_each_nested(key, keys, rem) {
707 memset(&parse, 0, sizeof(parse));
708 parse.idx = -1;
709
710 err = nl80211_parse_key_new(key, &parse);
711 if (err)
712 goto error;
713 err = -EINVAL;
714 if (!parse.p.key)
715 goto error;
716 if (parse.idx < 0 || parse.idx > 4)
717 goto error;
718 if (parse.def) {
719 if (def)
720 goto error;
721 def = 1;
722 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100723 if (!parse.def_uni || !parse.def_multi)
724 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200725 } else if (parse.defmgmt)
726 goto error;
727 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200728 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200729 if (err)
730 goto error;
731 result->params[parse.idx].cipher = parse.p.cipher;
732 result->params[parse.idx].key_len = parse.p.key_len;
733 result->params[parse.idx].key = result->data[parse.idx];
734 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
735 }
736
737 return result;
738 error:
739 kfree(result);
740 return ERR_PTR(err);
741}
742
743static int nl80211_key_allowed(struct wireless_dev *wdev)
744{
745 ASSERT_WDEV_LOCK(wdev);
746
Johannes Bergfffd0932009-07-08 14:22:54 +0200747 switch (wdev->iftype) {
748 case NL80211_IFTYPE_AP:
749 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200750 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700751 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200752 break;
753 case NL80211_IFTYPE_ADHOC:
754 if (!wdev->current_bss)
755 return -ENOLINK;
756 break;
757 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200758 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200759 if (wdev->sme_state != CFG80211_SME_CONNECTED)
760 return -ENOLINK;
761 break;
762 default:
763 return -EINVAL;
764 }
765
766 return 0;
767}
768
Johannes Berg7527a782011-05-13 10:58:57 +0200769static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
770{
771 struct nlattr *nl_modes = nla_nest_start(msg, attr);
772 int i;
773
774 if (!nl_modes)
775 goto nla_put_failure;
776
777 i = 0;
778 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400779 if ((ifmodes & 1) && nla_put_flag(msg, i))
780 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200781 ifmodes >>= 1;
782 i++;
783 }
784
785 nla_nest_end(msg, nl_modes);
786 return 0;
787
788nla_put_failure:
789 return -ENOBUFS;
790}
791
792static int nl80211_put_iface_combinations(struct wiphy *wiphy,
793 struct sk_buff *msg)
794{
795 struct nlattr *nl_combis;
796 int i, j;
797
798 nl_combis = nla_nest_start(msg,
799 NL80211_ATTR_INTERFACE_COMBINATIONS);
800 if (!nl_combis)
801 goto nla_put_failure;
802
803 for (i = 0; i < wiphy->n_iface_combinations; i++) {
804 const struct ieee80211_iface_combination *c;
805 struct nlattr *nl_combi, *nl_limits;
806
807 c = &wiphy->iface_combinations[i];
808
809 nl_combi = nla_nest_start(msg, i + 1);
810 if (!nl_combi)
811 goto nla_put_failure;
812
813 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
814 if (!nl_limits)
815 goto nla_put_failure;
816
817 for (j = 0; j < c->n_limits; j++) {
818 struct nlattr *nl_limit;
819
820 nl_limit = nla_nest_start(msg, j + 1);
821 if (!nl_limit)
822 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400823 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
824 c->limits[j].max))
825 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200826 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
827 c->limits[j].types))
828 goto nla_put_failure;
829 nla_nest_end(msg, nl_limit);
830 }
831
832 nla_nest_end(msg, nl_limits);
833
David S. Miller9360ffd2012-03-29 04:41:26 -0400834 if (c->beacon_int_infra_match &&
835 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
836 goto nla_put_failure;
837 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
838 c->num_different_channels) ||
839 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
840 c->max_interfaces))
841 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200842
843 nla_nest_end(msg, nl_combi);
844 }
845
846 nla_nest_end(msg, nl_combis);
847
848 return 0;
849nla_put_failure:
850 return -ENOBUFS;
851}
852
Johannes Berg55682962007-09-20 13:09:35 -0400853static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
854 struct cfg80211_registered_device *dev)
855{
856 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100857 struct nlattr *nl_bands, *nl_band;
858 struct nlattr *nl_freqs, *nl_freq;
859 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100860 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100861 enum ieee80211_band band;
862 struct ieee80211_channel *chan;
863 struct ieee80211_rate *rate;
864 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200865 const struct ieee80211_txrx_stypes *mgmt_stypes =
866 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400867
868 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
869 if (!hdr)
870 return -1;
871
David S. Miller9360ffd2012-03-29 04:41:26 -0400872 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
873 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
874 nla_put_u32(msg, NL80211_ATTR_GENERATION,
875 cfg80211_rdev_list_generation) ||
876 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
877 dev->wiphy.retry_short) ||
878 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
879 dev->wiphy.retry_long) ||
880 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
881 dev->wiphy.frag_threshold) ||
882 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
883 dev->wiphy.rts_threshold) ||
884 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
885 dev->wiphy.coverage_class) ||
886 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
887 dev->wiphy.max_scan_ssids) ||
888 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
889 dev->wiphy.max_sched_scan_ssids) ||
890 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
891 dev->wiphy.max_scan_ie_len) ||
892 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
893 dev->wiphy.max_sched_scan_ie_len) ||
894 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
895 dev->wiphy.max_match_sets))
896 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200897
David S. Miller9360ffd2012-03-29 04:41:26 -0400898 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
899 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
900 goto nla_put_failure;
901 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
902 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
903 goto nla_put_failure;
904 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
905 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
906 goto nla_put_failure;
907 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
908 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
909 goto nla_put_failure;
910 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
911 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
912 goto nla_put_failure;
913 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
914 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
915 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200916
David S. Miller9360ffd2012-03-29 04:41:26 -0400917 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
918 sizeof(u32) * dev->wiphy.n_cipher_suites,
919 dev->wiphy.cipher_suites))
920 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100921
David S. Miller9360ffd2012-03-29 04:41:26 -0400922 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
923 dev->wiphy.max_num_pmkids))
924 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530925
David S. Miller9360ffd2012-03-29 04:41:26 -0400926 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
927 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
928 goto nla_put_failure;
Johannes Berg25e47c12009-04-02 20:14:06 +0200929
David S. Miller9360ffd2012-03-29 04:41:26 -0400930 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
931 dev->wiphy.available_antennas_tx) ||
932 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
933 dev->wiphy.available_antennas_rx))
934 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100935
David S. Miller9360ffd2012-03-29 04:41:26 -0400936 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
937 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
938 dev->wiphy.probe_resp_offload))
939 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200940
Bruno Randolf7f531e02010-12-16 11:30:22 +0900941 if ((dev->wiphy.available_antennas_tx ||
942 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900943 u32 tx_ant = 0, rx_ant = 0;
944 int res;
945 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
946 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400947 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
948 tx_ant) ||
949 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
950 rx_ant))
951 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900952 }
953 }
954
Johannes Berg7527a782011-05-13 10:58:57 +0200955 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
956 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700957 goto nla_put_failure;
958
Johannes Bergee688b002008-01-24 19:38:39 +0100959 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
960 if (!nl_bands)
961 goto nla_put_failure;
962
963 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
964 if (!dev->wiphy.bands[band])
965 continue;
966
967 nl_band = nla_nest_start(msg, band);
968 if (!nl_band)
969 goto nla_put_failure;
970
Johannes Bergd51626d2008-10-09 12:20:13 +0200971 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400972 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
973 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
974 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
975 &dev->wiphy.bands[band]->ht_cap.mcs) ||
976 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
977 dev->wiphy.bands[band]->ht_cap.cap) ||
978 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
979 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
980 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
981 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
982 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200983
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000984 /* add VHT info */
985 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
986 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
987 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
988 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
989 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
990 dev->wiphy.bands[band]->vht_cap.cap)))
991 goto nla_put_failure;
992
Johannes Bergee688b002008-01-24 19:38:39 +0100993 /* add frequencies */
994 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
995 if (!nl_freqs)
996 goto nla_put_failure;
997
998 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
999 nl_freq = nla_nest_start(msg, i);
1000 if (!nl_freq)
1001 goto nla_put_failure;
1002
1003 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001004
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001005 if (nl80211_msg_put_channel(msg, chan))
1006 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001007
Johannes Bergee688b002008-01-24 19:38:39 +01001008 nla_nest_end(msg, nl_freq);
1009 }
1010
1011 nla_nest_end(msg, nl_freqs);
1012
1013 /* add bitrates */
1014 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1015 if (!nl_rates)
1016 goto nla_put_failure;
1017
1018 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1019 nl_rate = nla_nest_start(msg, i);
1020 if (!nl_rate)
1021 goto nla_put_failure;
1022
1023 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001024 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1025 rate->bitrate))
1026 goto nla_put_failure;
1027 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1028 nla_put_flag(msg,
1029 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1030 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001031
1032 nla_nest_end(msg, nl_rate);
1033 }
1034
1035 nla_nest_end(msg, nl_rates);
1036
1037 nla_nest_end(msg, nl_band);
1038 }
1039 nla_nest_end(msg, nl_bands);
1040
Johannes Berg8fdc6212009-03-14 09:34:01 +01001041 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1042 if (!nl_cmds)
1043 goto nla_put_failure;
1044
1045 i = 0;
1046#define CMD(op, n) \
1047 do { \
1048 if (dev->ops->op) { \
1049 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001050 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1051 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001052 } \
1053 } while (0)
1054
1055 CMD(add_virtual_intf, NEW_INTERFACE);
1056 CMD(change_virtual_intf, SET_INTERFACE);
1057 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001058 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001059 CMD(add_station, NEW_STATION);
1060 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001061 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001062 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001063 CMD(auth, AUTHENTICATE);
1064 CMD(assoc, ASSOCIATE);
1065 CMD(deauth, DEAUTHENTICATE);
1066 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001067 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001068 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001069 CMD(set_pmksa, SET_PMKSA);
1070 CMD(del_pmksa, DEL_PMKSA);
1071 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001072 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1073 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001074 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001075 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001076 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001077 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001078 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001079 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1080 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001081 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001082 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001083 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001084 i++;
1085 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1086 goto nla_put_failure;
1087 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001088 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001089 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1090 CMD(tdls_mgmt, TDLS_MGMT);
1091 CMD(tdls_oper, TDLS_OPER);
1092 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001093 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1094 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001095 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001096 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e7602302011-11-04 11:18:17 +01001097 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1098 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001099 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1100 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01001101 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001102
Kalle Valo4745fc02011-11-17 19:06:10 +02001103#ifdef CONFIG_NL80211_TESTMODE
1104 CMD(testmode_cmd, TESTMODE);
1105#endif
1106
Johannes Berg8fdc6212009-03-14 09:34:01 +01001107#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001108
Johannes Berg6829c872009-07-02 09:13:27 +02001109 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001110 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001111 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1112 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001113 }
1114
Johannes Berg6829c872009-07-02 09:13:27 +02001115 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001116 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001117 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1118 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001119 }
1120
Johannes Berg8fdc6212009-03-14 09:34:01 +01001121 nla_nest_end(msg, nl_cmds);
1122
Johannes Berg7c4ef712011-11-18 15:33:48 +01001123 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001124 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1125 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1126 dev->wiphy.max_remain_on_channel_duration))
1127 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001128
David S. Miller9360ffd2012-03-29 04:41:26 -04001129 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1130 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1131 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001132
Johannes Berg2e161f72010-08-12 15:38:38 +02001133 if (mgmt_stypes) {
1134 u16 stypes;
1135 struct nlattr *nl_ftypes, *nl_ifs;
1136 enum nl80211_iftype ift;
1137
1138 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1139 if (!nl_ifs)
1140 goto nla_put_failure;
1141
1142 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1143 nl_ftypes = nla_nest_start(msg, ift);
1144 if (!nl_ftypes)
1145 goto nla_put_failure;
1146 i = 0;
1147 stypes = mgmt_stypes[ift].tx;
1148 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001149 if ((stypes & 1) &&
1150 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1151 (i << 4) | IEEE80211_FTYPE_MGMT))
1152 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001153 stypes >>= 1;
1154 i++;
1155 }
1156 nla_nest_end(msg, nl_ftypes);
1157 }
1158
Johannes Berg74b70a42010-08-24 12:15:53 +02001159 nla_nest_end(msg, nl_ifs);
1160
Johannes Berg2e161f72010-08-12 15:38:38 +02001161 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1162 if (!nl_ifs)
1163 goto nla_put_failure;
1164
1165 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1166 nl_ftypes = nla_nest_start(msg, ift);
1167 if (!nl_ftypes)
1168 goto nla_put_failure;
1169 i = 0;
1170 stypes = mgmt_stypes[ift].rx;
1171 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001172 if ((stypes & 1) &&
1173 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1174 (i << 4) | IEEE80211_FTYPE_MGMT))
1175 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001176 stypes >>= 1;
1177 i++;
1178 }
1179 nla_nest_end(msg, nl_ftypes);
1180 }
1181 nla_nest_end(msg, nl_ifs);
1182 }
1183
Johannes Bergdfb89c52012-06-27 09:23:48 +02001184#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001185 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1186 struct nlattr *nl_wowlan;
1187
1188 nl_wowlan = nla_nest_start(msg,
1189 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1190 if (!nl_wowlan)
1191 goto nla_put_failure;
1192
David S. Miller9360ffd2012-03-29 04:41:26 -04001193 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1194 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1195 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1196 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1197 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1198 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1199 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1200 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1201 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1202 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1203 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1204 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1205 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1206 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1207 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1208 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1209 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001210 if (dev->wiphy.wowlan.n_patterns) {
1211 struct nl80211_wowlan_pattern_support pat = {
1212 .max_patterns = dev->wiphy.wowlan.n_patterns,
1213 .min_pattern_len =
1214 dev->wiphy.wowlan.pattern_min_len,
1215 .max_pattern_len =
1216 dev->wiphy.wowlan.pattern_max_len,
1217 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001218 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1219 sizeof(pat), &pat))
1220 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001221 }
1222
1223 nla_nest_end(msg, nl_wowlan);
1224 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001225#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001226
Johannes Berg7527a782011-05-13 10:58:57 +02001227 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1228 dev->wiphy.software_iftypes))
1229 goto nla_put_failure;
1230
1231 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1232 goto nla_put_failure;
1233
David S. Miller9360ffd2012-03-29 04:41:26 -04001234 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1235 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1236 dev->wiphy.ap_sme_capa))
1237 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001238
David S. Miller9360ffd2012-03-29 04:41:26 -04001239 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1240 dev->wiphy.features))
1241 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001242
David S. Miller9360ffd2012-03-29 04:41:26 -04001243 if (dev->wiphy.ht_capa_mod_mask &&
1244 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1245 sizeof(*dev->wiphy.ht_capa_mod_mask),
1246 dev->wiphy.ht_capa_mod_mask))
1247 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001248
Johannes Berg55682962007-09-20 13:09:35 -04001249 return genlmsg_end(msg, hdr);
1250
1251 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001252 genlmsg_cancel(msg, hdr);
1253 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001254}
1255
1256static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1257{
1258 int idx = 0;
1259 int start = cb->args[0];
1260 struct cfg80211_registered_device *dev;
1261
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001262 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001263 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001264 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1265 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001266 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001267 continue;
1268 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1269 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001270 dev) < 0) {
1271 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001272 break;
Julius Volzb4637272008-07-08 14:02:19 +02001273 }
Johannes Berg55682962007-09-20 13:09:35 -04001274 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001275 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001276
1277 cb->args[0] = idx;
1278
1279 return skb->len;
1280}
1281
1282static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1283{
1284 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001285 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001286
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001287 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001288 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001289 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001290
Johannes Berg4c476992010-10-04 21:36:35 +02001291 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1292 nlmsg_free(msg);
1293 return -ENOBUFS;
1294 }
Johannes Berg55682962007-09-20 13:09:35 -04001295
Johannes Berg134e6372009-07-10 09:51:34 +00001296 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001297}
1298
Jouni Malinen31888482008-10-30 16:59:24 +02001299static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1300 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1301 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1302 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1303 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1304 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1305};
1306
1307static int parse_txq_params(struct nlattr *tb[],
1308 struct ieee80211_txq_params *txq_params)
1309{
Johannes Berga3304b02012-03-28 11:04:24 +02001310 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001311 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1312 !tb[NL80211_TXQ_ATTR_AIFS])
1313 return -EINVAL;
1314
Johannes Berga3304b02012-03-28 11:04:24 +02001315 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001316 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1317 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1318 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1319 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1320
Johannes Berga3304b02012-03-28 11:04:24 +02001321 if (txq_params->ac >= NL80211_NUM_ACS)
1322 return -EINVAL;
1323
Jouni Malinen31888482008-10-30 16:59:24 +02001324 return 0;
1325}
1326
Johannes Bergf444de02010-05-05 15:25:02 +02001327static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1328{
1329 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001330 * You can only set the channel explicitly for WDS interfaces,
1331 * all others have their channel managed via their respective
1332 * "establish a connection" command (connect, join, ...)
1333 *
1334 * For AP/GO and mesh mode, the channel can be set with the
1335 * channel userspace API, but is only stored and passed to the
1336 * low-level driver when the AP starts or the mesh is joined.
1337 * This is for backward compatibility, userspace can also give
1338 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001339 *
1340 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001341 * whatever else is going on, so they have their own special
1342 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001343 */
1344 return !wdev ||
1345 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001346 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001347 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1348 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001349}
1350
Johannes Bergcd6c6592012-05-10 21:27:18 +02001351static bool nl80211_valid_channel_type(struct genl_info *info,
1352 enum nl80211_channel_type *channel_type)
1353{
1354 enum nl80211_channel_type tmp;
1355
1356 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1357 return false;
1358
1359 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1360 if (tmp != NL80211_CHAN_NO_HT &&
1361 tmp != NL80211_CHAN_HT20 &&
1362 tmp != NL80211_CHAN_HT40PLUS &&
1363 tmp != NL80211_CHAN_HT40MINUS)
1364 return false;
1365
1366 if (channel_type)
1367 *channel_type = tmp;
1368
1369 return true;
1370}
1371
Johannes Bergf444de02010-05-05 15:25:02 +02001372static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1373 struct wireless_dev *wdev,
1374 struct genl_info *info)
1375{
Johannes Bergaa430da2012-05-16 23:50:18 +02001376 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001377 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1378 u32 freq;
1379 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001380 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1381
1382 if (wdev)
1383 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001384
1385 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1386 return -EINVAL;
1387
1388 if (!nl80211_can_set_dev_channel(wdev))
1389 return -EOPNOTSUPP;
1390
Johannes Bergcd6c6592012-05-10 21:27:18 +02001391 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1392 !nl80211_valid_channel_type(info, &channel_type))
1393 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001394
1395 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1396
1397 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001398 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001399 case NL80211_IFTYPE_AP:
1400 case NL80211_IFTYPE_P2P_GO:
1401 if (wdev->beacon_interval) {
1402 result = -EBUSY;
1403 break;
1404 }
1405 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1406 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1407 channel,
1408 channel_type)) {
1409 result = -EINVAL;
1410 break;
1411 }
1412 wdev->preset_chan = channel;
1413 wdev->preset_chantype = channel_type;
1414 result = 0;
1415 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001416 case NL80211_IFTYPE_MESH_POINT:
1417 result = cfg80211_set_mesh_freq(rdev, wdev, freq, channel_type);
1418 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001419 case NL80211_IFTYPE_MONITOR:
1420 result = cfg80211_set_monitor_channel(rdev, freq, channel_type);
1421 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001422 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001423 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001424 }
1425 mutex_unlock(&rdev->devlist_mtx);
1426
1427 return result;
1428}
1429
1430static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1431{
Johannes Berg4c476992010-10-04 21:36:35 +02001432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1433 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001434
Johannes Berg4c476992010-10-04 21:36:35 +02001435 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001436}
1437
Bill Jordane8347eb2010-10-01 13:54:28 -04001438static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1439{
Johannes Berg43b19952010-10-07 13:10:30 +02001440 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1441 struct net_device *dev = info->user_ptr[1];
1442 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001443 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001444
1445 if (!info->attrs[NL80211_ATTR_MAC])
1446 return -EINVAL;
1447
Johannes Berg43b19952010-10-07 13:10:30 +02001448 if (netif_running(dev))
1449 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001450
Johannes Berg43b19952010-10-07 13:10:30 +02001451 if (!rdev->ops->set_wds_peer)
1452 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001453
Johannes Berg43b19952010-10-07 13:10:30 +02001454 if (wdev->iftype != NL80211_IFTYPE_WDS)
1455 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001456
1457 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001458 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001459}
1460
1461
Johannes Berg55682962007-09-20 13:09:35 -04001462static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1463{
1464 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001465 struct net_device *netdev = NULL;
1466 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001467 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001468 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001469 u32 changed;
1470 u8 retry_short = 0, retry_long = 0;
1471 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001472 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001473
Johannes Bergf444de02010-05-05 15:25:02 +02001474 /*
1475 * Try to find the wiphy and netdev. Normally this
1476 * function shouldn't need the netdev, but this is
1477 * done for backward compatibility -- previously
1478 * setting the channel was done per wiphy, but now
1479 * it is per netdev. Previous userland like hostapd
1480 * also passed a netdev to set_wiphy, so that it is
1481 * possible to let that go to the right netdev!
1482 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001483 mutex_lock(&cfg80211_mutex);
1484
Johannes Bergf444de02010-05-05 15:25:02 +02001485 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1486 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1487
1488 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1489 if (netdev && netdev->ieee80211_ptr) {
1490 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1491 mutex_lock(&rdev->mtx);
1492 } else
1493 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001494 }
1495
Johannes Bergf444de02010-05-05 15:25:02 +02001496 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001497 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1498 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001499 if (IS_ERR(rdev)) {
1500 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001501 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001502 }
1503 wdev = NULL;
1504 netdev = NULL;
1505 result = 0;
1506
1507 mutex_lock(&rdev->mtx);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001508 } else if (nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
Johannes Bergf444de02010-05-05 15:25:02 +02001509 wdev = netdev->ieee80211_ptr;
1510 else
1511 wdev = NULL;
1512
1513 /*
1514 * end workaround code, by now the rdev is available
1515 * and locked, and wdev may or may not be NULL.
1516 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001517
1518 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001519 result = cfg80211_dev_rename(
1520 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001521
1522 mutex_unlock(&cfg80211_mutex);
1523
1524 if (result)
1525 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001526
Jouni Malinen31888482008-10-30 16:59:24 +02001527 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1528 struct ieee80211_txq_params txq_params;
1529 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1530
1531 if (!rdev->ops->set_txq_params) {
1532 result = -EOPNOTSUPP;
1533 goto bad_res;
1534 }
1535
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001536 if (!netdev) {
1537 result = -EINVAL;
1538 goto bad_res;
1539 }
1540
Johannes Berg133a3ff2011-11-03 14:50:13 +01001541 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1542 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1543 result = -EINVAL;
1544 goto bad_res;
1545 }
1546
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001547 if (!netif_running(netdev)) {
1548 result = -ENETDOWN;
1549 goto bad_res;
1550 }
1551
Jouni Malinen31888482008-10-30 16:59:24 +02001552 nla_for_each_nested(nl_txq_params,
1553 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1554 rem_txq_params) {
1555 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1556 nla_data(nl_txq_params),
1557 nla_len(nl_txq_params),
1558 txq_params_policy);
1559 result = parse_txq_params(tb, &txq_params);
1560 if (result)
1561 goto bad_res;
1562
1563 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001564 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001565 &txq_params);
1566 if (result)
1567 goto bad_res;
1568 }
1569 }
1570
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001571 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001572 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001573 if (result)
1574 goto bad_res;
1575 }
1576
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001577 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1578 enum nl80211_tx_power_setting type;
1579 int idx, mbm = 0;
1580
1581 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001582 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001583 goto bad_res;
1584 }
1585
1586 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1587 type = nla_get_u32(info->attrs[idx]);
1588
1589 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1590 (type != NL80211_TX_POWER_AUTOMATIC)) {
1591 result = -EINVAL;
1592 goto bad_res;
1593 }
1594
1595 if (type != NL80211_TX_POWER_AUTOMATIC) {
1596 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1597 mbm = nla_get_u32(info->attrs[idx]);
1598 }
1599
1600 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1601 if (result)
1602 goto bad_res;
1603 }
1604
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001605 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1606 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1607 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001608 if ((!rdev->wiphy.available_antennas_tx &&
1609 !rdev->wiphy.available_antennas_rx) ||
1610 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001611 result = -EOPNOTSUPP;
1612 goto bad_res;
1613 }
1614
1615 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1616 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1617
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001618 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001619 * available antenna masks, except for the "all" mask */
1620 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1621 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001622 result = -EINVAL;
1623 goto bad_res;
1624 }
1625
Bruno Randolf7f531e02010-12-16 11:30:22 +09001626 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1627 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001628
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001629 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1630 if (result)
1631 goto bad_res;
1632 }
1633
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001634 changed = 0;
1635
1636 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1637 retry_short = nla_get_u8(
1638 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1639 if (retry_short == 0) {
1640 result = -EINVAL;
1641 goto bad_res;
1642 }
1643 changed |= WIPHY_PARAM_RETRY_SHORT;
1644 }
1645
1646 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1647 retry_long = nla_get_u8(
1648 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1649 if (retry_long == 0) {
1650 result = -EINVAL;
1651 goto bad_res;
1652 }
1653 changed |= WIPHY_PARAM_RETRY_LONG;
1654 }
1655
1656 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1657 frag_threshold = nla_get_u32(
1658 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1659 if (frag_threshold < 256) {
1660 result = -EINVAL;
1661 goto bad_res;
1662 }
1663 if (frag_threshold != (u32) -1) {
1664 /*
1665 * Fragments (apart from the last one) are required to
1666 * have even length. Make the fragmentation code
1667 * simpler by stripping LSB should someone try to use
1668 * odd threshold value.
1669 */
1670 frag_threshold &= ~0x1;
1671 }
1672 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1673 }
1674
1675 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1676 rts_threshold = nla_get_u32(
1677 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1678 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1679 }
1680
Lukáš Turek81077e82009-12-21 22:50:47 +01001681 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1682 coverage_class = nla_get_u8(
1683 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1684 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1685 }
1686
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001687 if (changed) {
1688 u8 old_retry_short, old_retry_long;
1689 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001690 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001691
1692 if (!rdev->ops->set_wiphy_params) {
1693 result = -EOPNOTSUPP;
1694 goto bad_res;
1695 }
1696
1697 old_retry_short = rdev->wiphy.retry_short;
1698 old_retry_long = rdev->wiphy.retry_long;
1699 old_frag_threshold = rdev->wiphy.frag_threshold;
1700 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001701 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001702
1703 if (changed & WIPHY_PARAM_RETRY_SHORT)
1704 rdev->wiphy.retry_short = retry_short;
1705 if (changed & WIPHY_PARAM_RETRY_LONG)
1706 rdev->wiphy.retry_long = retry_long;
1707 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1708 rdev->wiphy.frag_threshold = frag_threshold;
1709 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1710 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001711 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1712 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001713
1714 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1715 if (result) {
1716 rdev->wiphy.retry_short = old_retry_short;
1717 rdev->wiphy.retry_long = old_retry_long;
1718 rdev->wiphy.frag_threshold = old_frag_threshold;
1719 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001720 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001721 }
1722 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001723
Johannes Berg306d6112008-12-08 12:39:04 +01001724 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001725 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001726 if (netdev)
1727 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001728 return result;
1729}
1730
Johannes Berg71bbc992012-06-15 15:30:18 +02001731static inline u64 wdev_id(struct wireless_dev *wdev)
1732{
1733 return (u64)wdev->identifier |
1734 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1735}
Johannes Berg55682962007-09-20 13:09:35 -04001736
1737static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001738 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001739 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001740{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001741 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001742 void *hdr;
1743
1744 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1745 if (!hdr)
1746 return -1;
1747
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001748 if (dev &&
1749 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
1750 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name) ||
1751 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dev->dev_addr)))
1752 goto nla_put_failure;
1753
1754 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1755 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001756 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001757 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1758 rdev->devlist_generation ^
1759 (cfg80211_rdev_list_generation << 2)))
1760 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001761
Michal Kazior2e165b82012-06-29 12:47:06 +02001762 if (rdev->monitor_channel) {
1763 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1764 rdev->monitor_channel->center_freq) ||
1765 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1766 rdev->monitor_channel_type))
John W. Linville59ef43e2012-04-18 14:17:13 -04001767 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001768 }
1769
Johannes Berg55682962007-09-20 13:09:35 -04001770 return genlmsg_end(msg, hdr);
1771
1772 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001773 genlmsg_cancel(msg, hdr);
1774 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001775}
1776
1777static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1778{
1779 int wp_idx = 0;
1780 int if_idx = 0;
1781 int wp_start = cb->args[0];
1782 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001783 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001784 struct wireless_dev *wdev;
1785
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001786 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001787 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1788 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001789 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001790 if (wp_idx < wp_start) {
1791 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001792 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001793 }
Johannes Berg55682962007-09-20 13:09:35 -04001794 if_idx = 0;
1795
Johannes Bergf5ea9122009-08-07 16:17:38 +02001796 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001797 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001798 if (if_idx < if_start) {
1799 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001800 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001801 }
Johannes Berg55682962007-09-20 13:09:35 -04001802 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1803 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001804 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001805 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001806 goto out;
1807 }
1808 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001809 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001810 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001811
1812 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001813 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001814 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001815 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001816
1817 cb->args[0] = wp_idx;
1818 cb->args[1] = if_idx;
1819
1820 return skb->len;
1821}
1822
1823static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1824{
1825 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001826 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001827 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001828
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001829 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001830 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001831 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001832
Johannes Bergd7264052009-04-19 16:23:20 +02001833 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001834 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001835 nlmsg_free(msg);
1836 return -ENOBUFS;
1837 }
Johannes Berg55682962007-09-20 13:09:35 -04001838
Johannes Berg134e6372009-07-10 09:51:34 +00001839 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001840}
1841
Michael Wu66f7ac52008-01-31 19:48:22 +01001842static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1843 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1844 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1845 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1846 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1847 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1848};
1849
1850static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1851{
1852 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1853 int flag;
1854
1855 *mntrflags = 0;
1856
1857 if (!nla)
1858 return -EINVAL;
1859
1860 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1861 nla, mntr_flags_policy))
1862 return -EINVAL;
1863
1864 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1865 if (flags[flag])
1866 *mntrflags |= (1<<flag);
1867
1868 return 0;
1869}
1870
Johannes Berg9bc383d2009-11-19 11:55:19 +01001871static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001872 struct net_device *netdev, u8 use_4addr,
1873 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001874{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001875 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001876 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001877 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001878 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001879 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001880
1881 switch (iftype) {
1882 case NL80211_IFTYPE_AP_VLAN:
1883 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1884 return 0;
1885 break;
1886 case NL80211_IFTYPE_STATION:
1887 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1888 return 0;
1889 break;
1890 default:
1891 break;
1892 }
1893
1894 return -EOPNOTSUPP;
1895}
1896
Johannes Berg55682962007-09-20 13:09:35 -04001897static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1898{
Johannes Berg4c476992010-10-04 21:36:35 +02001899 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001900 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001901 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001902 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001903 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001904 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001905 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001906
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001907 memset(&params, 0, sizeof(params));
1908
Johannes Berg04a773a2009-04-19 21:24:32 +02001909 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001910
Johannes Berg723b0382008-09-16 20:22:09 +02001911 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001912 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001913 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001914 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001915 if (ntype > NL80211_IFTYPE_MAX)
1916 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001917 }
1918
Johannes Berg92ffe052008-09-16 20:39:36 +02001919 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001920 struct wireless_dev *wdev = dev->ieee80211_ptr;
1921
Johannes Berg4c476992010-10-04 21:36:35 +02001922 if (ntype != NL80211_IFTYPE_MESH_POINT)
1923 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001924 if (netif_running(dev))
1925 return -EBUSY;
1926
1927 wdev_lock(wdev);
1928 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1929 IEEE80211_MAX_MESH_ID_LEN);
1930 wdev->mesh_id_up_len =
1931 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1932 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1933 wdev->mesh_id_up_len);
1934 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001935 }
1936
Felix Fietkau8b787642009-11-10 18:53:10 +01001937 if (info->attrs[NL80211_ATTR_4ADDR]) {
1938 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1939 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001940 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001941 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001942 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001943 } else {
1944 params.use_4addr = -1;
1945 }
1946
Johannes Berg92ffe052008-09-16 20:39:36 +02001947 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001948 if (ntype != NL80211_IFTYPE_MONITOR)
1949 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001950 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1951 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001952 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001953 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001954
1955 flags = &_flags;
1956 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001957 }
Johannes Berg3b858752009-03-12 09:55:09 +01001958
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001959 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001960 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001961 else
1962 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001963
Johannes Berg9bc383d2009-11-19 11:55:19 +01001964 if (!err && params.use_4addr != -1)
1965 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1966
Johannes Berg55682962007-09-20 13:09:35 -04001967 return err;
1968}
1969
1970static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1971{
Johannes Berg4c476992010-10-04 21:36:35 +02001972 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001973 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001974 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001975 int err;
1976 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001977 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001978
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001979 memset(&params, 0, sizeof(params));
1980
Johannes Berg55682962007-09-20 13:09:35 -04001981 if (!info->attrs[NL80211_ATTR_IFNAME])
1982 return -EINVAL;
1983
1984 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1985 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1986 if (type > NL80211_IFTYPE_MAX)
1987 return -EINVAL;
1988 }
1989
Johannes Berg79c97e92009-07-07 03:56:12 +02001990 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001991 !(rdev->wiphy.interface_modes & (1 << type)))
1992 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001993
Johannes Berg9bc383d2009-11-19 11:55:19 +01001994 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001995 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001996 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001997 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001998 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001999 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002000
Michael Wu66f7ac52008-01-31 19:48:22 +01002001 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2002 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2003 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01002004 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01002005 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002006 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01002007 if (IS_ERR(dev))
2008 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002009
Johannes Berg29cbe682010-12-03 09:20:44 +01002010 if (type == NL80211_IFTYPE_MESH_POINT &&
2011 info->attrs[NL80211_ATTR_MESH_ID]) {
2012 struct wireless_dev *wdev = dev->ieee80211_ptr;
2013
2014 wdev_lock(wdev);
2015 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2016 IEEE80211_MAX_MESH_ID_LEN);
2017 wdev->mesh_id_up_len =
2018 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2019 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2020 wdev->mesh_id_up_len);
2021 wdev_unlock(wdev);
2022 }
2023
Johannes Bergf9e10ce2010-12-03 09:20:42 +01002024 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04002025}
2026
2027static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2028{
Johannes Berg4c476992010-10-04 21:36:35 +02002029 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2030 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002031
Johannes Berg4c476992010-10-04 21:36:35 +02002032 if (!rdev->ops->del_virtual_intf)
2033 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002034
Johannes Berg4c476992010-10-04 21:36:35 +02002035 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04002036}
2037
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002038static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2039{
2040 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2041 struct net_device *dev = info->user_ptr[1];
2042 u16 noack_map;
2043
2044 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2045 return -EINVAL;
2046
2047 if (!rdev->ops->set_noack_map)
2048 return -EOPNOTSUPP;
2049
2050 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2051
2052 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
2053}
2054
Johannes Berg41ade002007-12-19 02:03:29 +01002055struct get_key_cookie {
2056 struct sk_buff *msg;
2057 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002058 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002059};
2060
2061static void get_key_callback(void *c, struct key_params *params)
2062{
Johannes Bergb9454e82009-07-08 13:29:08 +02002063 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002064 struct get_key_cookie *cookie = c;
2065
David S. Miller9360ffd2012-03-29 04:41:26 -04002066 if ((params->key &&
2067 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2068 params->key_len, params->key)) ||
2069 (params->seq &&
2070 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2071 params->seq_len, params->seq)) ||
2072 (params->cipher &&
2073 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2074 params->cipher)))
2075 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002076
Johannes Bergb9454e82009-07-08 13:29:08 +02002077 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2078 if (!key)
2079 goto nla_put_failure;
2080
David S. Miller9360ffd2012-03-29 04:41:26 -04002081 if ((params->key &&
2082 nla_put(cookie->msg, NL80211_KEY_DATA,
2083 params->key_len, params->key)) ||
2084 (params->seq &&
2085 nla_put(cookie->msg, NL80211_KEY_SEQ,
2086 params->seq_len, params->seq)) ||
2087 (params->cipher &&
2088 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2089 params->cipher)))
2090 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002091
David S. Miller9360ffd2012-03-29 04:41:26 -04002092 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2093 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002094
2095 nla_nest_end(cookie->msg, key);
2096
Johannes Berg41ade002007-12-19 02:03:29 +01002097 return;
2098 nla_put_failure:
2099 cookie->error = 1;
2100}
2101
2102static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2103{
Johannes Berg4c476992010-10-04 21:36:35 +02002104 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002105 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002106 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002107 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002108 const u8 *mac_addr = NULL;
2109 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002110 struct get_key_cookie cookie = {
2111 .error = 0,
2112 };
2113 void *hdr;
2114 struct sk_buff *msg;
2115
2116 if (info->attrs[NL80211_ATTR_KEY_IDX])
2117 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2118
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002119 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002120 return -EINVAL;
2121
2122 if (info->attrs[NL80211_ATTR_MAC])
2123 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2124
Johannes Berge31b8212010-10-05 19:39:30 +02002125 pairwise = !!mac_addr;
2126 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2127 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2128 if (kt >= NUM_NL80211_KEYTYPES)
2129 return -EINVAL;
2130 if (kt != NL80211_KEYTYPE_GROUP &&
2131 kt != NL80211_KEYTYPE_PAIRWISE)
2132 return -EINVAL;
2133 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2134 }
2135
Johannes Berg4c476992010-10-04 21:36:35 +02002136 if (!rdev->ops->get_key)
2137 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002138
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002139 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002140 if (!msg)
2141 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002142
2143 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2144 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002145 if (IS_ERR(hdr))
2146 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002147
2148 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002149 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002150
David S. Miller9360ffd2012-03-29 04:41:26 -04002151 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2152 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2153 goto nla_put_failure;
2154 if (mac_addr &&
2155 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2156 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002157
Johannes Berge31b8212010-10-05 19:39:30 +02002158 if (pairwise && mac_addr &&
2159 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2160 return -ENOENT;
2161
2162 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
2163 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002164
2165 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002166 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002167
2168 if (cookie.error)
2169 goto nla_put_failure;
2170
2171 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002172 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002173
2174 nla_put_failure:
2175 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002176 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002177 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002178 return err;
2179}
2180
2181static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2182{
Johannes Berg4c476992010-10-04 21:36:35 +02002183 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002184 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002185 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002186 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002187
Johannes Bergb9454e82009-07-08 13:29:08 +02002188 err = nl80211_parse_key(info, &key);
2189 if (err)
2190 return err;
2191
2192 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002193 return -EINVAL;
2194
Johannes Bergb9454e82009-07-08 13:29:08 +02002195 /* only support setting default key */
2196 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002197 return -EINVAL;
2198
Johannes Bergfffd0932009-07-08 14:22:54 +02002199 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002200
2201 if (key.def) {
2202 if (!rdev->ops->set_default_key) {
2203 err = -EOPNOTSUPP;
2204 goto out;
2205 }
2206
2207 err = nl80211_key_allowed(dev->ieee80211_ptr);
2208 if (err)
2209 goto out;
2210
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002211 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2212 key.def_uni, key.def_multi);
2213
2214 if (err)
2215 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002216
Johannes Berg3d23e342009-09-29 23:27:28 +02002217#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002218 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002219#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002220 } else {
2221 if (key.def_uni || !key.def_multi) {
2222 err = -EINVAL;
2223 goto out;
2224 }
2225
2226 if (!rdev->ops->set_default_mgmt_key) {
2227 err = -EOPNOTSUPP;
2228 goto out;
2229 }
2230
2231 err = nl80211_key_allowed(dev->ieee80211_ptr);
2232 if (err)
2233 goto out;
2234
2235 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2236 dev, key.idx);
2237 if (err)
2238 goto out;
2239
2240#ifdef CONFIG_CFG80211_WEXT
2241 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2242#endif
2243 }
2244
2245 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002246 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002247
Johannes Berg41ade002007-12-19 02:03:29 +01002248 return err;
2249}
2250
2251static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2252{
Johannes Berg4c476992010-10-04 21:36:35 +02002253 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002254 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002255 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002256 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002257 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002258
Johannes Bergb9454e82009-07-08 13:29:08 +02002259 err = nl80211_parse_key(info, &key);
2260 if (err)
2261 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002262
Johannes Bergb9454e82009-07-08 13:29:08 +02002263 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002264 return -EINVAL;
2265
Johannes Berg41ade002007-12-19 02:03:29 +01002266 if (info->attrs[NL80211_ATTR_MAC])
2267 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2268
Johannes Berge31b8212010-10-05 19:39:30 +02002269 if (key.type == -1) {
2270 if (mac_addr)
2271 key.type = NL80211_KEYTYPE_PAIRWISE;
2272 else
2273 key.type = NL80211_KEYTYPE_GROUP;
2274 }
2275
2276 /* for now */
2277 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2278 key.type != NL80211_KEYTYPE_GROUP)
2279 return -EINVAL;
2280
Johannes Berg4c476992010-10-04 21:36:35 +02002281 if (!rdev->ops->add_key)
2282 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002283
Johannes Berge31b8212010-10-05 19:39:30 +02002284 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2285 key.type == NL80211_KEYTYPE_PAIRWISE,
2286 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002287 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002288
2289 wdev_lock(dev->ieee80211_ptr);
2290 err = nl80211_key_allowed(dev->ieee80211_ptr);
2291 if (!err)
2292 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002293 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002294 mac_addr, &key.p);
2295 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002296
Johannes Berg41ade002007-12-19 02:03:29 +01002297 return err;
2298}
2299
2300static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2301{
Johannes Berg4c476992010-10-04 21:36:35 +02002302 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002303 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002304 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002305 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002306 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002307
Johannes Bergb9454e82009-07-08 13:29:08 +02002308 err = nl80211_parse_key(info, &key);
2309 if (err)
2310 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002311
2312 if (info->attrs[NL80211_ATTR_MAC])
2313 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2314
Johannes Berge31b8212010-10-05 19:39:30 +02002315 if (key.type == -1) {
2316 if (mac_addr)
2317 key.type = NL80211_KEYTYPE_PAIRWISE;
2318 else
2319 key.type = NL80211_KEYTYPE_GROUP;
2320 }
2321
2322 /* for now */
2323 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2324 key.type != NL80211_KEYTYPE_GROUP)
2325 return -EINVAL;
2326
Johannes Berg4c476992010-10-04 21:36:35 +02002327 if (!rdev->ops->del_key)
2328 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002329
Johannes Bergfffd0932009-07-08 14:22:54 +02002330 wdev_lock(dev->ieee80211_ptr);
2331 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002332
2333 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2334 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2335 err = -ENOENT;
2336
Johannes Bergfffd0932009-07-08 14:22:54 +02002337 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002338 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2339 key.type == NL80211_KEYTYPE_PAIRWISE,
2340 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002341
Johannes Berg3d23e342009-09-29 23:27:28 +02002342#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002343 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002344 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002345 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002346 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002347 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2348 }
2349#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002350 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002351
Johannes Berg41ade002007-12-19 02:03:29 +01002352 return err;
2353}
2354
Johannes Berg88600202012-02-13 15:17:18 +01002355static int nl80211_parse_beacon(struct genl_info *info,
2356 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002357{
Johannes Berg88600202012-02-13 15:17:18 +01002358 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002359
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002360 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2361 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2362 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2363 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002364 return -EINVAL;
2365
Johannes Berg88600202012-02-13 15:17:18 +01002366 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002367
Johannes Berged1b6cc2007-12-19 02:03:32 +01002368 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002369 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2370 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2371 if (!bcn->head_len)
2372 return -EINVAL;
2373 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002374 }
2375
2376 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002377 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2378 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002379 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002380 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002381 }
2382
Johannes Berg4c476992010-10-04 21:36:35 +02002383 if (!haveinfo)
2384 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002385
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002386 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002387 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2388 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002389 }
2390
2391 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002392 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002393 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002394 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002395 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2396 }
2397
2398 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002399 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002400 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002401 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002402 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2403 }
2404
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002405 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002406 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002407 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002408 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002409 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2410 }
2411
Johannes Berg88600202012-02-13 15:17:18 +01002412 return 0;
2413}
2414
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002415static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2416 struct cfg80211_ap_settings *params)
2417{
2418 struct wireless_dev *wdev;
2419 bool ret = false;
2420
2421 mutex_lock(&rdev->devlist_mtx);
2422
Johannes Berg89a54e42012-06-15 14:33:17 +02002423 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002424 if (wdev->iftype != NL80211_IFTYPE_AP &&
2425 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2426 continue;
2427
2428 if (!wdev->preset_chan)
2429 continue;
2430
2431 params->channel = wdev->preset_chan;
2432 params->channel_type = wdev->preset_chantype;
2433 ret = true;
2434 break;
2435 }
2436
2437 mutex_unlock(&rdev->devlist_mtx);
2438
2439 return ret;
2440}
2441
Johannes Berg88600202012-02-13 15:17:18 +01002442static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2443{
2444 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2445 struct net_device *dev = info->user_ptr[1];
2446 struct wireless_dev *wdev = dev->ieee80211_ptr;
2447 struct cfg80211_ap_settings params;
2448 int err;
2449
2450 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2451 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2452 return -EOPNOTSUPP;
2453
2454 if (!rdev->ops->start_ap)
2455 return -EOPNOTSUPP;
2456
2457 if (wdev->beacon_interval)
2458 return -EALREADY;
2459
2460 memset(&params, 0, sizeof(params));
2461
2462 /* these are required for START_AP */
2463 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2464 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2465 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2466 return -EINVAL;
2467
2468 err = nl80211_parse_beacon(info, &params.beacon);
2469 if (err)
2470 return err;
2471
2472 params.beacon_interval =
2473 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2474 params.dtim_period =
2475 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2476
2477 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2478 if (err)
2479 return err;
2480
2481 /*
2482 * In theory, some of these attributes should be required here
2483 * but since they were not used when the command was originally
2484 * added, keep them optional for old user space programs to let
2485 * them continue to work with drivers that do not need the
2486 * additional information -- drivers must check!
2487 */
2488 if (info->attrs[NL80211_ATTR_SSID]) {
2489 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2490 params.ssid_len =
2491 nla_len(info->attrs[NL80211_ATTR_SSID]);
2492 if (params.ssid_len == 0 ||
2493 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2494 return -EINVAL;
2495 }
2496
2497 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2498 params.hidden_ssid = nla_get_u32(
2499 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2500 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2501 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2502 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2503 return -EINVAL;
2504 }
2505
2506 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2507
2508 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2509 params.auth_type = nla_get_u32(
2510 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2511 if (!nl80211_valid_auth_type(params.auth_type))
2512 return -EINVAL;
2513 } else
2514 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2515
2516 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2517 NL80211_MAX_NR_CIPHER_SUITES);
2518 if (err)
2519 return err;
2520
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302521 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2522 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2523 return -EOPNOTSUPP;
2524 params.inactivity_timeout = nla_get_u16(
2525 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2526 }
2527
Johannes Bergaa430da2012-05-16 23:50:18 +02002528 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2529 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2530
2531 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2532 !nl80211_valid_channel_type(info, &channel_type))
2533 return -EINVAL;
2534
2535 params.channel = rdev_freq_to_chan(rdev,
2536 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2537 channel_type);
2538 if (!params.channel)
2539 return -EINVAL;
2540 params.channel_type = channel_type;
2541 } else if (wdev->preset_chan) {
2542 params.channel = wdev->preset_chan;
2543 params.channel_type = wdev->preset_chantype;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002544 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002545 return -EINVAL;
2546
2547 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2548 params.channel_type))
2549 return -EINVAL;
2550
Michal Kaziore4e32452012-06-29 12:47:08 +02002551 mutex_lock(&rdev->devlist_mtx);
2552 err = cfg80211_can_use_chan(rdev, wdev, params.channel,
2553 CHAN_MODE_SHARED);
2554 mutex_unlock(&rdev->devlist_mtx);
2555
2556 if (err)
2557 return err;
2558
Johannes Berg88600202012-02-13 15:17:18 +01002559 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002560 if (!err) {
2561 wdev->preset_chan = params.channel;
2562 wdev->preset_chantype = params.channel_type;
Johannes Berg88600202012-02-13 15:17:18 +01002563 wdev->beacon_interval = params.beacon_interval;
Michal Kaziorf4489eb2012-06-29 12:46:58 +02002564 wdev->channel = params.channel;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002565 }
Johannes Berg56d18932011-05-09 18:41:15 +02002566 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002567}
2568
Johannes Berg88600202012-02-13 15:17:18 +01002569static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2570{
2571 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2572 struct net_device *dev = info->user_ptr[1];
2573 struct wireless_dev *wdev = dev->ieee80211_ptr;
2574 struct cfg80211_beacon_data params;
2575 int err;
2576
2577 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2578 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2579 return -EOPNOTSUPP;
2580
2581 if (!rdev->ops->change_beacon)
2582 return -EOPNOTSUPP;
2583
2584 if (!wdev->beacon_interval)
2585 return -EINVAL;
2586
2587 err = nl80211_parse_beacon(info, &params);
2588 if (err)
2589 return err;
2590
2591 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2592}
2593
2594static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002595{
Johannes Berg4c476992010-10-04 21:36:35 +02002596 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2597 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002598
Michal Kazior60771782012-06-29 12:46:56 +02002599 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002600}
2601
Johannes Berg5727ef12007-12-19 02:03:34 +01002602static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2603 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2604 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2605 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002606 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002607 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002608 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002609};
2610
Johannes Bergeccb8e82009-05-11 21:57:56 +03002611static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002612 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002613 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002614{
2615 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002616 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002617 int flag;
2618
Johannes Bergeccb8e82009-05-11 21:57:56 +03002619 /*
2620 * Try parsing the new attribute first so userspace
2621 * can specify both for older kernels.
2622 */
2623 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2624 if (nla) {
2625 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002626
Johannes Bergeccb8e82009-05-11 21:57:56 +03002627 sta_flags = nla_data(nla);
2628 params->sta_flags_mask = sta_flags->mask;
2629 params->sta_flags_set = sta_flags->set;
2630 if ((params->sta_flags_mask |
2631 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2632 return -EINVAL;
2633 return 0;
2634 }
2635
2636 /* if present, parse the old attribute */
2637
2638 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002639 if (!nla)
2640 return 0;
2641
2642 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2643 nla, sta_flags_policy))
2644 return -EINVAL;
2645
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002646 /*
2647 * Only allow certain flags for interface types so that
2648 * other attributes are silently ignored. Remember that
2649 * this is backward compatibility code with old userspace
2650 * and shouldn't be hit in other cases anyway.
2651 */
2652 switch (iftype) {
2653 case NL80211_IFTYPE_AP:
2654 case NL80211_IFTYPE_AP_VLAN:
2655 case NL80211_IFTYPE_P2P_GO:
2656 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2657 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2658 BIT(NL80211_STA_FLAG_WME) |
2659 BIT(NL80211_STA_FLAG_MFP);
2660 break;
2661 case NL80211_IFTYPE_P2P_CLIENT:
2662 case NL80211_IFTYPE_STATION:
2663 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2664 BIT(NL80211_STA_FLAG_TDLS_PEER);
2665 break;
2666 case NL80211_IFTYPE_MESH_POINT:
2667 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2668 BIT(NL80211_STA_FLAG_MFP) |
2669 BIT(NL80211_STA_FLAG_AUTHORIZED);
2670 default:
2671 return -EINVAL;
2672 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002673
Johannes Berg3383b5a2012-05-10 20:14:43 +02002674 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2675 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002676 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002677
Johannes Berg3383b5a2012-05-10 20:14:43 +02002678 /* no longer support new API additions in old API */
2679 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2680 return -EINVAL;
2681 }
2682 }
2683
Johannes Berg5727ef12007-12-19 02:03:34 +01002684 return 0;
2685}
2686
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002687static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2688 int attr)
2689{
2690 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002691 u32 bitrate;
2692 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002693
2694 rate = nla_nest_start(msg, attr);
2695 if (!rate)
2696 goto nla_put_failure;
2697
2698 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2699 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002700 /* report 16-bit bitrate only if we can */
2701 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002702 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002703 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2704 (bitrate_compat > 0 &&
2705 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002706 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2707 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2708 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2709 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2710 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2711 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2712 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002713
2714 nla_nest_end(msg, rate);
2715 return true;
2716
2717nla_put_failure:
2718 return false;
2719}
2720
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002721static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002722 int flags,
2723 struct cfg80211_registered_device *rdev,
2724 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002725 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002726{
2727 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002728 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002729
2730 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2731 if (!hdr)
2732 return -1;
2733
David S. Miller9360ffd2012-03-29 04:41:26 -04002734 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2735 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2736 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2737 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002738
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002739 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2740 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002741 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002742 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2743 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2744 sinfo->connected_time))
2745 goto nla_put_failure;
2746 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2747 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2748 sinfo->inactive_time))
2749 goto nla_put_failure;
2750 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2751 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2752 sinfo->rx_bytes))
2753 goto nla_put_failure;
2754 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2755 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2756 sinfo->tx_bytes))
2757 goto nla_put_failure;
2758 if ((sinfo->filled & STATION_INFO_LLID) &&
2759 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2760 goto nla_put_failure;
2761 if ((sinfo->filled & STATION_INFO_PLID) &&
2762 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2763 goto nla_put_failure;
2764 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2765 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2766 sinfo->plink_state))
2767 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002768 switch (rdev->wiphy.signal_type) {
2769 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002770 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2771 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2772 sinfo->signal))
2773 goto nla_put_failure;
2774 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2775 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2776 sinfo->signal_avg))
2777 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002778 break;
2779 default:
2780 break;
2781 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002782 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002783 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2784 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002785 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002786 }
2787 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2788 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2789 NL80211_STA_INFO_RX_BITRATE))
2790 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002791 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002792 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2793 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2794 sinfo->rx_packets))
2795 goto nla_put_failure;
2796 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2797 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2798 sinfo->tx_packets))
2799 goto nla_put_failure;
2800 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2801 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2802 sinfo->tx_retries))
2803 goto nla_put_failure;
2804 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2805 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2806 sinfo->tx_failed))
2807 goto nla_put_failure;
2808 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2809 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2810 sinfo->beacon_loss_count))
2811 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002812 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2813 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2814 if (!bss_param)
2815 goto nla_put_failure;
2816
David S. Miller9360ffd2012-03-29 04:41:26 -04002817 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2818 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2819 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2820 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2821 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2822 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2823 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2824 sinfo->bss_param.dtim_period) ||
2825 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2826 sinfo->bss_param.beacon_interval))
2827 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002828
2829 nla_nest_end(msg, bss_param);
2830 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002831 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2832 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2833 sizeof(struct nl80211_sta_flag_update),
2834 &sinfo->sta_flags))
2835 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002836 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2837 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2838 sinfo->t_offset))
2839 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002840 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002841
David S. Miller9360ffd2012-03-29 04:41:26 -04002842 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2843 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2844 sinfo->assoc_req_ies))
2845 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002846
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002847 return genlmsg_end(msg, hdr);
2848
2849 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002850 genlmsg_cancel(msg, hdr);
2851 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002852}
2853
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002854static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002855 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002856{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002857 struct station_info sinfo;
2858 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002859 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002860 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002861 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002862 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002863
Johannes Berg67748892010-10-04 21:14:06 +02002864 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2865 if (err)
2866 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002867
2868 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002869 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002870 goto out_err;
2871 }
2872
Johannes Bergbba95fe2008-07-29 13:22:51 +02002873 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002874 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002875 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2876 mac_addr, &sinfo);
2877 if (err == -ENOENT)
2878 break;
2879 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002880 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002881
2882 if (nl80211_send_station(skb,
2883 NETLINK_CB(cb->skb).pid,
2884 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002885 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002886 &sinfo) < 0)
2887 goto out;
2888
2889 sta_idx++;
2890 }
2891
2892
2893 out:
2894 cb->args[1] = sta_idx;
2895 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002896 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002897 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002898
2899 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002900}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002901
Johannes Berg5727ef12007-12-19 02:03:34 +01002902static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2903{
Johannes Berg4c476992010-10-04 21:36:35 +02002904 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2905 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002906 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002907 struct sk_buff *msg;
2908 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002909 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002910
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002911 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002912
2913 if (!info->attrs[NL80211_ATTR_MAC])
2914 return -EINVAL;
2915
2916 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2917
Johannes Berg4c476992010-10-04 21:36:35 +02002918 if (!rdev->ops->get_station)
2919 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002920
Johannes Berg79c97e92009-07-07 03:56:12 +02002921 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002922 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002923 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002924
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002925 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002926 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002927 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002928
2929 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04002930 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002931 nlmsg_free(msg);
2932 return -ENOBUFS;
2933 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002934
Johannes Berg4c476992010-10-04 21:36:35 +02002935 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002936}
2937
2938/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002939 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002940 */
Johannes Berg80b99892011-11-18 16:23:01 +01002941static struct net_device *get_vlan(struct genl_info *info,
2942 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01002943{
Johannes Berg463d0182009-07-14 00:33:35 +02002944 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01002945 struct net_device *v;
2946 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01002947
Johannes Berg80b99892011-11-18 16:23:01 +01002948 if (!vlanattr)
2949 return NULL;
2950
2951 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
2952 if (!v)
2953 return ERR_PTR(-ENODEV);
2954
2955 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
2956 ret = -EINVAL;
2957 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01002958 }
Johannes Berg80b99892011-11-18 16:23:01 +01002959
2960 if (!netif_running(v)) {
2961 ret = -ENETDOWN;
2962 goto error;
2963 }
2964
2965 return v;
2966 error:
2967 dev_put(v);
2968 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01002969}
2970
2971static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2972{
Johannes Berg4c476992010-10-04 21:36:35 +02002973 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002974 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002975 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002976 struct station_parameters params;
2977 u8 *mac_addr = NULL;
2978
2979 memset(&params, 0, sizeof(params));
2980
2981 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002982 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002983
2984 if (info->attrs[NL80211_ATTR_STA_AID])
2985 return -EINVAL;
2986
2987 if (!info->attrs[NL80211_ATTR_MAC])
2988 return -EINVAL;
2989
2990 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2991
2992 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2993 params.supported_rates =
2994 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2995 params.supported_rates_len =
2996 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2997 }
2998
2999 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3000 params.listen_interval =
3001 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3002
Jouni Malinen36aedc902008-08-25 11:58:58 +03003003 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3004 params.ht_capa =
3005 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3006
Johannes Bergbdd90d52011-12-14 12:20:27 +01003007 if (!rdev->ops->change_station)
3008 return -EOPNOTSUPP;
3009
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003010 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003011 return -EINVAL;
3012
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003013 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3014 params.plink_action =
3015 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3016
Javier Cardona9c3990a2011-05-03 16:57:11 -07003017 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3018 params.plink_state =
3019 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3020
Johannes Berga97f4422009-06-18 17:23:43 +02003021 switch (dev->ieee80211_ptr->iftype) {
3022 case NL80211_IFTYPE_AP:
3023 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003024 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003025 /* disallow mesh-specific things */
3026 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003027 return -EINVAL;
3028
3029 /* TDLS can't be set, ... */
3030 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3031 return -EINVAL;
3032 /*
3033 * ... but don't bother the driver with it. This works around
3034 * a hostapd/wpa_supplicant issue -- it always includes the
3035 * TLDS_PEER flag in the mask even for AP mode.
3036 */
3037 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3038
3039 /* accept only the listed bits */
3040 if (params.sta_flags_mask &
3041 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3042 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3043 BIT(NL80211_STA_FLAG_WME) |
3044 BIT(NL80211_STA_FLAG_MFP)))
3045 return -EINVAL;
3046
3047 /* must be last in here for error handling */
3048 params.vlan = get_vlan(info, rdev);
3049 if (IS_ERR(params.vlan))
3050 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003051 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003052 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003053 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003054 /*
3055 * Don't allow userspace to change the TDLS_PEER flag,
3056 * but silently ignore attempts to change it since we
3057 * don't have state here to verify that it doesn't try
3058 * to change the flag.
3059 */
3060 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003061 /* fall through */
3062 case NL80211_IFTYPE_ADHOC:
3063 /* disallow things sta doesn't support */
3064 if (params.plink_action)
3065 return -EINVAL;
3066 if (params.ht_capa)
3067 return -EINVAL;
3068 if (params.listen_interval >= 0)
3069 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003070 /* reject any changes other than AUTHORIZED */
3071 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3072 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003073 break;
3074 case NL80211_IFTYPE_MESH_POINT:
3075 /* disallow things mesh doesn't support */
3076 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003077 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003078 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003079 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003080 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003081 return -EINVAL;
3082 /*
3083 * No special handling for TDLS here -- the userspace
3084 * mesh code doesn't have this bug.
3085 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003086 if (params.sta_flags_mask &
3087 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003088 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003089 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003090 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003091 break;
3092 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003093 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003094 }
3095
Johannes Bergbdd90d52011-12-14 12:20:27 +01003096 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003097
Johannes Berg79c97e92009-07-07 03:56:12 +02003098 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003099
Johannes Berg5727ef12007-12-19 02:03:34 +01003100 if (params.vlan)
3101 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003102
Johannes Berg5727ef12007-12-19 02:03:34 +01003103 return err;
3104}
3105
Eliad Pellerc75786c2011-08-23 14:37:46 +03003106static struct nla_policy
3107nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3108 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3109 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3110};
3111
Johannes Berg5727ef12007-12-19 02:03:34 +01003112static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3113{
Johannes Berg4c476992010-10-04 21:36:35 +02003114 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003115 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003116 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003117 struct station_parameters params;
3118 u8 *mac_addr = NULL;
3119
3120 memset(&params, 0, sizeof(params));
3121
3122 if (!info->attrs[NL80211_ATTR_MAC])
3123 return -EINVAL;
3124
Johannes Berg5727ef12007-12-19 02:03:34 +01003125 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3126 return -EINVAL;
3127
3128 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3129 return -EINVAL;
3130
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003131 if (!info->attrs[NL80211_ATTR_STA_AID])
3132 return -EINVAL;
3133
Johannes Berg5727ef12007-12-19 02:03:34 +01003134 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3135 params.supported_rates =
3136 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3137 params.supported_rates_len =
3138 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3139 params.listen_interval =
3140 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003141
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003142 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3143 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3144 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003145
Jouni Malinen36aedc902008-08-25 11:58:58 +03003146 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3147 params.ht_capa =
3148 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003149
Javier Cardona96b78df2011-04-07 15:08:33 -07003150 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3151 params.plink_action =
3152 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3153
Johannes Bergbdd90d52011-12-14 12:20:27 +01003154 if (!rdev->ops->add_station)
3155 return -EOPNOTSUPP;
3156
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003157 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003158 return -EINVAL;
3159
Johannes Bergbdd90d52011-12-14 12:20:27 +01003160 switch (dev->ieee80211_ptr->iftype) {
3161 case NL80211_IFTYPE_AP:
3162 case NL80211_IFTYPE_AP_VLAN:
3163 case NL80211_IFTYPE_P2P_GO:
3164 /* parse WME attributes if sta is WME capable */
3165 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3166 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3167 info->attrs[NL80211_ATTR_STA_WME]) {
3168 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3169 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003170
Johannes Bergbdd90d52011-12-14 12:20:27 +01003171 nla = info->attrs[NL80211_ATTR_STA_WME];
3172 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3173 nl80211_sta_wme_policy);
3174 if (err)
3175 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003176
Johannes Bergbdd90d52011-12-14 12:20:27 +01003177 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3178 params.uapsd_queues =
3179 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3180 if (params.uapsd_queues &
3181 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3182 return -EINVAL;
3183
3184 if (tb[NL80211_STA_WME_MAX_SP])
3185 params.max_sp =
3186 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3187
3188 if (params.max_sp &
3189 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3190 return -EINVAL;
3191
3192 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3193 }
3194 /* TDLS peers cannot be added */
3195 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003196 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003197 /* but don't bother the driver with it */
3198 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003199
Johannes Bergbdd90d52011-12-14 12:20:27 +01003200 /* must be last in here for error handling */
3201 params.vlan = get_vlan(info, rdev);
3202 if (IS_ERR(params.vlan))
3203 return PTR_ERR(params.vlan);
3204 break;
3205 case NL80211_IFTYPE_MESH_POINT:
3206 /* TDLS peers cannot be added */
3207 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003208 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003209 break;
3210 case NL80211_IFTYPE_STATION:
3211 /* Only TDLS peers can be added */
3212 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3213 return -EINVAL;
3214 /* Can only add if TDLS ... */
3215 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3216 return -EOPNOTSUPP;
3217 /* ... with external setup is supported */
3218 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3219 return -EOPNOTSUPP;
3220 break;
3221 default:
3222 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003223 }
3224
Johannes Bergbdd90d52011-12-14 12:20:27 +01003225 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003226
Johannes Berg79c97e92009-07-07 03:56:12 +02003227 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003228
Johannes Berg5727ef12007-12-19 02:03:34 +01003229 if (params.vlan)
3230 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003231 return err;
3232}
3233
3234static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3235{
Johannes Berg4c476992010-10-04 21:36:35 +02003236 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3237 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003238 u8 *mac_addr = NULL;
3239
3240 if (info->attrs[NL80211_ATTR_MAC])
3241 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3242
Johannes Berge80cf852009-05-11 14:43:13 +02003243 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003244 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003245 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003246 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3247 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003248
Johannes Berg4c476992010-10-04 21:36:35 +02003249 if (!rdev->ops->del_station)
3250 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003251
Johannes Berg4c476992010-10-04 21:36:35 +02003252 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003253}
3254
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003255static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
3256 int flags, struct net_device *dev,
3257 u8 *dst, u8 *next_hop,
3258 struct mpath_info *pinfo)
3259{
3260 void *hdr;
3261 struct nlattr *pinfoattr;
3262
3263 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
3264 if (!hdr)
3265 return -1;
3266
David S. Miller9360ffd2012-03-29 04:41:26 -04003267 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3268 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3269 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3270 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3271 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003272
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003273 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3274 if (!pinfoattr)
3275 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003276 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3277 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3278 pinfo->frame_qlen))
3279 goto nla_put_failure;
3280 if (((pinfo->filled & MPATH_INFO_SN) &&
3281 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3282 ((pinfo->filled & MPATH_INFO_METRIC) &&
3283 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3284 pinfo->metric)) ||
3285 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3286 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3287 pinfo->exptime)) ||
3288 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3289 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3290 pinfo->flags)) ||
3291 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3292 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3293 pinfo->discovery_timeout)) ||
3294 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3295 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3296 pinfo->discovery_retries)))
3297 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003298
3299 nla_nest_end(msg, pinfoattr);
3300
3301 return genlmsg_end(msg, hdr);
3302
3303 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003304 genlmsg_cancel(msg, hdr);
3305 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003306}
3307
3308static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003309 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003310{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003311 struct mpath_info pinfo;
3312 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003313 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003314 u8 dst[ETH_ALEN];
3315 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003316 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003317 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003318
Johannes Berg67748892010-10-04 21:14:06 +02003319 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3320 if (err)
3321 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003322
3323 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003324 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003325 goto out_err;
3326 }
3327
Jouni Malineneec60b02009-03-20 21:21:19 +02003328 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3329 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003330 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003331 }
3332
Johannes Bergbba95fe2008-07-29 13:22:51 +02003333 while (1) {
3334 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3335 dst, next_hop, &pinfo);
3336 if (err == -ENOENT)
3337 break;
3338 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003339 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003340
3341 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
3342 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3343 netdev, dst, next_hop,
3344 &pinfo) < 0)
3345 goto out;
3346
3347 path_idx++;
3348 }
3349
3350
3351 out:
3352 cb->args[1] = path_idx;
3353 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003354 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003355 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003356 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003357}
3358
3359static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3360{
Johannes Berg4c476992010-10-04 21:36:35 +02003361 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003362 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003363 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003364 struct mpath_info pinfo;
3365 struct sk_buff *msg;
3366 u8 *dst = NULL;
3367 u8 next_hop[ETH_ALEN];
3368
3369 memset(&pinfo, 0, sizeof(pinfo));
3370
3371 if (!info->attrs[NL80211_ATTR_MAC])
3372 return -EINVAL;
3373
3374 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3375
Johannes Berg4c476992010-10-04 21:36:35 +02003376 if (!rdev->ops->get_mpath)
3377 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003378
Johannes Berg4c476992010-10-04 21:36:35 +02003379 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3380 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003381
Johannes Berg79c97e92009-07-07 03:56:12 +02003382 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003383 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003384 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003385
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003386 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003387 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003388 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003389
3390 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003391 dev, dst, next_hop, &pinfo) < 0) {
3392 nlmsg_free(msg);
3393 return -ENOBUFS;
3394 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003395
Johannes Berg4c476992010-10-04 21:36:35 +02003396 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003397}
3398
3399static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3400{
Johannes Berg4c476992010-10-04 21:36:35 +02003401 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3402 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003403 u8 *dst = NULL;
3404 u8 *next_hop = NULL;
3405
3406 if (!info->attrs[NL80211_ATTR_MAC])
3407 return -EINVAL;
3408
3409 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3410 return -EINVAL;
3411
3412 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3413 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3414
Johannes Berg4c476992010-10-04 21:36:35 +02003415 if (!rdev->ops->change_mpath)
3416 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003417
Johannes Berg4c476992010-10-04 21:36:35 +02003418 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3419 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003420
Johannes Berg4c476992010-10-04 21:36:35 +02003421 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003422}
Johannes Berg4c476992010-10-04 21:36:35 +02003423
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003424static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3425{
Johannes Berg4c476992010-10-04 21:36:35 +02003426 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3427 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003428 u8 *dst = NULL;
3429 u8 *next_hop = NULL;
3430
3431 if (!info->attrs[NL80211_ATTR_MAC])
3432 return -EINVAL;
3433
3434 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3435 return -EINVAL;
3436
3437 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3438 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3439
Johannes Berg4c476992010-10-04 21:36:35 +02003440 if (!rdev->ops->add_mpath)
3441 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003442
Johannes Berg4c476992010-10-04 21:36:35 +02003443 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3444 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003445
Johannes Berg4c476992010-10-04 21:36:35 +02003446 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003447}
3448
3449static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3450{
Johannes Berg4c476992010-10-04 21:36:35 +02003451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3452 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003453 u8 *dst = NULL;
3454
3455 if (info->attrs[NL80211_ATTR_MAC])
3456 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3457
Johannes Berg4c476992010-10-04 21:36:35 +02003458 if (!rdev->ops->del_mpath)
3459 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003460
Johannes Berg4c476992010-10-04 21:36:35 +02003461 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003462}
3463
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003464static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3465{
Johannes Berg4c476992010-10-04 21:36:35 +02003466 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3467 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003468 struct bss_parameters params;
3469
3470 memset(&params, 0, sizeof(params));
3471 /* default to not changing parameters */
3472 params.use_cts_prot = -1;
3473 params.use_short_preamble = -1;
3474 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003475 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003476 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003477
3478 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3479 params.use_cts_prot =
3480 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3481 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3482 params.use_short_preamble =
3483 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3484 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3485 params.use_short_slot_time =
3486 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003487 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3488 params.basic_rates =
3489 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3490 params.basic_rates_len =
3491 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3492 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003493 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3494 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003495 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3496 params.ht_opmode =
3497 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003498
Johannes Berg4c476992010-10-04 21:36:35 +02003499 if (!rdev->ops->change_bss)
3500 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003501
Johannes Berg074ac8d2010-09-16 14:58:22 +02003502 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003503 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3504 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003505
Johannes Berg4c476992010-10-04 21:36:35 +02003506 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003507}
3508
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003509static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003510 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3511 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3512 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3513 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3514 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3515 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3516};
3517
3518static int parse_reg_rule(struct nlattr *tb[],
3519 struct ieee80211_reg_rule *reg_rule)
3520{
3521 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3522 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3523
3524 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3525 return -EINVAL;
3526 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3527 return -EINVAL;
3528 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3529 return -EINVAL;
3530 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3531 return -EINVAL;
3532 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3533 return -EINVAL;
3534
3535 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3536
3537 freq_range->start_freq_khz =
3538 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3539 freq_range->end_freq_khz =
3540 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3541 freq_range->max_bandwidth_khz =
3542 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3543
3544 power_rule->max_eirp =
3545 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3546
3547 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3548 power_rule->max_antenna_gain =
3549 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3550
3551 return 0;
3552}
3553
3554static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3555{
3556 int r;
3557 char *data = NULL;
3558
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003559 /*
3560 * You should only get this when cfg80211 hasn't yet initialized
3561 * completely when built-in to the kernel right between the time
3562 * window between nl80211_init() and regulatory_init(), if that is
3563 * even possible.
3564 */
3565 mutex_lock(&cfg80211_mutex);
3566 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003567 mutex_unlock(&cfg80211_mutex);
3568 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003569 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003570 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003571
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003572 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3573 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003574
3575 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3576
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003577 r = regulatory_hint_user(data);
3578
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003579 return r;
3580}
3581
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003582static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003583 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003584{
Johannes Berg4c476992010-10-04 21:36:35 +02003585 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003586 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003587 struct wireless_dev *wdev = dev->ieee80211_ptr;
3588 struct mesh_config cur_params;
3589 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003590 void *hdr;
3591 struct nlattr *pinfoattr;
3592 struct sk_buff *msg;
3593
Johannes Berg29cbe682010-12-03 09:20:44 +01003594 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3595 return -EOPNOTSUPP;
3596
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003597 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003598 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003599
Johannes Berg29cbe682010-12-03 09:20:44 +01003600 wdev_lock(wdev);
3601 /* If not connected, get default parameters */
3602 if (!wdev->mesh_id_len)
3603 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3604 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003605 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003606 &cur_params);
3607 wdev_unlock(wdev);
3608
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003609 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003610 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003611
3612 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003613 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003614 if (!msg)
3615 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003616 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003617 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003618 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003619 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003620 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003621 if (!pinfoattr)
3622 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003623 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3624 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3625 cur_params.dot11MeshRetryTimeout) ||
3626 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3627 cur_params.dot11MeshConfirmTimeout) ||
3628 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3629 cur_params.dot11MeshHoldingTimeout) ||
3630 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3631 cur_params.dot11MeshMaxPeerLinks) ||
3632 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3633 cur_params.dot11MeshMaxRetries) ||
3634 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3635 cur_params.dot11MeshTTL) ||
3636 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3637 cur_params.element_ttl) ||
3638 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3639 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003640 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3641 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003642 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3643 cur_params.dot11MeshHWMPmaxPREQretries) ||
3644 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3645 cur_params.path_refresh_time) ||
3646 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3647 cur_params.min_discovery_timeout) ||
3648 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3649 cur_params.dot11MeshHWMPactivePathTimeout) ||
3650 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3651 cur_params.dot11MeshHWMPpreqMinInterval) ||
3652 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3653 cur_params.dot11MeshHWMPperrMinInterval) ||
3654 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3655 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3656 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3657 cur_params.dot11MeshHWMPRootMode) ||
3658 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3659 cur_params.dot11MeshHWMPRannInterval) ||
3660 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3661 cur_params.dot11MeshGateAnnouncementProtocol) ||
3662 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3663 cur_params.dot11MeshForwarding) ||
3664 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003665 cur_params.rssi_threshold) ||
3666 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003667 cur_params.ht_opmode) ||
3668 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3669 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3670 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003671 cur_params.dot11MeshHWMProotInterval) ||
3672 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3673 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003674 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003675 nla_nest_end(msg, pinfoattr);
3676 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003677 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003678
Johannes Berg3b858752009-03-12 09:55:09 +01003679 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003680 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003681 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003682 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003683 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003684}
3685
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003686static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003687 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3688 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3689 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3690 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3691 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3692 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003693 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003694 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003695 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003696 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3697 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3698 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3699 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3700 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003701 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003702 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003703 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003704 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003705 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003706 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003707 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3708 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003709 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3710 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003711 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003712};
3713
Javier Cardonac80d5452010-12-16 17:37:49 -08003714static const struct nla_policy
3715 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003716 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003717 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3718 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003719 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003720 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003721 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003722 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003723};
3724
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003725static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003726 struct mesh_config *cfg,
3727 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003728{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003729 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003730 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003731
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003732#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3733do {\
3734 if (table[attr_num]) {\
3735 cfg->param = nla_fn(table[attr_num]); \
3736 mask |= (1 << (attr_num - 1)); \
3737 } \
3738} while (0);\
3739
3740
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003741 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003742 return -EINVAL;
3743 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003744 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003745 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003746 return -EINVAL;
3747
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003748 /* This makes sure that there aren't more than 32 mesh config
3749 * parameters (otherwise our bitfield scheme would not work.) */
3750 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3751
3752 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003753 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003754 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3755 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003756 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003757 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3758 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003759 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003760 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3761 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003762 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003763 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3764 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003765 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003766 mask, NL80211_MESHCONF_MAX_RETRIES,
3767 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003768 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003769 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003770 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003771 mask, NL80211_MESHCONF_ELEMENT_TTL,
3772 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003773 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003774 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3775 nla_get_u8);
3776 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3777 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3778 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003779 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003780 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3781 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003782 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003783 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3784 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003785 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003786 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3787 nla_get_u16);
3788 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3789 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3790 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003791 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003792 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3793 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003794 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003795 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3796 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003797 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003798 dot11MeshHWMPnetDiameterTraversalTime, mask,
3799 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3800 nla_get_u16);
3801 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3802 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3803 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3804 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3805 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003806 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003807 dot11MeshGateAnnouncementProtocol, mask,
3808 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3809 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003810 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003811 mask, NL80211_MESHCONF_FORWARDING,
3812 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003813 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003814 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3815 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003816 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003817 mask, NL80211_MESHCONF_HT_OPMODE,
3818 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003819 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3820 mask,
3821 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3822 nla_get_u32);
3823 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3824 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3825 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003826 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3827 dot11MeshHWMPconfirmationInterval, mask,
3828 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3829 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003830 if (mask_out)
3831 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003832
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003833 return 0;
3834
3835#undef FILL_IN_MESH_PARAM_IF_SET
3836}
3837
Javier Cardonac80d5452010-12-16 17:37:49 -08003838static int nl80211_parse_mesh_setup(struct genl_info *info,
3839 struct mesh_setup *setup)
3840{
3841 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3842
3843 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3844 return -EINVAL;
3845 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3846 info->attrs[NL80211_ATTR_MESH_SETUP],
3847 nl80211_mesh_setup_params_policy))
3848 return -EINVAL;
3849
Javier Cardonad299a1f2012-03-31 11:31:33 -07003850 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3851 setup->sync_method =
3852 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3853 IEEE80211_SYNC_METHOD_VENDOR :
3854 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3855
Javier Cardonac80d5452010-12-16 17:37:49 -08003856 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3857 setup->path_sel_proto =
3858 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3859 IEEE80211_PATH_PROTOCOL_VENDOR :
3860 IEEE80211_PATH_PROTOCOL_HWMP;
3861
3862 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3863 setup->path_metric =
3864 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3865 IEEE80211_PATH_METRIC_VENDOR :
3866 IEEE80211_PATH_METRIC_AIRTIME;
3867
Javier Cardona581a8b02011-04-07 15:08:27 -07003868
3869 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003870 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003871 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003872 if (!is_valid_ie_attr(ieattr))
3873 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003874 setup->ie = nla_data(ieattr);
3875 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003876 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003877 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3878 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003879
3880 return 0;
3881}
3882
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003883static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003884 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003885{
3886 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3887 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003888 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003889 struct mesh_config cfg;
3890 u32 mask;
3891 int err;
3892
Johannes Berg29cbe682010-12-03 09:20:44 +01003893 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3894 return -EOPNOTSUPP;
3895
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003896 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003897 return -EOPNOTSUPP;
3898
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003899 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003900 if (err)
3901 return err;
3902
Johannes Berg29cbe682010-12-03 09:20:44 +01003903 wdev_lock(wdev);
3904 if (!wdev->mesh_id_len)
3905 err = -ENOLINK;
3906
3907 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003908 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003909 mask, &cfg);
3910
3911 wdev_unlock(wdev);
3912
3913 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003914}
3915
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003916static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3917{
3918 struct sk_buff *msg;
3919 void *hdr = NULL;
3920 struct nlattr *nl_reg_rules;
3921 unsigned int i;
3922 int err = -EINVAL;
3923
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003924 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003925
3926 if (!cfg80211_regdomain)
3927 goto out;
3928
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003929 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003930 if (!msg) {
3931 err = -ENOBUFS;
3932 goto out;
3933 }
3934
3935 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3936 NL80211_CMD_GET_REG);
3937 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003938 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003939
David S. Miller9360ffd2012-03-29 04:41:26 -04003940 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
3941 cfg80211_regdomain->alpha2) ||
3942 (cfg80211_regdomain->dfs_region &&
3943 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
3944 cfg80211_regdomain->dfs_region)))
3945 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003946
3947 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3948 if (!nl_reg_rules)
3949 goto nla_put_failure;
3950
3951 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3952 struct nlattr *nl_reg_rule;
3953 const struct ieee80211_reg_rule *reg_rule;
3954 const struct ieee80211_freq_range *freq_range;
3955 const struct ieee80211_power_rule *power_rule;
3956
3957 reg_rule = &cfg80211_regdomain->reg_rules[i];
3958 freq_range = &reg_rule->freq_range;
3959 power_rule = &reg_rule->power_rule;
3960
3961 nl_reg_rule = nla_nest_start(msg, i);
3962 if (!nl_reg_rule)
3963 goto nla_put_failure;
3964
David S. Miller9360ffd2012-03-29 04:41:26 -04003965 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3966 reg_rule->flags) ||
3967 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
3968 freq_range->start_freq_khz) ||
3969 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
3970 freq_range->end_freq_khz) ||
3971 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3972 freq_range->max_bandwidth_khz) ||
3973 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3974 power_rule->max_antenna_gain) ||
3975 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3976 power_rule->max_eirp))
3977 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003978
3979 nla_nest_end(msg, nl_reg_rule);
3980 }
3981
3982 nla_nest_end(msg, nl_reg_rules);
3983
3984 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003985 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003986 goto out;
3987
3988nla_put_failure:
3989 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003990put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003991 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003992 err = -EMSGSIZE;
3993out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003994 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003995 return err;
3996}
3997
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003998static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3999{
4000 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4001 struct nlattr *nl_reg_rule;
4002 char *alpha2 = NULL;
4003 int rem_reg_rules = 0, r = 0;
4004 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004005 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004006 struct ieee80211_regdomain *rd = NULL;
4007
4008 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4009 return -EINVAL;
4010
4011 if (!info->attrs[NL80211_ATTR_REG_RULES])
4012 return -EINVAL;
4013
4014 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4015
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004016 if (info->attrs[NL80211_ATTR_DFS_REGION])
4017 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4018
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004019 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4020 rem_reg_rules) {
4021 num_rules++;
4022 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004023 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004024 }
4025
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004026 mutex_lock(&cfg80211_mutex);
4027
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004028 if (!reg_is_valid_request(alpha2)) {
4029 r = -EINVAL;
4030 goto bad_reg;
4031 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004032
4033 size_of_regd = sizeof(struct ieee80211_regdomain) +
4034 (num_rules * sizeof(struct ieee80211_reg_rule));
4035
4036 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004037 if (!rd) {
4038 r = -ENOMEM;
4039 goto bad_reg;
4040 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004041
4042 rd->n_reg_rules = num_rules;
4043 rd->alpha2[0] = alpha2[0];
4044 rd->alpha2[1] = alpha2[1];
4045
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004046 /*
4047 * Disable DFS master mode if the DFS region was
4048 * not supported or known on this kernel.
4049 */
4050 if (reg_supported_dfs_region(dfs_region))
4051 rd->dfs_region = dfs_region;
4052
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004053 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4054 rem_reg_rules) {
4055 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4056 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4057 reg_rule_policy);
4058 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4059 if (r)
4060 goto bad_reg;
4061
4062 rule_idx++;
4063
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004064 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4065 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004066 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004067 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004068 }
4069
4070 BUG_ON(rule_idx != num_rules);
4071
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004072 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004073
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004074 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004075
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004076 return r;
4077
Johannes Bergd2372b32008-10-24 20:32:20 +02004078 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004079 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004080 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004081 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004082}
4083
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004084static int validate_scan_freqs(struct nlattr *freqs)
4085{
4086 struct nlattr *attr1, *attr2;
4087 int n_channels = 0, tmp1, tmp2;
4088
4089 nla_for_each_nested(attr1, freqs, tmp1) {
4090 n_channels++;
4091 /*
4092 * Some hardware has a limited channel list for
4093 * scanning, and it is pretty much nonsensical
4094 * to scan for a channel twice, so disallow that
4095 * and don't require drivers to check that the
4096 * channel list they get isn't longer than what
4097 * they can scan, as long as they can scan all
4098 * the channels they registered at once.
4099 */
4100 nla_for_each_nested(attr2, freqs, tmp2)
4101 if (attr1 != attr2 &&
4102 nla_get_u32(attr1) == nla_get_u32(attr2))
4103 return 0;
4104 }
4105
4106 return n_channels;
4107}
4108
Johannes Berg2a519312009-02-10 21:25:55 +01004109static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4110{
Johannes Berg4c476992010-10-04 21:36:35 +02004111 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4112 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004113 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004114 struct nlattr *attr;
4115 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004116 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004117 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004118
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004119 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4120 return -EINVAL;
4121
Johannes Berg79c97e92009-07-07 03:56:12 +02004122 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004123
Johannes Berg4c476992010-10-04 21:36:35 +02004124 if (!rdev->ops->scan)
4125 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004126
Johannes Berg4c476992010-10-04 21:36:35 +02004127 if (rdev->scan_req)
4128 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004129
4130 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004131 n_channels = validate_scan_freqs(
4132 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004133 if (!n_channels)
4134 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004135 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004136 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004137 n_channels = 0;
4138
Johannes Berg2a519312009-02-10 21:25:55 +01004139 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4140 if (wiphy->bands[band])
4141 n_channels += wiphy->bands[band]->n_channels;
4142 }
4143
4144 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4145 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4146 n_ssids++;
4147
Johannes Berg4c476992010-10-04 21:36:35 +02004148 if (n_ssids > wiphy->max_scan_ssids)
4149 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004150
Jouni Malinen70692ad2009-02-16 19:39:13 +02004151 if (info->attrs[NL80211_ATTR_IE])
4152 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4153 else
4154 ie_len = 0;
4155
Johannes Berg4c476992010-10-04 21:36:35 +02004156 if (ie_len > wiphy->max_scan_ie_len)
4157 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004158
Johannes Berg2a519312009-02-10 21:25:55 +01004159 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004160 + sizeof(*request->ssids) * n_ssids
4161 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004162 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004163 if (!request)
4164 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004165
Johannes Berg2a519312009-02-10 21:25:55 +01004166 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004167 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004168 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004169 if (ie_len) {
4170 if (request->ssids)
4171 request->ie = (void *)(request->ssids + n_ssids);
4172 else
4173 request->ie = (void *)(request->channels + n_channels);
4174 }
Johannes Berg2a519312009-02-10 21:25:55 +01004175
Johannes Berg584991d2009-11-02 13:32:03 +01004176 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004177 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4178 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004179 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004180 struct ieee80211_channel *chan;
4181
4182 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4183
4184 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004185 err = -EINVAL;
4186 goto out_free;
4187 }
Johannes Berg584991d2009-11-02 13:32:03 +01004188
4189 /* ignore disabled channels */
4190 if (chan->flags & IEEE80211_CHAN_DISABLED)
4191 continue;
4192
4193 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004194 i++;
4195 }
4196 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004197 enum ieee80211_band band;
4198
Johannes Berg2a519312009-02-10 21:25:55 +01004199 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004200 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4201 int j;
4202 if (!wiphy->bands[band])
4203 continue;
4204 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004205 struct ieee80211_channel *chan;
4206
4207 chan = &wiphy->bands[band]->channels[j];
4208
4209 if (chan->flags & IEEE80211_CHAN_DISABLED)
4210 continue;
4211
4212 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004213 i++;
4214 }
4215 }
4216 }
4217
Johannes Berg584991d2009-11-02 13:32:03 +01004218 if (!i) {
4219 err = -EINVAL;
4220 goto out_free;
4221 }
4222
4223 request->n_channels = i;
4224
Johannes Berg2a519312009-02-10 21:25:55 +01004225 i = 0;
4226 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4227 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004228 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004229 err = -EINVAL;
4230 goto out_free;
4231 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004232 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004233 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004234 i++;
4235 }
4236 }
4237
Jouni Malinen70692ad2009-02-16 19:39:13 +02004238 if (info->attrs[NL80211_ATTR_IE]) {
4239 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004240 memcpy((void *)request->ie,
4241 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004242 request->ie_len);
4243 }
4244
Johannes Berg34850ab2011-07-18 18:08:35 +02004245 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004246 if (wiphy->bands[i])
4247 request->rates[i] =
4248 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004249
4250 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4251 nla_for_each_nested(attr,
4252 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4253 tmp) {
4254 enum ieee80211_band band = nla_type(attr);
4255
Dan Carpenter84404622011-07-29 11:52:18 +03004256 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004257 err = -EINVAL;
4258 goto out_free;
4259 }
4260 err = ieee80211_get_ratemask(wiphy->bands[band],
4261 nla_data(attr),
4262 nla_len(attr),
4263 &request->rates[band]);
4264 if (err)
4265 goto out_free;
4266 }
4267 }
4268
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304269 request->no_cck =
4270 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4271
Johannes Berg463d0182009-07-14 00:33:35 +02004272 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004273 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004274
Johannes Berg79c97e92009-07-07 03:56:12 +02004275 rdev->scan_req = request;
4276 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004277
Johannes Berg463d0182009-07-14 00:33:35 +02004278 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02004279 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02004280 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02004281 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004282 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004283 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004284 kfree(request);
4285 }
Johannes Berg3b858752009-03-12 09:55:09 +01004286
Johannes Berg2a519312009-02-10 21:25:55 +01004287 return err;
4288}
4289
Luciano Coelho807f8a82011-05-11 17:09:35 +03004290static int nl80211_start_sched_scan(struct sk_buff *skb,
4291 struct genl_info *info)
4292{
4293 struct cfg80211_sched_scan_request *request;
4294 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4295 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004296 struct nlattr *attr;
4297 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004298 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004299 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004300 enum ieee80211_band band;
4301 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004302 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004303
4304 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4305 !rdev->ops->sched_scan_start)
4306 return -EOPNOTSUPP;
4307
4308 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4309 return -EINVAL;
4310
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004311 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4312 return -EINVAL;
4313
4314 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4315 if (interval == 0)
4316 return -EINVAL;
4317
Luciano Coelho807f8a82011-05-11 17:09:35 +03004318 wiphy = &rdev->wiphy;
4319
4320 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4321 n_channels = validate_scan_freqs(
4322 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4323 if (!n_channels)
4324 return -EINVAL;
4325 } else {
4326 n_channels = 0;
4327
4328 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4329 if (wiphy->bands[band])
4330 n_channels += wiphy->bands[band]->n_channels;
4331 }
4332
4333 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4334 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4335 tmp)
4336 n_ssids++;
4337
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004338 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004339 return -EINVAL;
4340
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004341 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4342 nla_for_each_nested(attr,
4343 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4344 tmp)
4345 n_match_sets++;
4346
4347 if (n_match_sets > wiphy->max_match_sets)
4348 return -EINVAL;
4349
Luciano Coelho807f8a82011-05-11 17:09:35 +03004350 if (info->attrs[NL80211_ATTR_IE])
4351 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4352 else
4353 ie_len = 0;
4354
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004355 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004356 return -EINVAL;
4357
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004358 mutex_lock(&rdev->sched_scan_mtx);
4359
4360 if (rdev->sched_scan_req) {
4361 err = -EINPROGRESS;
4362 goto out;
4363 }
4364
Luciano Coelho807f8a82011-05-11 17:09:35 +03004365 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004366 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004367 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004368 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004369 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004370 if (!request) {
4371 err = -ENOMEM;
4372 goto out;
4373 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004374
4375 if (n_ssids)
4376 request->ssids = (void *)&request->channels[n_channels];
4377 request->n_ssids = n_ssids;
4378 if (ie_len) {
4379 if (request->ssids)
4380 request->ie = (void *)(request->ssids + n_ssids);
4381 else
4382 request->ie = (void *)(request->channels + n_channels);
4383 }
4384
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004385 if (n_match_sets) {
4386 if (request->ie)
4387 request->match_sets = (void *)(request->ie + ie_len);
4388 else if (request->ssids)
4389 request->match_sets =
4390 (void *)(request->ssids + n_ssids);
4391 else
4392 request->match_sets =
4393 (void *)(request->channels + n_channels);
4394 }
4395 request->n_match_sets = n_match_sets;
4396
Luciano Coelho807f8a82011-05-11 17:09:35 +03004397 i = 0;
4398 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4399 /* user specified, bail out if channel not found */
4400 nla_for_each_nested(attr,
4401 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4402 tmp) {
4403 struct ieee80211_channel *chan;
4404
4405 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4406
4407 if (!chan) {
4408 err = -EINVAL;
4409 goto out_free;
4410 }
4411
4412 /* ignore disabled channels */
4413 if (chan->flags & IEEE80211_CHAN_DISABLED)
4414 continue;
4415
4416 request->channels[i] = chan;
4417 i++;
4418 }
4419 } else {
4420 /* all channels */
4421 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4422 int j;
4423 if (!wiphy->bands[band])
4424 continue;
4425 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4426 struct ieee80211_channel *chan;
4427
4428 chan = &wiphy->bands[band]->channels[j];
4429
4430 if (chan->flags & IEEE80211_CHAN_DISABLED)
4431 continue;
4432
4433 request->channels[i] = chan;
4434 i++;
4435 }
4436 }
4437 }
4438
4439 if (!i) {
4440 err = -EINVAL;
4441 goto out_free;
4442 }
4443
4444 request->n_channels = i;
4445
4446 i = 0;
4447 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4448 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4449 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004450 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004451 err = -EINVAL;
4452 goto out_free;
4453 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004454 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004455 memcpy(request->ssids[i].ssid, nla_data(attr),
4456 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004457 i++;
4458 }
4459 }
4460
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004461 i = 0;
4462 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4463 nla_for_each_nested(attr,
4464 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4465 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004466 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004467
4468 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4469 nla_data(attr), nla_len(attr),
4470 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004471 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004472 if (ssid) {
4473 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4474 err = -EINVAL;
4475 goto out_free;
4476 }
4477 memcpy(request->match_sets[i].ssid.ssid,
4478 nla_data(ssid), nla_len(ssid));
4479 request->match_sets[i].ssid.ssid_len =
4480 nla_len(ssid);
4481 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004482 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4483 if (rssi)
4484 request->rssi_thold = nla_get_u32(rssi);
4485 else
4486 request->rssi_thold =
4487 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004488 i++;
4489 }
4490 }
4491
Luciano Coelho807f8a82011-05-11 17:09:35 +03004492 if (info->attrs[NL80211_ATTR_IE]) {
4493 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4494 memcpy((void *)request->ie,
4495 nla_data(info->attrs[NL80211_ATTR_IE]),
4496 request->ie_len);
4497 }
4498
4499 request->dev = dev;
4500 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004501 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004502
4503 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4504 if (!err) {
4505 rdev->sched_scan_req = request;
4506 nl80211_send_sched_scan(rdev, dev,
4507 NL80211_CMD_START_SCHED_SCAN);
4508 goto out;
4509 }
4510
4511out_free:
4512 kfree(request);
4513out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004514 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004515 return err;
4516}
4517
4518static int nl80211_stop_sched_scan(struct sk_buff *skb,
4519 struct genl_info *info)
4520{
4521 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004522 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004523
4524 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4525 !rdev->ops->sched_scan_stop)
4526 return -EOPNOTSUPP;
4527
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004528 mutex_lock(&rdev->sched_scan_mtx);
4529 err = __cfg80211_stop_sched_scan(rdev, false);
4530 mutex_unlock(&rdev->sched_scan_mtx);
4531
4532 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004533}
4534
Johannes Berg9720bb32011-06-21 09:45:33 +02004535static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4536 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004537 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004538 struct wireless_dev *wdev,
4539 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004540{
Johannes Berg48ab9052009-07-10 18:42:31 +02004541 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004542 void *hdr;
4543 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004544
4545 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004546
Johannes Berg9720bb32011-06-21 09:45:33 +02004547 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004548 NL80211_CMD_NEW_SCAN_RESULTS);
4549 if (!hdr)
4550 return -1;
4551
Johannes Berg9720bb32011-06-21 09:45:33 +02004552 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4553
David S. Miller9360ffd2012-03-29 04:41:26 -04004554 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4555 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4556 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004557
4558 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4559 if (!bss)
4560 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004561 if ((!is_zero_ether_addr(res->bssid) &&
4562 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4563 (res->information_elements && res->len_information_elements &&
4564 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4565 res->len_information_elements,
4566 res->information_elements)) ||
4567 (res->beacon_ies && res->len_beacon_ies &&
4568 res->beacon_ies != res->information_elements &&
4569 nla_put(msg, NL80211_BSS_BEACON_IES,
4570 res->len_beacon_ies, res->beacon_ies)))
4571 goto nla_put_failure;
4572 if (res->tsf &&
4573 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4574 goto nla_put_failure;
4575 if (res->beacon_interval &&
4576 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4577 goto nla_put_failure;
4578 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4579 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4580 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4581 jiffies_to_msecs(jiffies - intbss->ts)))
4582 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004583
Johannes Berg77965c92009-02-18 18:45:06 +01004584 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004585 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004586 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4587 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004588 break;
4589 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004590 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4591 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004592 break;
4593 default:
4594 break;
4595 }
4596
Johannes Berg48ab9052009-07-10 18:42:31 +02004597 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004598 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004599 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004600 if (intbss == wdev->current_bss &&
4601 nla_put_u32(msg, NL80211_BSS_STATUS,
4602 NL80211_BSS_STATUS_ASSOCIATED))
4603 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004604 break;
4605 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004606 if (intbss == wdev->current_bss &&
4607 nla_put_u32(msg, NL80211_BSS_STATUS,
4608 NL80211_BSS_STATUS_IBSS_JOINED))
4609 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004610 break;
4611 default:
4612 break;
4613 }
4614
Johannes Berg2a519312009-02-10 21:25:55 +01004615 nla_nest_end(msg, bss);
4616
4617 return genlmsg_end(msg, hdr);
4618
4619 nla_put_failure:
4620 genlmsg_cancel(msg, hdr);
4621 return -EMSGSIZE;
4622}
4623
4624static int nl80211_dump_scan(struct sk_buff *skb,
4625 struct netlink_callback *cb)
4626{
Johannes Berg48ab9052009-07-10 18:42:31 +02004627 struct cfg80211_registered_device *rdev;
4628 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004629 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004630 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004631 int start = cb->args[1], idx = 0;
4632 int err;
4633
Johannes Berg67748892010-10-04 21:14:06 +02004634 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4635 if (err)
4636 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004637
Johannes Berg48ab9052009-07-10 18:42:31 +02004638 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004639
Johannes Berg48ab9052009-07-10 18:42:31 +02004640 wdev_lock(wdev);
4641 spin_lock_bh(&rdev->bss_lock);
4642 cfg80211_bss_expire(rdev);
4643
Johannes Berg9720bb32011-06-21 09:45:33 +02004644 cb->seq = rdev->bss_generation;
4645
Johannes Berg48ab9052009-07-10 18:42:31 +02004646 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004647 if (++idx <= start)
4648 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004649 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004650 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004651 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004652 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004653 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004654 }
4655 }
4656
Johannes Berg48ab9052009-07-10 18:42:31 +02004657 spin_unlock_bh(&rdev->bss_lock);
4658 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004659
4660 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004661 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004662
Johannes Berg67748892010-10-04 21:14:06 +02004663 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004664}
4665
Holger Schurig61fa7132009-11-11 12:25:40 +01004666static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
4667 int flags, struct net_device *dev,
4668 struct survey_info *survey)
4669{
4670 void *hdr;
4671 struct nlattr *infoattr;
4672
Holger Schurig61fa7132009-11-11 12:25:40 +01004673 hdr = nl80211hdr_put(msg, pid, seq, flags,
4674 NL80211_CMD_NEW_SURVEY_RESULTS);
4675 if (!hdr)
4676 return -ENOMEM;
4677
David S. Miller9360ffd2012-03-29 04:41:26 -04004678 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4679 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004680
4681 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4682 if (!infoattr)
4683 goto nla_put_failure;
4684
David S. Miller9360ffd2012-03-29 04:41:26 -04004685 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4686 survey->channel->center_freq))
4687 goto nla_put_failure;
4688
4689 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4690 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4691 goto nla_put_failure;
4692 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4693 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4694 goto nla_put_failure;
4695 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4696 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4697 survey->channel_time))
4698 goto nla_put_failure;
4699 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4700 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4701 survey->channel_time_busy))
4702 goto nla_put_failure;
4703 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4704 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4705 survey->channel_time_ext_busy))
4706 goto nla_put_failure;
4707 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4708 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4709 survey->channel_time_rx))
4710 goto nla_put_failure;
4711 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4712 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4713 survey->channel_time_tx))
4714 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004715
4716 nla_nest_end(msg, infoattr);
4717
4718 return genlmsg_end(msg, hdr);
4719
4720 nla_put_failure:
4721 genlmsg_cancel(msg, hdr);
4722 return -EMSGSIZE;
4723}
4724
4725static int nl80211_dump_survey(struct sk_buff *skb,
4726 struct netlink_callback *cb)
4727{
4728 struct survey_info survey;
4729 struct cfg80211_registered_device *dev;
4730 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004731 int survey_idx = cb->args[1];
4732 int res;
4733
Johannes Berg67748892010-10-04 21:14:06 +02004734 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4735 if (res)
4736 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004737
4738 if (!dev->ops->dump_survey) {
4739 res = -EOPNOTSUPP;
4740 goto out_err;
4741 }
4742
4743 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004744 struct ieee80211_channel *chan;
4745
Holger Schurig61fa7132009-11-11 12:25:40 +01004746 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4747 &survey);
4748 if (res == -ENOENT)
4749 break;
4750 if (res)
4751 goto out_err;
4752
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004753 /* Survey without a channel doesn't make sense */
4754 if (!survey.channel) {
4755 res = -EINVAL;
4756 goto out;
4757 }
4758
4759 chan = ieee80211_get_channel(&dev->wiphy,
4760 survey.channel->center_freq);
4761 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4762 survey_idx++;
4763 continue;
4764 }
4765
Holger Schurig61fa7132009-11-11 12:25:40 +01004766 if (nl80211_send_survey(skb,
4767 NETLINK_CB(cb->skb).pid,
4768 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4769 netdev,
4770 &survey) < 0)
4771 goto out;
4772 survey_idx++;
4773 }
4774
4775 out:
4776 cb->args[1] = survey_idx;
4777 res = skb->len;
4778 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004779 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004780 return res;
4781}
4782
Jouni Malinen255e7372009-03-20 21:21:17 +02004783static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4784{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004785 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004786}
4787
Samuel Ortizb23aa672009-07-01 21:26:54 +02004788static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4789{
4790 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4791 NL80211_WPA_VERSION_2));
4792}
4793
Jouni Malinen636a5d32009-03-19 13:39:22 +02004794static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4795{
Johannes Berg4c476992010-10-04 21:36:35 +02004796 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4797 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004798 struct ieee80211_channel *chan;
4799 const u8 *bssid, *ssid, *ie = NULL;
4800 int err, ssid_len, ie_len = 0;
4801 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004802 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004803 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004804
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004805 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4806 return -EINVAL;
4807
4808 if (!info->attrs[NL80211_ATTR_MAC])
4809 return -EINVAL;
4810
Jouni Malinen17780922009-03-27 20:52:47 +02004811 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4812 return -EINVAL;
4813
Johannes Berg19957bb2009-07-02 17:20:43 +02004814 if (!info->attrs[NL80211_ATTR_SSID])
4815 return -EINVAL;
4816
4817 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4818 return -EINVAL;
4819
Johannes Bergfffd0932009-07-08 14:22:54 +02004820 err = nl80211_parse_key(info, &key);
4821 if (err)
4822 return err;
4823
4824 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004825 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4826 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004827 if (!key.p.key || !key.p.key_len)
4828 return -EINVAL;
4829 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4830 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4831 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4832 key.p.key_len != WLAN_KEY_LEN_WEP104))
4833 return -EINVAL;
4834 if (key.idx > 4)
4835 return -EINVAL;
4836 } else {
4837 key.p.key_len = 0;
4838 key.p.key = NULL;
4839 }
4840
Johannes Bergafea0b72010-08-10 09:46:42 +02004841 if (key.idx >= 0) {
4842 int i;
4843 bool ok = false;
4844 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4845 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4846 ok = true;
4847 break;
4848 }
4849 }
Johannes Berg4c476992010-10-04 21:36:35 +02004850 if (!ok)
4851 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004852 }
4853
Johannes Berg4c476992010-10-04 21:36:35 +02004854 if (!rdev->ops->auth)
4855 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004856
Johannes Berg074ac8d2010-09-16 14:58:22 +02004857 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004858 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4859 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004860
Johannes Berg19957bb2009-07-02 17:20:43 +02004861 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004862 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004863 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004864 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4865 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004866
Johannes Berg19957bb2009-07-02 17:20:43 +02004867 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4868 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4869
4870 if (info->attrs[NL80211_ATTR_IE]) {
4871 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4872 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4873 }
4874
4875 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004876 if (!nl80211_valid_auth_type(auth_type))
4877 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004878
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004879 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4880
Johannes Berg95de8172012-01-20 13:55:25 +01004881 /*
4882 * Since we no longer track auth state, ignore
4883 * requests to only change local state.
4884 */
4885 if (local_state_change)
4886 return 0;
4887
Johannes Berg4c476992010-10-04 21:36:35 +02004888 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4889 ssid, ssid_len, ie, ie_len,
Johannes Berg95de8172012-01-20 13:55:25 +01004890 key.p.key, key.p.key_len, key.idx);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004891}
4892
Johannes Bergc0692b82010-08-27 14:26:53 +03004893static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4894 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004895 struct cfg80211_crypto_settings *settings,
4896 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004897{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004898 memset(settings, 0, sizeof(*settings));
4899
Samuel Ortizb23aa672009-07-01 21:26:54 +02004900 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4901
Johannes Bergc0692b82010-08-27 14:26:53 +03004902 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4903 u16 proto;
4904 proto = nla_get_u16(
4905 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4906 settings->control_port_ethertype = cpu_to_be16(proto);
4907 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4908 proto != ETH_P_PAE)
4909 return -EINVAL;
4910 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4911 settings->control_port_no_encrypt = true;
4912 } else
4913 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4914
Samuel Ortizb23aa672009-07-01 21:26:54 +02004915 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4916 void *data;
4917 int len, i;
4918
4919 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4920 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4921 settings->n_ciphers_pairwise = len / sizeof(u32);
4922
4923 if (len % sizeof(u32))
4924 return -EINVAL;
4925
Johannes Berg3dc27d22009-07-02 21:36:37 +02004926 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004927 return -EINVAL;
4928
4929 memcpy(settings->ciphers_pairwise, data, len);
4930
4931 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004932 if (!cfg80211_supported_cipher_suite(
4933 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004934 settings->ciphers_pairwise[i]))
4935 return -EINVAL;
4936 }
4937
4938 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4939 settings->cipher_group =
4940 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03004941 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
4942 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02004943 return -EINVAL;
4944 }
4945
4946 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4947 settings->wpa_versions =
4948 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4949 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4950 return -EINVAL;
4951 }
4952
4953 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4954 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03004955 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004956
4957 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4958 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4959 settings->n_akm_suites = len / sizeof(u32);
4960
4961 if (len % sizeof(u32))
4962 return -EINVAL;
4963
Jouni Malinen1b9ca022011-09-21 16:13:07 +03004964 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
4965 return -EINVAL;
4966
Samuel Ortizb23aa672009-07-01 21:26:54 +02004967 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004968 }
4969
4970 return 0;
4971}
4972
Jouni Malinen636a5d32009-03-19 13:39:22 +02004973static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4974{
Johannes Berg4c476992010-10-04 21:36:35 +02004975 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4976 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004977 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004978 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004979 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004980 int err, ssid_len, ie_len = 0;
4981 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08004982 u32 flags = 0;
4983 struct ieee80211_ht_cap *ht_capa = NULL;
4984 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004985
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004986 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4987 return -EINVAL;
4988
4989 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004990 !info->attrs[NL80211_ATTR_SSID] ||
4991 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004992 return -EINVAL;
4993
Johannes Berg4c476992010-10-04 21:36:35 +02004994 if (!rdev->ops->assoc)
4995 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004996
Johannes Berg074ac8d2010-09-16 14:58:22 +02004997 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004998 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4999 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005000
Johannes Berg19957bb2009-07-02 17:20:43 +02005001 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005002
Johannes Berg19957bb2009-07-02 17:20:43 +02005003 chan = ieee80211_get_channel(&rdev->wiphy,
5004 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005005 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5006 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005007
Johannes Berg19957bb2009-07-02 17:20:43 +02005008 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5009 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005010
5011 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005012 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5013 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005014 }
5015
Jouni Malinendc6382c2009-05-06 22:09:37 +03005016 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005017 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03005018 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005019 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005020 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005021 else if (mfp != NL80211_MFP_NO)
5022 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03005023 }
5024
Johannes Berg3e5d7642009-07-07 14:37:26 +02005025 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5026 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5027
Ben Greear7e7c8922011-11-18 11:31:59 -08005028 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5029 flags |= ASSOC_REQ_DISABLE_HT;
5030
5031 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5032 ht_capa_mask =
5033 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5034
5035 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5036 if (!ht_capa_mask)
5037 return -EINVAL;
5038 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5039 }
5040
Johannes Bergc0692b82010-08-27 14:26:53 +03005041 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005042 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005043 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5044 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005045 &crypto, flags, ht_capa,
5046 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005047
Jouni Malinen636a5d32009-03-19 13:39:22 +02005048 return err;
5049}
5050
5051static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5052{
Johannes Berg4c476992010-10-04 21:36:35 +02005053 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5054 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005055 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005056 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005057 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005058 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005059
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005060 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5061 return -EINVAL;
5062
5063 if (!info->attrs[NL80211_ATTR_MAC])
5064 return -EINVAL;
5065
5066 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5067 return -EINVAL;
5068
Johannes Berg4c476992010-10-04 21:36:35 +02005069 if (!rdev->ops->deauth)
5070 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005071
Johannes Berg074ac8d2010-09-16 14:58:22 +02005072 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005073 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5074 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005075
Johannes Berg19957bb2009-07-02 17:20:43 +02005076 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005077
Johannes Berg19957bb2009-07-02 17:20:43 +02005078 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5079 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005080 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005081 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005082 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005083
5084 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005085 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5086 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005087 }
5088
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005089 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5090
Johannes Berg4c476992010-10-04 21:36:35 +02005091 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5092 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005093}
5094
5095static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5096{
Johannes Berg4c476992010-10-04 21:36:35 +02005097 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5098 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005099 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005100 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005101 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005102 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005103
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005104 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5105 return -EINVAL;
5106
5107 if (!info->attrs[NL80211_ATTR_MAC])
5108 return -EINVAL;
5109
5110 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5111 return -EINVAL;
5112
Johannes Berg4c476992010-10-04 21:36:35 +02005113 if (!rdev->ops->disassoc)
5114 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005115
Johannes Berg074ac8d2010-09-16 14:58:22 +02005116 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005117 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5118 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005119
Johannes Berg19957bb2009-07-02 17:20:43 +02005120 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005121
Johannes Berg19957bb2009-07-02 17:20:43 +02005122 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5123 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005124 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005125 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005126 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005127
5128 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005129 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5130 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005131 }
5132
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005133 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5134
Johannes Berg4c476992010-10-04 21:36:35 +02005135 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5136 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005137}
5138
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005139static bool
5140nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5141 int mcast_rate[IEEE80211_NUM_BANDS],
5142 int rateval)
5143{
5144 struct wiphy *wiphy = &rdev->wiphy;
5145 bool found = false;
5146 int band, i;
5147
5148 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5149 struct ieee80211_supported_band *sband;
5150
5151 sband = wiphy->bands[band];
5152 if (!sband)
5153 continue;
5154
5155 for (i = 0; i < sband->n_bitrates; i++) {
5156 if (sband->bitrates[i].bitrate == rateval) {
5157 mcast_rate[band] = i + 1;
5158 found = true;
5159 break;
5160 }
5161 }
5162 }
5163
5164 return found;
5165}
5166
Johannes Berg04a773a2009-04-19 21:24:32 +02005167static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5168{
Johannes Berg4c476992010-10-04 21:36:35 +02005169 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5170 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005171 struct cfg80211_ibss_params ibss;
5172 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005173 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005174 int err;
5175
Johannes Berg8e30bc52009-04-22 17:45:38 +02005176 memset(&ibss, 0, sizeof(ibss));
5177
Johannes Berg04a773a2009-04-19 21:24:32 +02005178 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5179 return -EINVAL;
5180
5181 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5182 !info->attrs[NL80211_ATTR_SSID] ||
5183 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5184 return -EINVAL;
5185
Johannes Berg8e30bc52009-04-22 17:45:38 +02005186 ibss.beacon_interval = 100;
5187
5188 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5189 ibss.beacon_interval =
5190 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5191 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5192 return -EINVAL;
5193 }
5194
Johannes Berg4c476992010-10-04 21:36:35 +02005195 if (!rdev->ops->join_ibss)
5196 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005197
Johannes Berg4c476992010-10-04 21:36:35 +02005198 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5199 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005200
Johannes Berg79c97e92009-07-07 03:56:12 +02005201 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005202
Johannes Berg39193492011-09-16 13:45:25 +02005203 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005204 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005205
5206 if (!is_valid_ether_addr(ibss.bssid))
5207 return -EINVAL;
5208 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005209 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5210 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5211
5212 if (info->attrs[NL80211_ATTR_IE]) {
5213 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5214 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5215 }
5216
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005217 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5218 enum nl80211_channel_type channel_type;
5219
Johannes Bergcd6c6592012-05-10 21:27:18 +02005220 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005221 return -EINVAL;
5222
5223 if (channel_type != NL80211_CHAN_NO_HT &&
5224 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5225 return -EINVAL;
5226
5227 ibss.channel_type = channel_type;
5228 } else {
5229 ibss.channel_type = NL80211_CHAN_NO_HT;
5230 }
5231
5232 ibss.channel = rdev_freq_to_chan(rdev,
5233 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5234 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005235 if (!ibss.channel ||
5236 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005237 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5238 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005239
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005240 /* Both channels should be able to initiate communication */
5241 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5242 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5243 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5244 ibss.channel_type))
5245 return -EINVAL;
5246
Johannes Berg04a773a2009-04-19 21:24:32 +02005247 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005248 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005249
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005250 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5251 u8 *rates =
5252 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5253 int n_rates =
5254 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5255 struct ieee80211_supported_band *sband =
5256 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005257
Johannes Berg34850ab2011-07-18 18:08:35 +02005258 err = ieee80211_get_ratemask(sband, rates, n_rates,
5259 &ibss.basic_rates);
5260 if (err)
5261 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005262 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005263
5264 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5265 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5266 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5267 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005268
Johannes Berg4c476992010-10-04 21:36:35 +02005269 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5270 connkeys = nl80211_parse_connkeys(rdev,
5271 info->attrs[NL80211_ATTR_KEYS]);
5272 if (IS_ERR(connkeys))
5273 return PTR_ERR(connkeys);
5274 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005275
Antonio Quartulli267335d2012-01-31 20:25:47 +01005276 ibss.control_port =
5277 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5278
Johannes Berg4c476992010-10-04 21:36:35 +02005279 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005280 if (err)
5281 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005282 return err;
5283}
5284
5285static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5286{
Johannes Berg4c476992010-10-04 21:36:35 +02005287 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5288 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005289
Johannes Berg4c476992010-10-04 21:36:35 +02005290 if (!rdev->ops->leave_ibss)
5291 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005292
Johannes Berg4c476992010-10-04 21:36:35 +02005293 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5294 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005295
Johannes Berg4c476992010-10-04 21:36:35 +02005296 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005297}
5298
Johannes Bergaff89a92009-07-01 21:26:51 +02005299#ifdef CONFIG_NL80211_TESTMODE
5300static struct genl_multicast_group nl80211_testmode_mcgrp = {
5301 .name = "testmode",
5302};
5303
5304static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5305{
Johannes Berg4c476992010-10-04 21:36:35 +02005306 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005307 int err;
5308
5309 if (!info->attrs[NL80211_ATTR_TESTDATA])
5310 return -EINVAL;
5311
Johannes Bergaff89a92009-07-01 21:26:51 +02005312 err = -EOPNOTSUPP;
5313 if (rdev->ops->testmode_cmd) {
5314 rdev->testmode_info = info;
5315 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5316 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5317 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5318 rdev->testmode_info = NULL;
5319 }
5320
Johannes Bergaff89a92009-07-01 21:26:51 +02005321 return err;
5322}
5323
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005324static int nl80211_testmode_dump(struct sk_buff *skb,
5325 struct netlink_callback *cb)
5326{
Johannes Berg00918d32011-12-13 17:22:05 +01005327 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005328 int err;
5329 long phy_idx;
5330 void *data = NULL;
5331 int data_len = 0;
5332
5333 if (cb->args[0]) {
5334 /*
5335 * 0 is a valid index, but not valid for args[0],
5336 * so we need to offset by 1.
5337 */
5338 phy_idx = cb->args[0] - 1;
5339 } else {
5340 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5341 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5342 nl80211_policy);
5343 if (err)
5344 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005345
Johannes Berg2bd7e352012-06-15 14:23:16 +02005346 mutex_lock(&cfg80211_mutex);
5347 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5348 nl80211_fam.attrbuf);
5349 if (IS_ERR(rdev)) {
5350 mutex_unlock(&cfg80211_mutex);
5351 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005352 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005353 phy_idx = rdev->wiphy_idx;
5354 rdev = NULL;
5355 mutex_unlock(&cfg80211_mutex);
5356
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005357 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5358 cb->args[1] =
5359 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5360 }
5361
5362 if (cb->args[1]) {
5363 data = nla_data((void *)cb->args[1]);
5364 data_len = nla_len((void *)cb->args[1]);
5365 }
5366
5367 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005368 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5369 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005370 mutex_unlock(&cfg80211_mutex);
5371 return -ENOENT;
5372 }
Johannes Berg00918d32011-12-13 17:22:05 +01005373 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005374 mutex_unlock(&cfg80211_mutex);
5375
Johannes Berg00918d32011-12-13 17:22:05 +01005376 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005377 err = -EOPNOTSUPP;
5378 goto out_err;
5379 }
5380
5381 while (1) {
5382 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
5383 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5384 NL80211_CMD_TESTMODE);
5385 struct nlattr *tmdata;
5386
David S. Miller9360ffd2012-03-29 04:41:26 -04005387 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005388 genlmsg_cancel(skb, hdr);
5389 break;
5390 }
5391
5392 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5393 if (!tmdata) {
5394 genlmsg_cancel(skb, hdr);
5395 break;
5396 }
Johannes Berg00918d32011-12-13 17:22:05 +01005397 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5398 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005399 nla_nest_end(skb, tmdata);
5400
5401 if (err == -ENOBUFS || err == -ENOENT) {
5402 genlmsg_cancel(skb, hdr);
5403 break;
5404 } else if (err) {
5405 genlmsg_cancel(skb, hdr);
5406 goto out_err;
5407 }
5408
5409 genlmsg_end(skb, hdr);
5410 }
5411
5412 err = skb->len;
5413 /* see above */
5414 cb->args[0] = phy_idx + 1;
5415 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005416 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005417 return err;
5418}
5419
Johannes Bergaff89a92009-07-01 21:26:51 +02005420static struct sk_buff *
5421__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
5422 int approxlen, u32 pid, u32 seq, gfp_t gfp)
5423{
5424 struct sk_buff *skb;
5425 void *hdr;
5426 struct nlattr *data;
5427
5428 skb = nlmsg_new(approxlen + 100, gfp);
5429 if (!skb)
5430 return NULL;
5431
5432 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
5433 if (!hdr) {
5434 kfree_skb(skb);
5435 return NULL;
5436 }
5437
David S. Miller9360ffd2012-03-29 04:41:26 -04005438 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5439 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005440 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5441
5442 ((void **)skb->cb)[0] = rdev;
5443 ((void **)skb->cb)[1] = hdr;
5444 ((void **)skb->cb)[2] = data;
5445
5446 return skb;
5447
5448 nla_put_failure:
5449 kfree_skb(skb);
5450 return NULL;
5451}
5452
5453struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5454 int approxlen)
5455{
5456 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5457
5458 if (WARN_ON(!rdev->testmode_info))
5459 return NULL;
5460
5461 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
5462 rdev->testmode_info->snd_pid,
5463 rdev->testmode_info->snd_seq,
5464 GFP_KERNEL);
5465}
5466EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5467
5468int cfg80211_testmode_reply(struct sk_buff *skb)
5469{
5470 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5471 void *hdr = ((void **)skb->cb)[1];
5472 struct nlattr *data = ((void **)skb->cb)[2];
5473
5474 if (WARN_ON(!rdev->testmode_info)) {
5475 kfree_skb(skb);
5476 return -EINVAL;
5477 }
5478
5479 nla_nest_end(skb, data);
5480 genlmsg_end(skb, hdr);
5481 return genlmsg_reply(skb, rdev->testmode_info);
5482}
5483EXPORT_SYMBOL(cfg80211_testmode_reply);
5484
5485struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5486 int approxlen, gfp_t gfp)
5487{
5488 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5489
5490 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5491}
5492EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5493
5494void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5495{
5496 void *hdr = ((void **)skb->cb)[1];
5497 struct nlattr *data = ((void **)skb->cb)[2];
5498
5499 nla_nest_end(skb, data);
5500 genlmsg_end(skb, hdr);
5501 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5502}
5503EXPORT_SYMBOL(cfg80211_testmode_event);
5504#endif
5505
Samuel Ortizb23aa672009-07-01 21:26:54 +02005506static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5507{
Johannes Berg4c476992010-10-04 21:36:35 +02005508 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5509 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005510 struct cfg80211_connect_params connect;
5511 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005512 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005513 int err;
5514
5515 memset(&connect, 0, sizeof(connect));
5516
5517 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5518 return -EINVAL;
5519
5520 if (!info->attrs[NL80211_ATTR_SSID] ||
5521 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5522 return -EINVAL;
5523
5524 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5525 connect.auth_type =
5526 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
5527 if (!nl80211_valid_auth_type(connect.auth_type))
5528 return -EINVAL;
5529 } else
5530 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5531
5532 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5533
Johannes Bergc0692b82010-08-27 14:26:53 +03005534 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005535 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005536 if (err)
5537 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005538
Johannes Berg074ac8d2010-09-16 14:58:22 +02005539 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005540 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5541 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005542
Johannes Berg79c97e92009-07-07 03:56:12 +02005543 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005544
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305545 connect.bg_scan_period = -1;
5546 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5547 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5548 connect.bg_scan_period =
5549 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5550 }
5551
Samuel Ortizb23aa672009-07-01 21:26:54 +02005552 if (info->attrs[NL80211_ATTR_MAC])
5553 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5554 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5555 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5556
5557 if (info->attrs[NL80211_ATTR_IE]) {
5558 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5559 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5560 }
5561
5562 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5563 connect.channel =
5564 ieee80211_get_channel(wiphy,
5565 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5566 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005567 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5568 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005569 }
5570
Johannes Bergfffd0932009-07-08 14:22:54 +02005571 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5572 connkeys = nl80211_parse_connkeys(rdev,
5573 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005574 if (IS_ERR(connkeys))
5575 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005576 }
5577
Ben Greear7e7c8922011-11-18 11:31:59 -08005578 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5579 connect.flags |= ASSOC_REQ_DISABLE_HT;
5580
5581 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5582 memcpy(&connect.ht_capa_mask,
5583 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5584 sizeof(connect.ht_capa_mask));
5585
5586 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5587 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5588 return -EINVAL;
5589 memcpy(&connect.ht_capa,
5590 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5591 sizeof(connect.ht_capa));
5592 }
5593
Johannes Bergfffd0932009-07-08 14:22:54 +02005594 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005595 if (err)
5596 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005597 return err;
5598}
5599
5600static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5601{
Johannes Berg4c476992010-10-04 21:36:35 +02005602 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5603 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005604 u16 reason;
5605
5606 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5607 reason = WLAN_REASON_DEAUTH_LEAVING;
5608 else
5609 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5610
5611 if (reason == 0)
5612 return -EINVAL;
5613
Johannes Berg074ac8d2010-09-16 14:58:22 +02005614 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005615 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5616 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005617
Johannes Berg4c476992010-10-04 21:36:35 +02005618 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005619}
5620
Johannes Berg463d0182009-07-14 00:33:35 +02005621static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5622{
Johannes Berg4c476992010-10-04 21:36:35 +02005623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005624 struct net *net;
5625 int err;
5626 u32 pid;
5627
5628 if (!info->attrs[NL80211_ATTR_PID])
5629 return -EINVAL;
5630
5631 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5632
Johannes Berg463d0182009-07-14 00:33:35 +02005633 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005634 if (IS_ERR(net))
5635 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005636
5637 err = 0;
5638
5639 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005640 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5641 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005642
Johannes Berg463d0182009-07-14 00:33:35 +02005643 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005644 return err;
5645}
5646
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005647static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5648{
Johannes Berg4c476992010-10-04 21:36:35 +02005649 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005650 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5651 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005652 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005653 struct cfg80211_pmksa pmksa;
5654
5655 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5656
5657 if (!info->attrs[NL80211_ATTR_MAC])
5658 return -EINVAL;
5659
5660 if (!info->attrs[NL80211_ATTR_PMKID])
5661 return -EINVAL;
5662
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005663 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5664 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5665
Johannes Berg074ac8d2010-09-16 14:58:22 +02005666 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005667 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5668 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005669
5670 switch (info->genlhdr->cmd) {
5671 case NL80211_CMD_SET_PMKSA:
5672 rdev_ops = rdev->ops->set_pmksa;
5673 break;
5674 case NL80211_CMD_DEL_PMKSA:
5675 rdev_ops = rdev->ops->del_pmksa;
5676 break;
5677 default:
5678 WARN_ON(1);
5679 break;
5680 }
5681
Johannes Berg4c476992010-10-04 21:36:35 +02005682 if (!rdev_ops)
5683 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005684
Johannes Berg4c476992010-10-04 21:36:35 +02005685 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005686}
5687
5688static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5689{
Johannes Berg4c476992010-10-04 21:36:35 +02005690 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5691 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005692
Johannes Berg074ac8d2010-09-16 14:58:22 +02005693 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005694 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5695 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005696
Johannes Berg4c476992010-10-04 21:36:35 +02005697 if (!rdev->ops->flush_pmksa)
5698 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005699
Johannes Berg4c476992010-10-04 21:36:35 +02005700 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005701}
5702
Arik Nemtsov109086c2011-09-28 14:12:50 +03005703static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5704{
5705 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5706 struct net_device *dev = info->user_ptr[1];
5707 u8 action_code, dialog_token;
5708 u16 status_code;
5709 u8 *peer;
5710
5711 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5712 !rdev->ops->tdls_mgmt)
5713 return -EOPNOTSUPP;
5714
5715 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5716 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5717 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5718 !info->attrs[NL80211_ATTR_IE] ||
5719 !info->attrs[NL80211_ATTR_MAC])
5720 return -EINVAL;
5721
5722 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5723 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5724 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5725 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5726
5727 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5728 dialog_token, status_code,
5729 nla_data(info->attrs[NL80211_ATTR_IE]),
5730 nla_len(info->attrs[NL80211_ATTR_IE]));
5731}
5732
5733static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5734{
5735 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5736 struct net_device *dev = info->user_ptr[1];
5737 enum nl80211_tdls_operation operation;
5738 u8 *peer;
5739
5740 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5741 !rdev->ops->tdls_oper)
5742 return -EOPNOTSUPP;
5743
5744 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5745 !info->attrs[NL80211_ATTR_MAC])
5746 return -EINVAL;
5747
5748 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5749 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5750
5751 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5752}
5753
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005754static int nl80211_remain_on_channel(struct sk_buff *skb,
5755 struct genl_info *info)
5756{
Johannes Berg4c476992010-10-04 21:36:35 +02005757 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005758 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005759 struct ieee80211_channel *chan;
5760 struct sk_buff *msg;
5761 void *hdr;
5762 u64 cookie;
5763 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5764 u32 freq, duration;
5765 int err;
5766
5767 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5768 !info->attrs[NL80211_ATTR_DURATION])
5769 return -EINVAL;
5770
5771 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5772
Johannes Berg7c4ef712011-11-18 15:33:48 +01005773 if (!rdev->ops->remain_on_channel ||
5774 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005775 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005776
Johannes Bergebf348f2012-06-01 12:50:54 +02005777 /*
5778 * We should be on that channel for at least a minimum amount of
5779 * time (10ms) but no longer than the driver supports.
5780 */
5781 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5782 duration > rdev->wiphy.max_remain_on_channel_duration)
5783 return -EINVAL;
5784
Johannes Bergcd6c6592012-05-10 21:27:18 +02005785 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5786 !nl80211_valid_channel_type(info, &channel_type))
5787 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005788
5789 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5790 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005791 if (chan == NULL)
5792 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005793
5794 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005795 if (!msg)
5796 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005797
5798 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5799 NL80211_CMD_REMAIN_ON_CHANNEL);
5800
5801 if (IS_ERR(hdr)) {
5802 err = PTR_ERR(hdr);
5803 goto free_msg;
5804 }
5805
Johannes Berg71bbc992012-06-15 15:30:18 +02005806 err = rdev->ops->remain_on_channel(&rdev->wiphy, wdev, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005807 channel_type, duration, &cookie);
5808
5809 if (err)
5810 goto free_msg;
5811
David S. Miller9360ffd2012-03-29 04:41:26 -04005812 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5813 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005814
5815 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005816
5817 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005818
5819 nla_put_failure:
5820 err = -ENOBUFS;
5821 free_msg:
5822 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005823 return err;
5824}
5825
5826static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5827 struct genl_info *info)
5828{
Johannes Berg4c476992010-10-04 21:36:35 +02005829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005830 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005831 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005832
5833 if (!info->attrs[NL80211_ATTR_COOKIE])
5834 return -EINVAL;
5835
Johannes Berg4c476992010-10-04 21:36:35 +02005836 if (!rdev->ops->cancel_remain_on_channel)
5837 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005838
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005839 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5840
Johannes Berg71bbc992012-06-15 15:30:18 +02005841 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005842}
5843
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005844static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5845 u8 *rates, u8 rates_len)
5846{
5847 u8 i;
5848 u32 mask = 0;
5849
5850 for (i = 0; i < rates_len; i++) {
5851 int rate = (rates[i] & 0x7f) * 5;
5852 int ridx;
5853 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5854 struct ieee80211_rate *srate =
5855 &sband->bitrates[ridx];
5856 if (rate == srate->bitrate) {
5857 mask |= 1 << ridx;
5858 break;
5859 }
5860 }
5861 if (ridx == sband->n_bitrates)
5862 return 0; /* rate not found */
5863 }
5864
5865 return mask;
5866}
5867
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005868static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
5869 u8 *rates, u8 rates_len,
5870 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
5871{
5872 u8 i;
5873
5874 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
5875
5876 for (i = 0; i < rates_len; i++) {
5877 int ridx, rbit;
5878
5879 ridx = rates[i] / 8;
5880 rbit = BIT(rates[i] % 8);
5881
5882 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03005883 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005884 return false;
5885
5886 /* check availability */
5887 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
5888 mcs[ridx] |= rbit;
5889 else
5890 return false;
5891 }
5892
5893 return true;
5894}
5895
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00005896static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005897 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
5898 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005899 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
5900 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005901};
5902
5903static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
5904 struct genl_info *info)
5905{
5906 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02005907 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005908 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02005909 int rem, i;
5910 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005911 struct nlattr *tx_rates;
5912 struct ieee80211_supported_band *sband;
5913
5914 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
5915 return -EINVAL;
5916
Johannes Berg4c476992010-10-04 21:36:35 +02005917 if (!rdev->ops->set_bitrate_mask)
5918 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005919
5920 memset(&mask, 0, sizeof(mask));
5921 /* Default to all rates enabled */
5922 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5923 sband = rdev->wiphy.bands[i];
5924 mask.control[i].legacy =
5925 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005926 if (sband)
5927 memcpy(mask.control[i].mcs,
5928 sband->ht_cap.mcs.rx_mask,
5929 sizeof(mask.control[i].mcs));
5930 else
5931 memset(mask.control[i].mcs, 0,
5932 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005933 }
5934
5935 /*
5936 * The nested attribute uses enum nl80211_band as the index. This maps
5937 * directly to the enum ieee80211_band values used in cfg80211.
5938 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005939 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005940 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5941 {
5942 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005943 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5944 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005945 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005946 if (sband == NULL)
5947 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005948 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5949 nla_len(tx_rates), nl80211_txattr_policy);
5950 if (tb[NL80211_TXRATE_LEGACY]) {
5951 mask.control[band].legacy = rateset_to_mask(
5952 sband,
5953 nla_data(tb[NL80211_TXRATE_LEGACY]),
5954 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05305955 if ((mask.control[band].legacy == 0) &&
5956 nla_len(tb[NL80211_TXRATE_LEGACY]))
5957 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01005958 }
5959 if (tb[NL80211_TXRATE_MCS]) {
5960 if (!ht_rateset_to_mask(
5961 sband,
5962 nla_data(tb[NL80211_TXRATE_MCS]),
5963 nla_len(tb[NL80211_TXRATE_MCS]),
5964 mask.control[band].mcs))
5965 return -EINVAL;
5966 }
5967
5968 if (mask.control[band].legacy == 0) {
5969 /* don't allow empty legacy rates if HT
5970 * is not even supported. */
5971 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
5972 return -EINVAL;
5973
5974 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5975 if (mask.control[band].mcs[i])
5976 break;
5977
5978 /* legacy and mcs rates may not be both empty */
5979 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02005980 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005981 }
5982 }
5983
Johannes Berg4c476992010-10-04 21:36:35 +02005984 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005985}
5986
Johannes Berg2e161f72010-08-12 15:38:38 +02005987static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005988{
Johannes Berg4c476992010-10-04 21:36:35 +02005989 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005990 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005991 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005992
5993 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5994 return -EINVAL;
5995
Johannes Berg2e161f72010-08-12 15:38:38 +02005996 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5997 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005998
Johannes Berg71bbc992012-06-15 15:30:18 +02005999 switch (wdev->iftype) {
6000 case NL80211_IFTYPE_STATION:
6001 case NL80211_IFTYPE_ADHOC:
6002 case NL80211_IFTYPE_P2P_CLIENT:
6003 case NL80211_IFTYPE_AP:
6004 case NL80211_IFTYPE_AP_VLAN:
6005 case NL80211_IFTYPE_MESH_POINT:
6006 case NL80211_IFTYPE_P2P_GO:
6007 break;
6008 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006009 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006010 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006011
6012 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006013 if (!rdev->ops->mgmt_tx)
6014 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006015
Johannes Berg71bbc992012-06-15 15:30:18 +02006016 return cfg80211_mlme_register_mgmt(wdev, info->snd_pid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006017 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6018 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006019}
6020
Johannes Berg2e161f72010-08-12 15:38:38 +02006021static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006022{
Johannes Berg4c476992010-10-04 21:36:35 +02006023 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006024 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02006025 struct ieee80211_channel *chan;
6026 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02006027 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02006028 u32 freq;
6029 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006030 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006031 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006032 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006033 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006034 bool offchan, no_cck, dont_wait_for_ack;
6035
6036 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006037
6038 if (!info->attrs[NL80211_ATTR_FRAME] ||
6039 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6040 return -EINVAL;
6041
Johannes Berg4c476992010-10-04 21:36:35 +02006042 if (!rdev->ops->mgmt_tx)
6043 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006044
Johannes Berg71bbc992012-06-15 15:30:18 +02006045 switch (wdev->iftype) {
6046 case NL80211_IFTYPE_STATION:
6047 case NL80211_IFTYPE_ADHOC:
6048 case NL80211_IFTYPE_P2P_CLIENT:
6049 case NL80211_IFTYPE_AP:
6050 case NL80211_IFTYPE_AP_VLAN:
6051 case NL80211_IFTYPE_MESH_POINT:
6052 case NL80211_IFTYPE_P2P_GO:
6053 break;
6054 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006055 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006056 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006057
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006058 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006059 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006060 return -EINVAL;
6061 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006062
6063 /*
6064 * We should wait on the channel for at least a minimum amount
6065 * of time (10ms) but no longer than the driver supports.
6066 */
6067 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6068 wait > rdev->wiphy.max_remain_on_channel_duration)
6069 return -EINVAL;
6070
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006071 }
6072
Jouni Malinen026331c2010-02-15 12:53:10 +02006073 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02006074 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02006075 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02006076 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02006077 }
6078
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006079 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6080
Johannes Berg7c4ef712011-11-18 15:33:48 +01006081 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6082 return -EINVAL;
6083
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306084 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6085
Jouni Malinen026331c2010-02-15 12:53:10 +02006086 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6087 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02006088 if (chan == NULL)
6089 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006090
Johannes Berge247bd902011-11-04 11:18:21 +01006091 if (!dont_wait_for_ack) {
6092 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6093 if (!msg)
6094 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006095
Johannes Berge247bd902011-11-04 11:18:21 +01006096 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6097 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006098
Johannes Berge247bd902011-11-04 11:18:21 +01006099 if (IS_ERR(hdr)) {
6100 err = PTR_ERR(hdr);
6101 goto free_msg;
6102 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006103 }
Johannes Berge247bd902011-11-04 11:18:21 +01006104
Johannes Berg71bbc992012-06-15 15:30:18 +02006105 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chan, offchan, channel_type,
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006106 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006107 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6108 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006109 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006110 if (err)
6111 goto free_msg;
6112
Johannes Berge247bd902011-11-04 11:18:21 +01006113 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006114 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6115 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006116
Johannes Berge247bd902011-11-04 11:18:21 +01006117 genlmsg_end(msg, hdr);
6118 return genlmsg_reply(msg, info);
6119 }
6120
6121 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006122
6123 nla_put_failure:
6124 err = -ENOBUFS;
6125 free_msg:
6126 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006127 return err;
6128}
6129
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006130static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6131{
6132 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006133 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006134 u64 cookie;
6135
6136 if (!info->attrs[NL80211_ATTR_COOKIE])
6137 return -EINVAL;
6138
6139 if (!rdev->ops->mgmt_tx_cancel_wait)
6140 return -EOPNOTSUPP;
6141
Johannes Berg71bbc992012-06-15 15:30:18 +02006142 switch (wdev->iftype) {
6143 case NL80211_IFTYPE_STATION:
6144 case NL80211_IFTYPE_ADHOC:
6145 case NL80211_IFTYPE_P2P_CLIENT:
6146 case NL80211_IFTYPE_AP:
6147 case NL80211_IFTYPE_AP_VLAN:
6148 case NL80211_IFTYPE_P2P_GO:
6149 break;
6150 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006151 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006152 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006153
6154 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6155
Johannes Berg71bbc992012-06-15 15:30:18 +02006156 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006157}
6158
Kalle Valoffb9eb32010-02-17 17:58:10 +02006159static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6160{
Johannes Berg4c476992010-10-04 21:36:35 +02006161 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006162 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006163 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006164 u8 ps_state;
6165 bool state;
6166 int err;
6167
Johannes Berg4c476992010-10-04 21:36:35 +02006168 if (!info->attrs[NL80211_ATTR_PS_STATE])
6169 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006170
6171 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6172
Johannes Berg4c476992010-10-04 21:36:35 +02006173 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6174 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006175
6176 wdev = dev->ieee80211_ptr;
6177
Johannes Berg4c476992010-10-04 21:36:35 +02006178 if (!rdev->ops->set_power_mgmt)
6179 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006180
6181 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6182
6183 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006184 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006185
Johannes Berg4c476992010-10-04 21:36:35 +02006186 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
6187 wdev->ps_timeout);
6188 if (!err)
6189 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006190 return err;
6191}
6192
6193static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6194{
Johannes Berg4c476992010-10-04 21:36:35 +02006195 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006196 enum nl80211_ps_state ps_state;
6197 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006198 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006199 struct sk_buff *msg;
6200 void *hdr;
6201 int err;
6202
Kalle Valoffb9eb32010-02-17 17:58:10 +02006203 wdev = dev->ieee80211_ptr;
6204
Johannes Berg4c476992010-10-04 21:36:35 +02006205 if (!rdev->ops->set_power_mgmt)
6206 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006207
6208 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006209 if (!msg)
6210 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006211
6212 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6213 NL80211_CMD_GET_POWER_SAVE);
6214 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006215 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006216 goto free_msg;
6217 }
6218
6219 if (wdev->ps)
6220 ps_state = NL80211_PS_ENABLED;
6221 else
6222 ps_state = NL80211_PS_DISABLED;
6223
David S. Miller9360ffd2012-03-29 04:41:26 -04006224 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6225 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006226
6227 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006228 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006229
Johannes Berg4c476992010-10-04 21:36:35 +02006230 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006231 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006232 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006233 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006234 return err;
6235}
6236
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006237static struct nla_policy
6238nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6239 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6240 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6241 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
6242};
6243
6244static int nl80211_set_cqm_rssi(struct genl_info *info,
6245 s32 threshold, u32 hysteresis)
6246{
Johannes Berg4c476992010-10-04 21:36:35 +02006247 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006248 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006249 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006250
6251 if (threshold > 0)
6252 return -EINVAL;
6253
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006254 wdev = dev->ieee80211_ptr;
6255
Johannes Berg4c476992010-10-04 21:36:35 +02006256 if (!rdev->ops->set_cqm_rssi_config)
6257 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006258
Johannes Berg074ac8d2010-09-16 14:58:22 +02006259 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006260 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6261 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006262
Johannes Berg4c476992010-10-04 21:36:35 +02006263 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6264 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006265}
6266
6267static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6268{
6269 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6270 struct nlattr *cqm;
6271 int err;
6272
6273 cqm = info->attrs[NL80211_ATTR_CQM];
6274 if (!cqm) {
6275 err = -EINVAL;
6276 goto out;
6277 }
6278
6279 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6280 nl80211_attr_cqm_policy);
6281 if (err)
6282 goto out;
6283
6284 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6285 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6286 s32 threshold;
6287 u32 hysteresis;
6288 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6289 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6290 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
6291 } else
6292 err = -EINVAL;
6293
6294out:
6295 return err;
6296}
6297
Johannes Berg29cbe682010-12-03 09:20:44 +01006298static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6299{
6300 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6301 struct net_device *dev = info->user_ptr[1];
6302 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006303 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006304 int err;
6305
6306 /* start with default */
6307 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006308 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006309
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006310 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006311 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006312 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006313 if (err)
6314 return err;
6315 }
6316
6317 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6318 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6319 return -EINVAL;
6320
Javier Cardonac80d5452010-12-16 17:37:49 -08006321 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6322 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6323
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006324 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6325 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6326 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6327 return -EINVAL;
6328
Javier Cardonac80d5452010-12-16 17:37:49 -08006329 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6330 /* parse additional setup parameters if given */
6331 err = nl80211_parse_mesh_setup(info, &setup);
6332 if (err)
6333 return err;
6334 }
6335
Johannes Bergcc1d2802012-05-16 23:50:20 +02006336 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6337 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6338
6339 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6340 !nl80211_valid_channel_type(info, &channel_type))
6341 return -EINVAL;
6342
6343 setup.channel = rdev_freq_to_chan(rdev,
6344 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6345 channel_type);
6346 if (!setup.channel)
6347 return -EINVAL;
6348 setup.channel_type = channel_type;
6349 } else {
6350 /* cfg80211_join_mesh() will sort it out */
6351 setup.channel = NULL;
6352 }
6353
Javier Cardonac80d5452010-12-16 17:37:49 -08006354 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006355}
6356
6357static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6358{
6359 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6360 struct net_device *dev = info->user_ptr[1];
6361
6362 return cfg80211_leave_mesh(rdev, dev);
6363}
6364
Johannes Bergdfb89c52012-06-27 09:23:48 +02006365#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006366static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6367{
6368 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6369 struct sk_buff *msg;
6370 void *hdr;
6371
6372 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6373 return -EOPNOTSUPP;
6374
6375 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6376 if (!msg)
6377 return -ENOMEM;
6378
6379 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6380 NL80211_CMD_GET_WOWLAN);
6381 if (!hdr)
6382 goto nla_put_failure;
6383
6384 if (rdev->wowlan) {
6385 struct nlattr *nl_wowlan;
6386
6387 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6388 if (!nl_wowlan)
6389 goto nla_put_failure;
6390
David S. Miller9360ffd2012-03-29 04:41:26 -04006391 if ((rdev->wowlan->any &&
6392 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6393 (rdev->wowlan->disconnect &&
6394 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6395 (rdev->wowlan->magic_pkt &&
6396 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6397 (rdev->wowlan->gtk_rekey_failure &&
6398 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6399 (rdev->wowlan->eap_identity_req &&
6400 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6401 (rdev->wowlan->four_way_handshake &&
6402 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6403 (rdev->wowlan->rfkill_release &&
6404 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6405 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006406 if (rdev->wowlan->n_patterns) {
6407 struct nlattr *nl_pats, *nl_pat;
6408 int i, pat_len;
6409
6410 nl_pats = nla_nest_start(msg,
6411 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6412 if (!nl_pats)
6413 goto nla_put_failure;
6414
6415 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6416 nl_pat = nla_nest_start(msg, i + 1);
6417 if (!nl_pat)
6418 goto nla_put_failure;
6419 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006420 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6421 DIV_ROUND_UP(pat_len, 8),
6422 rdev->wowlan->patterns[i].mask) ||
6423 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6424 pat_len,
6425 rdev->wowlan->patterns[i].pattern))
6426 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006427 nla_nest_end(msg, nl_pat);
6428 }
6429 nla_nest_end(msg, nl_pats);
6430 }
6431
6432 nla_nest_end(msg, nl_wowlan);
6433 }
6434
6435 genlmsg_end(msg, hdr);
6436 return genlmsg_reply(msg, info);
6437
6438nla_put_failure:
6439 nlmsg_free(msg);
6440 return -ENOBUFS;
6441}
6442
6443static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6444{
6445 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6446 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
6447 struct cfg80211_wowlan no_triggers = {};
6448 struct cfg80211_wowlan new_triggers = {};
6449 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6450 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006451 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006452
6453 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6454 return -EOPNOTSUPP;
6455
6456 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
6457 goto no_triggers;
6458
6459 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6460 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6461 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6462 nl80211_wowlan_policy);
6463 if (err)
6464 return err;
6465
6466 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6467 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6468 return -EINVAL;
6469 new_triggers.any = true;
6470 }
6471
6472 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6473 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6474 return -EINVAL;
6475 new_triggers.disconnect = true;
6476 }
6477
6478 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6479 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6480 return -EINVAL;
6481 new_triggers.magic_pkt = true;
6482 }
6483
Johannes Berg77dbbb12011-07-13 10:48:55 +02006484 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6485 return -EINVAL;
6486
6487 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6488 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6489 return -EINVAL;
6490 new_triggers.gtk_rekey_failure = true;
6491 }
6492
6493 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6494 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6495 return -EINVAL;
6496 new_triggers.eap_identity_req = true;
6497 }
6498
6499 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6500 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6501 return -EINVAL;
6502 new_triggers.four_way_handshake = true;
6503 }
6504
6505 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6506 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6507 return -EINVAL;
6508 new_triggers.rfkill_release = true;
6509 }
6510
Johannes Bergff1b6e62011-05-04 15:37:28 +02006511 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6512 struct nlattr *pat;
6513 int n_patterns = 0;
6514 int rem, pat_len, mask_len;
6515 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6516
6517 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6518 rem)
6519 n_patterns++;
6520 if (n_patterns > wowlan->n_patterns)
6521 return -EINVAL;
6522
6523 new_triggers.patterns = kcalloc(n_patterns,
6524 sizeof(new_triggers.patterns[0]),
6525 GFP_KERNEL);
6526 if (!new_triggers.patterns)
6527 return -ENOMEM;
6528
6529 new_triggers.n_patterns = n_patterns;
6530 i = 0;
6531
6532 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6533 rem) {
6534 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6535 nla_data(pat), nla_len(pat), NULL);
6536 err = -EINVAL;
6537 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6538 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6539 goto error;
6540 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6541 mask_len = DIV_ROUND_UP(pat_len, 8);
6542 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6543 mask_len)
6544 goto error;
6545 if (pat_len > wowlan->pattern_max_len ||
6546 pat_len < wowlan->pattern_min_len)
6547 goto error;
6548
6549 new_triggers.patterns[i].mask =
6550 kmalloc(mask_len + pat_len, GFP_KERNEL);
6551 if (!new_triggers.patterns[i].mask) {
6552 err = -ENOMEM;
6553 goto error;
6554 }
6555 new_triggers.patterns[i].pattern =
6556 new_triggers.patterns[i].mask + mask_len;
6557 memcpy(new_triggers.patterns[i].mask,
6558 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6559 mask_len);
6560 new_triggers.patterns[i].pattern_len = pat_len;
6561 memcpy(new_triggers.patterns[i].pattern,
6562 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6563 pat_len);
6564 i++;
6565 }
6566 }
6567
6568 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
6569 struct cfg80211_wowlan *ntrig;
6570 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
6571 GFP_KERNEL);
6572 if (!ntrig) {
6573 err = -ENOMEM;
6574 goto error;
6575 }
6576 cfg80211_rdev_free_wowlan(rdev);
6577 rdev->wowlan = ntrig;
6578 } else {
6579 no_triggers:
6580 cfg80211_rdev_free_wowlan(rdev);
6581 rdev->wowlan = NULL;
6582 }
6583
Johannes Berg6d525632012-04-04 15:05:25 +02006584 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6585 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6586
Johannes Bergff1b6e62011-05-04 15:37:28 +02006587 return 0;
6588 error:
6589 for (i = 0; i < new_triggers.n_patterns; i++)
6590 kfree(new_triggers.patterns[i].mask);
6591 kfree(new_triggers.patterns);
6592 return err;
6593}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006594#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006595
Johannes Berge5497d72011-07-05 16:35:40 +02006596static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6597{
6598 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6599 struct net_device *dev = info->user_ptr[1];
6600 struct wireless_dev *wdev = dev->ieee80211_ptr;
6601 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6602 struct cfg80211_gtk_rekey_data rekey_data;
6603 int err;
6604
6605 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6606 return -EINVAL;
6607
6608 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6609 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6610 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6611 nl80211_rekey_policy);
6612 if (err)
6613 return err;
6614
6615 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6616 return -ERANGE;
6617 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6618 return -ERANGE;
6619 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6620 return -ERANGE;
6621
6622 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6623 NL80211_KEK_LEN);
6624 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6625 NL80211_KCK_LEN);
6626 memcpy(rekey_data.replay_ctr,
6627 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6628 NL80211_REPLAY_CTR_LEN);
6629
6630 wdev_lock(wdev);
6631 if (!wdev->current_bss) {
6632 err = -ENOTCONN;
6633 goto out;
6634 }
6635
6636 if (!rdev->ops->set_rekey_data) {
6637 err = -EOPNOTSUPP;
6638 goto out;
6639 }
6640
6641 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6642 out:
6643 wdev_unlock(wdev);
6644 return err;
6645}
6646
Johannes Berg28946da2011-11-04 11:18:12 +01006647static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6648 struct genl_info *info)
6649{
6650 struct net_device *dev = info->user_ptr[1];
6651 struct wireless_dev *wdev = dev->ieee80211_ptr;
6652
6653 if (wdev->iftype != NL80211_IFTYPE_AP &&
6654 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6655 return -EINVAL;
6656
6657 if (wdev->ap_unexpected_nlpid)
6658 return -EBUSY;
6659
6660 wdev->ap_unexpected_nlpid = info->snd_pid;
6661 return 0;
6662}
6663
Johannes Berg7f6cf312011-11-04 11:18:15 +01006664static int nl80211_probe_client(struct sk_buff *skb,
6665 struct genl_info *info)
6666{
6667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6668 struct net_device *dev = info->user_ptr[1];
6669 struct wireless_dev *wdev = dev->ieee80211_ptr;
6670 struct sk_buff *msg;
6671 void *hdr;
6672 const u8 *addr;
6673 u64 cookie;
6674 int err;
6675
6676 if (wdev->iftype != NL80211_IFTYPE_AP &&
6677 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6678 return -EOPNOTSUPP;
6679
6680 if (!info->attrs[NL80211_ATTR_MAC])
6681 return -EINVAL;
6682
6683 if (!rdev->ops->probe_client)
6684 return -EOPNOTSUPP;
6685
6686 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6687 if (!msg)
6688 return -ENOMEM;
6689
6690 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
6691 NL80211_CMD_PROBE_CLIENT);
6692
6693 if (IS_ERR(hdr)) {
6694 err = PTR_ERR(hdr);
6695 goto free_msg;
6696 }
6697
6698 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6699
6700 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6701 if (err)
6702 goto free_msg;
6703
David S. Miller9360ffd2012-03-29 04:41:26 -04006704 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6705 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006706
6707 genlmsg_end(msg, hdr);
6708
6709 return genlmsg_reply(msg, info);
6710
6711 nla_put_failure:
6712 err = -ENOBUFS;
6713 free_msg:
6714 nlmsg_free(msg);
6715 return err;
6716}
6717
Johannes Berg5e7602302011-11-04 11:18:17 +01006718static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6719{
6720 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6721
6722 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6723 return -EOPNOTSUPP;
6724
6725 if (rdev->ap_beacons_nlpid)
6726 return -EBUSY;
6727
6728 rdev->ap_beacons_nlpid = info->snd_pid;
6729
6730 return 0;
6731}
6732
Johannes Berg4c476992010-10-04 21:36:35 +02006733#define NL80211_FLAG_NEED_WIPHY 0x01
6734#define NL80211_FLAG_NEED_NETDEV 0x02
6735#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006736#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6737#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6738 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02006739#define NL80211_FLAG_NEED_WDEV 0x10
6740/* If a netdev is associated, it must be UP */
6741#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
6742 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006743
6744static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6745 struct genl_info *info)
6746{
6747 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02006748 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006749 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02006750 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6751
6752 if (rtnl)
6753 rtnl_lock();
6754
6755 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02006756 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02006757 if (IS_ERR(rdev)) {
6758 if (rtnl)
6759 rtnl_unlock();
6760 return PTR_ERR(rdev);
6761 }
6762 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02006763 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
6764 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02006765 mutex_lock(&cfg80211_mutex);
6766 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
6767 info->attrs);
6768 if (IS_ERR(wdev)) {
6769 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02006770 if (rtnl)
6771 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02006772 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02006773 }
Johannes Berg89a54e42012-06-15 14:33:17 +02006774
Johannes Berg89a54e42012-06-15 14:33:17 +02006775 dev = wdev->netdev;
6776 rdev = wiphy_to_dev(wdev->wiphy);
6777
Johannes Berg1bf614e2012-06-15 15:23:36 +02006778 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
6779 if (!dev) {
6780 mutex_unlock(&cfg80211_mutex);
6781 if (rtnl)
6782 rtnl_unlock();
6783 return -EINVAL;
6784 }
6785
6786 info->user_ptr[1] = dev;
6787 } else {
6788 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02006789 }
Johannes Berg89a54e42012-06-15 14:33:17 +02006790
Johannes Berg1bf614e2012-06-15 15:23:36 +02006791 if (dev) {
6792 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
6793 !netif_running(dev)) {
6794 mutex_unlock(&cfg80211_mutex);
6795 if (rtnl)
6796 rtnl_unlock();
6797 return -ENETDOWN;
6798 }
6799
6800 dev_hold(dev);
6801 }
6802
Johannes Berg89a54e42012-06-15 14:33:17 +02006803 cfg80211_lock_rdev(rdev);
6804
6805 mutex_unlock(&cfg80211_mutex);
6806
Johannes Berg4c476992010-10-04 21:36:35 +02006807 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006808 }
6809
6810 return 0;
6811}
6812
6813static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
6814 struct genl_info *info)
6815{
6816 if (info->user_ptr[0])
6817 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02006818 if (info->user_ptr[1]) {
6819 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
6820 struct wireless_dev *wdev = info->user_ptr[1];
6821
6822 if (wdev->netdev)
6823 dev_put(wdev->netdev);
6824 } else {
6825 dev_put(info->user_ptr[1]);
6826 }
6827 }
Johannes Berg4c476992010-10-04 21:36:35 +02006828 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
6829 rtnl_unlock();
6830}
6831
Johannes Berg55682962007-09-20 13:09:35 -04006832static struct genl_ops nl80211_ops[] = {
6833 {
6834 .cmd = NL80211_CMD_GET_WIPHY,
6835 .doit = nl80211_get_wiphy,
6836 .dumpit = nl80211_dump_wiphy,
6837 .policy = nl80211_policy,
6838 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006839 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04006840 },
6841 {
6842 .cmd = NL80211_CMD_SET_WIPHY,
6843 .doit = nl80211_set_wiphy,
6844 .policy = nl80211_policy,
6845 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006846 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006847 },
6848 {
6849 .cmd = NL80211_CMD_GET_INTERFACE,
6850 .doit = nl80211_get_interface,
6851 .dumpit = nl80211_dump_interface,
6852 .policy = nl80211_policy,
6853 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02006854 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04006855 },
6856 {
6857 .cmd = NL80211_CMD_SET_INTERFACE,
6858 .doit = nl80211_set_interface,
6859 .policy = nl80211_policy,
6860 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006861 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6862 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006863 },
6864 {
6865 .cmd = NL80211_CMD_NEW_INTERFACE,
6866 .doit = nl80211_new_interface,
6867 .policy = nl80211_policy,
6868 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006869 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6870 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006871 },
6872 {
6873 .cmd = NL80211_CMD_DEL_INTERFACE,
6874 .doit = nl80211_del_interface,
6875 .policy = nl80211_policy,
6876 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006877 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6878 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04006879 },
Johannes Berg41ade002007-12-19 02:03:29 +01006880 {
6881 .cmd = NL80211_CMD_GET_KEY,
6882 .doit = nl80211_get_key,
6883 .policy = nl80211_policy,
6884 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006885 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006886 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006887 },
6888 {
6889 .cmd = NL80211_CMD_SET_KEY,
6890 .doit = nl80211_set_key,
6891 .policy = nl80211_policy,
6892 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006893 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006894 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006895 },
6896 {
6897 .cmd = NL80211_CMD_NEW_KEY,
6898 .doit = nl80211_new_key,
6899 .policy = nl80211_policy,
6900 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006901 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006902 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006903 },
6904 {
6905 .cmd = NL80211_CMD_DEL_KEY,
6906 .doit = nl80211_del_key,
6907 .policy = nl80211_policy,
6908 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006909 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006910 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01006911 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01006912 {
6913 .cmd = NL80211_CMD_SET_BEACON,
6914 .policy = nl80211_policy,
6915 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006916 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006917 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006918 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006919 },
6920 {
Johannes Berg88600202012-02-13 15:17:18 +01006921 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006922 .policy = nl80211_policy,
6923 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006924 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006925 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006926 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006927 },
6928 {
Johannes Berg88600202012-02-13 15:17:18 +01006929 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006930 .policy = nl80211_policy,
6931 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01006932 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006933 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006934 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01006935 },
Johannes Berg5727ef12007-12-19 02:03:34 +01006936 {
6937 .cmd = NL80211_CMD_GET_STATION,
6938 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006939 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01006940 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02006941 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6942 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006943 },
6944 {
6945 .cmd = NL80211_CMD_SET_STATION,
6946 .doit = nl80211_set_station,
6947 .policy = nl80211_policy,
6948 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006949 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006950 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006951 },
6952 {
6953 .cmd = NL80211_CMD_NEW_STATION,
6954 .doit = nl80211_new_station,
6955 .policy = nl80211_policy,
6956 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006957 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006958 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006959 },
6960 {
6961 .cmd = NL80211_CMD_DEL_STATION,
6962 .doit = nl80211_del_station,
6963 .policy = nl80211_policy,
6964 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006965 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006966 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01006967 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006968 {
6969 .cmd = NL80211_CMD_GET_MPATH,
6970 .doit = nl80211_get_mpath,
6971 .dumpit = nl80211_dump_mpath,
6972 .policy = nl80211_policy,
6973 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006974 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006975 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006976 },
6977 {
6978 .cmd = NL80211_CMD_SET_MPATH,
6979 .doit = nl80211_set_mpath,
6980 .policy = nl80211_policy,
6981 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006982 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006983 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006984 },
6985 {
6986 .cmd = NL80211_CMD_NEW_MPATH,
6987 .doit = nl80211_new_mpath,
6988 .policy = nl80211_policy,
6989 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006990 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006991 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01006992 },
6993 {
6994 .cmd = NL80211_CMD_DEL_MPATH,
6995 .doit = nl80211_del_mpath,
6996 .policy = nl80211_policy,
6997 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02006998 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006999 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007000 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007001 {
7002 .cmd = NL80211_CMD_SET_BSS,
7003 .doit = nl80211_set_bss,
7004 .policy = nl80211_policy,
7005 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007006 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007007 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007008 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007009 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007010 .cmd = NL80211_CMD_GET_REG,
7011 .doit = nl80211_get_reg,
7012 .policy = nl80211_policy,
7013 /* can be retrieved by unprivileged users */
7014 },
7015 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007016 .cmd = NL80211_CMD_SET_REG,
7017 .doit = nl80211_set_reg,
7018 .policy = nl80211_policy,
7019 .flags = GENL_ADMIN_PERM,
7020 },
7021 {
7022 .cmd = NL80211_CMD_REQ_SET_REG,
7023 .doit = nl80211_req_set_reg,
7024 .policy = nl80211_policy,
7025 .flags = GENL_ADMIN_PERM,
7026 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007027 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007028 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7029 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007030 .policy = nl80211_policy,
7031 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007032 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007033 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007034 },
7035 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007036 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7037 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007038 .policy = nl80211_policy,
7039 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007040 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007041 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007042 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007043 {
Johannes Berg2a519312009-02-10 21:25:55 +01007044 .cmd = NL80211_CMD_TRIGGER_SCAN,
7045 .doit = nl80211_trigger_scan,
7046 .policy = nl80211_policy,
7047 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007048 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007049 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007050 },
7051 {
7052 .cmd = NL80211_CMD_GET_SCAN,
7053 .policy = nl80211_policy,
7054 .dumpit = nl80211_dump_scan,
7055 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007056 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007057 .cmd = NL80211_CMD_START_SCHED_SCAN,
7058 .doit = nl80211_start_sched_scan,
7059 .policy = nl80211_policy,
7060 .flags = GENL_ADMIN_PERM,
7061 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7062 NL80211_FLAG_NEED_RTNL,
7063 },
7064 {
7065 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7066 .doit = nl80211_stop_sched_scan,
7067 .policy = nl80211_policy,
7068 .flags = GENL_ADMIN_PERM,
7069 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7070 NL80211_FLAG_NEED_RTNL,
7071 },
7072 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007073 .cmd = NL80211_CMD_AUTHENTICATE,
7074 .doit = nl80211_authenticate,
7075 .policy = nl80211_policy,
7076 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007077 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007078 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007079 },
7080 {
7081 .cmd = NL80211_CMD_ASSOCIATE,
7082 .doit = nl80211_associate,
7083 .policy = nl80211_policy,
7084 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007085 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007086 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007087 },
7088 {
7089 .cmd = NL80211_CMD_DEAUTHENTICATE,
7090 .doit = nl80211_deauthenticate,
7091 .policy = nl80211_policy,
7092 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007093 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007094 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007095 },
7096 {
7097 .cmd = NL80211_CMD_DISASSOCIATE,
7098 .doit = nl80211_disassociate,
7099 .policy = nl80211_policy,
7100 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007101 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007102 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007103 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007104 {
7105 .cmd = NL80211_CMD_JOIN_IBSS,
7106 .doit = nl80211_join_ibss,
7107 .policy = nl80211_policy,
7108 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007109 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007110 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007111 },
7112 {
7113 .cmd = NL80211_CMD_LEAVE_IBSS,
7114 .doit = nl80211_leave_ibss,
7115 .policy = nl80211_policy,
7116 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007117 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007118 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007119 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007120#ifdef CONFIG_NL80211_TESTMODE
7121 {
7122 .cmd = NL80211_CMD_TESTMODE,
7123 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007124 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007125 .policy = nl80211_policy,
7126 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007127 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7128 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007129 },
7130#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007131 {
7132 .cmd = NL80211_CMD_CONNECT,
7133 .doit = nl80211_connect,
7134 .policy = nl80211_policy,
7135 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007136 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007137 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007138 },
7139 {
7140 .cmd = NL80211_CMD_DISCONNECT,
7141 .doit = nl80211_disconnect,
7142 .policy = nl80211_policy,
7143 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007144 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007145 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007146 },
Johannes Berg463d0182009-07-14 00:33:35 +02007147 {
7148 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7149 .doit = nl80211_wiphy_netns,
7150 .policy = nl80211_policy,
7151 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007152 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7153 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007154 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007155 {
7156 .cmd = NL80211_CMD_GET_SURVEY,
7157 .policy = nl80211_policy,
7158 .dumpit = nl80211_dump_survey,
7159 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007160 {
7161 .cmd = NL80211_CMD_SET_PMKSA,
7162 .doit = nl80211_setdel_pmksa,
7163 .policy = nl80211_policy,
7164 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007165 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007166 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007167 },
7168 {
7169 .cmd = NL80211_CMD_DEL_PMKSA,
7170 .doit = nl80211_setdel_pmksa,
7171 .policy = nl80211_policy,
7172 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007173 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007174 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007175 },
7176 {
7177 .cmd = NL80211_CMD_FLUSH_PMKSA,
7178 .doit = nl80211_flush_pmksa,
7179 .policy = nl80211_policy,
7180 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007181 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007182 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007183 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007184 {
7185 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7186 .doit = nl80211_remain_on_channel,
7187 .policy = nl80211_policy,
7188 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007189 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007190 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007191 },
7192 {
7193 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7194 .doit = nl80211_cancel_remain_on_channel,
7195 .policy = nl80211_policy,
7196 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007197 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007198 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007199 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007200 {
7201 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7202 .doit = nl80211_set_tx_bitrate_mask,
7203 .policy = nl80211_policy,
7204 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007205 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7206 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007207 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007208 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007209 .cmd = NL80211_CMD_REGISTER_FRAME,
7210 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007211 .policy = nl80211_policy,
7212 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007213 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007214 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007215 },
7216 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007217 .cmd = NL80211_CMD_FRAME,
7218 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007219 .policy = nl80211_policy,
7220 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007221 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007222 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007223 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007224 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007225 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7226 .doit = nl80211_tx_mgmt_cancel_wait,
7227 .policy = nl80211_policy,
7228 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007229 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007230 NL80211_FLAG_NEED_RTNL,
7231 },
7232 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007233 .cmd = NL80211_CMD_SET_POWER_SAVE,
7234 .doit = nl80211_set_power_save,
7235 .policy = nl80211_policy,
7236 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007237 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7238 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007239 },
7240 {
7241 .cmd = NL80211_CMD_GET_POWER_SAVE,
7242 .doit = nl80211_get_power_save,
7243 .policy = nl80211_policy,
7244 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007245 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7246 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007247 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007248 {
7249 .cmd = NL80211_CMD_SET_CQM,
7250 .doit = nl80211_set_cqm,
7251 .policy = nl80211_policy,
7252 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007253 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7254 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007255 },
Johannes Bergf444de02010-05-05 15:25:02 +02007256 {
7257 .cmd = NL80211_CMD_SET_CHANNEL,
7258 .doit = nl80211_set_channel,
7259 .policy = nl80211_policy,
7260 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007261 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7262 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007263 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007264 {
7265 .cmd = NL80211_CMD_SET_WDS_PEER,
7266 .doit = nl80211_set_wds_peer,
7267 .policy = nl80211_policy,
7268 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007269 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7270 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007271 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007272 {
7273 .cmd = NL80211_CMD_JOIN_MESH,
7274 .doit = nl80211_join_mesh,
7275 .policy = nl80211_policy,
7276 .flags = GENL_ADMIN_PERM,
7277 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7278 NL80211_FLAG_NEED_RTNL,
7279 },
7280 {
7281 .cmd = NL80211_CMD_LEAVE_MESH,
7282 .doit = nl80211_leave_mesh,
7283 .policy = nl80211_policy,
7284 .flags = GENL_ADMIN_PERM,
7285 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7286 NL80211_FLAG_NEED_RTNL,
7287 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007288#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007289 {
7290 .cmd = NL80211_CMD_GET_WOWLAN,
7291 .doit = nl80211_get_wowlan,
7292 .policy = nl80211_policy,
7293 /* can be retrieved by unprivileged users */
7294 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7295 NL80211_FLAG_NEED_RTNL,
7296 },
7297 {
7298 .cmd = NL80211_CMD_SET_WOWLAN,
7299 .doit = nl80211_set_wowlan,
7300 .policy = nl80211_policy,
7301 .flags = GENL_ADMIN_PERM,
7302 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7303 NL80211_FLAG_NEED_RTNL,
7304 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007305#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007306 {
7307 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7308 .doit = nl80211_set_rekey_data,
7309 .policy = nl80211_policy,
7310 .flags = GENL_ADMIN_PERM,
7311 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7312 NL80211_FLAG_NEED_RTNL,
7313 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007314 {
7315 .cmd = NL80211_CMD_TDLS_MGMT,
7316 .doit = nl80211_tdls_mgmt,
7317 .policy = nl80211_policy,
7318 .flags = GENL_ADMIN_PERM,
7319 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7320 NL80211_FLAG_NEED_RTNL,
7321 },
7322 {
7323 .cmd = NL80211_CMD_TDLS_OPER,
7324 .doit = nl80211_tdls_oper,
7325 .policy = nl80211_policy,
7326 .flags = GENL_ADMIN_PERM,
7327 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7328 NL80211_FLAG_NEED_RTNL,
7329 },
Johannes Berg28946da2011-11-04 11:18:12 +01007330 {
7331 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7332 .doit = nl80211_register_unexpected_frame,
7333 .policy = nl80211_policy,
7334 .flags = GENL_ADMIN_PERM,
7335 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7336 NL80211_FLAG_NEED_RTNL,
7337 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007338 {
7339 .cmd = NL80211_CMD_PROBE_CLIENT,
7340 .doit = nl80211_probe_client,
7341 .policy = nl80211_policy,
7342 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007343 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007344 NL80211_FLAG_NEED_RTNL,
7345 },
Johannes Berg5e7602302011-11-04 11:18:17 +01007346 {
7347 .cmd = NL80211_CMD_REGISTER_BEACONS,
7348 .doit = nl80211_register_beacons,
7349 .policy = nl80211_policy,
7350 .flags = GENL_ADMIN_PERM,
7351 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7352 NL80211_FLAG_NEED_RTNL,
7353 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007354 {
7355 .cmd = NL80211_CMD_SET_NOACK_MAP,
7356 .doit = nl80211_set_noack_map,
7357 .policy = nl80211_policy,
7358 .flags = GENL_ADMIN_PERM,
7359 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7360 NL80211_FLAG_NEED_RTNL,
7361 },
7362
Johannes Berg55682962007-09-20 13:09:35 -04007363};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007364
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007365static struct genl_multicast_group nl80211_mlme_mcgrp = {
7366 .name = "mlme",
7367};
Johannes Berg55682962007-09-20 13:09:35 -04007368
7369/* multicast groups */
7370static struct genl_multicast_group nl80211_config_mcgrp = {
7371 .name = "config",
7372};
Johannes Berg2a519312009-02-10 21:25:55 +01007373static struct genl_multicast_group nl80211_scan_mcgrp = {
7374 .name = "scan",
7375};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007376static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7377 .name = "regulatory",
7378};
Johannes Berg55682962007-09-20 13:09:35 -04007379
7380/* notification functions */
7381
7382void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7383{
7384 struct sk_buff *msg;
7385
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007386 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007387 if (!msg)
7388 return;
7389
7390 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7391 nlmsg_free(msg);
7392 return;
7393 }
7394
Johannes Berg463d0182009-07-14 00:33:35 +02007395 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7396 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007397}
7398
Johannes Berg362a4152009-05-24 16:43:15 +02007399static int nl80211_add_scan_req(struct sk_buff *msg,
7400 struct cfg80211_registered_device *rdev)
7401{
7402 struct cfg80211_scan_request *req = rdev->scan_req;
7403 struct nlattr *nest;
7404 int i;
7405
Johannes Berg667503dd2009-07-07 03:56:11 +02007406 ASSERT_RDEV_LOCK(rdev);
7407
Johannes Berg362a4152009-05-24 16:43:15 +02007408 if (WARN_ON(!req))
7409 return 0;
7410
7411 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7412 if (!nest)
7413 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007414 for (i = 0; i < req->n_ssids; i++) {
7415 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7416 goto nla_put_failure;
7417 }
Johannes Berg362a4152009-05-24 16:43:15 +02007418 nla_nest_end(msg, nest);
7419
7420 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7421 if (!nest)
7422 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007423 for (i = 0; i < req->n_channels; i++) {
7424 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7425 goto nla_put_failure;
7426 }
Johannes Berg362a4152009-05-24 16:43:15 +02007427 nla_nest_end(msg, nest);
7428
David S. Miller9360ffd2012-03-29 04:41:26 -04007429 if (req->ie &&
7430 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7431 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007432
7433 return 0;
7434 nla_put_failure:
7435 return -ENOBUFS;
7436}
7437
Johannes Berga538e2d2009-06-16 19:56:42 +02007438static int nl80211_send_scan_msg(struct sk_buff *msg,
7439 struct cfg80211_registered_device *rdev,
7440 struct net_device *netdev,
7441 u32 pid, u32 seq, int flags,
7442 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007443{
7444 void *hdr;
7445
7446 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7447 if (!hdr)
7448 return -1;
7449
David S. Miller9360ffd2012-03-29 04:41:26 -04007450 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7451 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7452 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007453
Johannes Berg362a4152009-05-24 16:43:15 +02007454 /* ignore errors and send incomplete event anyway */
7455 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007456
7457 return genlmsg_end(msg, hdr);
7458
7459 nla_put_failure:
7460 genlmsg_cancel(msg, hdr);
7461 return -EMSGSIZE;
7462}
7463
Luciano Coelho807f8a82011-05-11 17:09:35 +03007464static int
7465nl80211_send_sched_scan_msg(struct sk_buff *msg,
7466 struct cfg80211_registered_device *rdev,
7467 struct net_device *netdev,
7468 u32 pid, u32 seq, int flags, u32 cmd)
7469{
7470 void *hdr;
7471
7472 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
7473 if (!hdr)
7474 return -1;
7475
David S. Miller9360ffd2012-03-29 04:41:26 -04007476 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7477 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7478 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007479
7480 return genlmsg_end(msg, hdr);
7481
7482 nla_put_failure:
7483 genlmsg_cancel(msg, hdr);
7484 return -EMSGSIZE;
7485}
7486
Johannes Berga538e2d2009-06-16 19:56:42 +02007487void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
7488 struct net_device *netdev)
7489{
7490 struct sk_buff *msg;
7491
7492 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7493 if (!msg)
7494 return;
7495
7496 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7497 NL80211_CMD_TRIGGER_SCAN) < 0) {
7498 nlmsg_free(msg);
7499 return;
7500 }
7501
Johannes Berg463d0182009-07-14 00:33:35 +02007502 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7503 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007504}
7505
Johannes Berg2a519312009-02-10 21:25:55 +01007506void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
7507 struct net_device *netdev)
7508{
7509 struct sk_buff *msg;
7510
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007511 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007512 if (!msg)
7513 return;
7514
Johannes Berga538e2d2009-06-16 19:56:42 +02007515 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7516 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007517 nlmsg_free(msg);
7518 return;
7519 }
7520
Johannes Berg463d0182009-07-14 00:33:35 +02007521 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7522 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007523}
7524
7525void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
7526 struct net_device *netdev)
7527{
7528 struct sk_buff *msg;
7529
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007530 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007531 if (!msg)
7532 return;
7533
Johannes Berga538e2d2009-06-16 19:56:42 +02007534 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
7535 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007536 nlmsg_free(msg);
7537 return;
7538 }
7539
Johannes Berg463d0182009-07-14 00:33:35 +02007540 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7541 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007542}
7543
Luciano Coelho807f8a82011-05-11 17:09:35 +03007544void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7545 struct net_device *netdev)
7546{
7547 struct sk_buff *msg;
7548
7549 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7550 if (!msg)
7551 return;
7552
7553 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7554 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7555 nlmsg_free(msg);
7556 return;
7557 }
7558
7559 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7560 nl80211_scan_mcgrp.id, GFP_KERNEL);
7561}
7562
7563void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7564 struct net_device *netdev, u32 cmd)
7565{
7566 struct sk_buff *msg;
7567
7568 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
7569 if (!msg)
7570 return;
7571
7572 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7573 nlmsg_free(msg);
7574 return;
7575 }
7576
7577 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7578 nl80211_scan_mcgrp.id, GFP_KERNEL);
7579}
7580
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007581/*
7582 * This can happen on global regulatory changes or device specific settings
7583 * based on custom world regulatory domains.
7584 */
7585void nl80211_send_reg_change_event(struct regulatory_request *request)
7586{
7587 struct sk_buff *msg;
7588 void *hdr;
7589
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007591 if (!msg)
7592 return;
7593
7594 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7595 if (!hdr) {
7596 nlmsg_free(msg);
7597 return;
7598 }
7599
7600 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007601 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7602 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007603
David S. Miller9360ffd2012-03-29 04:41:26 -04007604 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7605 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7606 NL80211_REGDOM_TYPE_WORLD))
7607 goto nla_put_failure;
7608 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7609 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7610 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7611 goto nla_put_failure;
7612 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7613 request->intersect) {
7614 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7615 NL80211_REGDOM_TYPE_INTERSECTION))
7616 goto nla_put_failure;
7617 } else {
7618 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7619 NL80211_REGDOM_TYPE_COUNTRY) ||
7620 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7621 request->alpha2))
7622 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007623 }
7624
David S. Miller9360ffd2012-03-29 04:41:26 -04007625 if (wiphy_idx_valid(request->wiphy_idx) &&
7626 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7627 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007628
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007629 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007630
Johannes Bergbc43b282009-07-25 10:54:13 +02007631 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007632 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007633 GFP_ATOMIC);
7634 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007635
7636 return;
7637
7638nla_put_failure:
7639 genlmsg_cancel(msg, hdr);
7640 nlmsg_free(msg);
7641}
7642
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007643static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7644 struct net_device *netdev,
7645 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007646 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007647{
7648 struct sk_buff *msg;
7649 void *hdr;
7650
Johannes Berge6d6e342009-07-01 21:26:47 +02007651 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007652 if (!msg)
7653 return;
7654
7655 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7656 if (!hdr) {
7657 nlmsg_free(msg);
7658 return;
7659 }
7660
David S. Miller9360ffd2012-03-29 04:41:26 -04007661 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7662 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7663 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7664 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007665
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007666 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007667
Johannes Berg463d0182009-07-14 00:33:35 +02007668 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7669 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007670 return;
7671
7672 nla_put_failure:
7673 genlmsg_cancel(msg, hdr);
7674 nlmsg_free(msg);
7675}
7676
7677void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007678 struct net_device *netdev, const u8 *buf,
7679 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007680{
7681 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007682 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007683}
7684
7685void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7686 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007687 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007688{
Johannes Berge6d6e342009-07-01 21:26:47 +02007689 nl80211_send_mlme_event(rdev, netdev, buf, len,
7690 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007691}
7692
Jouni Malinen53b46b82009-03-27 20:53:56 +02007693void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007694 struct net_device *netdev, const u8 *buf,
7695 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007696{
7697 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007698 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007699}
7700
Jouni Malinen53b46b82009-03-27 20:53:56 +02007701void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7702 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007703 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007704{
7705 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007706 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007707}
7708
Jouni Malinencf4e5942010-12-16 00:52:40 +02007709void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7710 struct net_device *netdev, const u8 *buf,
7711 size_t len, gfp_t gfp)
7712{
7713 nl80211_send_mlme_event(rdev, netdev, buf, len,
7714 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7715}
7716
7717void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7718 struct net_device *netdev, const u8 *buf,
7719 size_t len, gfp_t gfp)
7720{
7721 nl80211_send_mlme_event(rdev, netdev, buf, len,
7722 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7723}
7724
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007725static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7726 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007727 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007728{
7729 struct sk_buff *msg;
7730 void *hdr;
7731
Johannes Berge6d6e342009-07-01 21:26:47 +02007732 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007733 if (!msg)
7734 return;
7735
7736 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7737 if (!hdr) {
7738 nlmsg_free(msg);
7739 return;
7740 }
7741
David S. Miller9360ffd2012-03-29 04:41:26 -04007742 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7743 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7744 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
7745 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
7746 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03007747
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007748 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03007749
Johannes Berg463d0182009-07-14 00:33:35 +02007750 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7751 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007752 return;
7753
7754 nla_put_failure:
7755 genlmsg_cancel(msg, hdr);
7756 nlmsg_free(msg);
7757}
7758
7759void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007760 struct net_device *netdev, const u8 *addr,
7761 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007762{
7763 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02007764 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007765}
7766
7767void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007768 struct net_device *netdev, const u8 *addr,
7769 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007770{
Johannes Berge6d6e342009-07-01 21:26:47 +02007771 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
7772 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007773}
7774
Samuel Ortizb23aa672009-07-01 21:26:54 +02007775void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
7776 struct net_device *netdev, const u8 *bssid,
7777 const u8 *req_ie, size_t req_ie_len,
7778 const u8 *resp_ie, size_t resp_ie_len,
7779 u16 status, gfp_t gfp)
7780{
7781 struct sk_buff *msg;
7782 void *hdr;
7783
7784 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7785 if (!msg)
7786 return;
7787
7788 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
7789 if (!hdr) {
7790 nlmsg_free(msg);
7791 return;
7792 }
7793
David S. Miller9360ffd2012-03-29 04:41:26 -04007794 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7795 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7796 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
7797 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
7798 (req_ie &&
7799 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7800 (resp_ie &&
7801 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7802 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007803
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007804 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007805
Johannes Berg463d0182009-07-14 00:33:35 +02007806 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7807 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007808 return;
7809
7810 nla_put_failure:
7811 genlmsg_cancel(msg, hdr);
7812 nlmsg_free(msg);
7813
7814}
7815
7816void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
7817 struct net_device *netdev, const u8 *bssid,
7818 const u8 *req_ie, size_t req_ie_len,
7819 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
7820{
7821 struct sk_buff *msg;
7822 void *hdr;
7823
7824 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7825 if (!msg)
7826 return;
7827
7828 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
7829 if (!hdr) {
7830 nlmsg_free(msg);
7831 return;
7832 }
7833
David S. Miller9360ffd2012-03-29 04:41:26 -04007834 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7835 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7836 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
7837 (req_ie &&
7838 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
7839 (resp_ie &&
7840 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
7841 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007842
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007843 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007844
Johannes Berg463d0182009-07-14 00:33:35 +02007845 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7846 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007847 return;
7848
7849 nla_put_failure:
7850 genlmsg_cancel(msg, hdr);
7851 nlmsg_free(msg);
7852
7853}
7854
7855void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
7856 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02007857 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02007858{
7859 struct sk_buff *msg;
7860 void *hdr;
7861
Johannes Berg667503dd2009-07-07 03:56:11 +02007862 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007863 if (!msg)
7864 return;
7865
7866 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
7867 if (!hdr) {
7868 nlmsg_free(msg);
7869 return;
7870 }
7871
David S. Miller9360ffd2012-03-29 04:41:26 -04007872 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7873 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7874 (from_ap && reason &&
7875 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
7876 (from_ap &&
7877 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
7878 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
7879 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007880
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007881 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007882
Johannes Berg463d0182009-07-14 00:33:35 +02007883 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7884 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007885 return;
7886
7887 nla_put_failure:
7888 genlmsg_cancel(msg, hdr);
7889 nlmsg_free(msg);
7890
7891}
7892
Johannes Berg04a773a2009-04-19 21:24:32 +02007893void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
7894 struct net_device *netdev, const u8 *bssid,
7895 gfp_t gfp)
7896{
7897 struct sk_buff *msg;
7898 void *hdr;
7899
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007900 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007901 if (!msg)
7902 return;
7903
7904 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
7905 if (!hdr) {
7906 nlmsg_free(msg);
7907 return;
7908 }
7909
David S. Miller9360ffd2012-03-29 04:41:26 -04007910 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7911 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7912 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
7913 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02007914
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007915 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02007916
Johannes Berg463d0182009-07-14 00:33:35 +02007917 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7918 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02007919 return;
7920
7921 nla_put_failure:
7922 genlmsg_cancel(msg, hdr);
7923 nlmsg_free(msg);
7924}
7925
Javier Cardonac93b5e72011-04-07 15:08:34 -07007926void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
7927 struct net_device *netdev,
7928 const u8 *macaddr, const u8* ie, u8 ie_len,
7929 gfp_t gfp)
7930{
7931 struct sk_buff *msg;
7932 void *hdr;
7933
7934 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7935 if (!msg)
7936 return;
7937
7938 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
7939 if (!hdr) {
7940 nlmsg_free(msg);
7941 return;
7942 }
7943
David S. Miller9360ffd2012-03-29 04:41:26 -04007944 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7945 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7946 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
7947 (ie_len && ie &&
7948 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
7949 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07007950
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007951 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07007952
7953 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7954 nl80211_mlme_mcgrp.id, gfp);
7955 return;
7956
7957 nla_put_failure:
7958 genlmsg_cancel(msg, hdr);
7959 nlmsg_free(msg);
7960}
7961
Jouni Malinena3b8b052009-03-27 21:59:49 +02007962void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
7963 struct net_device *netdev, const u8 *addr,
7964 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02007965 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02007966{
7967 struct sk_buff *msg;
7968 void *hdr;
7969
Johannes Berge6d6e342009-07-01 21:26:47 +02007970 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007971 if (!msg)
7972 return;
7973
7974 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
7975 if (!hdr) {
7976 nlmsg_free(msg);
7977 return;
7978 }
7979
David S. Miller9360ffd2012-03-29 04:41:26 -04007980 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7981 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7982 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
7983 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
7984 (key_id != -1 &&
7985 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
7986 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
7987 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02007988
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007989 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007990
Johannes Berg463d0182009-07-14 00:33:35 +02007991 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7992 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02007993 return;
7994
7995 nla_put_failure:
7996 genlmsg_cancel(msg, hdr);
7997 nlmsg_free(msg);
7998}
7999
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008000void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8001 struct ieee80211_channel *channel_before,
8002 struct ieee80211_channel *channel_after)
8003{
8004 struct sk_buff *msg;
8005 void *hdr;
8006 struct nlattr *nl_freq;
8007
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008008 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008009 if (!msg)
8010 return;
8011
8012 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8013 if (!hdr) {
8014 nlmsg_free(msg);
8015 return;
8016 }
8017
8018 /*
8019 * Since we are applying the beacon hint to a wiphy we know its
8020 * wiphy_idx is valid
8021 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008022 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8023 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008024
8025 /* Before */
8026 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8027 if (!nl_freq)
8028 goto nla_put_failure;
8029 if (nl80211_msg_put_channel(msg, channel_before))
8030 goto nla_put_failure;
8031 nla_nest_end(msg, nl_freq);
8032
8033 /* After */
8034 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8035 if (!nl_freq)
8036 goto nla_put_failure;
8037 if (nl80211_msg_put_channel(msg, channel_after))
8038 goto nla_put_failure;
8039 nla_nest_end(msg, nl_freq);
8040
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008041 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008042
Johannes Berg463d0182009-07-14 00:33:35 +02008043 rcu_read_lock();
8044 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8045 GFP_ATOMIC);
8046 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008047
8048 return;
8049
8050nla_put_failure:
8051 genlmsg_cancel(msg, hdr);
8052 nlmsg_free(msg);
8053}
8054
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008055static void nl80211_send_remain_on_chan_event(
8056 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008057 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008058 struct ieee80211_channel *chan,
8059 enum nl80211_channel_type channel_type,
8060 unsigned int duration, gfp_t gfp)
8061{
8062 struct sk_buff *msg;
8063 void *hdr;
8064
8065 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8066 if (!msg)
8067 return;
8068
8069 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8070 if (!hdr) {
8071 nlmsg_free(msg);
8072 return;
8073 }
8074
David S. Miller9360ffd2012-03-29 04:41:26 -04008075 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008076 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8077 wdev->netdev->ifindex)) ||
8078 nla_put_u32(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008079 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
8080 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
8081 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8082 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008083
David S. Miller9360ffd2012-03-29 04:41:26 -04008084 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8085 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8086 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008087
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008088 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008089
8090 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8091 nl80211_mlme_mcgrp.id, gfp);
8092 return;
8093
8094 nla_put_failure:
8095 genlmsg_cancel(msg, hdr);
8096 nlmsg_free(msg);
8097}
8098
8099void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008100 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008101 struct ieee80211_channel *chan,
8102 enum nl80211_channel_type channel_type,
8103 unsigned int duration, gfp_t gfp)
8104{
8105 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008106 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008107 channel_type, duration, gfp);
8108}
8109
8110void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008111 struct cfg80211_registered_device *rdev,
8112 struct wireless_dev *wdev,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008113 u64 cookie, struct ieee80211_channel *chan,
8114 enum nl80211_channel_type channel_type, gfp_t gfp)
8115{
8116 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008117 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008118 channel_type, 0, gfp);
8119}
8120
Johannes Berg98b62182009-12-23 13:15:44 +01008121void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8122 struct net_device *dev, const u8 *mac_addr,
8123 struct station_info *sinfo, gfp_t gfp)
8124{
8125 struct sk_buff *msg;
8126
8127 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8128 if (!msg)
8129 return;
8130
John W. Linville66266b32012-03-15 13:25:41 -04008131 if (nl80211_send_station(msg, 0, 0, 0,
8132 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008133 nlmsg_free(msg);
8134 return;
8135 }
8136
8137 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8138 nl80211_mlme_mcgrp.id, gfp);
8139}
8140
Jouni Malinenec15e682011-03-23 15:29:52 +02008141void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8142 struct net_device *dev, const u8 *mac_addr,
8143 gfp_t gfp)
8144{
8145 struct sk_buff *msg;
8146 void *hdr;
8147
8148 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8149 if (!msg)
8150 return;
8151
8152 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8153 if (!hdr) {
8154 nlmsg_free(msg);
8155 return;
8156 }
8157
David S. Miller9360ffd2012-03-29 04:41:26 -04008158 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8159 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8160 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008161
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008162 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008163
8164 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8165 nl80211_mlme_mcgrp.id, gfp);
8166 return;
8167
8168 nla_put_failure:
8169 genlmsg_cancel(msg, hdr);
8170 nlmsg_free(msg);
8171}
8172
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008173static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8174 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008175{
8176 struct wireless_dev *wdev = dev->ieee80211_ptr;
8177 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8178 struct sk_buff *msg;
8179 void *hdr;
8180 int err;
8181 u32 nlpid = ACCESS_ONCE(wdev->ap_unexpected_nlpid);
8182
8183 if (!nlpid)
8184 return false;
8185
8186 msg = nlmsg_new(100, gfp);
8187 if (!msg)
8188 return true;
8189
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008190 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008191 if (!hdr) {
8192 nlmsg_free(msg);
8193 return true;
8194 }
8195
David S. Miller9360ffd2012-03-29 04:41:26 -04008196 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8197 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8198 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8199 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008200
8201 err = genlmsg_end(msg, hdr);
8202 if (err < 0) {
8203 nlmsg_free(msg);
8204 return true;
8205 }
8206
8207 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8208 return true;
8209
8210 nla_put_failure:
8211 genlmsg_cancel(msg, hdr);
8212 nlmsg_free(msg);
8213 return true;
8214}
8215
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008216bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8217{
8218 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8219 addr, gfp);
8220}
8221
8222bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8223 const u8 *addr, gfp_t gfp)
8224{
8225 return __nl80211_unexpected_frame(dev,
8226 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8227 addr, gfp);
8228}
8229
Johannes Berg2e161f72010-08-12 15:38:38 +02008230int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008231 struct wireless_dev *wdev, u32 nlpid,
Johannes Berg804483e2012-03-05 22:18:41 +01008232 int freq, int sig_dbm,
8233 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008234{
Johannes Berg71bbc992012-06-15 15:30:18 +02008235 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008236 struct sk_buff *msg;
8237 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008238
8239 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8240 if (!msg)
8241 return -ENOMEM;
8242
Johannes Berg2e161f72010-08-12 15:38:38 +02008243 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008244 if (!hdr) {
8245 nlmsg_free(msg);
8246 return -ENOMEM;
8247 }
8248
David S. Miller9360ffd2012-03-29 04:41:26 -04008249 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008250 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8251 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008252 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8253 (sig_dbm &&
8254 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8255 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8256 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008257
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008258 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008259
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008260 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008261
8262 nla_put_failure:
8263 genlmsg_cancel(msg, hdr);
8264 nlmsg_free(msg);
8265 return -ENOBUFS;
8266}
8267
Johannes Berg2e161f72010-08-12 15:38:38 +02008268void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008269 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008270 const u8 *buf, size_t len, bool ack,
8271 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008272{
Johannes Berg71bbc992012-06-15 15:30:18 +02008273 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008274 struct sk_buff *msg;
8275 void *hdr;
8276
8277 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8278 if (!msg)
8279 return;
8280
Johannes Berg2e161f72010-08-12 15:38:38 +02008281 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008282 if (!hdr) {
8283 nlmsg_free(msg);
8284 return;
8285 }
8286
David S. Miller9360ffd2012-03-29 04:41:26 -04008287 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008288 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8289 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008290 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8291 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8292 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8293 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008294
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008295 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008296
8297 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8298 return;
8299
8300 nla_put_failure:
8301 genlmsg_cancel(msg, hdr);
8302 nlmsg_free(msg);
8303}
8304
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008305void
8306nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8307 struct net_device *netdev,
8308 enum nl80211_cqm_rssi_threshold_event rssi_event,
8309 gfp_t gfp)
8310{
8311 struct sk_buff *msg;
8312 struct nlattr *pinfoattr;
8313 void *hdr;
8314
8315 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8316 if (!msg)
8317 return;
8318
8319 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8320 if (!hdr) {
8321 nlmsg_free(msg);
8322 return;
8323 }
8324
David S. Miller9360ffd2012-03-29 04:41:26 -04008325 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8326 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8327 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008328
8329 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8330 if (!pinfoattr)
8331 goto nla_put_failure;
8332
David S. Miller9360ffd2012-03-29 04:41:26 -04008333 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8334 rssi_event))
8335 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008336
8337 nla_nest_end(msg, pinfoattr);
8338
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008339 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008340
8341 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8342 nl80211_mlme_mcgrp.id, gfp);
8343 return;
8344
8345 nla_put_failure:
8346 genlmsg_cancel(msg, hdr);
8347 nlmsg_free(msg);
8348}
8349
Johannes Berge5497d72011-07-05 16:35:40 +02008350void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8351 struct net_device *netdev, const u8 *bssid,
8352 const u8 *replay_ctr, gfp_t gfp)
8353{
8354 struct sk_buff *msg;
8355 struct nlattr *rekey_attr;
8356 void *hdr;
8357
8358 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8359 if (!msg)
8360 return;
8361
8362 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8363 if (!hdr) {
8364 nlmsg_free(msg);
8365 return;
8366 }
8367
David S. Miller9360ffd2012-03-29 04:41:26 -04008368 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8369 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8370 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8371 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008372
8373 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8374 if (!rekey_attr)
8375 goto nla_put_failure;
8376
David S. Miller9360ffd2012-03-29 04:41:26 -04008377 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8378 NL80211_REPLAY_CTR_LEN, replay_ctr))
8379 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008380
8381 nla_nest_end(msg, rekey_attr);
8382
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008383 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008384
8385 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8386 nl80211_mlme_mcgrp.id, gfp);
8387 return;
8388
8389 nla_put_failure:
8390 genlmsg_cancel(msg, hdr);
8391 nlmsg_free(msg);
8392}
8393
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008394void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8395 struct net_device *netdev, int index,
8396 const u8 *bssid, bool preauth, gfp_t gfp)
8397{
8398 struct sk_buff *msg;
8399 struct nlattr *attr;
8400 void *hdr;
8401
8402 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8403 if (!msg)
8404 return;
8405
8406 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8407 if (!hdr) {
8408 nlmsg_free(msg);
8409 return;
8410 }
8411
David S. Miller9360ffd2012-03-29 04:41:26 -04008412 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8413 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8414 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008415
8416 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8417 if (!attr)
8418 goto nla_put_failure;
8419
David S. Miller9360ffd2012-03-29 04:41:26 -04008420 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8421 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8422 (preauth &&
8423 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8424 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008425
8426 nla_nest_end(msg, attr);
8427
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008428 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008429
8430 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8431 nl80211_mlme_mcgrp.id, gfp);
8432 return;
8433
8434 nla_put_failure:
8435 genlmsg_cancel(msg, hdr);
8436 nlmsg_free(msg);
8437}
8438
Thomas Pedersen53145262012-04-06 13:35:47 -07008439void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8440 struct net_device *netdev, int freq,
8441 enum nl80211_channel_type type, gfp_t gfp)
8442{
8443 struct sk_buff *msg;
8444 void *hdr;
8445
8446 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8447 if (!msg)
8448 return;
8449
8450 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8451 if (!hdr) {
8452 nlmsg_free(msg);
8453 return;
8454 }
8455
John W. Linville7eab0f62012-04-12 14:25:14 -04008456 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8457 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8458 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8459 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008460
8461 genlmsg_end(msg, hdr);
8462
8463 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8464 nl80211_mlme_mcgrp.id, gfp);
8465 return;
8466
8467 nla_put_failure:
8468 genlmsg_cancel(msg, hdr);
8469 nlmsg_free(msg);
8470}
8471
Johannes Bergc063dbf2010-11-24 08:10:05 +01008472void
8473nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8474 struct net_device *netdev, const u8 *peer,
8475 u32 num_packets, gfp_t gfp)
8476{
8477 struct sk_buff *msg;
8478 struct nlattr *pinfoattr;
8479 void *hdr;
8480
8481 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8482 if (!msg)
8483 return;
8484
8485 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8486 if (!hdr) {
8487 nlmsg_free(msg);
8488 return;
8489 }
8490
David S. Miller9360ffd2012-03-29 04:41:26 -04008491 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8492 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8493 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8494 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008495
8496 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8497 if (!pinfoattr)
8498 goto nla_put_failure;
8499
David S. Miller9360ffd2012-03-29 04:41:26 -04008500 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8501 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008502
8503 nla_nest_end(msg, pinfoattr);
8504
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008505 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008506
8507 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8508 nl80211_mlme_mcgrp.id, gfp);
8509 return;
8510
8511 nla_put_failure:
8512 genlmsg_cancel(msg, hdr);
8513 nlmsg_free(msg);
8514}
8515
Johannes Berg7f6cf312011-11-04 11:18:15 +01008516void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8517 u64 cookie, bool acked, gfp_t gfp)
8518{
8519 struct wireless_dev *wdev = dev->ieee80211_ptr;
8520 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8521 struct sk_buff *msg;
8522 void *hdr;
8523 int err;
8524
8525 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8526 if (!msg)
8527 return;
8528
8529 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8530 if (!hdr) {
8531 nlmsg_free(msg);
8532 return;
8533 }
8534
David S. Miller9360ffd2012-03-29 04:41:26 -04008535 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8536 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8537 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8538 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8539 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8540 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008541
8542 err = genlmsg_end(msg, hdr);
8543 if (err < 0) {
8544 nlmsg_free(msg);
8545 return;
8546 }
8547
8548 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8549 nl80211_mlme_mcgrp.id, gfp);
8550 return;
8551
8552 nla_put_failure:
8553 genlmsg_cancel(msg, hdr);
8554 nlmsg_free(msg);
8555}
8556EXPORT_SYMBOL(cfg80211_probe_status);
8557
Johannes Berg5e7602302011-11-04 11:18:17 +01008558void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8559 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008560 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e7602302011-11-04 11:18:17 +01008561{
8562 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8563 struct sk_buff *msg;
8564 void *hdr;
8565 u32 nlpid = ACCESS_ONCE(rdev->ap_beacons_nlpid);
8566
8567 if (!nlpid)
8568 return;
8569
8570 msg = nlmsg_new(len + 100, gfp);
8571 if (!msg)
8572 return;
8573
8574 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8575 if (!hdr) {
8576 nlmsg_free(msg);
8577 return;
8578 }
8579
David S. Miller9360ffd2012-03-29 04:41:26 -04008580 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8581 (freq &&
8582 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8583 (sig_dbm &&
8584 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8585 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8586 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +01008587
8588 genlmsg_end(msg, hdr);
8589
8590 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
8591 return;
8592
8593 nla_put_failure:
8594 genlmsg_cancel(msg, hdr);
8595 nlmsg_free(msg);
8596}
8597EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8598
Jouni Malinen026331c2010-02-15 12:53:10 +02008599static int nl80211_netlink_notify(struct notifier_block * nb,
8600 unsigned long state,
8601 void *_notify)
8602{
8603 struct netlink_notify *notify = _notify;
8604 struct cfg80211_registered_device *rdev;
8605 struct wireless_dev *wdev;
8606
8607 if (state != NETLINK_URELEASE)
8608 return NOTIFY_DONE;
8609
8610 rcu_read_lock();
8611
Johannes Berg5e7602302011-11-04 11:18:17 +01008612 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008613 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02008614 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Johannes Berg5e7602302011-11-04 11:18:17 +01008615 if (rdev->ap_beacons_nlpid == notify->pid)
8616 rdev->ap_beacons_nlpid = 0;
8617 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008618
8619 rcu_read_unlock();
8620
8621 return NOTIFY_DONE;
8622}
8623
8624static struct notifier_block nl80211_netlink_notifier = {
8625 .notifier_call = nl80211_netlink_notify,
8626};
8627
Johannes Berg55682962007-09-20 13:09:35 -04008628/* initialisation/exit functions */
8629
8630int nl80211_init(void)
8631{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008632 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008633
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008634 err = genl_register_family_with_ops(&nl80211_fam,
8635 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008636 if (err)
8637 return err;
8638
Johannes Berg55682962007-09-20 13:09:35 -04008639 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8640 if (err)
8641 goto err_out;
8642
Johannes Berg2a519312009-02-10 21:25:55 +01008643 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8644 if (err)
8645 goto err_out;
8646
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008647 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8648 if (err)
8649 goto err_out;
8650
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008651 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
8652 if (err)
8653 goto err_out;
8654
Johannes Bergaff89a92009-07-01 21:26:51 +02008655#ifdef CONFIG_NL80211_TESTMODE
8656 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
8657 if (err)
8658 goto err_out;
8659#endif
8660
Jouni Malinen026331c2010-02-15 12:53:10 +02008661 err = netlink_register_notifier(&nl80211_netlink_notifier);
8662 if (err)
8663 goto err_out;
8664
Johannes Berg55682962007-09-20 13:09:35 -04008665 return 0;
8666 err_out:
8667 genl_unregister_family(&nl80211_fam);
8668 return err;
8669}
8670
8671void nl80211_exit(void)
8672{
Jouni Malinen026331c2010-02-15 12:53:10 +02008673 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04008674 genl_unregister_family(&nl80211_fam);
8675}