blob: 801e57da88b466f9515b07dbd4961d1234dd71b8 [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 Berg2a0e0472013-01-23 22:57:40 +010022#include <net/inet_connection_sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040023#include "core.h"
24#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070025#include "reg.h"
Hila Gonene35e4d22012-06-27 17:19:42 +030026#include "rdev-ops.h"
Johannes Berg55682962007-09-20 13:09:35 -040027
Jouni Malinen5fb628e2011-08-10 23:54:35 +030028static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
29 struct genl_info *info,
30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit);
32
Johannes Berg4c476992010-10-04 21:36:35 +020033static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info);
37
Johannes Berg55682962007-09-20 13:09:35 -040038/* the netlink family */
39static struct genl_family nl80211_fam = {
Marcel Holtmannfb4e1562013-04-28 16:22:06 -070040 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
41 .name = NL80211_GENL_NAME, /* have users key off the name instead */
42 .hdrsize = 0, /* no private header */
43 .version = 1, /* no particular meaning now */
Johannes Berg55682962007-09-20 13:09:35 -040044 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020045 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020046 .pre_doit = nl80211_pre_doit,
47 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040048};
49
Johannes Berg89a54e42012-06-15 14:33:17 +020050/* returns ERR_PTR values */
51static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040053{
Johannes Berg89a54e42012-06-15 14:33:17 +020054 struct cfg80211_registered_device *rdev;
55 struct wireless_dev *result = NULL;
56 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
57 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
58 u64 wdev_id;
59 int wiphy_idx = -1;
60 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg5fe231e2013-05-08 21:45:15 +020062 ASSERT_RTNL();
Johannes Berg55682962007-09-20 13:09:35 -040063
Johannes Berg89a54e42012-06-15 14:33:17 +020064 if (!have_ifidx && !have_wdev_id)
65 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040066
Johannes Berg89a54e42012-06-15 14:33:17 +020067 if (have_ifidx)
68 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
69 if (have_wdev_id) {
70 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
71 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040072 }
73
Johannes Berg89a54e42012-06-15 14:33:17 +020074 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
75 struct wireless_dev *wdev;
76
77 if (wiphy_net(&rdev->wiphy) != netns)
78 continue;
79
80 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
81 continue;
82
Johannes Berg89a54e42012-06-15 14:33:17 +020083 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 }
Johannes Berg89a54e42012-06-15 14:33:17 +020094
95 if (result)
96 break;
97 }
98
99 if (result)
100 return result;
101 return ERR_PTR(-ENODEV);
Johannes Berg55682962007-09-20 13:09:35 -0400102}
103
Johannes Berga9455402012-06-15 13:32:49 +0200104static struct cfg80211_registered_device *
Johannes Berg878d9ec2012-06-15 14:18:32 +0200105__cfg80211_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berga9455402012-06-15 13:32:49 +0200106{
Johannes Berg7fee4772012-06-15 14:09:58 +0200107 struct cfg80211_registered_device *rdev = NULL, *tmp;
108 struct net_device *netdev;
Johannes Berga9455402012-06-15 13:32:49 +0200109
Johannes Berg5fe231e2013-05-08 21:45:15 +0200110 ASSERT_RTNL();
Johannes Berga9455402012-06-15 13:32:49 +0200111
Johannes Berg878d9ec2012-06-15 14:18:32 +0200112 if (!attrs[NL80211_ATTR_WIPHY] &&
Johannes Berg89a54e42012-06-15 14:33:17 +0200113 !attrs[NL80211_ATTR_IFINDEX] &&
114 !attrs[NL80211_ATTR_WDEV])
Johannes Berg7fee4772012-06-15 14:09:58 +0200115 return ERR_PTR(-EINVAL);
116
Johannes Berg878d9ec2012-06-15 14:18:32 +0200117 if (attrs[NL80211_ATTR_WIPHY])
Johannes Berg7fee4772012-06-15 14:09:58 +0200118 rdev = cfg80211_rdev_by_wiphy_idx(
Johannes Berg878d9ec2012-06-15 14:18:32 +0200119 nla_get_u32(attrs[NL80211_ATTR_WIPHY]));
Johannes Berga9455402012-06-15 13:32:49 +0200120
Johannes Berg89a54e42012-06-15 14:33:17 +0200121 if (attrs[NL80211_ATTR_WDEV]) {
122 u64 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
123 struct wireless_dev *wdev;
124 bool found = false;
125
126 tmp = cfg80211_rdev_by_wiphy_idx(wdev_id >> 32);
127 if (tmp) {
128 /* make sure wdev exists */
Johannes Berg89a54e42012-06-15 14:33:17 +0200129 list_for_each_entry(wdev, &tmp->wdev_list, list) {
130 if (wdev->identifier != (u32)wdev_id)
131 continue;
132 found = true;
133 break;
134 }
Johannes Berg89a54e42012-06-15 14:33:17 +0200135
136 if (!found)
137 tmp = NULL;
138
139 if (rdev && tmp != rdev)
140 return ERR_PTR(-EINVAL);
141 rdev = tmp;
142 }
143 }
144
Johannes Berg878d9ec2012-06-15 14:18:32 +0200145 if (attrs[NL80211_ATTR_IFINDEX]) {
146 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200147 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200148 if (netdev) {
149 if (netdev->ieee80211_ptr)
150 tmp = wiphy_to_dev(
151 netdev->ieee80211_ptr->wiphy);
152 else
153 tmp = NULL;
154
155 dev_put(netdev);
156
157 /* not wireless device -- return error */
158 if (!tmp)
159 return ERR_PTR(-EINVAL);
160
161 /* mismatch -- return error */
162 if (rdev && tmp != rdev)
163 return ERR_PTR(-EINVAL);
164
165 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200166 }
Johannes Berga9455402012-06-15 13:32:49 +0200167 }
168
Johannes Berg4f7eff12012-06-15 14:14:22 +0200169 if (!rdev)
170 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200171
Johannes Berg4f7eff12012-06-15 14:14:22 +0200172 if (netns != wiphy_net(&rdev->wiphy))
173 return ERR_PTR(-ENODEV);
174
175 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200176}
177
178/*
179 * This function returns a pointer to the driver
180 * that the genl_info item that is passed refers to.
Johannes Berga9455402012-06-15 13:32:49 +0200181 *
182 * The result of this can be a PTR_ERR and hence must
183 * be checked with IS_ERR() for errors.
184 */
185static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200186cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200187{
Johannes Berg5fe231e2013-05-08 21:45:15 +0200188 return __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200189}
190
Johannes Berg55682962007-09-20 13:09:35 -0400191/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000192static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400193 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
194 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700195 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200196 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100197
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200198 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530199 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Johannes Berg3d9d1d62012-11-08 23:14:50 +0100200 [NL80211_ATTR_CHANNEL_WIDTH] = { .type = NLA_U32 },
201 [NL80211_ATTR_CENTER_FREQ1] = { .type = NLA_U32 },
202 [NL80211_ATTR_CENTER_FREQ2] = { .type = NLA_U32 },
203
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200204 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
205 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
206 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
207 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100208 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400209
210 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
211 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
212 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100213
Eliad Pellere007b852011-11-24 18:13:56 +0200214 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
215 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100216
Johannes Bergb9454e82009-07-08 13:29:08 +0200217 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100218 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
219 .len = WLAN_MAX_KEY_LEN },
220 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
221 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
222 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200223 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200224 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100225
226 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
227 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
228 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
229 .len = IEEE80211_MAX_DATA_LEN },
230 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
231 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100232 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
233 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
234 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
235 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
236 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100237 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100238 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200239 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100240 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800241 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100242 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300243
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700244 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
245 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
246
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300247 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
248 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
249 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200250 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
251 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100252 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300253
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800254 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700255 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700256
Johannes Berg6c739412011-11-03 09:27:01 +0100257 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200258
259 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
260 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
261 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100262 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
263 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200264
265 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
266 .len = IEEE80211_MAX_SSID_LEN },
267 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
268 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200269 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300270 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300271 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300272 [NL80211_ATTR_STA_FLAGS2] = {
273 .len = sizeof(struct nl80211_sta_flag_update),
274 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300275 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300276 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
277 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200278 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
279 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
280 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200281 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100282 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100283 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
284 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100285 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
286 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200287 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200288 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
289 .len = IEEE80211_MAX_DATA_LEN },
290 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200291 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200292 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300293 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200294 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300295 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
296 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200297 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900298 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
299 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100300 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100301 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100302 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200303 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700304 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300305 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200306 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200307 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300308 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300309 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
310 .len = IEEE80211_MAX_DATA_LEN },
311 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
312 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530313 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300314 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530315 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300316 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
317 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
318 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
319 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
320 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100321 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200322 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
323 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700324 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800325 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
326 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
327 .len = NL80211_HT_CAPABILITY_LEN
328 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100329 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530330 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530331 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200332 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700333 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300334 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000335 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700336 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg53cabad2012-11-14 15:17:28 +0100337 [NL80211_ATTR_P2P_CTWINDOW] = { .type = NLA_U8 },
338 [NL80211_ATTR_P2P_OPPPS] = { .type = NLA_U8 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +0530339 [NL80211_ATTR_ACL_POLICY] = {. type = NLA_U32 },
340 [NL80211_ATTR_MAC_ADDRS] = { .type = NLA_NESTED },
Jouni Malinen9d62a982013-02-14 21:10:13 +0200341 [NL80211_ATTR_STA_CAPABILITY] = { .type = NLA_U16 },
342 [NL80211_ATTR_STA_EXT_CAPABILITY] = { .type = NLA_BINARY, },
Johannes Berg3713b4e2013-02-14 16:19:38 +0100343 [NL80211_ATTR_SPLIT_WIPHY_DUMP] = { .type = NLA_FLAG, },
Johannes Bergee2aca32013-02-21 17:36:01 +0100344 [NL80211_ATTR_DISABLE_VHT] = { .type = NLA_FLAG },
345 [NL80211_ATTR_VHT_CAPABILITY_MASK] = {
346 .len = NL80211_VHT_CAPABILITY_LEN,
347 },
Jouni Malinen355199e2013-02-27 17:14:27 +0200348 [NL80211_ATTR_MDID] = { .type = NLA_U16 },
349 [NL80211_ATTR_IE_RIC] = { .type = NLA_BINARY,
350 .len = IEEE80211_MAX_DATA_LEN },
Jouni Malinen5e4b6f52013-05-16 20:11:08 +0300351 [NL80211_ATTR_PEER_AID] = { .type = NLA_U16 },
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +0200352 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
353 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
354 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
355 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
356 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
Sunil Duttc01fc9a2013-10-09 20:45:21 +0530357 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
358 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200359 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +0100360 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
Johannes Bergad7e7182013-11-13 13:37:47 +0100361 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
362 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
363 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
Johannes Berg55682962007-09-20 13:09:35 -0400364};
365
Johannes Berge31b8212010-10-05 19:39:30 +0200366/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000367static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200368 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200369 [NL80211_KEY_IDX] = { .type = NLA_U8 },
370 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200371 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200372 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
373 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200374 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100375 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
376};
377
378/* policy for the key default flags */
379static const struct nla_policy
380nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
381 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
382 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200383};
384
Johannes Bergff1b6e62011-05-04 15:37:28 +0200385/* policy for WoWLAN attributes */
386static const struct nla_policy
387nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
388 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
389 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
390 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
391 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200392 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
393 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
394 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
395 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100396 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
397};
398
399static const struct nla_policy
400nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
401 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
402 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
403 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
404 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
405 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
406 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
407 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
408 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
409 },
410 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
411 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
412 },
413 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
414 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
415 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200416};
417
Amitkumar Karwarbe29b992013-06-28 11:51:26 -0700418/* policy for coalesce rule attributes */
419static const struct nla_policy
420nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
421 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
422 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
423 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
424};
425
Johannes Berge5497d72011-07-05 16:35:40 +0200426/* policy for GTK rekey offload attributes */
427static const struct nla_policy
428nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
429 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
430 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
431 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
432};
433
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300434static const struct nla_policy
435nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200436 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300437 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700438 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300439};
440
Johannes Berg97990a02013-04-19 01:02:55 +0200441static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
442 struct netlink_callback *cb,
443 struct cfg80211_registered_device **rdev,
444 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100445{
Johannes Berg67748892010-10-04 21:14:06 +0200446 int err;
447
Johannes Berg67748892010-10-04 21:14:06 +0200448 rtnl_lock();
449
Johannes Berg97990a02013-04-19 01:02:55 +0200450 if (!cb->args[0]) {
451 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
452 nl80211_fam.attrbuf, nl80211_fam.maxattr,
453 nl80211_policy);
454 if (err)
455 goto out_unlock;
456
457 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
458 nl80211_fam.attrbuf);
459 if (IS_ERR(*wdev)) {
460 err = PTR_ERR(*wdev);
461 goto out_unlock;
462 }
463 *rdev = wiphy_to_dev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200464 /* 0 is the first index - add 1 to parse only once */
465 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200466 cb->args[1] = (*wdev)->identifier;
467 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200468 /* subtract the 1 again here */
469 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200470 struct wireless_dev *tmp;
471
472 if (!wiphy) {
473 err = -ENODEV;
474 goto out_unlock;
475 }
476 *rdev = wiphy_to_dev(wiphy);
477 *wdev = NULL;
478
Johannes Berg97990a02013-04-19 01:02:55 +0200479 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
480 if (tmp->identifier == cb->args[1]) {
481 *wdev = tmp;
482 break;
483 }
484 }
Johannes Berg97990a02013-04-19 01:02:55 +0200485
486 if (!*wdev) {
487 err = -ENODEV;
488 goto out_unlock;
489 }
Johannes Berg67748892010-10-04 21:14:06 +0200490 }
491
Johannes Berg67748892010-10-04 21:14:06 +0200492 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200493 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200494 rtnl_unlock();
495 return err;
496}
497
Johannes Berg97990a02013-04-19 01:02:55 +0200498static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200499{
Johannes Berg67748892010-10-04 21:14:06 +0200500 rtnl_unlock();
501}
502
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100503/* IE validation */
504static bool is_valid_ie_attr(const struct nlattr *attr)
505{
506 const u8 *pos;
507 int len;
508
509 if (!attr)
510 return true;
511
512 pos = nla_data(attr);
513 len = nla_len(attr);
514
515 while (len) {
516 u8 elemlen;
517
518 if (len < 2)
519 return false;
520 len -= 2;
521
522 elemlen = pos[1];
523 if (elemlen > len)
524 return false;
525
526 len -= elemlen;
527 pos += 2 + elemlen;
528 }
529
530 return true;
531}
532
Johannes Berg55682962007-09-20 13:09:35 -0400533/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000534static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400535 int flags, u8 cmd)
536{
537 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000538 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400539}
540
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400541static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100542 struct ieee80211_channel *chan,
543 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400544{
David S. Miller9360ffd2012-03-29 04:41:26 -0400545 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
546 chan->center_freq))
547 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400548
David S. Miller9360ffd2012-03-29 04:41:26 -0400549 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
550 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
551 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200552 if (chan->flags & IEEE80211_CHAN_NO_IR) {
553 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
554 goto nla_put_failure;
555 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
556 goto nla_put_failure;
557 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100558 if (chan->flags & IEEE80211_CHAN_RADAR) {
559 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
560 goto nla_put_failure;
561 if (large) {
562 u32 time;
563
564 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
565
566 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
567 chan->dfs_state))
568 goto nla_put_failure;
569 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
570 time))
571 goto nla_put_failure;
572 }
573 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400574
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100575 if (large) {
576 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
577 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
578 goto nla_put_failure;
579 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
580 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
581 goto nla_put_failure;
582 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
583 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
584 goto nla_put_failure;
585 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
586 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
587 goto nla_put_failure;
588 }
589
David S. Miller9360ffd2012-03-29 04:41:26 -0400590 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
591 DBM_TO_MBM(chan->max_power)))
592 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400593
594 return 0;
595
596 nla_put_failure:
597 return -ENOBUFS;
598}
599
Johannes Berg55682962007-09-20 13:09:35 -0400600/* netlink command implementations */
601
Johannes Bergb9454e82009-07-08 13:29:08 +0200602struct key_parse {
603 struct key_params p;
604 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200605 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200606 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100607 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200608};
609
610static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
611{
612 struct nlattr *tb[NL80211_KEY_MAX + 1];
613 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
614 nl80211_key_policy);
615 if (err)
616 return err;
617
618 k->def = !!tb[NL80211_KEY_DEFAULT];
619 k->defmgmt = !!tb[NL80211_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 Bergb9454e82009-07-08 13:29:08 +0200628 if (tb[NL80211_KEY_IDX])
629 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
630
631 if (tb[NL80211_KEY_DATA]) {
632 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
633 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
634 }
635
636 if (tb[NL80211_KEY_SEQ]) {
637 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
638 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
639 }
640
641 if (tb[NL80211_KEY_CIPHER])
642 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
643
Johannes Berge31b8212010-10-05 19:39:30 +0200644 if (tb[NL80211_KEY_TYPE]) {
645 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
646 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
647 return -EINVAL;
648 }
649
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100650 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
651 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100652 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
653 tb[NL80211_KEY_DEFAULT_TYPES],
654 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100655 if (err)
656 return err;
657
658 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
659 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
660 }
661
Johannes Bergb9454e82009-07-08 13:29:08 +0200662 return 0;
663}
664
665static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
666{
667 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
668 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
669 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
670 }
671
672 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
673 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
674 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
675 }
676
677 if (info->attrs[NL80211_ATTR_KEY_IDX])
678 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
679
680 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
681 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
682
683 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
684 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
685
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100686 if (k->def) {
687 k->def_uni = true;
688 k->def_multi = true;
689 }
690 if (k->defmgmt)
691 k->def_multi = true;
692
Johannes Berge31b8212010-10-05 19:39:30 +0200693 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
694 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
695 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
696 return -EINVAL;
697 }
698
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100699 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
700 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
701 int err = nla_parse_nested(
702 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
703 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
704 nl80211_key_default_policy);
705 if (err)
706 return err;
707
708 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
709 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
710 }
711
Johannes Bergb9454e82009-07-08 13:29:08 +0200712 return 0;
713}
714
715static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
716{
717 int err;
718
719 memset(k, 0, sizeof(*k));
720 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200721 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200722
723 if (info->attrs[NL80211_ATTR_KEY])
724 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
725 else
726 err = nl80211_parse_key_old(info, k);
727
728 if (err)
729 return err;
730
731 if (k->def && k->defmgmt)
732 return -EINVAL;
733
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100734 if (k->defmgmt) {
735 if (k->def_uni || !k->def_multi)
736 return -EINVAL;
737 }
738
Johannes Bergb9454e82009-07-08 13:29:08 +0200739 if (k->idx != -1) {
740 if (k->defmgmt) {
741 if (k->idx < 4 || k->idx > 5)
742 return -EINVAL;
743 } else if (k->def) {
744 if (k->idx < 0 || k->idx > 3)
745 return -EINVAL;
746 } else {
747 if (k->idx < 0 || k->idx > 5)
748 return -EINVAL;
749 }
750 }
751
752 return 0;
753}
754
Johannes Bergfffd0932009-07-08 14:22:54 +0200755static struct cfg80211_cached_keys *
756nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530757 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200758{
759 struct key_parse parse;
760 struct nlattr *key;
761 struct cfg80211_cached_keys *result;
762 int rem, err, def = 0;
763
764 result = kzalloc(sizeof(*result), GFP_KERNEL);
765 if (!result)
766 return ERR_PTR(-ENOMEM);
767
768 result->def = -1;
769 result->defmgmt = -1;
770
771 nla_for_each_nested(key, keys, rem) {
772 memset(&parse, 0, sizeof(parse));
773 parse.idx = -1;
774
775 err = nl80211_parse_key_new(key, &parse);
776 if (err)
777 goto error;
778 err = -EINVAL;
779 if (!parse.p.key)
780 goto error;
781 if (parse.idx < 0 || parse.idx > 4)
782 goto error;
783 if (parse.def) {
784 if (def)
785 goto error;
786 def = 1;
787 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100788 if (!parse.def_uni || !parse.def_multi)
789 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200790 } else if (parse.defmgmt)
791 goto error;
792 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200793 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200794 if (err)
795 goto error;
796 result->params[parse.idx].cipher = parse.p.cipher;
797 result->params[parse.idx].key_len = parse.p.key_len;
798 result->params[parse.idx].key = result->data[parse.idx];
799 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530800
801 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
802 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
803 if (no_ht)
804 *no_ht = true;
805 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200806 }
807
808 return result;
809 error:
810 kfree(result);
811 return ERR_PTR(err);
812}
813
814static int nl80211_key_allowed(struct wireless_dev *wdev)
815{
816 ASSERT_WDEV_LOCK(wdev);
817
Johannes Bergfffd0932009-07-08 14:22:54 +0200818 switch (wdev->iftype) {
819 case NL80211_IFTYPE_AP:
820 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200821 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700822 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200823 break;
824 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200825 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200826 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200827 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200828 return -ENOLINK;
829 break;
830 default:
831 return -EINVAL;
832 }
833
834 return 0;
835}
836
Johannes Berg7527a782011-05-13 10:58:57 +0200837static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
838{
839 struct nlattr *nl_modes = nla_nest_start(msg, attr);
840 int i;
841
842 if (!nl_modes)
843 goto nla_put_failure;
844
845 i = 0;
846 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400847 if ((ifmodes & 1) && nla_put_flag(msg, i))
848 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200849 ifmodes >>= 1;
850 i++;
851 }
852
853 nla_nest_end(msg, nl_modes);
854 return 0;
855
856nla_put_failure:
857 return -ENOBUFS;
858}
859
860static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100861 struct sk_buff *msg,
862 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200863{
864 struct nlattr *nl_combis;
865 int i, j;
866
867 nl_combis = nla_nest_start(msg,
868 NL80211_ATTR_INTERFACE_COMBINATIONS);
869 if (!nl_combis)
870 goto nla_put_failure;
871
872 for (i = 0; i < wiphy->n_iface_combinations; i++) {
873 const struct ieee80211_iface_combination *c;
874 struct nlattr *nl_combi, *nl_limits;
875
876 c = &wiphy->iface_combinations[i];
877
878 nl_combi = nla_nest_start(msg, i + 1);
879 if (!nl_combi)
880 goto nla_put_failure;
881
882 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
883 if (!nl_limits)
884 goto nla_put_failure;
885
886 for (j = 0; j < c->n_limits; j++) {
887 struct nlattr *nl_limit;
888
889 nl_limit = nla_nest_start(msg, j + 1);
890 if (!nl_limit)
891 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400892 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
893 c->limits[j].max))
894 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200895 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
896 c->limits[j].types))
897 goto nla_put_failure;
898 nla_nest_end(msg, nl_limit);
899 }
900
901 nla_nest_end(msg, nl_limits);
902
David S. Miller9360ffd2012-03-29 04:41:26 -0400903 if (c->beacon_int_infra_match &&
904 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
905 goto nla_put_failure;
906 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
907 c->num_different_channels) ||
908 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
909 c->max_interfaces))
910 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100911 if (large &&
912 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
913 c->radar_detect_widths))
914 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200915
916 nla_nest_end(msg, nl_combi);
917 }
918
919 nla_nest_end(msg, nl_combis);
920
921 return 0;
922nla_put_failure:
923 return -ENOBUFS;
924}
925
Johannes Berg3713b4e2013-02-14 16:19:38 +0100926#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100927static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
928 struct sk_buff *msg)
929{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200930 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100931 struct nlattr *nl_tcp;
932
933 if (!tcp)
934 return 0;
935
936 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
937 if (!nl_tcp)
938 return -ENOBUFS;
939
940 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
941 tcp->data_payload_max))
942 return -ENOBUFS;
943
944 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
945 tcp->data_payload_max))
946 return -ENOBUFS;
947
948 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
949 return -ENOBUFS;
950
951 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
952 sizeof(*tcp->tok), tcp->tok))
953 return -ENOBUFS;
954
955 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
956 tcp->data_interval_max))
957 return -ENOBUFS;
958
959 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
960 tcp->wake_payload_max))
961 return -ENOBUFS;
962
963 nla_nest_end(msg, nl_tcp);
964 return 0;
965}
966
Johannes Berg3713b4e2013-02-14 16:19:38 +0100967static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +0100968 struct cfg80211_registered_device *dev,
969 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100970{
971 struct nlattr *nl_wowlan;
972
Johannes Berg964dc9e2013-06-03 17:25:34 +0200973 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100974 return 0;
975
976 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
977 if (!nl_wowlan)
978 return -ENOBUFS;
979
Johannes Berg964dc9e2013-06-03 17:25:34 +0200980 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100981 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200982 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100983 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200984 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100985 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200986 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100987 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200988 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100989 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200990 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100991 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200992 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100993 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200994 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100995 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
996 return -ENOBUFS;
997
Johannes Berg964dc9e2013-06-03 17:25:34 +0200998 if (dev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -0700999 struct nl80211_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +02001000 .max_patterns = dev->wiphy.wowlan->n_patterns,
1001 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1002 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1003 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001004 };
1005
1006 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1007 sizeof(pat), &pat))
1008 return -ENOBUFS;
1009 }
1010
Johannes Bergb56cf722013-02-20 01:02:38 +01001011 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1012 return -ENOBUFS;
1013
Johannes Berg3713b4e2013-02-14 16:19:38 +01001014 nla_nest_end(msg, nl_wowlan);
1015
1016 return 0;
1017}
1018#endif
1019
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001020static int nl80211_send_coalesce(struct sk_buff *msg,
1021 struct cfg80211_registered_device *dev)
1022{
1023 struct nl80211_coalesce_rule_support rule;
1024
1025 if (!dev->wiphy.coalesce)
1026 return 0;
1027
1028 rule.max_rules = dev->wiphy.coalesce->n_rules;
1029 rule.max_delay = dev->wiphy.coalesce->max_delay;
1030 rule.pat.max_patterns = dev->wiphy.coalesce->n_patterns;
1031 rule.pat.min_pattern_len = dev->wiphy.coalesce->pattern_min_len;
1032 rule.pat.max_pattern_len = dev->wiphy.coalesce->pattern_max_len;
1033 rule.pat.max_pkt_offset = dev->wiphy.coalesce->max_pkt_offset;
1034
1035 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1036 return -ENOBUFS;
1037
1038 return 0;
1039}
1040
Johannes Berg3713b4e2013-02-14 16:19:38 +01001041static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1042 struct ieee80211_supported_band *sband)
1043{
1044 struct nlattr *nl_rates, *nl_rate;
1045 struct ieee80211_rate *rate;
1046 int i;
1047
1048 /* add HT info */
1049 if (sband->ht_cap.ht_supported &&
1050 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1051 sizeof(sband->ht_cap.mcs),
1052 &sband->ht_cap.mcs) ||
1053 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1054 sband->ht_cap.cap) ||
1055 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1056 sband->ht_cap.ampdu_factor) ||
1057 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1058 sband->ht_cap.ampdu_density)))
1059 return -ENOBUFS;
1060
1061 /* add VHT info */
1062 if (sband->vht_cap.vht_supported &&
1063 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1064 sizeof(sband->vht_cap.vht_mcs),
1065 &sband->vht_cap.vht_mcs) ||
1066 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1067 sband->vht_cap.cap)))
1068 return -ENOBUFS;
1069
1070 /* add bitrates */
1071 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1072 if (!nl_rates)
1073 return -ENOBUFS;
1074
1075 for (i = 0; i < sband->n_bitrates; i++) {
1076 nl_rate = nla_nest_start(msg, i);
1077 if (!nl_rate)
1078 return -ENOBUFS;
1079
1080 rate = &sband->bitrates[i];
1081 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1082 rate->bitrate))
1083 return -ENOBUFS;
1084 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1085 nla_put_flag(msg,
1086 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1087 return -ENOBUFS;
1088
1089 nla_nest_end(msg, nl_rate);
1090 }
1091
1092 nla_nest_end(msg, nl_rates);
1093
1094 return 0;
1095}
1096
1097static int
1098nl80211_send_mgmt_stypes(struct sk_buff *msg,
1099 const struct ieee80211_txrx_stypes *mgmt_stypes)
1100{
1101 u16 stypes;
1102 struct nlattr *nl_ftypes, *nl_ifs;
1103 enum nl80211_iftype ift;
1104 int i;
1105
1106 if (!mgmt_stypes)
1107 return 0;
1108
1109 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1110 if (!nl_ifs)
1111 return -ENOBUFS;
1112
1113 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1114 nl_ftypes = nla_nest_start(msg, ift);
1115 if (!nl_ftypes)
1116 return -ENOBUFS;
1117 i = 0;
1118 stypes = mgmt_stypes[ift].tx;
1119 while (stypes) {
1120 if ((stypes & 1) &&
1121 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1122 (i << 4) | IEEE80211_FTYPE_MGMT))
1123 return -ENOBUFS;
1124 stypes >>= 1;
1125 i++;
1126 }
1127 nla_nest_end(msg, nl_ftypes);
1128 }
1129
1130 nla_nest_end(msg, nl_ifs);
1131
1132 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1133 if (!nl_ifs)
1134 return -ENOBUFS;
1135
1136 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1137 nl_ftypes = nla_nest_start(msg, ift);
1138 if (!nl_ftypes)
1139 return -ENOBUFS;
1140 i = 0;
1141 stypes = mgmt_stypes[ift].rx;
1142 while (stypes) {
1143 if ((stypes & 1) &&
1144 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1145 (i << 4) | IEEE80211_FTYPE_MGMT))
1146 return -ENOBUFS;
1147 stypes >>= 1;
1148 i++;
1149 }
1150 nla_nest_end(msg, nl_ftypes);
1151 }
1152 nla_nest_end(msg, nl_ifs);
1153
1154 return 0;
1155}
1156
Johannes Berg86e8cf92013-06-19 10:57:22 +02001157struct nl80211_dump_wiphy_state {
1158 s64 filter_wiphy;
1159 long start;
1160 long split_start, band_start, chan_start;
1161 bool split;
1162};
1163
Johannes Berg3713b4e2013-02-14 16:19:38 +01001164static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1165 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001166 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001167{
1168 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001169 struct nlattr *nl_bands, *nl_band;
1170 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001171 struct nlattr *nl_cmds;
Johannes Bergad7e7182013-11-13 13:37:47 +01001172 struct nlattr *nl_vendor_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001173 enum ieee80211_band band;
1174 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001175 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001176 const struct ieee80211_txrx_stypes *mgmt_stypes =
1177 dev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001178 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001179
Eric W. Biederman15e47302012-09-07 20:12:54 +00001180 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001181 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001182 return -ENOBUFS;
1183
Johannes Berg86e8cf92013-06-19 10:57:22 +02001184 if (WARN_ON(!state))
1185 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001186
David S. Miller9360ffd2012-03-29 04:41:26 -04001187 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001188 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1189 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001190 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001191 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001192 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001193
Johannes Berg86e8cf92013-06-19 10:57:22 +02001194 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001195 case 0:
1196 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1197 dev->wiphy.retry_short) ||
1198 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1199 dev->wiphy.retry_long) ||
1200 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1201 dev->wiphy.frag_threshold) ||
1202 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1203 dev->wiphy.rts_threshold) ||
1204 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1205 dev->wiphy.coverage_class) ||
1206 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1207 dev->wiphy.max_scan_ssids) ||
1208 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1209 dev->wiphy.max_sched_scan_ssids) ||
1210 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1211 dev->wiphy.max_scan_ie_len) ||
1212 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1213 dev->wiphy.max_sched_scan_ie_len) ||
1214 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1215 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001216 goto nla_put_failure;
1217
Johannes Berg3713b4e2013-02-14 16:19:38 +01001218 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1219 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1220 goto nla_put_failure;
1221 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1222 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1223 goto nla_put_failure;
1224 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1225 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1226 goto nla_put_failure;
1227 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1228 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1229 goto nla_put_failure;
1230 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1231 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1232 goto nla_put_failure;
1233 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1234 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001235 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001236 state->split_start++;
1237 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001238 break;
1239 case 1:
1240 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1241 sizeof(u32) * dev->wiphy.n_cipher_suites,
1242 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001243 goto nla_put_failure;
1244
Johannes Berg3713b4e2013-02-14 16:19:38 +01001245 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1246 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001247 goto nla_put_failure;
1248
Johannes Berg3713b4e2013-02-14 16:19:38 +01001249 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1250 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1251 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001252
Johannes Berg3713b4e2013-02-14 16:19:38 +01001253 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1254 dev->wiphy.available_antennas_tx) ||
1255 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1256 dev->wiphy.available_antennas_rx))
1257 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001258
Johannes Berg3713b4e2013-02-14 16:19:38 +01001259 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1260 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1261 dev->wiphy.probe_resp_offload))
1262 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001263
Johannes Berg3713b4e2013-02-14 16:19:38 +01001264 if ((dev->wiphy.available_antennas_tx ||
1265 dev->wiphy.available_antennas_rx) &&
1266 dev->ops->get_antenna) {
1267 u32 tx_ant = 0, rx_ant = 0;
1268 int res;
1269 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1270 if (!res) {
1271 if (nla_put_u32(msg,
1272 NL80211_ATTR_WIPHY_ANTENNA_TX,
1273 tx_ant) ||
1274 nla_put_u32(msg,
1275 NL80211_ATTR_WIPHY_ANTENNA_RX,
1276 rx_ant))
1277 goto nla_put_failure;
1278 }
Johannes Bergee688b002008-01-24 19:38:39 +01001279 }
1280
Johannes Berg86e8cf92013-06-19 10:57:22 +02001281 state->split_start++;
1282 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001283 break;
1284 case 2:
1285 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1286 dev->wiphy.interface_modes))
1287 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001288 state->split_start++;
1289 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001290 break;
1291 case 3:
1292 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1293 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001294 goto nla_put_failure;
1295
Johannes Berg86e8cf92013-06-19 10:57:22 +02001296 for (band = state->band_start;
1297 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001298 struct ieee80211_supported_band *sband;
1299
1300 sband = dev->wiphy.bands[band];
1301
1302 if (!sband)
1303 continue;
1304
1305 nl_band = nla_nest_start(msg, band);
1306 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001307 goto nla_put_failure;
1308
Johannes Berg86e8cf92013-06-19 10:57:22 +02001309 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001310 case 0:
1311 if (nl80211_send_band_rateinfo(msg, sband))
1312 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001313 state->chan_start++;
1314 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001315 break;
1316 default:
1317 /* add frequencies */
1318 nl_freqs = nla_nest_start(
1319 msg, NL80211_BAND_ATTR_FREQS);
1320 if (!nl_freqs)
1321 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001322
Johannes Berg86e8cf92013-06-19 10:57:22 +02001323 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001324 i < sband->n_channels;
1325 i++) {
1326 nl_freq = nla_nest_start(msg, i);
1327 if (!nl_freq)
1328 goto nla_put_failure;
1329
1330 chan = &sband->channels[i];
1331
Johannes Berg86e8cf92013-06-19 10:57:22 +02001332 if (nl80211_msg_put_channel(
1333 msg, chan,
1334 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001335 goto nla_put_failure;
1336
1337 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001338 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001339 break;
1340 }
1341 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001342 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001343 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001344 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001345 nla_nest_end(msg, nl_freqs);
1346 }
1347
1348 nla_nest_end(msg, nl_band);
1349
Johannes Berg86e8cf92013-06-19 10:57:22 +02001350 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001351 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001352 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001353 band--;
1354 break;
1355 }
Johannes Bergee688b002008-01-24 19:38:39 +01001356 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001357 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001358
Johannes Berg3713b4e2013-02-14 16:19:38 +01001359 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001360 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001361 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001362 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001363
Johannes Berg3713b4e2013-02-14 16:19:38 +01001364 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001365 if (state->band_start == 0 && state->chan_start == 0)
1366 state->split_start++;
1367 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001368 break;
1369 case 4:
1370 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1371 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001372 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001373
1374 i = 0;
1375#define CMD(op, n) \
1376 do { \
1377 if (dev->ops->op) { \
1378 i++; \
1379 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1380 goto nla_put_failure; \
1381 } \
1382 } while (0)
1383
1384 CMD(add_virtual_intf, NEW_INTERFACE);
1385 CMD(change_virtual_intf, SET_INTERFACE);
1386 CMD(add_key, NEW_KEY);
1387 CMD(start_ap, START_AP);
1388 CMD(add_station, NEW_STATION);
1389 CMD(add_mpath, NEW_MPATH);
1390 CMD(update_mesh_config, SET_MESH_CONFIG);
1391 CMD(change_bss, SET_BSS);
1392 CMD(auth, AUTHENTICATE);
1393 CMD(assoc, ASSOCIATE);
1394 CMD(deauth, DEAUTHENTICATE);
1395 CMD(disassoc, DISASSOCIATE);
1396 CMD(join_ibss, JOIN_IBSS);
1397 CMD(join_mesh, JOIN_MESH);
1398 CMD(set_pmksa, SET_PMKSA);
1399 CMD(del_pmksa, DEL_PMKSA);
1400 CMD(flush_pmksa, FLUSH_PMKSA);
1401 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1402 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1403 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1404 CMD(mgmt_tx, FRAME);
1405 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1406 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1407 i++;
1408 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1409 goto nla_put_failure;
1410 }
1411 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1412 dev->ops->join_mesh) {
1413 i++;
1414 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1415 goto nla_put_failure;
1416 }
1417 CMD(set_wds_peer, SET_WDS_PEER);
1418 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1419 CMD(tdls_mgmt, TDLS_MGMT);
1420 CMD(tdls_oper, TDLS_OPER);
1421 }
1422 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1423 CMD(sched_scan_start, START_SCHED_SCAN);
1424 CMD(probe_client, PROBE_CLIENT);
1425 CMD(set_noack_map, SET_NOACK_MAP);
1426 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1427 i++;
1428 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1429 goto nla_put_failure;
1430 }
1431 CMD(start_p2p_device, START_P2P_DEVICE);
1432 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001433 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001434 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1435 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001436 if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1437 CMD(channel_switch, CHANNEL_SWITCH);
Arend van Spriel5de17982013-04-18 15:49:00 +02001438 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001439
Kalle Valo4745fc02011-11-17 19:06:10 +02001440#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001441 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001442#endif
1443
Johannes Berg8fdc6212009-03-14 09:34:01 +01001444#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001445
Johannes Berg3713b4e2013-02-14 16:19:38 +01001446 if (dev->ops->connect || dev->ops->auth) {
1447 i++;
1448 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001449 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001450 }
1451
Johannes Berg3713b4e2013-02-14 16:19:38 +01001452 if (dev->ops->disconnect || dev->ops->deauth) {
1453 i++;
1454 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1455 goto nla_put_failure;
1456 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001457
Johannes Berg3713b4e2013-02-14 16:19:38 +01001458 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001459 state->split_start++;
1460 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001461 break;
1462 case 5:
1463 if (dev->ops->remain_on_channel &&
1464 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1465 nla_put_u32(msg,
1466 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1467 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001468 goto nla_put_failure;
1469
Johannes Berg3713b4e2013-02-14 16:19:38 +01001470 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1471 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1472 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001473
Johannes Berg3713b4e2013-02-14 16:19:38 +01001474 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1475 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001476 state->split_start++;
1477 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001478 break;
1479 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001480#ifdef CONFIG_PM
Johannes Berg86e8cf92013-06-19 10:57:22 +02001481 if (nl80211_send_wowlan(msg, dev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001482 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001483 state->split_start++;
1484 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001485 break;
1486#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001487 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001488#endif
1489 case 7:
1490 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1491 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001492 goto nla_put_failure;
1493
Johannes Berg86e8cf92013-06-19 10:57:22 +02001494 if (nl80211_put_iface_combinations(&dev->wiphy, msg,
1495 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001496 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001497
Johannes Berg86e8cf92013-06-19 10:57:22 +02001498 state->split_start++;
1499 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001500 break;
1501 case 8:
1502 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1503 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1504 dev->wiphy.ap_sme_capa))
1505 goto nla_put_failure;
1506
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001507 features = dev->wiphy.features;
1508 /*
1509 * We can only add the per-channel limit information if the
1510 * dump is split, otherwise it makes it too big. Therefore
1511 * only advertise it in that case.
1512 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001513 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001514 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1515 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001516 goto nla_put_failure;
1517
1518 if (dev->wiphy.ht_capa_mod_mask &&
1519 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1520 sizeof(*dev->wiphy.ht_capa_mod_mask),
1521 dev->wiphy.ht_capa_mod_mask))
1522 goto nla_put_failure;
1523
1524 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1525 dev->wiphy.max_acl_mac_addrs &&
1526 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1527 dev->wiphy.max_acl_mac_addrs))
1528 goto nla_put_failure;
1529
1530 /*
1531 * Any information below this point is only available to
1532 * applications that can deal with it being split. This
1533 * helps ensure that newly added capabilities don't break
1534 * older tools by overrunning their buffers.
1535 *
1536 * We still increment split_start so that in the split
1537 * case we'll continue with more data in the next round,
1538 * but break unconditionally so unsplit data stops here.
1539 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001540 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001541 break;
1542 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001543 if (dev->wiphy.extended_capabilities &&
1544 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1545 dev->wiphy.extended_capabilities_len,
1546 dev->wiphy.extended_capabilities) ||
1547 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1548 dev->wiphy.extended_capabilities_len,
1549 dev->wiphy.extended_capabilities_mask)))
1550 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001551
Johannes Bergee2aca32013-02-21 17:36:01 +01001552 if (dev->wiphy.vht_capa_mod_mask &&
1553 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1554 sizeof(*dev->wiphy.vht_capa_mod_mask),
1555 dev->wiphy.vht_capa_mod_mask))
1556 goto nla_put_failure;
1557
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001558 state->split_start++;
1559 break;
1560 case 10:
1561 if (nl80211_send_coalesce(msg, dev))
1562 goto nla_put_failure;
1563
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001564 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1565 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1566 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1567 goto nla_put_failure;
Johannes Bergad7e7182013-11-13 13:37:47 +01001568 state->split_start++;
1569 break;
1570 case 11:
1571 nl_vendor_cmds = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1572 if (!nl_vendor_cmds)
1573 goto nla_put_failure;
1574
1575 for (i = 0; i < dev->wiphy.n_vendor_commands; i++)
1576 if (nla_put(msg, i + 1,
1577 sizeof(struct nl80211_vendor_cmd_info),
1578 &dev->wiphy.vendor_commands[i].info))
1579 goto nla_put_failure;
1580 nla_nest_end(msg, nl_vendor_cmds);
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001581
Johannes Berg3713b4e2013-02-14 16:19:38 +01001582 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001583 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001584 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001585 }
Johannes Berg55682962007-09-20 13:09:35 -04001586 return genlmsg_end(msg, hdr);
1587
1588 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001589 genlmsg_cancel(msg, hdr);
1590 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001591}
1592
Johannes Berg86e8cf92013-06-19 10:57:22 +02001593static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1594 struct netlink_callback *cb,
1595 struct nl80211_dump_wiphy_state *state)
1596{
1597 struct nlattr **tb = nl80211_fam.attrbuf;
1598 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1599 tb, nl80211_fam.maxattr, nl80211_policy);
1600 /* ignore parse errors for backward compatibility */
1601 if (ret)
1602 return 0;
1603
1604 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1605 if (tb[NL80211_ATTR_WIPHY])
1606 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1607 if (tb[NL80211_ATTR_WDEV])
1608 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1609 if (tb[NL80211_ATTR_IFINDEX]) {
1610 struct net_device *netdev;
1611 struct cfg80211_registered_device *rdev;
1612 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1613
1614 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1615 if (!netdev)
1616 return -ENODEV;
1617 if (netdev->ieee80211_ptr) {
1618 rdev = wiphy_to_dev(
1619 netdev->ieee80211_ptr->wiphy);
1620 state->filter_wiphy = rdev->wiphy_idx;
1621 }
1622 dev_put(netdev);
1623 }
1624
1625 return 0;
1626}
1627
Johannes Berg55682962007-09-20 13:09:35 -04001628static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1629{
Johannes Berg645e77d2013-03-01 14:03:49 +01001630 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001631 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Johannes Berg55682962007-09-20 13:09:35 -04001632 struct cfg80211_registered_device *dev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001633
Johannes Berg5fe231e2013-05-08 21:45:15 +02001634 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001635 if (!state) {
1636 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001637 if (!state) {
1638 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001639 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001640 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001641 state->filter_wiphy = -1;
1642 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1643 if (ret) {
1644 kfree(state);
1645 rtnl_unlock();
1646 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001647 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001648 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001649 }
1650
Johannes Berg79c97e92009-07-07 03:56:12 +02001651 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001652 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1653 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001654 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001655 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001656 if (state->filter_wiphy != -1 &&
1657 state->filter_wiphy != dev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001658 continue;
1659 /* attempt to fit multiple wiphy data chunks into the skb */
1660 do {
1661 ret = nl80211_send_wiphy(dev, skb,
1662 NETLINK_CB(cb->skb).portid,
1663 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001664 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001665 if (ret < 0) {
1666 /*
1667 * If sending the wiphy data didn't fit (ENOBUFS
1668 * or EMSGSIZE returned), this SKB is still
1669 * empty (so it's not too big because another
1670 * wiphy dataset is already in the skb) and
1671 * we've not tried to adjust the dump allocation
1672 * yet ... then adjust the alloc size to be
1673 * bigger, and return 1 but with the empty skb.
1674 * This results in an empty message being RX'ed
1675 * in userspace, but that is ignored.
1676 *
1677 * We can then retry with the larger buffer.
1678 */
1679 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1680 !skb->len &&
1681 cb->min_dump_alloc < 4096) {
1682 cb->min_dump_alloc = 4096;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001683 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001684 return 1;
1685 }
1686 idx--;
1687 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001688 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001689 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001690 break;
Johannes Berg55682962007-09-20 13:09:35 -04001691 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001692 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001693
Johannes Berg86e8cf92013-06-19 10:57:22 +02001694 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001695
1696 return skb->len;
1697}
1698
Johannes Berg86e8cf92013-06-19 10:57:22 +02001699static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1700{
1701 kfree((void *)cb->args[0]);
1702 return 0;
1703}
1704
Johannes Berg55682962007-09-20 13:09:35 -04001705static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1706{
1707 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001708 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001709 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001710
Johannes Berg645e77d2013-03-01 14:03:49 +01001711 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001712 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001713 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001714
Johannes Berg3713b4e2013-02-14 16:19:38 +01001715 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001716 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001717 nlmsg_free(msg);
1718 return -ENOBUFS;
1719 }
Johannes Berg55682962007-09-20 13:09:35 -04001720
Johannes Berg134e6372009-07-10 09:51:34 +00001721 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001722}
1723
Jouni Malinen31888482008-10-30 16:59:24 +02001724static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1725 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1726 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1727 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1728 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1729 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1730};
1731
1732static int parse_txq_params(struct nlattr *tb[],
1733 struct ieee80211_txq_params *txq_params)
1734{
Johannes Berga3304b02012-03-28 11:04:24 +02001735 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001736 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1737 !tb[NL80211_TXQ_ATTR_AIFS])
1738 return -EINVAL;
1739
Johannes Berga3304b02012-03-28 11:04:24 +02001740 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001741 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1742 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1743 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1744 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1745
Johannes Berga3304b02012-03-28 11:04:24 +02001746 if (txq_params->ac >= NL80211_NUM_ACS)
1747 return -EINVAL;
1748
Jouni Malinen31888482008-10-30 16:59:24 +02001749 return 0;
1750}
1751
Johannes Bergf444de02010-05-05 15:25:02 +02001752static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1753{
1754 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001755 * You can only set the channel explicitly for WDS interfaces,
1756 * all others have their channel managed via their respective
1757 * "establish a connection" command (connect, join, ...)
1758 *
1759 * For AP/GO and mesh mode, the channel can be set with the
1760 * channel userspace API, but is only stored and passed to the
1761 * low-level driver when the AP starts or the mesh is joined.
1762 * This is for backward compatibility, userspace can also give
1763 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001764 *
1765 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001766 * whatever else is going on, so they have their own special
1767 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001768 */
1769 return !wdev ||
1770 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001771 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001772 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1773 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001774}
1775
Johannes Berg683b6d32012-11-08 21:25:48 +01001776static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1777 struct genl_info *info,
1778 struct cfg80211_chan_def *chandef)
1779{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301780 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001781
1782 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1783 return -EINVAL;
1784
1785 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1786
1787 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001788 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1789 chandef->center_freq1 = control_freq;
1790 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001791
1792 /* Primary channel not allowed */
1793 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1794 return -EINVAL;
1795
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001796 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1797 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001798
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001799 chantype = nla_get_u32(
1800 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1801
1802 switch (chantype) {
1803 case NL80211_CHAN_NO_HT:
1804 case NL80211_CHAN_HT20:
1805 case NL80211_CHAN_HT40PLUS:
1806 case NL80211_CHAN_HT40MINUS:
1807 cfg80211_chandef_create(chandef, chandef->chan,
1808 chantype);
1809 break;
1810 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001811 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001812 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001813 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1814 chandef->width =
1815 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1816 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1817 chandef->center_freq1 =
1818 nla_get_u32(
1819 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1820 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1821 chandef->center_freq2 =
1822 nla_get_u32(
1823 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1824 }
1825
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001826 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001827 return -EINVAL;
1828
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001829 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1830 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001831 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001832
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001833 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1834 chandef->width == NL80211_CHAN_WIDTH_10) &&
1835 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1836 return -EINVAL;
1837
Johannes Berg683b6d32012-11-08 21:25:48 +01001838 return 0;
1839}
1840
Johannes Bergf444de02010-05-05 15:25:02 +02001841static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1842 struct wireless_dev *wdev,
1843 struct genl_info *info)
1844{
Johannes Berg683b6d32012-11-08 21:25:48 +01001845 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001846 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001847 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1848
1849 if (wdev)
1850 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001851
Johannes Bergf444de02010-05-05 15:25:02 +02001852 if (!nl80211_can_set_dev_channel(wdev))
1853 return -EOPNOTSUPP;
1854
Johannes Berg683b6d32012-11-08 21:25:48 +01001855 result = nl80211_parse_chandef(rdev, info, &chandef);
1856 if (result)
1857 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001858
Johannes Berge8c9bd52012-06-06 08:18:22 +02001859 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001860 case NL80211_IFTYPE_AP:
1861 case NL80211_IFTYPE_P2P_GO:
1862 if (wdev->beacon_interval) {
1863 result = -EBUSY;
1864 break;
1865 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001866 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001867 result = -EINVAL;
1868 break;
1869 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001870 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001871 result = 0;
1872 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001873 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001874 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001875 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001876 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001877 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001878 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001879 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001880 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001881 }
Johannes Bergf444de02010-05-05 15:25:02 +02001882
1883 return result;
1884}
1885
1886static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1887{
Johannes Berg4c476992010-10-04 21:36:35 +02001888 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1889 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001890
Johannes Berg4c476992010-10-04 21:36:35 +02001891 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001892}
1893
Bill Jordane8347eb2010-10-01 13:54:28 -04001894static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1895{
Johannes Berg43b19952010-10-07 13:10:30 +02001896 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1897 struct net_device *dev = info->user_ptr[1];
1898 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001899 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001900
1901 if (!info->attrs[NL80211_ATTR_MAC])
1902 return -EINVAL;
1903
Johannes Berg43b19952010-10-07 13:10:30 +02001904 if (netif_running(dev))
1905 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001906
Johannes Berg43b19952010-10-07 13:10:30 +02001907 if (!rdev->ops->set_wds_peer)
1908 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001909
Johannes Berg43b19952010-10-07 13:10:30 +02001910 if (wdev->iftype != NL80211_IFTYPE_WDS)
1911 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001912
1913 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001914 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001915}
1916
1917
Johannes Berg55682962007-09-20 13:09:35 -04001918static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1919{
1920 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001921 struct net_device *netdev = NULL;
1922 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001923 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001924 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001925 u32 changed;
1926 u8 retry_short = 0, retry_long = 0;
1927 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001928 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001929
Johannes Berg5fe231e2013-05-08 21:45:15 +02001930 ASSERT_RTNL();
1931
Johannes Bergf444de02010-05-05 15:25:02 +02001932 /*
1933 * Try to find the wiphy and netdev. Normally this
1934 * function shouldn't need the netdev, but this is
1935 * done for backward compatibility -- previously
1936 * setting the channel was done per wiphy, but now
1937 * it is per netdev. Previous userland like hostapd
1938 * also passed a netdev to set_wiphy, so that it is
1939 * possible to let that go to the right netdev!
1940 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001941
Johannes Bergf444de02010-05-05 15:25:02 +02001942 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1943 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1944
1945 netdev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001946 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02001947 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001948 else
Johannes Bergf444de02010-05-05 15:25:02 +02001949 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001950 }
1951
Johannes Bergf444de02010-05-05 15:25:02 +02001952 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001953 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1954 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001955 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02001956 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001957 wdev = NULL;
1958 netdev = NULL;
1959 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02001960 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001961 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001962
1963 /*
1964 * end workaround code, by now the rdev is available
1965 * and locked, and wdev may or may not be NULL.
1966 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001967
1968 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001969 result = cfg80211_dev_rename(
1970 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001971
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001972 if (result)
1973 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001974
Jouni Malinen31888482008-10-30 16:59:24 +02001975 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1976 struct ieee80211_txq_params txq_params;
1977 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1978
1979 if (!rdev->ops->set_txq_params) {
1980 result = -EOPNOTSUPP;
1981 goto bad_res;
1982 }
1983
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001984 if (!netdev) {
1985 result = -EINVAL;
1986 goto bad_res;
1987 }
1988
Johannes Berg133a3ff2011-11-03 14:50:13 +01001989 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1990 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1991 result = -EINVAL;
1992 goto bad_res;
1993 }
1994
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001995 if (!netif_running(netdev)) {
1996 result = -ENETDOWN;
1997 goto bad_res;
1998 }
1999
Jouni Malinen31888482008-10-30 16:59:24 +02002000 nla_for_each_nested(nl_txq_params,
2001 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2002 rem_txq_params) {
2003 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2004 nla_data(nl_txq_params),
2005 nla_len(nl_txq_params),
2006 txq_params_policy);
2007 result = parse_txq_params(tb, &txq_params);
2008 if (result)
2009 goto bad_res;
2010
Hila Gonene35e4d22012-06-27 17:19:42 +03002011 result = rdev_set_txq_params(rdev, netdev,
2012 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002013 if (result)
2014 goto bad_res;
2015 }
2016 }
2017
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002018 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002019 result = __nl80211_set_channel(rdev,
2020 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2021 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002022 if (result)
2023 goto bad_res;
2024 }
2025
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002026 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002027 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002028 enum nl80211_tx_power_setting type;
2029 int idx, mbm = 0;
2030
Johannes Bergc8442112012-10-24 10:17:18 +02002031 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2032 txp_wdev = NULL;
2033
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002034 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02002035 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002036 goto bad_res;
2037 }
2038
2039 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2040 type = nla_get_u32(info->attrs[idx]);
2041
2042 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2043 (type != NL80211_TX_POWER_AUTOMATIC)) {
2044 result = -EINVAL;
2045 goto bad_res;
2046 }
2047
2048 if (type != NL80211_TX_POWER_AUTOMATIC) {
2049 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2050 mbm = nla_get_u32(info->attrs[idx]);
2051 }
2052
Johannes Bergc8442112012-10-24 10:17:18 +02002053 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002054 if (result)
2055 goto bad_res;
2056 }
2057
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002058 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2059 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2060 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002061 if ((!rdev->wiphy.available_antennas_tx &&
2062 !rdev->wiphy.available_antennas_rx) ||
2063 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002064 result = -EOPNOTSUPP;
2065 goto bad_res;
2066 }
2067
2068 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2069 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2070
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002071 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002072 * available antenna masks, except for the "all" mask */
2073 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2074 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002075 result = -EINVAL;
2076 goto bad_res;
2077 }
2078
Bruno Randolf7f531e02010-12-16 11:30:22 +09002079 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2080 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002081
Hila Gonene35e4d22012-06-27 17:19:42 +03002082 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002083 if (result)
2084 goto bad_res;
2085 }
2086
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002087 changed = 0;
2088
2089 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2090 retry_short = nla_get_u8(
2091 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2092 if (retry_short == 0) {
2093 result = -EINVAL;
2094 goto bad_res;
2095 }
2096 changed |= WIPHY_PARAM_RETRY_SHORT;
2097 }
2098
2099 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2100 retry_long = nla_get_u8(
2101 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2102 if (retry_long == 0) {
2103 result = -EINVAL;
2104 goto bad_res;
2105 }
2106 changed |= WIPHY_PARAM_RETRY_LONG;
2107 }
2108
2109 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2110 frag_threshold = nla_get_u32(
2111 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2112 if (frag_threshold < 256) {
2113 result = -EINVAL;
2114 goto bad_res;
2115 }
2116 if (frag_threshold != (u32) -1) {
2117 /*
2118 * Fragments (apart from the last one) are required to
2119 * have even length. Make the fragmentation code
2120 * simpler by stripping LSB should someone try to use
2121 * odd threshold value.
2122 */
2123 frag_threshold &= ~0x1;
2124 }
2125 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2126 }
2127
2128 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2129 rts_threshold = nla_get_u32(
2130 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2131 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2132 }
2133
Lukáš Turek81077e82009-12-21 22:50:47 +01002134 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2135 coverage_class = nla_get_u8(
2136 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2137 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2138 }
2139
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002140 if (changed) {
2141 u8 old_retry_short, old_retry_long;
2142 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002143 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002144
2145 if (!rdev->ops->set_wiphy_params) {
2146 result = -EOPNOTSUPP;
2147 goto bad_res;
2148 }
2149
2150 old_retry_short = rdev->wiphy.retry_short;
2151 old_retry_long = rdev->wiphy.retry_long;
2152 old_frag_threshold = rdev->wiphy.frag_threshold;
2153 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002154 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002155
2156 if (changed & WIPHY_PARAM_RETRY_SHORT)
2157 rdev->wiphy.retry_short = retry_short;
2158 if (changed & WIPHY_PARAM_RETRY_LONG)
2159 rdev->wiphy.retry_long = retry_long;
2160 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2161 rdev->wiphy.frag_threshold = frag_threshold;
2162 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2163 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002164 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2165 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002166
Hila Gonene35e4d22012-06-27 17:19:42 +03002167 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002168 if (result) {
2169 rdev->wiphy.retry_short = old_retry_short;
2170 rdev->wiphy.retry_long = old_retry_long;
2171 rdev->wiphy.frag_threshold = old_frag_threshold;
2172 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002173 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002174 }
2175 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002176
Johannes Berg306d6112008-12-08 12:39:04 +01002177 bad_res:
Johannes Bergf444de02010-05-05 15:25:02 +02002178 if (netdev)
2179 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002180 return result;
2181}
2182
Johannes Berg71bbc992012-06-15 15:30:18 +02002183static inline u64 wdev_id(struct wireless_dev *wdev)
2184{
2185 return (u64)wdev->identifier |
2186 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2187}
Johannes Berg55682962007-09-20 13:09:35 -04002188
Johannes Berg683b6d32012-11-08 21:25:48 +01002189static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002190 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002191{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002192 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002193
Johannes Berg683b6d32012-11-08 21:25:48 +01002194 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2195 chandef->chan->center_freq))
2196 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002197 switch (chandef->width) {
2198 case NL80211_CHAN_WIDTH_20_NOHT:
2199 case NL80211_CHAN_WIDTH_20:
2200 case NL80211_CHAN_WIDTH_40:
2201 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2202 cfg80211_get_chandef_type(chandef)))
2203 return -ENOBUFS;
2204 break;
2205 default:
2206 break;
2207 }
2208 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2209 return -ENOBUFS;
2210 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2211 return -ENOBUFS;
2212 if (chandef->center_freq2 &&
2213 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002214 return -ENOBUFS;
2215 return 0;
2216}
2217
Eric W. Biederman15e47302012-09-07 20:12:54 +00002218static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002219 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002220 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002221{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002222 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002223 void *hdr;
2224
Eric W. Biederman15e47302012-09-07 20:12:54 +00002225 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002226 if (!hdr)
2227 return -1;
2228
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002229 if (dev &&
2230 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002231 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002232 goto nla_put_failure;
2233
2234 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2235 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002236 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002237 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002238 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2239 rdev->devlist_generation ^
2240 (cfg80211_rdev_list_generation << 2)))
2241 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002242
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002243 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002244 int ret;
2245 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002246
Johannes Berg683b6d32012-11-08 21:25:48 +01002247 ret = rdev_get_channel(rdev, wdev, &chandef);
2248 if (ret == 0) {
2249 if (nl80211_send_chandef(msg, &chandef))
2250 goto nla_put_failure;
2251 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002252 }
2253
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002254 if (wdev->ssid_len) {
2255 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2256 goto nla_put_failure;
2257 }
2258
Johannes Berg55682962007-09-20 13:09:35 -04002259 return genlmsg_end(msg, hdr);
2260
2261 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002262 genlmsg_cancel(msg, hdr);
2263 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002264}
2265
2266static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2267{
2268 int wp_idx = 0;
2269 int if_idx = 0;
2270 int wp_start = cb->args[0];
2271 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002272 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002273 struct wireless_dev *wdev;
2274
Johannes Berg5fe231e2013-05-08 21:45:15 +02002275 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002276 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2277 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002278 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002279 if (wp_idx < wp_start) {
2280 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002281 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002282 }
Johannes Berg55682962007-09-20 13:09:35 -04002283 if_idx = 0;
2284
Johannes Berg89a54e42012-06-15 14:33:17 +02002285 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002286 if (if_idx < if_start) {
2287 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002288 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002289 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002290 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002291 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002292 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002293 goto out;
2294 }
2295 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002296 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002297
2298 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002299 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002300 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002301 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002302
2303 cb->args[0] = wp_idx;
2304 cb->args[1] = if_idx;
2305
2306 return skb->len;
2307}
2308
2309static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2310{
2311 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002312 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002313 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002314
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002315 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002316 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002317 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002318
Eric W. Biederman15e47302012-09-07 20:12:54 +00002319 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002320 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002321 nlmsg_free(msg);
2322 return -ENOBUFS;
2323 }
Johannes Berg55682962007-09-20 13:09:35 -04002324
Johannes Berg134e6372009-07-10 09:51:34 +00002325 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002326}
2327
Michael Wu66f7ac52008-01-31 19:48:22 +01002328static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2329 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2330 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2331 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2332 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2333 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002334 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002335};
2336
2337static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2338{
2339 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2340 int flag;
2341
2342 *mntrflags = 0;
2343
2344 if (!nla)
2345 return -EINVAL;
2346
2347 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2348 nla, mntr_flags_policy))
2349 return -EINVAL;
2350
2351 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2352 if (flags[flag])
2353 *mntrflags |= (1<<flag);
2354
2355 return 0;
2356}
2357
Johannes Berg9bc383d2009-11-19 11:55:19 +01002358static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002359 struct net_device *netdev, u8 use_4addr,
2360 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002361{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002362 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002363 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002364 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002365 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002366 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002367
2368 switch (iftype) {
2369 case NL80211_IFTYPE_AP_VLAN:
2370 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2371 return 0;
2372 break;
2373 case NL80211_IFTYPE_STATION:
2374 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2375 return 0;
2376 break;
2377 default:
2378 break;
2379 }
2380
2381 return -EOPNOTSUPP;
2382}
2383
Johannes Berg55682962007-09-20 13:09:35 -04002384static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2385{
Johannes Berg4c476992010-10-04 21:36:35 +02002386 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002387 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002388 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002389 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002390 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002391 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002392 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002393
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002394 memset(&params, 0, sizeof(params));
2395
Johannes Berg04a773a2009-04-19 21:24:32 +02002396 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002397
Johannes Berg723b0382008-09-16 20:22:09 +02002398 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002399 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002400 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002401 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002402 if (ntype > NL80211_IFTYPE_MAX)
2403 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002404 }
2405
Johannes Berg92ffe052008-09-16 20:39:36 +02002406 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002407 struct wireless_dev *wdev = dev->ieee80211_ptr;
2408
Johannes Berg4c476992010-10-04 21:36:35 +02002409 if (ntype != NL80211_IFTYPE_MESH_POINT)
2410 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002411 if (netif_running(dev))
2412 return -EBUSY;
2413
2414 wdev_lock(wdev);
2415 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2416 IEEE80211_MAX_MESH_ID_LEN);
2417 wdev->mesh_id_up_len =
2418 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2419 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2420 wdev->mesh_id_up_len);
2421 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002422 }
2423
Felix Fietkau8b787642009-11-10 18:53:10 +01002424 if (info->attrs[NL80211_ATTR_4ADDR]) {
2425 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2426 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002427 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002428 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002429 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002430 } else {
2431 params.use_4addr = -1;
2432 }
2433
Johannes Berg92ffe052008-09-16 20:39:36 +02002434 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002435 if (ntype != NL80211_IFTYPE_MONITOR)
2436 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002437 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2438 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002439 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002440 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002441
2442 flags = &_flags;
2443 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002444 }
Johannes Berg3b858752009-03-12 09:55:09 +01002445
Luciano Coelho18003292013-08-29 13:26:57 +03002446 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002447 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2448 return -EOPNOTSUPP;
2449
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002450 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002451 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002452 else
2453 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002454
Johannes Berg9bc383d2009-11-19 11:55:19 +01002455 if (!err && params.use_4addr != -1)
2456 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2457
Johannes Berg55682962007-09-20 13:09:35 -04002458 return err;
2459}
2460
2461static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2462{
Johannes Berg4c476992010-10-04 21:36:35 +02002463 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002464 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002465 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002466 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002467 int err;
2468 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002469 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002470
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002471 memset(&params, 0, sizeof(params));
2472
Johannes Berg55682962007-09-20 13:09:35 -04002473 if (!info->attrs[NL80211_ATTR_IFNAME])
2474 return -EINVAL;
2475
2476 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2477 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2478 if (type > NL80211_IFTYPE_MAX)
2479 return -EINVAL;
2480 }
2481
Johannes Berg79c97e92009-07-07 03:56:12 +02002482 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002483 !(rdev->wiphy.interface_modes & (1 << type)))
2484 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002485
Arend van Spriel1c18f142013-01-08 10:17:27 +01002486 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2487 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2488 ETH_ALEN);
2489 if (!is_valid_ether_addr(params.macaddr))
2490 return -EADDRNOTAVAIL;
2491 }
2492
Johannes Berg9bc383d2009-11-19 11:55:19 +01002493 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002494 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002495 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002496 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002497 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002498 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002499
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002500 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2501 if (!msg)
2502 return -ENOMEM;
2503
Michael Wu66f7ac52008-01-31 19:48:22 +01002504 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2505 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2506 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002507
Luciano Coelho18003292013-08-29 13:26:57 +03002508 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002509 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2510 return -EOPNOTSUPP;
2511
Hila Gonene35e4d22012-06-27 17:19:42 +03002512 wdev = rdev_add_virtual_intf(rdev,
2513 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2514 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002515 if (IS_ERR(wdev)) {
2516 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002517 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002518 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002519
Johannes Berg98104fde2012-06-16 00:19:54 +02002520 switch (type) {
2521 case NL80211_IFTYPE_MESH_POINT:
2522 if (!info->attrs[NL80211_ATTR_MESH_ID])
2523 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002524 wdev_lock(wdev);
2525 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2526 IEEE80211_MAX_MESH_ID_LEN);
2527 wdev->mesh_id_up_len =
2528 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2529 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2530 wdev->mesh_id_up_len);
2531 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002532 break;
2533 case NL80211_IFTYPE_P2P_DEVICE:
2534 /*
2535 * P2P Device doesn't have a netdev, so doesn't go
2536 * through the netdev notifier and must be added here
2537 */
2538 mutex_init(&wdev->mtx);
2539 INIT_LIST_HEAD(&wdev->event_list);
2540 spin_lock_init(&wdev->event_lock);
2541 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2542 spin_lock_init(&wdev->mgmt_registrations_lock);
2543
Johannes Berg98104fde2012-06-16 00:19:54 +02002544 wdev->identifier = ++rdev->wdev_id;
2545 list_add_rcu(&wdev->list, &rdev->wdev_list);
2546 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002547 break;
2548 default:
2549 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002550 }
2551
Eric W. Biederman15e47302012-09-07 20:12:54 +00002552 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002553 rdev, wdev) < 0) {
2554 nlmsg_free(msg);
2555 return -ENOBUFS;
2556 }
2557
2558 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002559}
2560
2561static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2562{
Johannes Berg4c476992010-10-04 21:36:35 +02002563 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002564 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002565
Johannes Berg4c476992010-10-04 21:36:35 +02002566 if (!rdev->ops->del_virtual_intf)
2567 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002568
Johannes Berg84efbb82012-06-16 00:00:26 +02002569 /*
2570 * If we remove a wireless device without a netdev then clear
2571 * user_ptr[1] so that nl80211_post_doit won't dereference it
2572 * to check if it needs to do dev_put(). Otherwise it crashes
2573 * since the wdev has been freed, unlike with a netdev where
2574 * we need the dev_put() for the netdev to really be freed.
2575 */
2576 if (!wdev->netdev)
2577 info->user_ptr[1] = NULL;
2578
Hila Gonene35e4d22012-06-27 17:19:42 +03002579 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002580}
2581
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002582static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2583{
2584 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2585 struct net_device *dev = info->user_ptr[1];
2586 u16 noack_map;
2587
2588 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2589 return -EINVAL;
2590
2591 if (!rdev->ops->set_noack_map)
2592 return -EOPNOTSUPP;
2593
2594 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2595
Hila Gonene35e4d22012-06-27 17:19:42 +03002596 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002597}
2598
Johannes Berg41ade002007-12-19 02:03:29 +01002599struct get_key_cookie {
2600 struct sk_buff *msg;
2601 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002602 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002603};
2604
2605static void get_key_callback(void *c, struct key_params *params)
2606{
Johannes Bergb9454e82009-07-08 13:29:08 +02002607 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002608 struct get_key_cookie *cookie = c;
2609
David S. Miller9360ffd2012-03-29 04:41:26 -04002610 if ((params->key &&
2611 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2612 params->key_len, params->key)) ||
2613 (params->seq &&
2614 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2615 params->seq_len, params->seq)) ||
2616 (params->cipher &&
2617 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2618 params->cipher)))
2619 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002620
Johannes Bergb9454e82009-07-08 13:29:08 +02002621 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2622 if (!key)
2623 goto nla_put_failure;
2624
David S. Miller9360ffd2012-03-29 04:41:26 -04002625 if ((params->key &&
2626 nla_put(cookie->msg, NL80211_KEY_DATA,
2627 params->key_len, params->key)) ||
2628 (params->seq &&
2629 nla_put(cookie->msg, NL80211_KEY_SEQ,
2630 params->seq_len, params->seq)) ||
2631 (params->cipher &&
2632 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2633 params->cipher)))
2634 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002635
David S. Miller9360ffd2012-03-29 04:41:26 -04002636 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2637 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002638
2639 nla_nest_end(cookie->msg, key);
2640
Johannes Berg41ade002007-12-19 02:03:29 +01002641 return;
2642 nla_put_failure:
2643 cookie->error = 1;
2644}
2645
2646static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2647{
Johannes Berg4c476992010-10-04 21:36:35 +02002648 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002649 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002650 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002651 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002652 const u8 *mac_addr = NULL;
2653 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002654 struct get_key_cookie cookie = {
2655 .error = 0,
2656 };
2657 void *hdr;
2658 struct sk_buff *msg;
2659
2660 if (info->attrs[NL80211_ATTR_KEY_IDX])
2661 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2662
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002663 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002664 return -EINVAL;
2665
2666 if (info->attrs[NL80211_ATTR_MAC])
2667 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2668
Johannes Berge31b8212010-10-05 19:39:30 +02002669 pairwise = !!mac_addr;
2670 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2671 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2672 if (kt >= NUM_NL80211_KEYTYPES)
2673 return -EINVAL;
2674 if (kt != NL80211_KEYTYPE_GROUP &&
2675 kt != NL80211_KEYTYPE_PAIRWISE)
2676 return -EINVAL;
2677 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2678 }
2679
Johannes Berg4c476992010-10-04 21:36:35 +02002680 if (!rdev->ops->get_key)
2681 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002682
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002684 if (!msg)
2685 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002686
Eric W. Biederman15e47302012-09-07 20:12:54 +00002687 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002688 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002689 if (!hdr)
2690 return -ENOBUFS;
Johannes Berg41ade002007-12-19 02:03:29 +01002691
2692 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002693 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002694
David S. Miller9360ffd2012-03-29 04:41:26 -04002695 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2696 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2697 goto nla_put_failure;
2698 if (mac_addr &&
2699 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2700 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002701
Johannes Berge31b8212010-10-05 19:39:30 +02002702 if (pairwise && mac_addr &&
2703 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2704 return -ENOENT;
2705
Hila Gonene35e4d22012-06-27 17:19:42 +03002706 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2707 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002708
2709 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002710 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002711
2712 if (cookie.error)
2713 goto nla_put_failure;
2714
2715 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002716 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002717
2718 nla_put_failure:
2719 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002720 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002721 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002722 return err;
2723}
2724
2725static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2726{
Johannes Berg4c476992010-10-04 21:36:35 +02002727 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002728 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002729 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002730 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002731
Johannes Bergb9454e82009-07-08 13:29:08 +02002732 err = nl80211_parse_key(info, &key);
2733 if (err)
2734 return err;
2735
2736 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002737 return -EINVAL;
2738
Johannes Bergb9454e82009-07-08 13:29:08 +02002739 /* only support setting default key */
2740 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002741 return -EINVAL;
2742
Johannes Bergfffd0932009-07-08 14:22:54 +02002743 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002744
2745 if (key.def) {
2746 if (!rdev->ops->set_default_key) {
2747 err = -EOPNOTSUPP;
2748 goto out;
2749 }
2750
2751 err = nl80211_key_allowed(dev->ieee80211_ptr);
2752 if (err)
2753 goto out;
2754
Hila Gonene35e4d22012-06-27 17:19:42 +03002755 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002756 key.def_uni, key.def_multi);
2757
2758 if (err)
2759 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002760
Johannes Berg3d23e342009-09-29 23:27:28 +02002761#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002762 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002763#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002764 } else {
2765 if (key.def_uni || !key.def_multi) {
2766 err = -EINVAL;
2767 goto out;
2768 }
2769
2770 if (!rdev->ops->set_default_mgmt_key) {
2771 err = -EOPNOTSUPP;
2772 goto out;
2773 }
2774
2775 err = nl80211_key_allowed(dev->ieee80211_ptr);
2776 if (err)
2777 goto out;
2778
Hila Gonene35e4d22012-06-27 17:19:42 +03002779 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002780 if (err)
2781 goto out;
2782
2783#ifdef CONFIG_CFG80211_WEXT
2784 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2785#endif
2786 }
2787
2788 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002789 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002790
Johannes Berg41ade002007-12-19 02:03:29 +01002791 return err;
2792}
2793
2794static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2795{
Johannes Berg4c476992010-10-04 21:36:35 +02002796 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002797 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002798 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002799 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002800 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002801
Johannes Bergb9454e82009-07-08 13:29:08 +02002802 err = nl80211_parse_key(info, &key);
2803 if (err)
2804 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002805
Johannes Bergb9454e82009-07-08 13:29:08 +02002806 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002807 return -EINVAL;
2808
Johannes Berg41ade002007-12-19 02:03:29 +01002809 if (info->attrs[NL80211_ATTR_MAC])
2810 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2811
Johannes Berge31b8212010-10-05 19:39:30 +02002812 if (key.type == -1) {
2813 if (mac_addr)
2814 key.type = NL80211_KEYTYPE_PAIRWISE;
2815 else
2816 key.type = NL80211_KEYTYPE_GROUP;
2817 }
2818
2819 /* for now */
2820 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2821 key.type != NL80211_KEYTYPE_GROUP)
2822 return -EINVAL;
2823
Johannes Berg4c476992010-10-04 21:36:35 +02002824 if (!rdev->ops->add_key)
2825 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002826
Johannes Berge31b8212010-10-05 19:39:30 +02002827 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2828 key.type == NL80211_KEYTYPE_PAIRWISE,
2829 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002830 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002831
2832 wdev_lock(dev->ieee80211_ptr);
2833 err = nl80211_key_allowed(dev->ieee80211_ptr);
2834 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002835 err = rdev_add_key(rdev, dev, key.idx,
2836 key.type == NL80211_KEYTYPE_PAIRWISE,
2837 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002838 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002839
Johannes Berg41ade002007-12-19 02:03:29 +01002840 return err;
2841}
2842
2843static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2844{
Johannes Berg4c476992010-10-04 21:36:35 +02002845 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002846 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002847 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002848 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002849 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002850
Johannes Bergb9454e82009-07-08 13:29:08 +02002851 err = nl80211_parse_key(info, &key);
2852 if (err)
2853 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002854
2855 if (info->attrs[NL80211_ATTR_MAC])
2856 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2857
Johannes Berge31b8212010-10-05 19:39:30 +02002858 if (key.type == -1) {
2859 if (mac_addr)
2860 key.type = NL80211_KEYTYPE_PAIRWISE;
2861 else
2862 key.type = NL80211_KEYTYPE_GROUP;
2863 }
2864
2865 /* for now */
2866 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2867 key.type != NL80211_KEYTYPE_GROUP)
2868 return -EINVAL;
2869
Johannes Berg4c476992010-10-04 21:36:35 +02002870 if (!rdev->ops->del_key)
2871 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002872
Johannes Bergfffd0932009-07-08 14:22:54 +02002873 wdev_lock(dev->ieee80211_ptr);
2874 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002875
2876 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2877 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2878 err = -ENOENT;
2879
Johannes Bergfffd0932009-07-08 14:22:54 +02002880 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002881 err = rdev_del_key(rdev, dev, key.idx,
2882 key.type == NL80211_KEYTYPE_PAIRWISE,
2883 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002884
Johannes Berg3d23e342009-09-29 23:27:28 +02002885#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002886 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002887 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002888 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002889 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002890 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2891 }
2892#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002893 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002894
Johannes Berg41ade002007-12-19 02:03:29 +01002895 return err;
2896}
2897
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302898/* This function returns an error or the number of nested attributes */
2899static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2900{
2901 struct nlattr *attr;
2902 int n_entries = 0, tmp;
2903
2904 nla_for_each_nested(attr, nl_attr, tmp) {
2905 if (nla_len(attr) != ETH_ALEN)
2906 return -EINVAL;
2907
2908 n_entries++;
2909 }
2910
2911 return n_entries;
2912}
2913
2914/*
2915 * This function parses ACL information and allocates memory for ACL data.
2916 * On successful return, the calling function is responsible to free the
2917 * ACL buffer returned by this function.
2918 */
2919static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2920 struct genl_info *info)
2921{
2922 enum nl80211_acl_policy acl_policy;
2923 struct nlattr *attr;
2924 struct cfg80211_acl_data *acl;
2925 int i = 0, n_entries, tmp;
2926
2927 if (!wiphy->max_acl_mac_addrs)
2928 return ERR_PTR(-EOPNOTSUPP);
2929
2930 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2931 return ERR_PTR(-EINVAL);
2932
2933 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2934 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2935 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2936 return ERR_PTR(-EINVAL);
2937
2938 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2939 return ERR_PTR(-EINVAL);
2940
2941 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2942 if (n_entries < 0)
2943 return ERR_PTR(n_entries);
2944
2945 if (n_entries > wiphy->max_acl_mac_addrs)
2946 return ERR_PTR(-ENOTSUPP);
2947
2948 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2949 GFP_KERNEL);
2950 if (!acl)
2951 return ERR_PTR(-ENOMEM);
2952
2953 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2954 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2955 i++;
2956 }
2957
2958 acl->n_acl_entries = n_entries;
2959 acl->acl_policy = acl_policy;
2960
2961 return acl;
2962}
2963
2964static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2965{
2966 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2967 struct net_device *dev = info->user_ptr[1];
2968 struct cfg80211_acl_data *acl;
2969 int err;
2970
2971 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2972 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2973 return -EOPNOTSUPP;
2974
2975 if (!dev->ieee80211_ptr->beacon_interval)
2976 return -EINVAL;
2977
2978 acl = parse_acl_data(&rdev->wiphy, info);
2979 if (IS_ERR(acl))
2980 return PTR_ERR(acl);
2981
2982 err = rdev_set_mac_acl(rdev, dev, acl);
2983
2984 kfree(acl);
2985
2986 return err;
2987}
2988
Simon Wunderlicha1193be2013-06-14 14:15:19 +02002989static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01002990 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002991{
Johannes Berg88600202012-02-13 15:17:18 +01002992 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002993
Simon Wunderlicha1193be2013-06-14 14:15:19 +02002994 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
2995 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
2996 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2997 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002998 return -EINVAL;
2999
Johannes Berg88600202012-02-13 15:17:18 +01003000 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003001
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003002 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3003 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3004 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003005 if (!bcn->head_len)
3006 return -EINVAL;
3007 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003008 }
3009
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003010 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3011 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3012 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003013 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003014 }
3015
Johannes Berg4c476992010-10-04 21:36:35 +02003016 if (!haveinfo)
3017 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003018
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003019 if (attrs[NL80211_ATTR_IE]) {
3020 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3021 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003022 }
3023
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003024 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003025 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003026 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003027 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003028 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003029 }
3030
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003031 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003032 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003033 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003034 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003035 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003036 }
3037
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003038 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3039 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3040 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003041 }
3042
Johannes Berg88600202012-02-13 15:17:18 +01003043 return 0;
3044}
3045
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003046static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3047 struct cfg80211_ap_settings *params)
3048{
3049 struct wireless_dev *wdev;
3050 bool ret = false;
3051
Johannes Berg89a54e42012-06-15 14:33:17 +02003052 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003053 if (wdev->iftype != NL80211_IFTYPE_AP &&
3054 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3055 continue;
3056
Johannes Berg683b6d32012-11-08 21:25:48 +01003057 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003058 continue;
3059
Johannes Berg683b6d32012-11-08 21:25:48 +01003060 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003061 ret = true;
3062 break;
3063 }
3064
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003065 return ret;
3066}
3067
Jouni Malinene39e5b52012-09-30 19:29:39 +03003068static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3069 enum nl80211_auth_type auth_type,
3070 enum nl80211_commands cmd)
3071{
3072 if (auth_type > NL80211_AUTHTYPE_MAX)
3073 return false;
3074
3075 switch (cmd) {
3076 case NL80211_CMD_AUTHENTICATE:
3077 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3078 auth_type == NL80211_AUTHTYPE_SAE)
3079 return false;
3080 return true;
3081 case NL80211_CMD_CONNECT:
3082 case NL80211_CMD_START_AP:
3083 /* SAE not supported yet */
3084 if (auth_type == NL80211_AUTHTYPE_SAE)
3085 return false;
3086 return true;
3087 default:
3088 return false;
3089 }
3090}
3091
Johannes Berg88600202012-02-13 15:17:18 +01003092static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3093{
3094 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3095 struct net_device *dev = info->user_ptr[1];
3096 struct wireless_dev *wdev = dev->ieee80211_ptr;
3097 struct cfg80211_ap_settings params;
3098 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003099 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003100
3101 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3102 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3103 return -EOPNOTSUPP;
3104
3105 if (!rdev->ops->start_ap)
3106 return -EOPNOTSUPP;
3107
3108 if (wdev->beacon_interval)
3109 return -EALREADY;
3110
3111 memset(&params, 0, sizeof(params));
3112
3113 /* these are required for START_AP */
3114 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3115 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3116 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3117 return -EINVAL;
3118
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003119 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003120 if (err)
3121 return err;
3122
3123 params.beacon_interval =
3124 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3125 params.dtim_period =
3126 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3127
3128 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3129 if (err)
3130 return err;
3131
3132 /*
3133 * In theory, some of these attributes should be required here
3134 * but since they were not used when the command was originally
3135 * added, keep them optional for old user space programs to let
3136 * them continue to work with drivers that do not need the
3137 * additional information -- drivers must check!
3138 */
3139 if (info->attrs[NL80211_ATTR_SSID]) {
3140 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3141 params.ssid_len =
3142 nla_len(info->attrs[NL80211_ATTR_SSID]);
3143 if (params.ssid_len == 0 ||
3144 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3145 return -EINVAL;
3146 }
3147
3148 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3149 params.hidden_ssid = nla_get_u32(
3150 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3151 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3152 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3153 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3154 return -EINVAL;
3155 }
3156
3157 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3158
3159 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3160 params.auth_type = nla_get_u32(
3161 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003162 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3163 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003164 return -EINVAL;
3165 } else
3166 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3167
3168 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3169 NL80211_MAX_NR_CIPHER_SUITES);
3170 if (err)
3171 return err;
3172
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303173 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3174 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3175 return -EOPNOTSUPP;
3176 params.inactivity_timeout = nla_get_u16(
3177 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3178 }
3179
Johannes Berg53cabad2012-11-14 15:17:28 +01003180 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3181 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3182 return -EINVAL;
3183 params.p2p_ctwindow =
3184 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3185 if (params.p2p_ctwindow > 127)
3186 return -EINVAL;
3187 if (params.p2p_ctwindow != 0 &&
3188 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3189 return -EINVAL;
3190 }
3191
3192 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3193 u8 tmp;
3194
3195 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3196 return -EINVAL;
3197 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3198 if (tmp > 1)
3199 return -EINVAL;
3200 params.p2p_opp_ps = tmp;
3201 if (params.p2p_opp_ps != 0 &&
3202 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3203 return -EINVAL;
3204 }
3205
Johannes Bergaa430da2012-05-16 23:50:18 +02003206 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003207 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3208 if (err)
3209 return err;
3210 } else if (wdev->preset_chandef.chan) {
3211 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003212 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003213 return -EINVAL;
3214
Johannes Berg683b6d32012-11-08 21:25:48 +01003215 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003216 return -EINVAL;
3217
Simon Wunderlich04f39042013-02-08 18:16:19 +01003218 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3219 if (err < 0)
3220 return err;
3221 if (err) {
3222 radar_detect_width = BIT(params.chandef.width);
3223 params.radar_required = true;
3224 }
3225
Simon Wunderlich04f39042013-02-08 18:16:19 +01003226 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3227 params.chandef.chan,
3228 CHAN_MODE_SHARED,
3229 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003230 if (err)
3231 return err;
3232
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303233 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3234 params.acl = parse_acl_data(&rdev->wiphy, info);
3235 if (IS_ERR(params.acl))
3236 return PTR_ERR(params.acl);
3237 }
3238
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003239 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003240 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003241 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003242 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003243 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003244 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003245 wdev->ssid_len = params.ssid_len;
3246 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003247 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003248 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303249
3250 kfree(params.acl);
3251
Johannes Berg56d18932011-05-09 18:41:15 +02003252 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003253}
3254
Johannes Berg88600202012-02-13 15:17:18 +01003255static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3256{
3257 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3258 struct net_device *dev = info->user_ptr[1];
3259 struct wireless_dev *wdev = dev->ieee80211_ptr;
3260 struct cfg80211_beacon_data params;
3261 int err;
3262
3263 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3264 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3265 return -EOPNOTSUPP;
3266
3267 if (!rdev->ops->change_beacon)
3268 return -EOPNOTSUPP;
3269
3270 if (!wdev->beacon_interval)
3271 return -EINVAL;
3272
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003273 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003274 if (err)
3275 return err;
3276
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003277 wdev_lock(wdev);
3278 err = rdev_change_beacon(rdev, dev, &params);
3279 wdev_unlock(wdev);
3280
3281 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003282}
3283
3284static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003285{
Johannes Berg4c476992010-10-04 21:36:35 +02003286 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3287 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003288
Michal Kazior60771782012-06-29 12:46:56 +02003289 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003290}
3291
Johannes Berg5727ef12007-12-19 02:03:34 +01003292static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3293 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3294 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3295 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003296 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003297 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003298 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003299};
3300
Johannes Bergeccb8e82009-05-11 21:57:56 +03003301static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003302 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003303 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003304{
3305 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003306 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003307 int flag;
3308
Johannes Bergeccb8e82009-05-11 21:57:56 +03003309 /*
3310 * Try parsing the new attribute first so userspace
3311 * can specify both for older kernels.
3312 */
3313 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3314 if (nla) {
3315 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003316
Johannes Bergeccb8e82009-05-11 21:57:56 +03003317 sta_flags = nla_data(nla);
3318 params->sta_flags_mask = sta_flags->mask;
3319 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003320 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003321 if ((params->sta_flags_mask |
3322 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3323 return -EINVAL;
3324 return 0;
3325 }
3326
3327 /* if present, parse the old attribute */
3328
3329 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003330 if (!nla)
3331 return 0;
3332
3333 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3334 nla, sta_flags_policy))
3335 return -EINVAL;
3336
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003337 /*
3338 * Only allow certain flags for interface types so that
3339 * other attributes are silently ignored. Remember that
3340 * this is backward compatibility code with old userspace
3341 * and shouldn't be hit in other cases anyway.
3342 */
3343 switch (iftype) {
3344 case NL80211_IFTYPE_AP:
3345 case NL80211_IFTYPE_AP_VLAN:
3346 case NL80211_IFTYPE_P2P_GO:
3347 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3348 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3349 BIT(NL80211_STA_FLAG_WME) |
3350 BIT(NL80211_STA_FLAG_MFP);
3351 break;
3352 case NL80211_IFTYPE_P2P_CLIENT:
3353 case NL80211_IFTYPE_STATION:
3354 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3355 BIT(NL80211_STA_FLAG_TDLS_PEER);
3356 break;
3357 case NL80211_IFTYPE_MESH_POINT:
3358 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3359 BIT(NL80211_STA_FLAG_MFP) |
3360 BIT(NL80211_STA_FLAG_AUTHORIZED);
3361 default:
3362 return -EINVAL;
3363 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003364
Johannes Berg3383b5a2012-05-10 20:14:43 +02003365 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3366 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003367 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003368
Johannes Berg3383b5a2012-05-10 20:14:43 +02003369 /* no longer support new API additions in old API */
3370 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3371 return -EINVAL;
3372 }
3373 }
3374
Johannes Berg5727ef12007-12-19 02:03:34 +01003375 return 0;
3376}
3377
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003378static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3379 int attr)
3380{
3381 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003382 u32 bitrate;
3383 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003384
3385 rate = nla_nest_start(msg, attr);
3386 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003387 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003388
3389 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3390 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003391 /* report 16-bit bitrate only if we can */
3392 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003393 if (bitrate > 0 &&
3394 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3395 return false;
3396 if (bitrate_compat > 0 &&
3397 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3398 return false;
3399
3400 if (info->flags & RATE_INFO_FLAGS_MCS) {
3401 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3402 return false;
3403 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3404 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3405 return false;
3406 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3407 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3408 return false;
3409 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3410 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3411 return false;
3412 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3413 return false;
3414 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3415 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3416 return false;
3417 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3418 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3419 return false;
3420 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3421 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3422 return false;
3423 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3424 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3425 return false;
3426 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3427 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3428 return false;
3429 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003430
3431 nla_nest_end(msg, rate);
3432 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003433}
3434
Felix Fietkau119363c2013-04-22 16:29:30 +02003435static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3436 int id)
3437{
3438 void *attr;
3439 int i = 0;
3440
3441 if (!mask)
3442 return true;
3443
3444 attr = nla_nest_start(msg, id);
3445 if (!attr)
3446 return false;
3447
3448 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3449 if (!(mask & BIT(i)))
3450 continue;
3451
3452 if (nla_put_u8(msg, i, signal[i]))
3453 return false;
3454 }
3455
3456 nla_nest_end(msg, attr);
3457
3458 return true;
3459}
3460
Eric W. Biederman15e47302012-09-07 20:12:54 +00003461static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003462 int flags,
3463 struct cfg80211_registered_device *rdev,
3464 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003465 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003466{
3467 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003468 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003469
Eric W. Biederman15e47302012-09-07 20:12:54 +00003470 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003471 if (!hdr)
3472 return -1;
3473
David S. Miller9360ffd2012-03-29 04:41:26 -04003474 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3475 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3476 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3477 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003478
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003479 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3480 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003481 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003482 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3483 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3484 sinfo->connected_time))
3485 goto nla_put_failure;
3486 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3487 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3488 sinfo->inactive_time))
3489 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003490 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3491 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003492 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003493 (u32)sinfo->rx_bytes))
3494 goto nla_put_failure;
3495 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003496 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003497 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3498 (u32)sinfo->tx_bytes))
3499 goto nla_put_failure;
3500 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3501 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003502 sinfo->rx_bytes))
3503 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003504 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3505 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003506 sinfo->tx_bytes))
3507 goto nla_put_failure;
3508 if ((sinfo->filled & STATION_INFO_LLID) &&
3509 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3510 goto nla_put_failure;
3511 if ((sinfo->filled & STATION_INFO_PLID) &&
3512 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3513 goto nla_put_failure;
3514 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3515 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3516 sinfo->plink_state))
3517 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003518 switch (rdev->wiphy.signal_type) {
3519 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003520 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3521 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3522 sinfo->signal))
3523 goto nla_put_failure;
3524 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3525 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3526 sinfo->signal_avg))
3527 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003528 break;
3529 default:
3530 break;
3531 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003532 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3533 if (!nl80211_put_signal(msg, sinfo->chains,
3534 sinfo->chain_signal,
3535 NL80211_STA_INFO_CHAIN_SIGNAL))
3536 goto nla_put_failure;
3537 }
3538 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3539 if (!nl80211_put_signal(msg, sinfo->chains,
3540 sinfo->chain_signal_avg,
3541 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3542 goto nla_put_failure;
3543 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003544 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003545 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3546 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003547 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003548 }
3549 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3550 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3551 NL80211_STA_INFO_RX_BITRATE))
3552 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003553 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003554 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3555 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3556 sinfo->rx_packets))
3557 goto nla_put_failure;
3558 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3559 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3560 sinfo->tx_packets))
3561 goto nla_put_failure;
3562 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3563 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3564 sinfo->tx_retries))
3565 goto nla_put_failure;
3566 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3567 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3568 sinfo->tx_failed))
3569 goto nla_put_failure;
3570 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3571 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3572 sinfo->beacon_loss_count))
3573 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003574 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3575 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3576 sinfo->local_pm))
3577 goto nla_put_failure;
3578 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3579 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3580 sinfo->peer_pm))
3581 goto nla_put_failure;
3582 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3583 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3584 sinfo->nonpeer_pm))
3585 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003586 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3587 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3588 if (!bss_param)
3589 goto nla_put_failure;
3590
David S. Miller9360ffd2012-03-29 04:41:26 -04003591 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3592 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3593 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3594 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3595 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3596 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3597 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3598 sinfo->bss_param.dtim_period) ||
3599 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3600 sinfo->bss_param.beacon_interval))
3601 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003602
3603 nla_nest_end(msg, bss_param);
3604 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003605 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3606 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3607 sizeof(struct nl80211_sta_flag_update),
3608 &sinfo->sta_flags))
3609 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003610 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3611 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3612 sinfo->t_offset))
3613 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003614 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003615
David S. Miller9360ffd2012-03-29 04:41:26 -04003616 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3617 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3618 sinfo->assoc_req_ies))
3619 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003620
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003621 return genlmsg_end(msg, hdr);
3622
3623 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003624 genlmsg_cancel(msg, hdr);
3625 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003626}
3627
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003628static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003629 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003630{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003631 struct station_info sinfo;
3632 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003633 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003634 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003635 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003636 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003637
Johannes Berg97990a02013-04-19 01:02:55 +02003638 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003639 if (err)
3640 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003641
Johannes Berg97990a02013-04-19 01:02:55 +02003642 if (!wdev->netdev) {
3643 err = -EINVAL;
3644 goto out_err;
3645 }
3646
Johannes Bergbba95fe2008-07-29 13:22:51 +02003647 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003648 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003649 goto out_err;
3650 }
3651
Johannes Bergbba95fe2008-07-29 13:22:51 +02003652 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003653 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003654 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003655 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003656 if (err == -ENOENT)
3657 break;
3658 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003659 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003660
3661 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003662 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003663 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003664 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003665 &sinfo) < 0)
3666 goto out;
3667
3668 sta_idx++;
3669 }
3670
3671
3672 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003673 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003674 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003675 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003676 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003677
3678 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003679}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003680
Johannes Berg5727ef12007-12-19 02:03:34 +01003681static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3682{
Johannes Berg4c476992010-10-04 21:36:35 +02003683 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3684 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003685 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003686 struct sk_buff *msg;
3687 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003688 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003689
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003690 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003691
3692 if (!info->attrs[NL80211_ATTR_MAC])
3693 return -EINVAL;
3694
3695 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3696
Johannes Berg4c476992010-10-04 21:36:35 +02003697 if (!rdev->ops->get_station)
3698 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003699
Hila Gonene35e4d22012-06-27 17:19:42 +03003700 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003701 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003702 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003703
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003704 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003705 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003706 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003707
Eric W. Biederman15e47302012-09-07 20:12:54 +00003708 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003709 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003710 nlmsg_free(msg);
3711 return -ENOBUFS;
3712 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003713
Johannes Berg4c476992010-10-04 21:36:35 +02003714 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003715}
3716
Johannes Berg77ee7c82013-02-15 00:48:33 +01003717int cfg80211_check_station_change(struct wiphy *wiphy,
3718 struct station_parameters *params,
3719 enum cfg80211_station_type statype)
3720{
3721 if (params->listen_interval != -1)
3722 return -EINVAL;
3723 if (params->aid)
3724 return -EINVAL;
3725
3726 /* When you run into this, adjust the code below for the new flag */
3727 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3728
3729 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003730 case CFG80211_STA_MESH_PEER_KERNEL:
3731 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003732 /*
3733 * No ignoring the TDLS flag here -- the userspace mesh
3734 * code doesn't have the bug of including TDLS in the
3735 * mask everywhere.
3736 */
3737 if (params->sta_flags_mask &
3738 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3739 BIT(NL80211_STA_FLAG_MFP) |
3740 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3741 return -EINVAL;
3742 break;
3743 case CFG80211_STA_TDLS_PEER_SETUP:
3744 case CFG80211_STA_TDLS_PEER_ACTIVE:
3745 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3746 return -EINVAL;
3747 /* ignore since it can't change */
3748 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3749 break;
3750 default:
3751 /* disallow mesh-specific things */
3752 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3753 return -EINVAL;
3754 if (params->local_pm)
3755 return -EINVAL;
3756 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3757 return -EINVAL;
3758 }
3759
3760 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3761 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3762 /* TDLS can't be set, ... */
3763 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3764 return -EINVAL;
3765 /*
3766 * ... but don't bother the driver with it. This works around
3767 * a hostapd/wpa_supplicant issue -- it always includes the
3768 * TLDS_PEER flag in the mask even for AP mode.
3769 */
3770 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3771 }
3772
3773 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3774 /* reject other things that can't change */
3775 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3776 return -EINVAL;
3777 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3778 return -EINVAL;
3779 if (params->supported_rates)
3780 return -EINVAL;
3781 if (params->ext_capab || params->ht_capa || params->vht_capa)
3782 return -EINVAL;
3783 }
3784
3785 if (statype != CFG80211_STA_AP_CLIENT) {
3786 if (params->vlan)
3787 return -EINVAL;
3788 }
3789
3790 switch (statype) {
3791 case CFG80211_STA_AP_MLME_CLIENT:
3792 /* Use this only for authorizing/unauthorizing a station */
3793 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3794 return -EOPNOTSUPP;
3795 break;
3796 case CFG80211_STA_AP_CLIENT:
3797 /* accept only the listed bits */
3798 if (params->sta_flags_mask &
3799 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3800 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3801 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3802 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3803 BIT(NL80211_STA_FLAG_WME) |
3804 BIT(NL80211_STA_FLAG_MFP)))
3805 return -EINVAL;
3806
3807 /* but authenticated/associated only if driver handles it */
3808 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3809 params->sta_flags_mask &
3810 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3811 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3812 return -EINVAL;
3813 break;
3814 case CFG80211_STA_IBSS:
3815 case CFG80211_STA_AP_STA:
3816 /* reject any changes other than AUTHORIZED */
3817 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3818 return -EINVAL;
3819 break;
3820 case CFG80211_STA_TDLS_PEER_SETUP:
3821 /* reject any changes other than AUTHORIZED or WME */
3822 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3823 BIT(NL80211_STA_FLAG_WME)))
3824 return -EINVAL;
3825 /* force (at least) rates when authorizing */
3826 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3827 !params->supported_rates)
3828 return -EINVAL;
3829 break;
3830 case CFG80211_STA_TDLS_PEER_ACTIVE:
3831 /* reject any changes */
3832 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003833 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003834 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3835 return -EINVAL;
3836 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003837 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003838 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3839 return -EINVAL;
3840 break;
3841 }
3842
3843 return 0;
3844}
3845EXPORT_SYMBOL(cfg80211_check_station_change);
3846
Johannes Berg5727ef12007-12-19 02:03:34 +01003847/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003848 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003849 */
Johannes Berg80b99892011-11-18 16:23:01 +01003850static struct net_device *get_vlan(struct genl_info *info,
3851 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003852{
Johannes Berg463d0182009-07-14 00:33:35 +02003853 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003854 struct net_device *v;
3855 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003856
Johannes Berg80b99892011-11-18 16:23:01 +01003857 if (!vlanattr)
3858 return NULL;
3859
3860 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3861 if (!v)
3862 return ERR_PTR(-ENODEV);
3863
3864 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3865 ret = -EINVAL;
3866 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003867 }
Johannes Berg80b99892011-11-18 16:23:01 +01003868
Johannes Berg77ee7c82013-02-15 00:48:33 +01003869 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3870 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3871 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3872 ret = -EINVAL;
3873 goto error;
3874 }
3875
Johannes Berg80b99892011-11-18 16:23:01 +01003876 if (!netif_running(v)) {
3877 ret = -ENETDOWN;
3878 goto error;
3879 }
3880
3881 return v;
3882 error:
3883 dev_put(v);
3884 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003885}
3886
Jouni Malinendf881292013-02-14 21:10:54 +02003887static struct nla_policy
3888nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3889 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3890 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3891};
3892
Johannes Bergff276692013-02-15 00:09:01 +01003893static int nl80211_parse_sta_wme(struct genl_info *info,
3894 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003895{
Jouni Malinendf881292013-02-14 21:10:54 +02003896 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3897 struct nlattr *nla;
3898 int err;
3899
Jouni Malinendf881292013-02-14 21:10:54 +02003900 /* parse WME attributes if present */
3901 if (!info->attrs[NL80211_ATTR_STA_WME])
3902 return 0;
3903
3904 nla = info->attrs[NL80211_ATTR_STA_WME];
3905 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3906 nl80211_sta_wme_policy);
3907 if (err)
3908 return err;
3909
3910 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3911 params->uapsd_queues = nla_get_u8(
3912 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3913 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3914 return -EINVAL;
3915
3916 if (tb[NL80211_STA_WME_MAX_SP])
3917 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3918
3919 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3920 return -EINVAL;
3921
3922 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3923
3924 return 0;
3925}
3926
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303927static int nl80211_parse_sta_channel_info(struct genl_info *info,
3928 struct station_parameters *params)
3929{
3930 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3931 params->supported_channels =
3932 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3933 params->supported_channels_len =
3934 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3935 /*
3936 * Need to include at least one (first channel, number of
3937 * channels) tuple for each subband, and must have proper
3938 * tuples for the rest of the data as well.
3939 */
3940 if (params->supported_channels_len < 2)
3941 return -EINVAL;
3942 if (params->supported_channels_len % 2)
3943 return -EINVAL;
3944 }
3945
3946 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
3947 params->supported_oper_classes =
3948 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3949 params->supported_oper_classes_len =
3950 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3951 /*
3952 * The value of the Length field of the Supported Operating
3953 * Classes element is between 2 and 253.
3954 */
3955 if (params->supported_oper_classes_len < 2 ||
3956 params->supported_oper_classes_len > 253)
3957 return -EINVAL;
3958 }
3959 return 0;
3960}
3961
Johannes Bergff276692013-02-15 00:09:01 +01003962static int nl80211_set_station_tdls(struct genl_info *info,
3963 struct station_parameters *params)
3964{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303965 int err;
Johannes Bergff276692013-02-15 00:09:01 +01003966 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03003967 if (info->attrs[NL80211_ATTR_PEER_AID])
3968 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01003969 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3970 params->ht_capa =
3971 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3972 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3973 params->vht_capa =
3974 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3975
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303976 err = nl80211_parse_sta_channel_info(info, params);
3977 if (err)
3978 return err;
3979
Johannes Bergff276692013-02-15 00:09:01 +01003980 return nl80211_parse_sta_wme(info, params);
3981}
3982
Johannes Berg5727ef12007-12-19 02:03:34 +01003983static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3984{
Johannes Berg4c476992010-10-04 21:36:35 +02003985 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003986 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003987 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003988 u8 *mac_addr;
3989 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003990
3991 memset(&params, 0, sizeof(params));
3992
3993 params.listen_interval = -1;
3994
Johannes Berg77ee7c82013-02-15 00:48:33 +01003995 if (!rdev->ops->change_station)
3996 return -EOPNOTSUPP;
3997
Johannes Berg5727ef12007-12-19 02:03:34 +01003998 if (info->attrs[NL80211_ATTR_STA_AID])
3999 return -EINVAL;
4000
4001 if (!info->attrs[NL80211_ATTR_MAC])
4002 return -EINVAL;
4003
4004 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4005
4006 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4007 params.supported_rates =
4008 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4009 params.supported_rates_len =
4010 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4011 }
4012
Jouni Malinen9d62a982013-02-14 21:10:13 +02004013 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4014 params.capability =
4015 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4016 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4017 }
4018
4019 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4020 params.ext_capab =
4021 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4022 params.ext_capab_len =
4023 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4024 }
4025
Jouni Malinendf881292013-02-14 21:10:54 +02004026 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004027 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03004028
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004029 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004030 return -EINVAL;
4031
Johannes Bergf8bacc22013-02-14 23:27:01 +01004032 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004033 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004034 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4035 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4036 return -EINVAL;
4037 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004038
Johannes Bergf8bacc22013-02-14 23:27:01 +01004039 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004040 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004041 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4042 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4043 return -EINVAL;
4044 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4045 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004046
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004047 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4048 enum nl80211_mesh_power_mode pm = nla_get_u32(
4049 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4050
4051 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4052 pm > NL80211_MESH_POWER_MAX)
4053 return -EINVAL;
4054
4055 params.local_pm = pm;
4056 }
4057
Johannes Berg77ee7c82013-02-15 00:48:33 +01004058 /* Include parameters for TDLS peer (will check later) */
4059 err = nl80211_set_station_tdls(info, &params);
4060 if (err)
4061 return err;
4062
4063 params.vlan = get_vlan(info, rdev);
4064 if (IS_ERR(params.vlan))
4065 return PTR_ERR(params.vlan);
4066
Johannes Berga97f4422009-06-18 17:23:43 +02004067 switch (dev->ieee80211_ptr->iftype) {
4068 case NL80211_IFTYPE_AP:
4069 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004070 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004071 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004072 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004073 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004074 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004075 break;
4076 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004077 err = -EOPNOTSUPP;
4078 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004079 }
4080
Johannes Berg77ee7c82013-02-15 00:48:33 +01004081 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004082 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004083
Johannes Berg77ee7c82013-02-15 00:48:33 +01004084 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004085 if (params.vlan)
4086 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004087
Johannes Berg5727ef12007-12-19 02:03:34 +01004088 return err;
4089}
4090
4091static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4092{
Johannes Berg4c476992010-10-04 21:36:35 +02004093 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004094 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004095 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004096 struct station_parameters params;
4097 u8 *mac_addr = NULL;
4098
4099 memset(&params, 0, sizeof(params));
4100
Johannes Berg984c3112013-02-14 23:43:25 +01004101 if (!rdev->ops->add_station)
4102 return -EOPNOTSUPP;
4103
Johannes Berg5727ef12007-12-19 02:03:34 +01004104 if (!info->attrs[NL80211_ATTR_MAC])
4105 return -EINVAL;
4106
Johannes Berg5727ef12007-12-19 02:03:34 +01004107 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4108 return -EINVAL;
4109
4110 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4111 return -EINVAL;
4112
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004113 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4114 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004115 return -EINVAL;
4116
Johannes Berg5727ef12007-12-19 02:03:34 +01004117 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4118 params.supported_rates =
4119 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4120 params.supported_rates_len =
4121 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4122 params.listen_interval =
4123 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004124
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004125 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004126 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004127 else
4128 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004129 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4130 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004131
Jouni Malinen9d62a982013-02-14 21:10:13 +02004132 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4133 params.capability =
4134 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4135 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4136 }
4137
4138 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4139 params.ext_capab =
4140 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4141 params.ext_capab_len =
4142 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4143 }
4144
Jouni Malinen36aedc92008-08-25 11:58:58 +03004145 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4146 params.ht_capa =
4147 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004148
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004149 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4150 params.vht_capa =
4151 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4152
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004153 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4154 params.opmode_notif_used = true;
4155 params.opmode_notif =
4156 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4157 }
4158
Johannes Bergf8bacc22013-02-14 23:27:01 +01004159 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004160 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004161 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4162 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4163 return -EINVAL;
4164 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004165
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304166 err = nl80211_parse_sta_channel_info(info, &params);
4167 if (err)
4168 return err;
4169
Johannes Bergff276692013-02-15 00:09:01 +01004170 err = nl80211_parse_sta_wme(info, &params);
4171 if (err)
4172 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004173
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004174 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004175 return -EINVAL;
4176
Johannes Berg77ee7c82013-02-15 00:48:33 +01004177 /* When you run into this, adjust the code below for the new flag */
4178 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4179
Johannes Bergbdd90d52011-12-14 12:20:27 +01004180 switch (dev->ieee80211_ptr->iftype) {
4181 case NL80211_IFTYPE_AP:
4182 case NL80211_IFTYPE_AP_VLAN:
4183 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004184 /* ignore WME attributes if iface/sta is not capable */
4185 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4186 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4187 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004188
Johannes Bergbdd90d52011-12-14 12:20:27 +01004189 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004190 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4191 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004192 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004193 /* but don't bother the driver with it */
4194 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004195
Johannes Bergd582cff2012-10-26 17:53:44 +02004196 /* allow authenticated/associated only if driver handles it */
4197 if (!(rdev->wiphy.features &
4198 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4199 params.sta_flags_mask &
4200 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4201 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4202 return -EINVAL;
4203
Johannes Bergbdd90d52011-12-14 12:20:27 +01004204 /* must be last in here for error handling */
4205 params.vlan = get_vlan(info, rdev);
4206 if (IS_ERR(params.vlan))
4207 return PTR_ERR(params.vlan);
4208 break;
4209 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004210 /* ignore uAPSD data */
4211 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4212
Johannes Bergd582cff2012-10-26 17:53:44 +02004213 /* associated is disallowed */
4214 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4215 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004216 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004217 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4218 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004219 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004220 break;
4221 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004222 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004223 /* ignore uAPSD data */
4224 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4225
Johannes Berg77ee7c82013-02-15 00:48:33 +01004226 /* these are disallowed */
4227 if (params.sta_flags_mask &
4228 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4229 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004230 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004231 /* Only TDLS peers can be added */
4232 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4233 return -EINVAL;
4234 /* Can only add if TDLS ... */
4235 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4236 return -EOPNOTSUPP;
4237 /* ... with external setup is supported */
4238 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4239 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004240 /*
4241 * Older wpa_supplicant versions always mark the TDLS peer
4242 * as authorized, but it shouldn't yet be.
4243 */
4244 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004245 break;
4246 default:
4247 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004248 }
4249
Johannes Bergbdd90d52011-12-14 12:20:27 +01004250 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004251
Hila Gonene35e4d22012-06-27 17:19:42 +03004252 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004253
Johannes Berg5727ef12007-12-19 02:03:34 +01004254 if (params.vlan)
4255 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004256 return err;
4257}
4258
4259static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4260{
Johannes Berg4c476992010-10-04 21:36:35 +02004261 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4262 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004263 u8 *mac_addr = NULL;
4264
4265 if (info->attrs[NL80211_ATTR_MAC])
4266 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4267
Johannes Berge80cf852009-05-11 14:43:13 +02004268 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004269 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004270 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004271 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4272 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004273
Johannes Berg4c476992010-10-04 21:36:35 +02004274 if (!rdev->ops->del_station)
4275 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004276
Hila Gonene35e4d22012-06-27 17:19:42 +03004277 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004278}
4279
Eric W. Biederman15e47302012-09-07 20:12:54 +00004280static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004281 int flags, struct net_device *dev,
4282 u8 *dst, u8 *next_hop,
4283 struct mpath_info *pinfo)
4284{
4285 void *hdr;
4286 struct nlattr *pinfoattr;
4287
Eric W. Biederman15e47302012-09-07 20:12:54 +00004288 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004289 if (!hdr)
4290 return -1;
4291
David S. Miller9360ffd2012-03-29 04:41:26 -04004292 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4293 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4294 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4295 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4296 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004297
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004298 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4299 if (!pinfoattr)
4300 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004301 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4302 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4303 pinfo->frame_qlen))
4304 goto nla_put_failure;
4305 if (((pinfo->filled & MPATH_INFO_SN) &&
4306 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4307 ((pinfo->filled & MPATH_INFO_METRIC) &&
4308 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4309 pinfo->metric)) ||
4310 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4311 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4312 pinfo->exptime)) ||
4313 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4314 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4315 pinfo->flags)) ||
4316 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4317 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4318 pinfo->discovery_timeout)) ||
4319 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4320 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4321 pinfo->discovery_retries)))
4322 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004323
4324 nla_nest_end(msg, pinfoattr);
4325
4326 return genlmsg_end(msg, hdr);
4327
4328 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004329 genlmsg_cancel(msg, hdr);
4330 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004331}
4332
4333static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004334 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004335{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004336 struct mpath_info pinfo;
4337 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004338 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004339 u8 dst[ETH_ALEN];
4340 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004341 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004342 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004343
Johannes Berg97990a02013-04-19 01:02:55 +02004344 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004345 if (err)
4346 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004347
4348 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004349 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004350 goto out_err;
4351 }
4352
Johannes Berg97990a02013-04-19 01:02:55 +02004353 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004354 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004355 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004356 }
4357
Johannes Bergbba95fe2008-07-29 13:22:51 +02004358 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004359 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4360 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004361 if (err == -ENOENT)
4362 break;
4363 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004364 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004365
Eric W. Biederman15e47302012-09-07 20:12:54 +00004366 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004367 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004368 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004369 &pinfo) < 0)
4370 goto out;
4371
4372 path_idx++;
4373 }
4374
4375
4376 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004377 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004378 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004379 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004380 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004381 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004382}
4383
4384static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4385{
Johannes Berg4c476992010-10-04 21:36:35 +02004386 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004387 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004388 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004389 struct mpath_info pinfo;
4390 struct sk_buff *msg;
4391 u8 *dst = NULL;
4392 u8 next_hop[ETH_ALEN];
4393
4394 memset(&pinfo, 0, sizeof(pinfo));
4395
4396 if (!info->attrs[NL80211_ATTR_MAC])
4397 return -EINVAL;
4398
4399 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4400
Johannes Berg4c476992010-10-04 21:36:35 +02004401 if (!rdev->ops->get_mpath)
4402 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004403
Johannes Berg4c476992010-10-04 21:36:35 +02004404 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4405 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004406
Hila Gonene35e4d22012-06-27 17:19:42 +03004407 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004408 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004409 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004410
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004411 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004412 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004413 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004414
Eric W. Biederman15e47302012-09-07 20:12:54 +00004415 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004416 dev, dst, next_hop, &pinfo) < 0) {
4417 nlmsg_free(msg);
4418 return -ENOBUFS;
4419 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004420
Johannes Berg4c476992010-10-04 21:36:35 +02004421 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004422}
4423
4424static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4425{
Johannes Berg4c476992010-10-04 21:36:35 +02004426 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4427 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004428 u8 *dst = NULL;
4429 u8 *next_hop = NULL;
4430
4431 if (!info->attrs[NL80211_ATTR_MAC])
4432 return -EINVAL;
4433
4434 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4435 return -EINVAL;
4436
4437 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4438 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4439
Johannes Berg4c476992010-10-04 21:36:35 +02004440 if (!rdev->ops->change_mpath)
4441 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004442
Johannes Berg4c476992010-10-04 21:36:35 +02004443 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4444 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004445
Hila Gonene35e4d22012-06-27 17:19:42 +03004446 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004447}
Johannes Berg4c476992010-10-04 21:36:35 +02004448
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004449static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4450{
Johannes Berg4c476992010-10-04 21:36:35 +02004451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4452 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004453 u8 *dst = NULL;
4454 u8 *next_hop = NULL;
4455
4456 if (!info->attrs[NL80211_ATTR_MAC])
4457 return -EINVAL;
4458
4459 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4460 return -EINVAL;
4461
4462 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4463 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4464
Johannes Berg4c476992010-10-04 21:36:35 +02004465 if (!rdev->ops->add_mpath)
4466 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004467
Johannes Berg4c476992010-10-04 21:36:35 +02004468 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4469 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004470
Hila Gonene35e4d22012-06-27 17:19:42 +03004471 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004472}
4473
4474static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4475{
Johannes Berg4c476992010-10-04 21:36:35 +02004476 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4477 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004478 u8 *dst = NULL;
4479
4480 if (info->attrs[NL80211_ATTR_MAC])
4481 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4482
Johannes Berg4c476992010-10-04 21:36:35 +02004483 if (!rdev->ops->del_mpath)
4484 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004485
Hila Gonene35e4d22012-06-27 17:19:42 +03004486 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004487}
4488
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004489static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4490{
Johannes Berg4c476992010-10-04 21:36:35 +02004491 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4492 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004493 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004494 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004495 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004496
4497 memset(&params, 0, sizeof(params));
4498 /* default to not changing parameters */
4499 params.use_cts_prot = -1;
4500 params.use_short_preamble = -1;
4501 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004502 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004503 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004504 params.p2p_ctwindow = -1;
4505 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004506
4507 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4508 params.use_cts_prot =
4509 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4510 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4511 params.use_short_preamble =
4512 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4513 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4514 params.use_short_slot_time =
4515 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004516 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4517 params.basic_rates =
4518 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4519 params.basic_rates_len =
4520 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4521 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004522 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4523 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004524 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4525 params.ht_opmode =
4526 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004527
Johannes Berg53cabad2012-11-14 15:17:28 +01004528 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4529 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4530 return -EINVAL;
4531 params.p2p_ctwindow =
4532 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4533 if (params.p2p_ctwindow < 0)
4534 return -EINVAL;
4535 if (params.p2p_ctwindow != 0 &&
4536 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4537 return -EINVAL;
4538 }
4539
4540 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4541 u8 tmp;
4542
4543 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4544 return -EINVAL;
4545 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4546 if (tmp > 1)
4547 return -EINVAL;
4548 params.p2p_opp_ps = tmp;
4549 if (params.p2p_opp_ps &&
4550 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4551 return -EINVAL;
4552 }
4553
Johannes Berg4c476992010-10-04 21:36:35 +02004554 if (!rdev->ops->change_bss)
4555 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004556
Johannes Berg074ac8d2010-09-16 14:58:22 +02004557 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004558 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4559 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004560
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004561 wdev_lock(wdev);
4562 err = rdev_change_bss(rdev, dev, &params);
4563 wdev_unlock(wdev);
4564
4565 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004566}
4567
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004568static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004569 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4570 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4571 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4572 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4573 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4574 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4575};
4576
4577static int parse_reg_rule(struct nlattr *tb[],
4578 struct ieee80211_reg_rule *reg_rule)
4579{
4580 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4581 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4582
4583 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4584 return -EINVAL;
4585 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4586 return -EINVAL;
4587 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4588 return -EINVAL;
4589 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4590 return -EINVAL;
4591 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4592 return -EINVAL;
4593
4594 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4595
4596 freq_range->start_freq_khz =
4597 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4598 freq_range->end_freq_khz =
4599 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4600 freq_range->max_bandwidth_khz =
4601 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4602
4603 power_rule->max_eirp =
4604 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4605
4606 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4607 power_rule->max_antenna_gain =
4608 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4609
4610 return 0;
4611}
4612
4613static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4614{
4615 int r;
4616 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004617 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004618
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004619 /*
4620 * You should only get this when cfg80211 hasn't yet initialized
4621 * completely when built-in to the kernel right between the time
4622 * window between nl80211_init() and regulatory_init(), if that is
4623 * even possible.
4624 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004625 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004626 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004627
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004628 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4629 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004630
4631 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4632
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004633 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4634 user_reg_hint_type =
4635 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4636 else
4637 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4638
4639 switch (user_reg_hint_type) {
4640 case NL80211_USER_REG_HINT_USER:
4641 case NL80211_USER_REG_HINT_CELL_BASE:
4642 break;
4643 default:
4644 return -EINVAL;
4645 }
4646
4647 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004648
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004649 return r;
4650}
4651
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004652static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004653 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004654{
Johannes Berg4c476992010-10-04 21:36:35 +02004655 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004656 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004657 struct wireless_dev *wdev = dev->ieee80211_ptr;
4658 struct mesh_config cur_params;
4659 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004660 void *hdr;
4661 struct nlattr *pinfoattr;
4662 struct sk_buff *msg;
4663
Johannes Berg29cbe682010-12-03 09:20:44 +01004664 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4665 return -EOPNOTSUPP;
4666
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004667 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004668 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004669
Johannes Berg29cbe682010-12-03 09:20:44 +01004670 wdev_lock(wdev);
4671 /* If not connected, get default parameters */
4672 if (!wdev->mesh_id_len)
4673 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4674 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004675 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004676 wdev_unlock(wdev);
4677
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004678 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004679 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004680
4681 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004682 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004683 if (!msg)
4684 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004685 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004686 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004687 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004688 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004689 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004690 if (!pinfoattr)
4691 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004692 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4693 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4694 cur_params.dot11MeshRetryTimeout) ||
4695 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4696 cur_params.dot11MeshConfirmTimeout) ||
4697 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4698 cur_params.dot11MeshHoldingTimeout) ||
4699 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4700 cur_params.dot11MeshMaxPeerLinks) ||
4701 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4702 cur_params.dot11MeshMaxRetries) ||
4703 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4704 cur_params.dot11MeshTTL) ||
4705 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4706 cur_params.element_ttl) ||
4707 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4708 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004709 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4710 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004711 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4712 cur_params.dot11MeshHWMPmaxPREQretries) ||
4713 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4714 cur_params.path_refresh_time) ||
4715 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4716 cur_params.min_discovery_timeout) ||
4717 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4718 cur_params.dot11MeshHWMPactivePathTimeout) ||
4719 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4720 cur_params.dot11MeshHWMPpreqMinInterval) ||
4721 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4722 cur_params.dot11MeshHWMPperrMinInterval) ||
4723 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4724 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4725 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4726 cur_params.dot11MeshHWMPRootMode) ||
4727 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4728 cur_params.dot11MeshHWMPRannInterval) ||
4729 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4730 cur_params.dot11MeshGateAnnouncementProtocol) ||
4731 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4732 cur_params.dot11MeshForwarding) ||
4733 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004734 cur_params.rssi_threshold) ||
4735 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004736 cur_params.ht_opmode) ||
4737 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4738 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4739 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004740 cur_params.dot11MeshHWMProotInterval) ||
4741 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004742 cur_params.dot11MeshHWMPconfirmationInterval) ||
4743 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4744 cur_params.power_mode) ||
4745 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004746 cur_params.dot11MeshAwakeWindowDuration) ||
4747 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4748 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004749 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004750 nla_nest_end(msg, pinfoattr);
4751 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004752 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004753
Johannes Berg3b858752009-03-12 09:55:09 +01004754 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004755 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004756 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004757 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004758 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004759}
4760
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004761static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004762 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4763 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4764 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4765 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4766 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4767 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004768 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004769 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004770 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004771 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4772 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4773 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4774 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4775 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004776 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004777 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004778 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004779 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004780 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004781 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004782 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4783 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004784 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4785 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004786 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004787 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4788 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004789 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004790};
4791
Javier Cardonac80d5452010-12-16 17:37:49 -08004792static const struct nla_policy
4793 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004794 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004795 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4796 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004797 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004798 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004799 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004800 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004801 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004802 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004803};
4804
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004805static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004806 struct mesh_config *cfg,
4807 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004808{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004809 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004810 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004811
Marco Porschea54fba2013-01-07 16:04:48 +01004812#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4813do { \
4814 if (tb[attr]) { \
4815 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4816 return -EINVAL; \
4817 cfg->param = fn(tb[attr]); \
4818 mask |= (1 << (attr - 1)); \
4819 } \
4820} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004821
4822
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004823 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004824 return -EINVAL;
4825 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004826 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004827 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004828 return -EINVAL;
4829
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004830 /* This makes sure that there aren't more than 32 mesh config
4831 * parameters (otherwise our bitfield scheme would not work.) */
4832 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4833
4834 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004835 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004836 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4837 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004838 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004839 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4840 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004841 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004842 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4843 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004844 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004845 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4846 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004847 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004848 mask, NL80211_MESHCONF_MAX_RETRIES,
4849 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004850 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004851 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004852 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004853 mask, NL80211_MESHCONF_ELEMENT_TTL,
4854 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004855 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004856 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4857 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004858 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4859 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004860 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4861 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004862 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004863 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4864 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004865 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004866 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4867 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004868 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004869 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4870 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004871 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4872 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004873 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4874 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004875 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004876 1, 65535, mask,
4877 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004878 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004879 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004880 1, 65535, mask,
4881 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004882 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004883 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004884 dot11MeshHWMPnetDiameterTraversalTime,
4885 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004886 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4887 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004888 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4889 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4890 nla_get_u8);
4891 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4892 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004893 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004894 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004895 dot11MeshGateAnnouncementProtocol, 0, 1,
4896 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004897 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004898 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004899 mask, NL80211_MESHCONF_FORWARDING,
4900 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004901 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004902 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004903 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004904 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004905 mask, NL80211_MESHCONF_HT_OPMODE,
4906 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004907 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004908 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004909 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4910 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004911 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004912 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4913 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004914 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004915 dot11MeshHWMPconfirmationInterval,
4916 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004917 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4918 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4920 NL80211_MESH_POWER_ACTIVE,
4921 NL80211_MESH_POWER_MAX,
4922 mask, NL80211_MESHCONF_POWER_MODE,
4923 nla_get_u32);
4924 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4925 0, 65535, mask,
4926 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004927 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4928 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4929 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004930 if (mask_out)
4931 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004932
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004933 return 0;
4934
4935#undef FILL_IN_MESH_PARAM_IF_SET
4936}
4937
Javier Cardonac80d5452010-12-16 17:37:49 -08004938static int nl80211_parse_mesh_setup(struct genl_info *info,
4939 struct mesh_setup *setup)
4940{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004942 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4943
4944 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4945 return -EINVAL;
4946 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4947 info->attrs[NL80211_ATTR_MESH_SETUP],
4948 nl80211_mesh_setup_params_policy))
4949 return -EINVAL;
4950
Javier Cardonad299a1f2012-03-31 11:31:33 -07004951 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4952 setup->sync_method =
4953 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4954 IEEE80211_SYNC_METHOD_VENDOR :
4955 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4956
Javier Cardonac80d5452010-12-16 17:37:49 -08004957 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4958 setup->path_sel_proto =
4959 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4960 IEEE80211_PATH_PROTOCOL_VENDOR :
4961 IEEE80211_PATH_PROTOCOL_HWMP;
4962
4963 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4964 setup->path_metric =
4965 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4966 IEEE80211_PATH_METRIC_VENDOR :
4967 IEEE80211_PATH_METRIC_AIRTIME;
4968
Javier Cardona581a8b02011-04-07 15:08:27 -07004969
4970 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004971 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004972 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004973 if (!is_valid_ie_attr(ieattr))
4974 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004975 setup->ie = nla_data(ieattr);
4976 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004977 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004978 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4979 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4980 return -EINVAL;
4981 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004982 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4983 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004984 if (setup->is_secure)
4985 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08004986
Colleen Twitty6e16d902013-05-08 11:45:59 -07004987 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
4988 if (!setup->user_mpm)
4989 return -EINVAL;
4990 setup->auth_id =
4991 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
4992 }
4993
Javier Cardonac80d5452010-12-16 17:37:49 -08004994 return 0;
4995}
4996
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004997static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004998 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004999{
5000 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5001 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005002 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005003 struct mesh_config cfg;
5004 u32 mask;
5005 int err;
5006
Johannes Berg29cbe682010-12-03 09:20:44 +01005007 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5008 return -EOPNOTSUPP;
5009
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005010 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005011 return -EOPNOTSUPP;
5012
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005013 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005014 if (err)
5015 return err;
5016
Johannes Berg29cbe682010-12-03 09:20:44 +01005017 wdev_lock(wdev);
5018 if (!wdev->mesh_id_len)
5019 err = -ENOLINK;
5020
5021 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005022 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005023
5024 wdev_unlock(wdev);
5025
5026 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005027}
5028
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005029static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5030{
Johannes Berg458f4f92012-12-06 15:47:38 +01005031 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005032 struct sk_buff *msg;
5033 void *hdr = NULL;
5034 struct nlattr *nl_reg_rules;
5035 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005036
5037 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005038 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005039
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005040 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005041 if (!msg)
5042 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005043
Eric W. Biederman15e47302012-09-07 20:12:54 +00005044 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005045 NL80211_CMD_GET_REG);
5046 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005047 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005048
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005049 if (reg_last_request_cell_base() &&
5050 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5051 NL80211_USER_REG_HINT_CELL_BASE))
5052 goto nla_put_failure;
5053
Johannes Berg458f4f92012-12-06 15:47:38 +01005054 rcu_read_lock();
5055 regdom = rcu_dereference(cfg80211_regdomain);
5056
5057 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5058 (regdom->dfs_region &&
5059 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5060 goto nla_put_failure_rcu;
5061
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005062 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5063 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005064 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005065
Johannes Berg458f4f92012-12-06 15:47:38 +01005066 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005067 struct nlattr *nl_reg_rule;
5068 const struct ieee80211_reg_rule *reg_rule;
5069 const struct ieee80211_freq_range *freq_range;
5070 const struct ieee80211_power_rule *power_rule;
5071
Johannes Berg458f4f92012-12-06 15:47:38 +01005072 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005073 freq_range = &reg_rule->freq_range;
5074 power_rule = &reg_rule->power_rule;
5075
5076 nl_reg_rule = nla_nest_start(msg, i);
5077 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005078 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005079
David S. Miller9360ffd2012-03-29 04:41:26 -04005080 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5081 reg_rule->flags) ||
5082 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5083 freq_range->start_freq_khz) ||
5084 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5085 freq_range->end_freq_khz) ||
5086 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
5087 freq_range->max_bandwidth_khz) ||
5088 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5089 power_rule->max_antenna_gain) ||
5090 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
5091 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01005092 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005093
5094 nla_nest_end(msg, nl_reg_rule);
5095 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005096 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005097
5098 nla_nest_end(msg, nl_reg_rules);
5099
5100 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005101 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005102
Johannes Berg458f4f92012-12-06 15:47:38 +01005103nla_put_failure_rcu:
5104 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005105nla_put_failure:
5106 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005107put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005108 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005109 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005110}
5111
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005112static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5113{
5114 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5115 struct nlattr *nl_reg_rule;
5116 char *alpha2 = NULL;
5117 int rem_reg_rules = 0, r = 0;
5118 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005119 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005120 struct ieee80211_regdomain *rd = NULL;
5121
5122 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5123 return -EINVAL;
5124
5125 if (!info->attrs[NL80211_ATTR_REG_RULES])
5126 return -EINVAL;
5127
5128 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5129
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005130 if (info->attrs[NL80211_ATTR_DFS_REGION])
5131 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5132
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005133 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005134 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005135 num_rules++;
5136 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005137 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005138 }
5139
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005140 if (!reg_is_valid_request(alpha2))
5141 return -EINVAL;
5142
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005143 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005144 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005145
5146 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005147 if (!rd)
5148 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005149
5150 rd->n_reg_rules = num_rules;
5151 rd->alpha2[0] = alpha2[0];
5152 rd->alpha2[1] = alpha2[1];
5153
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005154 /*
5155 * Disable DFS master mode if the DFS region was
5156 * not supported or known on this kernel.
5157 */
5158 if (reg_supported_dfs_region(dfs_region))
5159 rd->dfs_region = dfs_region;
5160
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005161 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005162 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005163 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01005164 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5165 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005166 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5167 if (r)
5168 goto bad_reg;
5169
5170 rule_idx++;
5171
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005172 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5173 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005174 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005175 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005176 }
5177
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005178 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005179 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005180 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005181
Johannes Bergd2372b32008-10-24 20:32:20 +02005182 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005183 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005184 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005185}
5186
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005187static int validate_scan_freqs(struct nlattr *freqs)
5188{
5189 struct nlattr *attr1, *attr2;
5190 int n_channels = 0, tmp1, tmp2;
5191
5192 nla_for_each_nested(attr1, freqs, tmp1) {
5193 n_channels++;
5194 /*
5195 * Some hardware has a limited channel list for
5196 * scanning, and it is pretty much nonsensical
5197 * to scan for a channel twice, so disallow that
5198 * and don't require drivers to check that the
5199 * channel list they get isn't longer than what
5200 * they can scan, as long as they can scan all
5201 * the channels they registered at once.
5202 */
5203 nla_for_each_nested(attr2, freqs, tmp2)
5204 if (attr1 != attr2 &&
5205 nla_get_u32(attr1) == nla_get_u32(attr2))
5206 return 0;
5207 }
5208
5209 return n_channels;
5210}
5211
Johannes Berg2a519312009-02-10 21:25:55 +01005212static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5213{
Johannes Berg4c476992010-10-04 21:36:35 +02005214 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005215 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005216 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005217 struct nlattr *attr;
5218 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005219 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005220 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005221
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005222 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5223 return -EINVAL;
5224
Johannes Berg79c97e92009-07-07 03:56:12 +02005225 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005226
Johannes Berg4c476992010-10-04 21:36:35 +02005227 if (!rdev->ops->scan)
5228 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005229
Johannes Bergf9f47522013-03-19 15:04:07 +01005230 if (rdev->scan_req) {
5231 err = -EBUSY;
5232 goto unlock;
5233 }
Johannes Berg2a519312009-02-10 21:25:55 +01005234
5235 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005236 n_channels = validate_scan_freqs(
5237 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005238 if (!n_channels) {
5239 err = -EINVAL;
5240 goto unlock;
5241 }
Johannes Berg2a519312009-02-10 21:25:55 +01005242 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005243 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005244 n_channels = 0;
5245
Johannes Berg2a519312009-02-10 21:25:55 +01005246 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5247 if (wiphy->bands[band])
5248 n_channels += wiphy->bands[band]->n_channels;
5249 }
5250
5251 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5252 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5253 n_ssids++;
5254
Johannes Bergf9f47522013-03-19 15:04:07 +01005255 if (n_ssids > wiphy->max_scan_ssids) {
5256 err = -EINVAL;
5257 goto unlock;
5258 }
Johannes Berg2a519312009-02-10 21:25:55 +01005259
Jouni Malinen70692ad2009-02-16 19:39:13 +02005260 if (info->attrs[NL80211_ATTR_IE])
5261 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5262 else
5263 ie_len = 0;
5264
Johannes Bergf9f47522013-03-19 15:04:07 +01005265 if (ie_len > wiphy->max_scan_ie_len) {
5266 err = -EINVAL;
5267 goto unlock;
5268 }
Johannes Berg18a83652009-03-31 12:12:05 +02005269
Johannes Berg2a519312009-02-10 21:25:55 +01005270 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005271 + sizeof(*request->ssids) * n_ssids
5272 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005273 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005274 if (!request) {
5275 err = -ENOMEM;
5276 goto unlock;
5277 }
Johannes Berg2a519312009-02-10 21:25:55 +01005278
Johannes Berg2a519312009-02-10 21:25:55 +01005279 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005280 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005281 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005282 if (ie_len) {
5283 if (request->ssids)
5284 request->ie = (void *)(request->ssids + n_ssids);
5285 else
5286 request->ie = (void *)(request->channels + n_channels);
5287 }
Johannes Berg2a519312009-02-10 21:25:55 +01005288
Johannes Berg584991d2009-11-02 13:32:03 +01005289 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005290 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5291 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005292 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005293 struct ieee80211_channel *chan;
5294
5295 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5296
5297 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005298 err = -EINVAL;
5299 goto out_free;
5300 }
Johannes Berg584991d2009-11-02 13:32:03 +01005301
5302 /* ignore disabled channels */
5303 if (chan->flags & IEEE80211_CHAN_DISABLED)
5304 continue;
5305
5306 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005307 i++;
5308 }
5309 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005310 enum ieee80211_band band;
5311
Johannes Berg2a519312009-02-10 21:25:55 +01005312 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005313 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5314 int j;
5315 if (!wiphy->bands[band])
5316 continue;
5317 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005318 struct ieee80211_channel *chan;
5319
5320 chan = &wiphy->bands[band]->channels[j];
5321
5322 if (chan->flags & IEEE80211_CHAN_DISABLED)
5323 continue;
5324
5325 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005326 i++;
5327 }
5328 }
5329 }
5330
Johannes Berg584991d2009-11-02 13:32:03 +01005331 if (!i) {
5332 err = -EINVAL;
5333 goto out_free;
5334 }
5335
5336 request->n_channels = i;
5337
Johannes Berg2a519312009-02-10 21:25:55 +01005338 i = 0;
5339 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5340 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005341 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005342 err = -EINVAL;
5343 goto out_free;
5344 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005345 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005346 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005347 i++;
5348 }
5349 }
5350
Jouni Malinen70692ad2009-02-16 19:39:13 +02005351 if (info->attrs[NL80211_ATTR_IE]) {
5352 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005353 memcpy((void *)request->ie,
5354 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005355 request->ie_len);
5356 }
5357
Johannes Berg34850ab2011-07-18 18:08:35 +02005358 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005359 if (wiphy->bands[i])
5360 request->rates[i] =
5361 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005362
5363 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5364 nla_for_each_nested(attr,
5365 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5366 tmp) {
5367 enum ieee80211_band band = nla_type(attr);
5368
Dan Carpenter84404622011-07-29 11:52:18 +03005369 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005370 err = -EINVAL;
5371 goto out_free;
5372 }
5373 err = ieee80211_get_ratemask(wiphy->bands[band],
5374 nla_data(attr),
5375 nla_len(attr),
5376 &request->rates[band]);
5377 if (err)
5378 goto out_free;
5379 }
5380 }
5381
Sam Leffler46856bb2012-10-11 21:03:32 -07005382 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005383 request->flags = nla_get_u32(
5384 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005385 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5386 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005387 err = -EOPNOTSUPP;
5388 goto out_free;
5389 }
5390 }
Sam Lefflered4737712012-10-11 21:03:31 -07005391
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305392 request->no_cck =
5393 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5394
Johannes Bergfd014282012-06-18 19:17:03 +02005395 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005396 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005397 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005398
Johannes Berg79c97e92009-07-07 03:56:12 +02005399 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005400 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005401
Johannes Berg463d0182009-07-14 00:33:35 +02005402 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005403 nl80211_send_scan_start(rdev, wdev);
5404 if (wdev->netdev)
5405 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005406 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005407 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005408 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005409 kfree(request);
5410 }
Johannes Berg3b858752009-03-12 09:55:09 +01005411
Johannes Bergf9f47522013-03-19 15:04:07 +01005412 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005413 return err;
5414}
5415
Luciano Coelho807f8a82011-05-11 17:09:35 +03005416static int nl80211_start_sched_scan(struct sk_buff *skb,
5417 struct genl_info *info)
5418{
5419 struct cfg80211_sched_scan_request *request;
5420 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5421 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005422 struct nlattr *attr;
5423 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005424 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005425 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005426 enum ieee80211_band band;
5427 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005428 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005429
5430 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5431 !rdev->ops->sched_scan_start)
5432 return -EOPNOTSUPP;
5433
5434 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5435 return -EINVAL;
5436
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005437 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5438 return -EINVAL;
5439
5440 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5441 if (interval == 0)
5442 return -EINVAL;
5443
Luciano Coelho807f8a82011-05-11 17:09:35 +03005444 wiphy = &rdev->wiphy;
5445
5446 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5447 n_channels = validate_scan_freqs(
5448 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5449 if (!n_channels)
5450 return -EINVAL;
5451 } else {
5452 n_channels = 0;
5453
5454 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5455 if (wiphy->bands[band])
5456 n_channels += wiphy->bands[band]->n_channels;
5457 }
5458
5459 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5460 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5461 tmp)
5462 n_ssids++;
5463
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005464 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005465 return -EINVAL;
5466
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005467 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5468 nla_for_each_nested(attr,
5469 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5470 tmp)
5471 n_match_sets++;
5472
5473 if (n_match_sets > wiphy->max_match_sets)
5474 return -EINVAL;
5475
Luciano Coelho807f8a82011-05-11 17:09:35 +03005476 if (info->attrs[NL80211_ATTR_IE])
5477 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5478 else
5479 ie_len = 0;
5480
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005481 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005482 return -EINVAL;
5483
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005484 if (rdev->sched_scan_req) {
5485 err = -EINPROGRESS;
5486 goto out;
5487 }
5488
Luciano Coelho807f8a82011-05-11 17:09:35 +03005489 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005490 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005491 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005492 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005493 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005494 if (!request) {
5495 err = -ENOMEM;
5496 goto out;
5497 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005498
5499 if (n_ssids)
5500 request->ssids = (void *)&request->channels[n_channels];
5501 request->n_ssids = n_ssids;
5502 if (ie_len) {
5503 if (request->ssids)
5504 request->ie = (void *)(request->ssids + n_ssids);
5505 else
5506 request->ie = (void *)(request->channels + n_channels);
5507 }
5508
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005509 if (n_match_sets) {
5510 if (request->ie)
5511 request->match_sets = (void *)(request->ie + ie_len);
5512 else if (request->ssids)
5513 request->match_sets =
5514 (void *)(request->ssids + n_ssids);
5515 else
5516 request->match_sets =
5517 (void *)(request->channels + n_channels);
5518 }
5519 request->n_match_sets = n_match_sets;
5520
Luciano Coelho807f8a82011-05-11 17:09:35 +03005521 i = 0;
5522 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5523 /* user specified, bail out if channel not found */
5524 nla_for_each_nested(attr,
5525 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5526 tmp) {
5527 struct ieee80211_channel *chan;
5528
5529 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5530
5531 if (!chan) {
5532 err = -EINVAL;
5533 goto out_free;
5534 }
5535
5536 /* ignore disabled channels */
5537 if (chan->flags & IEEE80211_CHAN_DISABLED)
5538 continue;
5539
5540 request->channels[i] = chan;
5541 i++;
5542 }
5543 } else {
5544 /* all channels */
5545 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5546 int j;
5547 if (!wiphy->bands[band])
5548 continue;
5549 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5550 struct ieee80211_channel *chan;
5551
5552 chan = &wiphy->bands[band]->channels[j];
5553
5554 if (chan->flags & IEEE80211_CHAN_DISABLED)
5555 continue;
5556
5557 request->channels[i] = chan;
5558 i++;
5559 }
5560 }
5561 }
5562
5563 if (!i) {
5564 err = -EINVAL;
5565 goto out_free;
5566 }
5567
5568 request->n_channels = i;
5569
5570 i = 0;
5571 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5572 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5573 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005574 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005575 err = -EINVAL;
5576 goto out_free;
5577 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005578 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005579 memcpy(request->ssids[i].ssid, nla_data(attr),
5580 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005581 i++;
5582 }
5583 }
5584
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005585 i = 0;
5586 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5587 nla_for_each_nested(attr,
5588 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5589 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005590 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005591
5592 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5593 nla_data(attr), nla_len(attr),
5594 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005595 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005596 if (ssid) {
5597 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5598 err = -EINVAL;
5599 goto out_free;
5600 }
5601 memcpy(request->match_sets[i].ssid.ssid,
5602 nla_data(ssid), nla_len(ssid));
5603 request->match_sets[i].ssid.ssid_len =
5604 nla_len(ssid);
5605 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005606 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5607 if (rssi)
5608 request->rssi_thold = nla_get_u32(rssi);
5609 else
5610 request->rssi_thold =
5611 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005612 i++;
5613 }
5614 }
5615
Luciano Coelho807f8a82011-05-11 17:09:35 +03005616 if (info->attrs[NL80211_ATTR_IE]) {
5617 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5618 memcpy((void *)request->ie,
5619 nla_data(info->attrs[NL80211_ATTR_IE]),
5620 request->ie_len);
5621 }
5622
Sam Leffler46856bb2012-10-11 21:03:32 -07005623 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005624 request->flags = nla_get_u32(
5625 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005626 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5627 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005628 err = -EOPNOTSUPP;
5629 goto out_free;
5630 }
5631 }
Sam Lefflered4737712012-10-11 21:03:31 -07005632
Luciano Coelho807f8a82011-05-11 17:09:35 +03005633 request->dev = dev;
5634 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005635 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005636 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005637
Hila Gonene35e4d22012-06-27 17:19:42 +03005638 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005639 if (!err) {
5640 rdev->sched_scan_req = request;
5641 nl80211_send_sched_scan(rdev, dev,
5642 NL80211_CMD_START_SCHED_SCAN);
5643 goto out;
5644 }
5645
5646out_free:
5647 kfree(request);
5648out:
5649 return err;
5650}
5651
5652static int nl80211_stop_sched_scan(struct sk_buff *skb,
5653 struct genl_info *info)
5654{
5655 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5656
5657 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5658 !rdev->ops->sched_scan_stop)
5659 return -EOPNOTSUPP;
5660
Johannes Berg5fe231e2013-05-08 21:45:15 +02005661 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005662}
5663
Simon Wunderlich04f39042013-02-08 18:16:19 +01005664static int nl80211_start_radar_detection(struct sk_buff *skb,
5665 struct genl_info *info)
5666{
5667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5668 struct net_device *dev = info->user_ptr[1];
5669 struct wireless_dev *wdev = dev->ieee80211_ptr;
5670 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005671 enum nl80211_dfs_regions dfs_region;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005672 int err;
5673
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005674 dfs_region = reg_get_dfs_region(wdev->wiphy);
5675 if (dfs_region == NL80211_DFS_UNSET)
5676 return -EINVAL;
5677
Simon Wunderlich04f39042013-02-08 18:16:19 +01005678 err = nl80211_parse_chandef(rdev, info, &chandef);
5679 if (err)
5680 return err;
5681
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005682 if (netif_carrier_ok(dev))
5683 return -EBUSY;
5684
Simon Wunderlich04f39042013-02-08 18:16:19 +01005685 if (wdev->cac_started)
5686 return -EBUSY;
5687
5688 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5689 if (err < 0)
5690 return err;
5691
5692 if (err == 0)
5693 return -EINVAL;
5694
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005695 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005696 return -EINVAL;
5697
5698 if (!rdev->ops->start_radar_detection)
5699 return -EOPNOTSUPP;
5700
Simon Wunderlich04f39042013-02-08 18:16:19 +01005701 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5702 chandef.chan, CHAN_MODE_SHARED,
5703 BIT(chandef.width));
5704 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005705 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005706
5707 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5708 if (!err) {
5709 wdev->channel = chandef.chan;
5710 wdev->cac_started = true;
5711 wdev->cac_start_time = jiffies;
5712 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005713 return err;
5714}
5715
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005716static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5717{
5718 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5719 struct net_device *dev = info->user_ptr[1];
5720 struct wireless_dev *wdev = dev->ieee80211_ptr;
5721 struct cfg80211_csa_settings params;
5722 /* csa_attrs is defined static to avoid waste of stack size - this
5723 * function is called under RTNL lock, so this should not be a problem.
5724 */
5725 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5726 u8 radar_detect_width = 0;
5727 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005728 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005729
5730 if (!rdev->ops->channel_switch ||
5731 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5732 return -EOPNOTSUPP;
5733
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005734 switch (dev->ieee80211_ptr->iftype) {
5735 case NL80211_IFTYPE_AP:
5736 case NL80211_IFTYPE_P2P_GO:
5737 need_new_beacon = true;
5738
5739 /* useless if AP is not running */
5740 if (!wdev->beacon_interval)
5741 return -EINVAL;
5742 break;
5743 case NL80211_IFTYPE_ADHOC:
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005744 case NL80211_IFTYPE_MESH_POINT:
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005745 break;
5746 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005747 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005748 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005749
5750 memset(&params, 0, sizeof(params));
5751
5752 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5753 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5754 return -EINVAL;
5755
5756 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005757 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005758 return -EINVAL;
5759
5760 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5761
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005762 if (!need_new_beacon)
5763 goto skip_beacons;
5764
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005765 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5766 if (err)
5767 return err;
5768
5769 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5770 info->attrs[NL80211_ATTR_CSA_IES],
5771 nl80211_policy);
5772 if (err)
5773 return err;
5774
5775 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5776 if (err)
5777 return err;
5778
5779 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5780 return -EINVAL;
5781
5782 params.counter_offset_beacon =
5783 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5784 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5785 return -EINVAL;
5786
5787 /* sanity check - counters should be the same */
5788 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5789 params.count)
5790 return -EINVAL;
5791
5792 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5793 params.counter_offset_presp =
5794 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5795 if (params.counter_offset_presp >=
5796 params.beacon_csa.probe_resp_len)
5797 return -EINVAL;
5798
5799 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5800 params.count)
5801 return -EINVAL;
5802 }
5803
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005804skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005805 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5806 if (err)
5807 return err;
5808
5809 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5810 return -EINVAL;
5811
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005812 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP ||
Simon Wunderlich5336fa82013-10-07 18:41:05 +02005813 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO ||
5814 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_ADHOC) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005815 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5816 &params.chandef);
5817 if (err < 0) {
5818 return err;
5819 } else if (err) {
5820 radar_detect_width = BIT(params.chandef.width);
5821 params.radar_required = true;
5822 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005823 }
5824
5825 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5826 params.chandef.chan,
5827 CHAN_MODE_SHARED,
5828 radar_detect_width);
5829 if (err)
5830 return err;
5831
5832 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5833 params.block_tx = true;
5834
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005835 wdev_lock(wdev);
5836 err = rdev_channel_switch(rdev, dev, &params);
5837 wdev_unlock(wdev);
5838
5839 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005840}
5841
Johannes Berg9720bb32011-06-21 09:45:33 +02005842static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5843 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005844 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005845 struct wireless_dev *wdev,
5846 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005847{
Johannes Berg48ab9052009-07-10 18:42:31 +02005848 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005849 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005850 void *hdr;
5851 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005852 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005853
5854 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005855
Eric W. Biederman15e47302012-09-07 20:12:54 +00005856 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005857 NL80211_CMD_NEW_SCAN_RESULTS);
5858 if (!hdr)
5859 return -1;
5860
Johannes Berg9720bb32011-06-21 09:45:33 +02005861 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5862
Johannes Berg97990a02013-04-19 01:02:55 +02005863 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5864 goto nla_put_failure;
5865 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005866 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5867 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005868 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5869 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005870
5871 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5872 if (!bss)
5873 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005874 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005875 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005876 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005877
5878 rcu_read_lock();
5879 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005880 if (ies) {
5881 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5882 goto fail_unlock_rcu;
5883 tsf = true;
5884 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5885 ies->len, ies->data))
5886 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005887 }
5888 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005889 if (ies) {
5890 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5891 goto fail_unlock_rcu;
5892 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5893 ies->len, ies->data))
5894 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005895 }
5896 rcu_read_unlock();
5897
David S. Miller9360ffd2012-03-29 04:41:26 -04005898 if (res->beacon_interval &&
5899 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5900 goto nla_put_failure;
5901 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5902 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02005903 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005904 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5905 jiffies_to_msecs(jiffies - intbss->ts)))
5906 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005907
Johannes Berg77965c92009-02-18 18:45:06 +01005908 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005909 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005910 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5911 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005912 break;
5913 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005914 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5915 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005916 break;
5917 default:
5918 break;
5919 }
5920
Johannes Berg48ab9052009-07-10 18:42:31 +02005921 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005922 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005923 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005924 if (intbss == wdev->current_bss &&
5925 nla_put_u32(msg, NL80211_BSS_STATUS,
5926 NL80211_BSS_STATUS_ASSOCIATED))
5927 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005928 break;
5929 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005930 if (intbss == wdev->current_bss &&
5931 nla_put_u32(msg, NL80211_BSS_STATUS,
5932 NL80211_BSS_STATUS_IBSS_JOINED))
5933 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005934 break;
5935 default:
5936 break;
5937 }
5938
Johannes Berg2a519312009-02-10 21:25:55 +01005939 nla_nest_end(msg, bss);
5940
5941 return genlmsg_end(msg, hdr);
5942
Johannes Berg8cef2c92013-02-05 16:54:31 +01005943 fail_unlock_rcu:
5944 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005945 nla_put_failure:
5946 genlmsg_cancel(msg, hdr);
5947 return -EMSGSIZE;
5948}
5949
Johannes Berg97990a02013-04-19 01:02:55 +02005950static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005951{
Johannes Berg48ab9052009-07-10 18:42:31 +02005952 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005953 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005954 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005955 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005956 int err;
5957
Johannes Berg97990a02013-04-19 01:02:55 +02005958 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005959 if (err)
5960 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005961
Johannes Berg48ab9052009-07-10 18:42:31 +02005962 wdev_lock(wdev);
5963 spin_lock_bh(&rdev->bss_lock);
5964 cfg80211_bss_expire(rdev);
5965
Johannes Berg9720bb32011-06-21 09:45:33 +02005966 cb->seq = rdev->bss_generation;
5967
Johannes Berg48ab9052009-07-10 18:42:31 +02005968 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005969 if (++idx <= start)
5970 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005971 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005972 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005973 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005974 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005975 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005976 }
5977 }
5978
Johannes Berg48ab9052009-07-10 18:42:31 +02005979 spin_unlock_bh(&rdev->bss_lock);
5980 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005981
Johannes Berg97990a02013-04-19 01:02:55 +02005982 cb->args[2] = idx;
5983 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005984
Johannes Berg67748892010-10-04 21:14:06 +02005985 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005986}
5987
Eric W. Biederman15e47302012-09-07 20:12:54 +00005988static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005989 int flags, struct net_device *dev,
5990 struct survey_info *survey)
5991{
5992 void *hdr;
5993 struct nlattr *infoattr;
5994
Eric W. Biederman15e47302012-09-07 20:12:54 +00005995 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005996 NL80211_CMD_NEW_SURVEY_RESULTS);
5997 if (!hdr)
5998 return -ENOMEM;
5999
David S. Miller9360ffd2012-03-29 04:41:26 -04006000 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6001 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006002
6003 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6004 if (!infoattr)
6005 goto nla_put_failure;
6006
David S. Miller9360ffd2012-03-29 04:41:26 -04006007 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6008 survey->channel->center_freq))
6009 goto nla_put_failure;
6010
6011 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6012 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6013 goto nla_put_failure;
6014 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6015 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6016 goto nla_put_failure;
6017 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6018 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6019 survey->channel_time))
6020 goto nla_put_failure;
6021 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6022 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6023 survey->channel_time_busy))
6024 goto nla_put_failure;
6025 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6026 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6027 survey->channel_time_ext_busy))
6028 goto nla_put_failure;
6029 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6030 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6031 survey->channel_time_rx))
6032 goto nla_put_failure;
6033 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6034 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6035 survey->channel_time_tx))
6036 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006037
6038 nla_nest_end(msg, infoattr);
6039
6040 return genlmsg_end(msg, hdr);
6041
6042 nla_put_failure:
6043 genlmsg_cancel(msg, hdr);
6044 return -EMSGSIZE;
6045}
6046
6047static int nl80211_dump_survey(struct sk_buff *skb,
6048 struct netlink_callback *cb)
6049{
6050 struct survey_info survey;
6051 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006052 struct wireless_dev *wdev;
6053 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006054 int res;
6055
Johannes Berg97990a02013-04-19 01:02:55 +02006056 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006057 if (res)
6058 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006059
Johannes Berg97990a02013-04-19 01:02:55 +02006060 if (!wdev->netdev) {
6061 res = -EINVAL;
6062 goto out_err;
6063 }
6064
Holger Schurig61fa7132009-11-11 12:25:40 +01006065 if (!dev->ops->dump_survey) {
6066 res = -EOPNOTSUPP;
6067 goto out_err;
6068 }
6069
6070 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006071 struct ieee80211_channel *chan;
6072
Johannes Berg97990a02013-04-19 01:02:55 +02006073 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006074 if (res == -ENOENT)
6075 break;
6076 if (res)
6077 goto out_err;
6078
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006079 /* Survey without a channel doesn't make sense */
6080 if (!survey.channel) {
6081 res = -EINVAL;
6082 goto out;
6083 }
6084
6085 chan = ieee80211_get_channel(&dev->wiphy,
6086 survey.channel->center_freq);
6087 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6088 survey_idx++;
6089 continue;
6090 }
6091
Holger Schurig61fa7132009-11-11 12:25:40 +01006092 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006093 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006094 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006095 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006096 goto out;
6097 survey_idx++;
6098 }
6099
6100 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006101 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006102 res = skb->len;
6103 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006104 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006105 return res;
6106}
6107
Samuel Ortizb23aa672009-07-01 21:26:54 +02006108static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6109{
6110 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6111 NL80211_WPA_VERSION_2));
6112}
6113
Jouni Malinen636a5d32009-03-19 13:39:22 +02006114static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6115{
Johannes Berg4c476992010-10-04 21:36:35 +02006116 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6117 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006118 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006119 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6120 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006121 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006122 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006123 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006124
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006125 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6126 return -EINVAL;
6127
6128 if (!info->attrs[NL80211_ATTR_MAC])
6129 return -EINVAL;
6130
Jouni Malinen17780922009-03-27 20:52:47 +02006131 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6132 return -EINVAL;
6133
Johannes Berg19957bb2009-07-02 17:20:43 +02006134 if (!info->attrs[NL80211_ATTR_SSID])
6135 return -EINVAL;
6136
6137 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6138 return -EINVAL;
6139
Johannes Bergfffd0932009-07-08 14:22:54 +02006140 err = nl80211_parse_key(info, &key);
6141 if (err)
6142 return err;
6143
6144 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006145 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6146 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006147 if (!key.p.key || !key.p.key_len)
6148 return -EINVAL;
6149 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6150 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6151 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6152 key.p.key_len != WLAN_KEY_LEN_WEP104))
6153 return -EINVAL;
6154 if (key.idx > 4)
6155 return -EINVAL;
6156 } else {
6157 key.p.key_len = 0;
6158 key.p.key = NULL;
6159 }
6160
Johannes Bergafea0b72010-08-10 09:46:42 +02006161 if (key.idx >= 0) {
6162 int i;
6163 bool ok = false;
6164 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6165 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6166 ok = true;
6167 break;
6168 }
6169 }
Johannes Berg4c476992010-10-04 21:36:35 +02006170 if (!ok)
6171 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006172 }
6173
Johannes Berg4c476992010-10-04 21:36:35 +02006174 if (!rdev->ops->auth)
6175 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006176
Johannes Berg074ac8d2010-09-16 14:58:22 +02006177 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006178 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6179 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006180
Johannes Berg19957bb2009-07-02 17:20:43 +02006181 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02006182 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02006183 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006184 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6185 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006186
Johannes Berg19957bb2009-07-02 17:20:43 +02006187 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6188 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6189
6190 if (info->attrs[NL80211_ATTR_IE]) {
6191 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6192 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6193 }
6194
6195 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006196 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006197 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006198
Jouni Malinene39e5b52012-09-30 19:29:39 +03006199 if (auth_type == NL80211_AUTHTYPE_SAE &&
6200 !info->attrs[NL80211_ATTR_SAE_DATA])
6201 return -EINVAL;
6202
6203 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6204 if (auth_type != NL80211_AUTHTYPE_SAE)
6205 return -EINVAL;
6206 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6207 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6208 /* need to include at least Auth Transaction and Status Code */
6209 if (sae_data_len < 4)
6210 return -EINVAL;
6211 }
6212
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006213 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6214
Johannes Berg95de8172012-01-20 13:55:25 +01006215 /*
6216 * Since we no longer track auth state, ignore
6217 * requests to only change local state.
6218 */
6219 if (local_state_change)
6220 return 0;
6221
Johannes Berg91bf9b22013-05-15 17:44:01 +02006222 wdev_lock(dev->ieee80211_ptr);
6223 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6224 ssid, ssid_len, ie, ie_len,
6225 key.p.key, key.p.key_len, key.idx,
6226 sae_data, sae_data_len);
6227 wdev_unlock(dev->ieee80211_ptr);
6228 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006229}
6230
Johannes Bergc0692b82010-08-27 14:26:53 +03006231static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6232 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006233 struct cfg80211_crypto_settings *settings,
6234 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006235{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006236 memset(settings, 0, sizeof(*settings));
6237
Samuel Ortizb23aa672009-07-01 21:26:54 +02006238 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6239
Johannes Bergc0692b82010-08-27 14:26:53 +03006240 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6241 u16 proto;
6242 proto = nla_get_u16(
6243 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6244 settings->control_port_ethertype = cpu_to_be16(proto);
6245 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6246 proto != ETH_P_PAE)
6247 return -EINVAL;
6248 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6249 settings->control_port_no_encrypt = true;
6250 } else
6251 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6252
Samuel Ortizb23aa672009-07-01 21:26:54 +02006253 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6254 void *data;
6255 int len, i;
6256
6257 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6258 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6259 settings->n_ciphers_pairwise = len / sizeof(u32);
6260
6261 if (len % sizeof(u32))
6262 return -EINVAL;
6263
Johannes Berg3dc27d22009-07-02 21:36:37 +02006264 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006265 return -EINVAL;
6266
6267 memcpy(settings->ciphers_pairwise, data, len);
6268
6269 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006270 if (!cfg80211_supported_cipher_suite(
6271 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006272 settings->ciphers_pairwise[i]))
6273 return -EINVAL;
6274 }
6275
6276 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6277 settings->cipher_group =
6278 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006279 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6280 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006281 return -EINVAL;
6282 }
6283
6284 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6285 settings->wpa_versions =
6286 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6287 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6288 return -EINVAL;
6289 }
6290
6291 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6292 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006293 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006294
6295 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6296 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6297 settings->n_akm_suites = len / sizeof(u32);
6298
6299 if (len % sizeof(u32))
6300 return -EINVAL;
6301
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006302 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6303 return -EINVAL;
6304
Samuel Ortizb23aa672009-07-01 21:26:54 +02006305 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006306 }
6307
6308 return 0;
6309}
6310
Jouni Malinen636a5d32009-03-19 13:39:22 +02006311static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6312{
Johannes Berg4c476992010-10-04 21:36:35 +02006313 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6314 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006315 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006316 struct cfg80211_assoc_request req = {};
6317 const u8 *bssid, *ssid;
6318 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006319
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006320 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6321 return -EINVAL;
6322
6323 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006324 !info->attrs[NL80211_ATTR_SSID] ||
6325 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006326 return -EINVAL;
6327
Johannes Berg4c476992010-10-04 21:36:35 +02006328 if (!rdev->ops->assoc)
6329 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006330
Johannes Berg074ac8d2010-09-16 14:58:22 +02006331 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006332 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6333 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006334
Johannes Berg19957bb2009-07-02 17:20:43 +02006335 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006336
Johannes Berg19957bb2009-07-02 17:20:43 +02006337 chan = ieee80211_get_channel(&rdev->wiphy,
6338 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006339 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6340 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006341
Johannes Berg19957bb2009-07-02 17:20:43 +02006342 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6343 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006344
6345 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006346 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6347 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006348 }
6349
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006350 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006351 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006352 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006353 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006354 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006355 else if (mfp != NL80211_MFP_NO)
6356 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03006357 }
6358
Johannes Berg3e5d7642009-07-07 14:37:26 +02006359 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006360 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006361
Ben Greear7e7c8922011-11-18 11:31:59 -08006362 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006363 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006364
6365 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006366 memcpy(&req.ht_capa_mask,
6367 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6368 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006369
6370 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006371 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006372 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006373 memcpy(&req.ht_capa,
6374 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6375 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006376 }
6377
Johannes Bergee2aca32013-02-21 17:36:01 +01006378 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006379 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006380
6381 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006382 memcpy(&req.vht_capa_mask,
6383 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6384 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006385
6386 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006387 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006388 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006389 memcpy(&req.vht_capa,
6390 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6391 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006392 }
6393
Johannes Bergf62fab72013-02-21 20:09:09 +01006394 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006395 if (!err) {
6396 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006397 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6398 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006399 wdev_unlock(dev->ieee80211_ptr);
6400 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006401
Jouni Malinen636a5d32009-03-19 13:39:22 +02006402 return err;
6403}
6404
6405static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6406{
Johannes Berg4c476992010-10-04 21:36:35 +02006407 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6408 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006409 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006410 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006411 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006412 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006413
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006414 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6415 return -EINVAL;
6416
6417 if (!info->attrs[NL80211_ATTR_MAC])
6418 return -EINVAL;
6419
6420 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6421 return -EINVAL;
6422
Johannes Berg4c476992010-10-04 21:36:35 +02006423 if (!rdev->ops->deauth)
6424 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006425
Johannes Berg074ac8d2010-09-16 14:58:22 +02006426 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006427 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6428 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006429
Johannes Berg19957bb2009-07-02 17:20:43 +02006430 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006431
Johannes Berg19957bb2009-07-02 17:20:43 +02006432 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6433 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006434 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006435 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006436 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006437
6438 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006439 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6440 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006441 }
6442
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006443 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6444
Johannes Berg91bf9b22013-05-15 17:44:01 +02006445 wdev_lock(dev->ieee80211_ptr);
6446 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6447 local_state_change);
6448 wdev_unlock(dev->ieee80211_ptr);
6449 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006450}
6451
6452static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6453{
Johannes Berg4c476992010-10-04 21:36:35 +02006454 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6455 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006456 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006457 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006458 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006459 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006460
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006461 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6462 return -EINVAL;
6463
6464 if (!info->attrs[NL80211_ATTR_MAC])
6465 return -EINVAL;
6466
6467 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6468 return -EINVAL;
6469
Johannes Berg4c476992010-10-04 21:36:35 +02006470 if (!rdev->ops->disassoc)
6471 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006472
Johannes Berg074ac8d2010-09-16 14:58:22 +02006473 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006474 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6475 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006476
Johannes Berg19957bb2009-07-02 17:20:43 +02006477 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006478
Johannes Berg19957bb2009-07-02 17:20:43 +02006479 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6480 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006481 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006482 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006483 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006484
6485 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006486 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6487 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006488 }
6489
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006490 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6491
Johannes Berg91bf9b22013-05-15 17:44:01 +02006492 wdev_lock(dev->ieee80211_ptr);
6493 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6494 local_state_change);
6495 wdev_unlock(dev->ieee80211_ptr);
6496 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006497}
6498
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006499static bool
6500nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6501 int mcast_rate[IEEE80211_NUM_BANDS],
6502 int rateval)
6503{
6504 struct wiphy *wiphy = &rdev->wiphy;
6505 bool found = false;
6506 int band, i;
6507
6508 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6509 struct ieee80211_supported_band *sband;
6510
6511 sband = wiphy->bands[band];
6512 if (!sband)
6513 continue;
6514
6515 for (i = 0; i < sband->n_bitrates; i++) {
6516 if (sband->bitrates[i].bitrate == rateval) {
6517 mcast_rate[band] = i + 1;
6518 found = true;
6519 break;
6520 }
6521 }
6522 }
6523
6524 return found;
6525}
6526
Johannes Berg04a773a2009-04-19 21:24:32 +02006527static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6528{
Johannes Berg4c476992010-10-04 21:36:35 +02006529 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6530 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006531 struct cfg80211_ibss_params ibss;
6532 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006533 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006534 int err;
6535
Johannes Berg8e30bc52009-04-22 17:45:38 +02006536 memset(&ibss, 0, sizeof(ibss));
6537
Johannes Berg04a773a2009-04-19 21:24:32 +02006538 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6539 return -EINVAL;
6540
Johannes Berg683b6d32012-11-08 21:25:48 +01006541 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006542 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6543 return -EINVAL;
6544
Johannes Berg8e30bc52009-04-22 17:45:38 +02006545 ibss.beacon_interval = 100;
6546
6547 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6548 ibss.beacon_interval =
6549 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6550 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6551 return -EINVAL;
6552 }
6553
Johannes Berg4c476992010-10-04 21:36:35 +02006554 if (!rdev->ops->join_ibss)
6555 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006556
Johannes Berg4c476992010-10-04 21:36:35 +02006557 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6558 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006559
Johannes Berg79c97e92009-07-07 03:56:12 +02006560 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006561
Johannes Berg39193492011-09-16 13:45:25 +02006562 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006563 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006564
6565 if (!is_valid_ether_addr(ibss.bssid))
6566 return -EINVAL;
6567 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006568 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6569 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6570
6571 if (info->attrs[NL80211_ATTR_IE]) {
6572 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6573 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6574 }
6575
Johannes Berg683b6d32012-11-08 21:25:48 +01006576 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6577 if (err)
6578 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006579
Johannes Berg683b6d32012-11-08 21:25:48 +01006580 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006581 return -EINVAL;
6582
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006583 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006584 case NL80211_CHAN_WIDTH_5:
6585 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006586 case NL80211_CHAN_WIDTH_20_NOHT:
6587 break;
6588 case NL80211_CHAN_WIDTH_20:
6589 case NL80211_CHAN_WIDTH_40:
6590 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6591 break;
6592 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006593 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006594 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006595
Johannes Berg04a773a2009-04-19 21:24:32 +02006596 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006597 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006598
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006599 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6600 u8 *rates =
6601 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6602 int n_rates =
6603 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6604 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006605 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006606
Johannes Berg34850ab2011-07-18 18:08:35 +02006607 err = ieee80211_get_ratemask(sband, rates, n_rates,
6608 &ibss.basic_rates);
6609 if (err)
6610 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006611 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006612
Simon Wunderlich803768f2013-06-28 10:39:58 +02006613 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6614 memcpy(&ibss.ht_capa_mask,
6615 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6616 sizeof(ibss.ht_capa_mask));
6617
6618 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6619 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6620 return -EINVAL;
6621 memcpy(&ibss.ht_capa,
6622 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6623 sizeof(ibss.ht_capa));
6624 }
6625
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006626 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6627 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6628 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6629 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006630
Johannes Berg4c476992010-10-04 21:36:35 +02006631 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306632 bool no_ht = false;
6633
Johannes Berg4c476992010-10-04 21:36:35 +02006634 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306635 info->attrs[NL80211_ATTR_KEYS],
6636 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006637 if (IS_ERR(connkeys))
6638 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306639
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006640 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6641 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306642 kfree(connkeys);
6643 return -EINVAL;
6644 }
Johannes Berg4c476992010-10-04 21:36:35 +02006645 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006646
Antonio Quartulli267335d2012-01-31 20:25:47 +01006647 ibss.control_port =
6648 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6649
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006650 ibss.userspace_handles_dfs =
6651 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6652
Johannes Berg4c476992010-10-04 21:36:35 +02006653 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006654 if (err)
6655 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006656 return err;
6657}
6658
6659static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6660{
Johannes Berg4c476992010-10-04 21:36:35 +02006661 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6662 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006663
Johannes Berg4c476992010-10-04 21:36:35 +02006664 if (!rdev->ops->leave_ibss)
6665 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006666
Johannes Berg4c476992010-10-04 21:36:35 +02006667 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6668 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006669
Johannes Berg4c476992010-10-04 21:36:35 +02006670 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006671}
6672
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006673static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6674{
6675 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6676 struct net_device *dev = info->user_ptr[1];
6677 int mcast_rate[IEEE80211_NUM_BANDS];
6678 u32 nla_rate;
6679 int err;
6680
6681 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6682 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6683 return -EOPNOTSUPP;
6684
6685 if (!rdev->ops->set_mcast_rate)
6686 return -EOPNOTSUPP;
6687
6688 memset(mcast_rate, 0, sizeof(mcast_rate));
6689
6690 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6691 return -EINVAL;
6692
6693 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6694 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6695 return -EINVAL;
6696
6697 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6698
6699 return err;
6700}
6701
Johannes Bergad7e7182013-11-13 13:37:47 +01006702static struct sk_buff *
6703__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6704 int approxlen, u32 portid, u32 seq,
6705 enum nl80211_commands cmd,
6706 enum nl80211_attrs attr, gfp_t gfp)
6707{
6708 struct sk_buff *skb;
6709 void *hdr;
6710 struct nlattr *data;
6711
6712 skb = nlmsg_new(approxlen + 100, gfp);
6713 if (!skb)
6714 return NULL;
6715
6716 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6717 if (!hdr) {
6718 kfree_skb(skb);
6719 return NULL;
6720 }
6721
6722 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6723 goto nla_put_failure;
6724 data = nla_nest_start(skb, attr);
6725
6726 ((void **)skb->cb)[0] = rdev;
6727 ((void **)skb->cb)[1] = hdr;
6728 ((void **)skb->cb)[2] = data;
6729
6730 return skb;
6731
6732 nla_put_failure:
6733 kfree_skb(skb);
6734 return NULL;
6735}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006736
Johannes Bergaff89a92009-07-01 21:26:51 +02006737#ifdef CONFIG_NL80211_TESTMODE
6738static struct genl_multicast_group nl80211_testmode_mcgrp = {
6739 .name = "testmode",
6740};
6741
6742static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6743{
Johannes Berg4c476992010-10-04 21:36:35 +02006744 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006745 struct wireless_dev *wdev =
6746 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006747 int err;
6748
David Spinadelfc73f112013-07-31 18:04:15 +03006749 if (!rdev->ops->testmode_cmd)
6750 return -EOPNOTSUPP;
6751
6752 if (IS_ERR(wdev)) {
6753 err = PTR_ERR(wdev);
6754 if (err != -EINVAL)
6755 return err;
6756 wdev = NULL;
6757 } else if (wdev->wiphy != &rdev->wiphy) {
6758 return -EINVAL;
6759 }
6760
Johannes Bergaff89a92009-07-01 21:26:51 +02006761 if (!info->attrs[NL80211_ATTR_TESTDATA])
6762 return -EINVAL;
6763
Johannes Bergad7e7182013-11-13 13:37:47 +01006764 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006765 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006766 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6767 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006768 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006769
Johannes Bergaff89a92009-07-01 21:26:51 +02006770 return err;
6771}
6772
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006773static int nl80211_testmode_dump(struct sk_buff *skb,
6774 struct netlink_callback *cb)
6775{
Johannes Berg00918d32011-12-13 17:22:05 +01006776 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006777 int err;
6778 long phy_idx;
6779 void *data = NULL;
6780 int data_len = 0;
6781
Johannes Berg5fe231e2013-05-08 21:45:15 +02006782 rtnl_lock();
6783
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006784 if (cb->args[0]) {
6785 /*
6786 * 0 is a valid index, but not valid for args[0],
6787 * so we need to offset by 1.
6788 */
6789 phy_idx = cb->args[0] - 1;
6790 } else {
6791 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6792 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6793 nl80211_policy);
6794 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006795 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006796
Johannes Berg2bd7e352012-06-15 14:23:16 +02006797 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6798 nl80211_fam.attrbuf);
6799 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006800 err = PTR_ERR(rdev);
6801 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006802 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006803 phy_idx = rdev->wiphy_idx;
6804 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02006805
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006806 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6807 cb->args[1] =
6808 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6809 }
6810
6811 if (cb->args[1]) {
6812 data = nla_data((void *)cb->args[1]);
6813 data_len = nla_len((void *)cb->args[1]);
6814 }
6815
Johannes Berg00918d32011-12-13 17:22:05 +01006816 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6817 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006818 err = -ENOENT;
6819 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006820 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006821
Johannes Berg00918d32011-12-13 17:22:05 +01006822 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006823 err = -EOPNOTSUPP;
6824 goto out_err;
6825 }
6826
6827 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006828 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006829 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6830 NL80211_CMD_TESTMODE);
6831 struct nlattr *tmdata;
6832
Dan Carpentercb35fba2013-08-14 14:50:01 +03006833 if (!hdr)
6834 break;
6835
David S. Miller9360ffd2012-03-29 04:41:26 -04006836 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006837 genlmsg_cancel(skb, hdr);
6838 break;
6839 }
6840
6841 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6842 if (!tmdata) {
6843 genlmsg_cancel(skb, hdr);
6844 break;
6845 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006846 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006847 nla_nest_end(skb, tmdata);
6848
6849 if (err == -ENOBUFS || err == -ENOENT) {
6850 genlmsg_cancel(skb, hdr);
6851 break;
6852 } else if (err) {
6853 genlmsg_cancel(skb, hdr);
6854 goto out_err;
6855 }
6856
6857 genlmsg_end(skb, hdr);
6858 }
6859
6860 err = skb->len;
6861 /* see above */
6862 cb->args[0] = phy_idx + 1;
6863 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02006864 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006865 return err;
6866}
6867
Johannes Bergaff89a92009-07-01 21:26:51 +02006868struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6869 int approxlen, gfp_t gfp)
6870{
6871 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6872
Johannes Bergad7e7182013-11-13 13:37:47 +01006873 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6874 NL80211_CMD_TESTMODE,
6875 NL80211_ATTR_TESTDATA, gfp);
Johannes Bergaff89a92009-07-01 21:26:51 +02006876}
6877EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6878
6879void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6880{
Michal Kaziora0ec5702013-06-25 09:17:17 +02006881 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006882 void *hdr = ((void **)skb->cb)[1];
6883 struct nlattr *data = ((void **)skb->cb)[2];
6884
6885 nla_nest_end(skb, data);
6886 genlmsg_end(skb, hdr);
Michal Kaziora0ec5702013-06-25 09:17:17 +02006887 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), skb, 0,
6888 nl80211_testmode_mcgrp.id, gfp);
Johannes Bergaff89a92009-07-01 21:26:51 +02006889}
6890EXPORT_SYMBOL(cfg80211_testmode_event);
6891#endif
6892
Samuel Ortizb23aa672009-07-01 21:26:54 +02006893static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6894{
Johannes Berg4c476992010-10-04 21:36:35 +02006895 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6896 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006897 struct cfg80211_connect_params connect;
6898 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006899 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006900 int err;
6901
6902 memset(&connect, 0, sizeof(connect));
6903
6904 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6905 return -EINVAL;
6906
6907 if (!info->attrs[NL80211_ATTR_SSID] ||
6908 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6909 return -EINVAL;
6910
6911 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6912 connect.auth_type =
6913 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006914 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6915 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006916 return -EINVAL;
6917 } else
6918 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6919
6920 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6921
Johannes Bergc0692b82010-08-27 14:26:53 +03006922 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006923 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006924 if (err)
6925 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006926
Johannes Berg074ac8d2010-09-16 14:58:22 +02006927 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006928 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6929 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006930
Johannes Berg79c97e92009-07-07 03:56:12 +02006931 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006932
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306933 connect.bg_scan_period = -1;
6934 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6935 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6936 connect.bg_scan_period =
6937 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6938 }
6939
Samuel Ortizb23aa672009-07-01 21:26:54 +02006940 if (info->attrs[NL80211_ATTR_MAC])
6941 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6942 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6943 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6944
6945 if (info->attrs[NL80211_ATTR_IE]) {
6946 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6947 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6948 }
6949
Jouni Malinencee00a92013-01-15 17:15:57 +02006950 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6951 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6952 if (connect.mfp != NL80211_MFP_REQUIRED &&
6953 connect.mfp != NL80211_MFP_NO)
6954 return -EINVAL;
6955 } else {
6956 connect.mfp = NL80211_MFP_NO;
6957 }
6958
Samuel Ortizb23aa672009-07-01 21:26:54 +02006959 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6960 connect.channel =
6961 ieee80211_get_channel(wiphy,
6962 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6963 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006964 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6965 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006966 }
6967
Johannes Bergfffd0932009-07-08 14:22:54 +02006968 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6969 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306970 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006971 if (IS_ERR(connkeys))
6972 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006973 }
6974
Ben Greear7e7c8922011-11-18 11:31:59 -08006975 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6976 connect.flags |= ASSOC_REQ_DISABLE_HT;
6977
6978 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6979 memcpy(&connect.ht_capa_mask,
6980 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6981 sizeof(connect.ht_capa_mask));
6982
6983 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006984 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6985 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006986 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006987 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006988 memcpy(&connect.ht_capa,
6989 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6990 sizeof(connect.ht_capa));
6991 }
6992
Johannes Bergee2aca32013-02-21 17:36:01 +01006993 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6994 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6995
6996 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6997 memcpy(&connect.vht_capa_mask,
6998 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6999 sizeof(connect.vht_capa_mask));
7000
7001 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7002 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7003 kfree(connkeys);
7004 return -EINVAL;
7005 }
7006 memcpy(&connect.vht_capa,
7007 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7008 sizeof(connect.vht_capa));
7009 }
7010
Johannes Berg83739b02013-05-15 17:44:01 +02007011 wdev_lock(dev->ieee80211_ptr);
7012 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7013 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007014 if (err)
7015 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007016 return err;
7017}
7018
7019static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7020{
Johannes Berg4c476992010-10-04 21:36:35 +02007021 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7022 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007023 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007024 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007025
7026 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7027 reason = WLAN_REASON_DEAUTH_LEAVING;
7028 else
7029 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7030
7031 if (reason == 0)
7032 return -EINVAL;
7033
Johannes Berg074ac8d2010-09-16 14:58:22 +02007034 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007035 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7036 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007037
Johannes Berg83739b02013-05-15 17:44:01 +02007038 wdev_lock(dev->ieee80211_ptr);
7039 ret = cfg80211_disconnect(rdev, dev, reason, true);
7040 wdev_unlock(dev->ieee80211_ptr);
7041 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007042}
7043
Johannes Berg463d0182009-07-14 00:33:35 +02007044static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7045{
Johannes Berg4c476992010-10-04 21:36:35 +02007046 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007047 struct net *net;
7048 int err;
7049 u32 pid;
7050
7051 if (!info->attrs[NL80211_ATTR_PID])
7052 return -EINVAL;
7053
7054 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7055
Johannes Berg463d0182009-07-14 00:33:35 +02007056 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007057 if (IS_ERR(net))
7058 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007059
7060 err = 0;
7061
7062 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007063 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7064 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007065
Johannes Berg463d0182009-07-14 00:33:35 +02007066 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007067 return err;
7068}
7069
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007070static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7071{
Johannes Berg4c476992010-10-04 21:36:35 +02007072 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007073 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7074 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007075 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007076 struct cfg80211_pmksa pmksa;
7077
7078 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7079
7080 if (!info->attrs[NL80211_ATTR_MAC])
7081 return -EINVAL;
7082
7083 if (!info->attrs[NL80211_ATTR_PMKID])
7084 return -EINVAL;
7085
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007086 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7087 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7088
Johannes Berg074ac8d2010-09-16 14:58:22 +02007089 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007090 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7091 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007092
7093 switch (info->genlhdr->cmd) {
7094 case NL80211_CMD_SET_PMKSA:
7095 rdev_ops = rdev->ops->set_pmksa;
7096 break;
7097 case NL80211_CMD_DEL_PMKSA:
7098 rdev_ops = rdev->ops->del_pmksa;
7099 break;
7100 default:
7101 WARN_ON(1);
7102 break;
7103 }
7104
Johannes Berg4c476992010-10-04 21:36:35 +02007105 if (!rdev_ops)
7106 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007107
Johannes Berg4c476992010-10-04 21:36:35 +02007108 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007109}
7110
7111static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7112{
Johannes Berg4c476992010-10-04 21:36:35 +02007113 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7114 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007115
Johannes Berg074ac8d2010-09-16 14:58:22 +02007116 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007117 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7118 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007119
Johannes Berg4c476992010-10-04 21:36:35 +02007120 if (!rdev->ops->flush_pmksa)
7121 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007122
Hila Gonene35e4d22012-06-27 17:19:42 +03007123 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007124}
7125
Arik Nemtsov109086c2011-09-28 14:12:50 +03007126static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7127{
7128 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7129 struct net_device *dev = info->user_ptr[1];
7130 u8 action_code, dialog_token;
7131 u16 status_code;
7132 u8 *peer;
7133
7134 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7135 !rdev->ops->tdls_mgmt)
7136 return -EOPNOTSUPP;
7137
7138 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7139 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7140 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7141 !info->attrs[NL80211_ATTR_IE] ||
7142 !info->attrs[NL80211_ATTR_MAC])
7143 return -EINVAL;
7144
7145 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7146 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7147 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7148 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
7149
Hila Gonene35e4d22012-06-27 17:19:42 +03007150 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
7151 dialog_token, status_code,
7152 nla_data(info->attrs[NL80211_ATTR_IE]),
7153 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007154}
7155
7156static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7157{
7158 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7159 struct net_device *dev = info->user_ptr[1];
7160 enum nl80211_tdls_operation operation;
7161 u8 *peer;
7162
7163 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7164 !rdev->ops->tdls_oper)
7165 return -EOPNOTSUPP;
7166
7167 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7168 !info->attrs[NL80211_ATTR_MAC])
7169 return -EINVAL;
7170
7171 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7172 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7173
Hila Gonene35e4d22012-06-27 17:19:42 +03007174 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007175}
7176
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007177static int nl80211_remain_on_channel(struct sk_buff *skb,
7178 struct genl_info *info)
7179{
Johannes Berg4c476992010-10-04 21:36:35 +02007180 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007181 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007182 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007183 struct sk_buff *msg;
7184 void *hdr;
7185 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007186 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007187 int err;
7188
7189 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7190 !info->attrs[NL80211_ATTR_DURATION])
7191 return -EINVAL;
7192
7193 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7194
Johannes Berg7c4ef712011-11-18 15:33:48 +01007195 if (!rdev->ops->remain_on_channel ||
7196 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007197 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007198
Johannes Bergebf348f2012-06-01 12:50:54 +02007199 /*
7200 * We should be on that channel for at least a minimum amount of
7201 * time (10ms) but no longer than the driver supports.
7202 */
7203 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7204 duration > rdev->wiphy.max_remain_on_channel_duration)
7205 return -EINVAL;
7206
Johannes Berg683b6d32012-11-08 21:25:48 +01007207 err = nl80211_parse_chandef(rdev, info, &chandef);
7208 if (err)
7209 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007210
7211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007212 if (!msg)
7213 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007214
Eric W. Biederman15e47302012-09-07 20:12:54 +00007215 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007216 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007217 if (!hdr) {
7218 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007219 goto free_msg;
7220 }
7221
Johannes Berg683b6d32012-11-08 21:25:48 +01007222 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7223 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007224
7225 if (err)
7226 goto free_msg;
7227
David S. Miller9360ffd2012-03-29 04:41:26 -04007228 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7229 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007230
7231 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007232
7233 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007234
7235 nla_put_failure:
7236 err = -ENOBUFS;
7237 free_msg:
7238 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007239 return err;
7240}
7241
7242static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7243 struct genl_info *info)
7244{
Johannes Berg4c476992010-10-04 21:36:35 +02007245 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007246 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007247 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007248
7249 if (!info->attrs[NL80211_ATTR_COOKIE])
7250 return -EINVAL;
7251
Johannes Berg4c476992010-10-04 21:36:35 +02007252 if (!rdev->ops->cancel_remain_on_channel)
7253 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007254
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007255 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7256
Hila Gonene35e4d22012-06-27 17:19:42 +03007257 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007258}
7259
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007260static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7261 u8 *rates, u8 rates_len)
7262{
7263 u8 i;
7264 u32 mask = 0;
7265
7266 for (i = 0; i < rates_len; i++) {
7267 int rate = (rates[i] & 0x7f) * 5;
7268 int ridx;
7269 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7270 struct ieee80211_rate *srate =
7271 &sband->bitrates[ridx];
7272 if (rate == srate->bitrate) {
7273 mask |= 1 << ridx;
7274 break;
7275 }
7276 }
7277 if (ridx == sband->n_bitrates)
7278 return 0; /* rate not found */
7279 }
7280
7281 return mask;
7282}
7283
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007284static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7285 u8 *rates, u8 rates_len,
7286 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7287{
7288 u8 i;
7289
7290 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7291
7292 for (i = 0; i < rates_len; i++) {
7293 int ridx, rbit;
7294
7295 ridx = rates[i] / 8;
7296 rbit = BIT(rates[i] % 8);
7297
7298 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007299 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007300 return false;
7301
7302 /* check availability */
7303 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7304 mcs[ridx] |= rbit;
7305 else
7306 return false;
7307 }
7308
7309 return true;
7310}
7311
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007312static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007313 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7314 .len = NL80211_MAX_SUPP_RATES },
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007315 [NL80211_TXRATE_HT] = { .type = NLA_BINARY,
7316 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007317};
7318
7319static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7320 struct genl_info *info)
7321{
7322 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007323 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007324 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007325 int rem, i;
7326 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007327 struct nlattr *tx_rates;
7328 struct ieee80211_supported_band *sband;
7329
Johannes Berg4c476992010-10-04 21:36:35 +02007330 if (!rdev->ops->set_bitrate_mask)
7331 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007332
7333 memset(&mask, 0, sizeof(mask));
7334 /* Default to all rates enabled */
7335 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7336 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007337
7338 if (!sband)
7339 continue;
7340
7341 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007342 memcpy(mask.control[i].ht_mcs,
Janusz Dziedzic78693032013-12-03 09:50:44 +01007343 sband->ht_cap.mcs.rx_mask,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007344 sizeof(mask.control[i].ht_mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007345 }
7346
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007347 /* if no rates are given set it back to the defaults */
7348 if (!info->attrs[NL80211_ATTR_TX_RATES])
7349 goto out;
7350
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007351 /*
7352 * The nested attribute uses enum nl80211_band as the index. This maps
7353 * directly to the enum ieee80211_band values used in cfg80211.
7354 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007355 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007356 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7357 {
7358 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007359 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7360 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007361 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007362 if (sband == NULL)
7363 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007364 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7365 nla_len(tx_rates), nl80211_txattr_policy);
7366 if (tb[NL80211_TXRATE_LEGACY]) {
7367 mask.control[band].legacy = rateset_to_mask(
7368 sband,
7369 nla_data(tb[NL80211_TXRATE_LEGACY]),
7370 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307371 if ((mask.control[band].legacy == 0) &&
7372 nla_len(tb[NL80211_TXRATE_LEGACY]))
7373 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007374 }
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007375 if (tb[NL80211_TXRATE_HT]) {
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007376 if (!ht_rateset_to_mask(
7377 sband,
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007378 nla_data(tb[NL80211_TXRATE_HT]),
7379 nla_len(tb[NL80211_TXRATE_HT]),
7380 mask.control[band].ht_mcs))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007381 return -EINVAL;
7382 }
7383
7384 if (mask.control[band].legacy == 0) {
7385 /* don't allow empty legacy rates if HT
7386 * is not even supported. */
7387 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7388 return -EINVAL;
7389
7390 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
Janusz Dziedzicd1e33e62013-12-05 10:02:15 +01007391 if (mask.control[band].ht_mcs[i])
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007392 break;
7393
7394 /* legacy and mcs rates may not be both empty */
7395 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02007396 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007397 }
7398 }
7399
Janusz Dziedzicb9243ab2013-12-05 10:02:14 +01007400out:
Hila Gonene35e4d22012-06-27 17:19:42 +03007401 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007402}
7403
Johannes Berg2e161f72010-08-12 15:38:38 +02007404static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007405{
Johannes Berg4c476992010-10-04 21:36:35 +02007406 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007407 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007408 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007409
7410 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7411 return -EINVAL;
7412
Johannes Berg2e161f72010-08-12 15:38:38 +02007413 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7414 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007415
Johannes Berg71bbc992012-06-15 15:30:18 +02007416 switch (wdev->iftype) {
7417 case NL80211_IFTYPE_STATION:
7418 case NL80211_IFTYPE_ADHOC:
7419 case NL80211_IFTYPE_P2P_CLIENT:
7420 case NL80211_IFTYPE_AP:
7421 case NL80211_IFTYPE_AP_VLAN:
7422 case NL80211_IFTYPE_MESH_POINT:
7423 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007424 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007425 break;
7426 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007427 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007428 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007429
7430 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007431 if (!rdev->ops->mgmt_tx)
7432 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007433
Eric W. Biederman15e47302012-09-07 20:12:54 +00007434 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007435 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7436 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007437}
7438
Johannes Berg2e161f72010-08-12 15:38:38 +02007439static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007440{
Johannes Berg4c476992010-10-04 21:36:35 +02007441 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007442 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007443 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007444 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007445 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007446 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007447 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007448 struct cfg80211_mgmt_tx_params params = {
7449 .dont_wait_for_ack =
7450 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7451 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007452
Johannes Berg683b6d32012-11-08 21:25:48 +01007453 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007454 return -EINVAL;
7455
Johannes Berg4c476992010-10-04 21:36:35 +02007456 if (!rdev->ops->mgmt_tx)
7457 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007458
Johannes Berg71bbc992012-06-15 15:30:18 +02007459 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007460 case NL80211_IFTYPE_P2P_DEVICE:
7461 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7462 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007463 case NL80211_IFTYPE_STATION:
7464 case NL80211_IFTYPE_ADHOC:
7465 case NL80211_IFTYPE_P2P_CLIENT:
7466 case NL80211_IFTYPE_AP:
7467 case NL80211_IFTYPE_AP_VLAN:
7468 case NL80211_IFTYPE_MESH_POINT:
7469 case NL80211_IFTYPE_P2P_GO:
7470 break;
7471 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007472 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007473 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007474
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007475 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007476 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007477 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007478 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007479
7480 /*
7481 * We should wait on the channel for at least a minimum amount
7482 * of time (10ms) but no longer than the driver supports.
7483 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007484 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7485 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007486 return -EINVAL;
7487
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007488 }
7489
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007490 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007491
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007492 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007493 return -EINVAL;
7494
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007495 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307496
Antonio Quartulliea141b752013-06-11 14:20:03 +02007497 /* get the channel if any has been specified, otherwise pass NULL to
7498 * the driver. The latter will use the current one
7499 */
7500 chandef.chan = NULL;
7501 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7502 err = nl80211_parse_chandef(rdev, info, &chandef);
7503 if (err)
7504 return err;
7505 }
7506
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007507 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007508 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007509
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007510 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007511 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7512 if (!msg)
7513 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007514
Eric W. Biederman15e47302012-09-07 20:12:54 +00007515 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007516 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007517 if (!hdr) {
7518 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007519 goto free_msg;
7520 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007521 }
Johannes Berge247bd902011-11-04 11:18:21 +01007522
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007523 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7524 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7525 params.chan = chandef.chan;
7526 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007527 if (err)
7528 goto free_msg;
7529
Johannes Berge247bd902011-11-04 11:18:21 +01007530 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007531 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7532 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007533
Johannes Berge247bd902011-11-04 11:18:21 +01007534 genlmsg_end(msg, hdr);
7535 return genlmsg_reply(msg, info);
7536 }
7537
7538 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007539
7540 nla_put_failure:
7541 err = -ENOBUFS;
7542 free_msg:
7543 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007544 return err;
7545}
7546
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007547static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7548{
7549 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007550 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007551 u64 cookie;
7552
7553 if (!info->attrs[NL80211_ATTR_COOKIE])
7554 return -EINVAL;
7555
7556 if (!rdev->ops->mgmt_tx_cancel_wait)
7557 return -EOPNOTSUPP;
7558
Johannes Berg71bbc992012-06-15 15:30:18 +02007559 switch (wdev->iftype) {
7560 case NL80211_IFTYPE_STATION:
7561 case NL80211_IFTYPE_ADHOC:
7562 case NL80211_IFTYPE_P2P_CLIENT:
7563 case NL80211_IFTYPE_AP:
7564 case NL80211_IFTYPE_AP_VLAN:
7565 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007566 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007567 break;
7568 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007569 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007570 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007571
7572 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7573
Hila Gonene35e4d22012-06-27 17:19:42 +03007574 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007575}
7576
Kalle Valoffb9eb32010-02-17 17:58:10 +02007577static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7578{
Johannes Berg4c476992010-10-04 21:36:35 +02007579 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007580 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007581 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007582 u8 ps_state;
7583 bool state;
7584 int err;
7585
Johannes Berg4c476992010-10-04 21:36:35 +02007586 if (!info->attrs[NL80211_ATTR_PS_STATE])
7587 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007588
7589 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7590
Johannes Berg4c476992010-10-04 21:36:35 +02007591 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7592 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007593
7594 wdev = dev->ieee80211_ptr;
7595
Johannes Berg4c476992010-10-04 21:36:35 +02007596 if (!rdev->ops->set_power_mgmt)
7597 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007598
7599 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7600
7601 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007602 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007603
Hila Gonene35e4d22012-06-27 17:19:42 +03007604 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007605 if (!err)
7606 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007607 return err;
7608}
7609
7610static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7611{
Johannes Berg4c476992010-10-04 21:36:35 +02007612 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007613 enum nl80211_ps_state ps_state;
7614 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007615 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007616 struct sk_buff *msg;
7617 void *hdr;
7618 int err;
7619
Kalle Valoffb9eb32010-02-17 17:58:10 +02007620 wdev = dev->ieee80211_ptr;
7621
Johannes Berg4c476992010-10-04 21:36:35 +02007622 if (!rdev->ops->set_power_mgmt)
7623 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007624
7625 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007626 if (!msg)
7627 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007628
Eric W. Biederman15e47302012-09-07 20:12:54 +00007629 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007630 NL80211_CMD_GET_POWER_SAVE);
7631 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007632 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007633 goto free_msg;
7634 }
7635
7636 if (wdev->ps)
7637 ps_state = NL80211_PS_ENABLED;
7638 else
7639 ps_state = NL80211_PS_DISABLED;
7640
David S. Miller9360ffd2012-03-29 04:41:26 -04007641 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7642 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007643
7644 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007645 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007646
Johannes Berg4c476992010-10-04 21:36:35 +02007647 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007648 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007649 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007650 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007651 return err;
7652}
7653
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007654static struct nla_policy
7655nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7656 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7657 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7658 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007659 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7660 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7661 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007662};
7663
Thomas Pedersen84f10702012-07-12 16:17:33 -07007664static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007665 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007666{
7667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007668 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007669 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007670
Johannes Bergd9d8b012012-11-26 12:51:52 +01007671 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007672 return -EINVAL;
7673
Thomas Pedersen84f10702012-07-12 16:17:33 -07007674 if (!rdev->ops->set_cqm_txe_config)
7675 return -EOPNOTSUPP;
7676
7677 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7678 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7679 return -EOPNOTSUPP;
7680
Hila Gonene35e4d22012-06-27 17:19:42 +03007681 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007682}
7683
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007684static int nl80211_set_cqm_rssi(struct genl_info *info,
7685 s32 threshold, u32 hysteresis)
7686{
Johannes Berg4c476992010-10-04 21:36:35 +02007687 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007688 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007689 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007690
7691 if (threshold > 0)
7692 return -EINVAL;
7693
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007694 /* disabling - hysteresis should also be zero then */
7695 if (threshold == 0)
7696 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007697
Johannes Berg4c476992010-10-04 21:36:35 +02007698 if (!rdev->ops->set_cqm_rssi_config)
7699 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007700
Johannes Berg074ac8d2010-09-16 14:58:22 +02007701 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007702 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7703 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007704
Hila Gonene35e4d22012-06-27 17:19:42 +03007705 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007706}
7707
7708static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7709{
7710 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7711 struct nlattr *cqm;
7712 int err;
7713
7714 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007715 if (!cqm)
7716 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007717
7718 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7719 nl80211_attr_cqm_policy);
7720 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007721 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007722
7723 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7724 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007725 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7726 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007727
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007728 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
7729 }
7730
7731 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7732 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7733 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7734 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7735 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7736 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7737
7738 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
7739 }
7740
7741 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007742}
7743
Johannes Berg29cbe682010-12-03 09:20:44 +01007744static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7745{
7746 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7747 struct net_device *dev = info->user_ptr[1];
7748 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007749 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007750 int err;
7751
7752 /* start with default */
7753 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007754 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007755
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007756 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007757 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007758 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007759 if (err)
7760 return err;
7761 }
7762
7763 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7764 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7765 return -EINVAL;
7766
Javier Cardonac80d5452010-12-16 17:37:49 -08007767 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7768 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7769
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007770 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7771 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7772 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7773 return -EINVAL;
7774
Marco Porsch9bdbf042013-01-07 16:04:51 +01007775 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7776 setup.beacon_interval =
7777 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7778 if (setup.beacon_interval < 10 ||
7779 setup.beacon_interval > 10000)
7780 return -EINVAL;
7781 }
7782
7783 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7784 setup.dtim_period =
7785 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7786 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7787 return -EINVAL;
7788 }
7789
Javier Cardonac80d5452010-12-16 17:37:49 -08007790 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7791 /* parse additional setup parameters if given */
7792 err = nl80211_parse_mesh_setup(info, &setup);
7793 if (err)
7794 return err;
7795 }
7796
Thomas Pedersend37bb182013-03-04 13:06:13 -08007797 if (setup.user_mpm)
7798 cfg.auto_open_plinks = false;
7799
Johannes Bergcc1d2802012-05-16 23:50:20 +02007800 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007801 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7802 if (err)
7803 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007804 } else {
7805 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007806 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007807 }
7808
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07007809 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7810 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7811 int n_rates =
7812 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7813 struct ieee80211_supported_band *sband;
7814
7815 if (!setup.chandef.chan)
7816 return -EINVAL;
7817
7818 sband = rdev->wiphy.bands[setup.chandef.chan->band];
7819
7820 err = ieee80211_get_ratemask(sband, rates, n_rates,
7821 &setup.basic_rates);
7822 if (err)
7823 return err;
7824 }
7825
Javier Cardonac80d5452010-12-16 17:37:49 -08007826 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007827}
7828
7829static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7830{
7831 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7832 struct net_device *dev = info->user_ptr[1];
7833
7834 return cfg80211_leave_mesh(rdev, dev);
7835}
7836
Johannes Bergdfb89c52012-06-27 09:23:48 +02007837#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007838static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7839 struct cfg80211_registered_device *rdev)
7840{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007841 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007842 struct nlattr *nl_pats, *nl_pat;
7843 int i, pat_len;
7844
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007845 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007846 return 0;
7847
7848 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7849 if (!nl_pats)
7850 return -ENOBUFS;
7851
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007852 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007853 nl_pat = nla_nest_start(msg, i + 1);
7854 if (!nl_pat)
7855 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007856 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07007857 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007858 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07007859 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
7860 wowlan->patterns[i].pattern) ||
7861 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007862 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007863 return -ENOBUFS;
7864 nla_nest_end(msg, nl_pat);
7865 }
7866 nla_nest_end(msg, nl_pats);
7867
7868 return 0;
7869}
7870
Johannes Berg2a0e0472013-01-23 22:57:40 +01007871static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7872 struct cfg80211_wowlan_tcp *tcp)
7873{
7874 struct nlattr *nl_tcp;
7875
7876 if (!tcp)
7877 return 0;
7878
7879 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7880 if (!nl_tcp)
7881 return -ENOBUFS;
7882
7883 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7884 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7885 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7886 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7887 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7888 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7889 tcp->payload_len, tcp->payload) ||
7890 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7891 tcp->data_interval) ||
7892 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7893 tcp->wake_len, tcp->wake_data) ||
7894 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7895 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7896 return -ENOBUFS;
7897
7898 if (tcp->payload_seq.len &&
7899 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7900 sizeof(tcp->payload_seq), &tcp->payload_seq))
7901 return -ENOBUFS;
7902
7903 if (tcp->payload_tok.len &&
7904 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7905 sizeof(tcp->payload_tok) + tcp->tokens_size,
7906 &tcp->payload_tok))
7907 return -ENOBUFS;
7908
Johannes Berge248ad32013-05-16 10:24:28 +02007909 nla_nest_end(msg, nl_tcp);
7910
Johannes Berg2a0e0472013-01-23 22:57:40 +01007911 return 0;
7912}
7913
Johannes Bergff1b6e62011-05-04 15:37:28 +02007914static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7915{
7916 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7917 struct sk_buff *msg;
7918 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007919 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007920
Johannes Berg964dc9e2013-06-03 17:25:34 +02007921 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007922 return -EOPNOTSUPP;
7923
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007924 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01007925 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007926 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
7927 rdev->wiphy.wowlan_config->tcp->payload_len +
7928 rdev->wiphy.wowlan_config->tcp->wake_len +
7929 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007930 }
7931
7932 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007933 if (!msg)
7934 return -ENOMEM;
7935
Eric W. Biederman15e47302012-09-07 20:12:54 +00007936 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007937 NL80211_CMD_GET_WOWLAN);
7938 if (!hdr)
7939 goto nla_put_failure;
7940
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007941 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02007942 struct nlattr *nl_wowlan;
7943
7944 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7945 if (!nl_wowlan)
7946 goto nla_put_failure;
7947
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007948 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007949 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007950 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007951 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007952 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007953 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007954 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007955 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007956 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007957 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007958 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007959 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007960 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007961 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7962 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007963
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007964 if (nl80211_send_wowlan_patterns(msg, rdev))
7965 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007966
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007967 if (nl80211_send_wowlan_tcp(msg,
7968 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01007969 goto nla_put_failure;
7970
Johannes Bergff1b6e62011-05-04 15:37:28 +02007971 nla_nest_end(msg, nl_wowlan);
7972 }
7973
7974 genlmsg_end(msg, hdr);
7975 return genlmsg_reply(msg, info);
7976
7977nla_put_failure:
7978 nlmsg_free(msg);
7979 return -ENOBUFS;
7980}
7981
Johannes Berg2a0e0472013-01-23 22:57:40 +01007982static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7983 struct nlattr *attr,
7984 struct cfg80211_wowlan *trig)
7985{
7986 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7987 struct cfg80211_wowlan_tcp *cfg;
7988 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7989 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7990 u32 size;
7991 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7992 int err, port;
7993
Johannes Berg964dc9e2013-06-03 17:25:34 +02007994 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007995 return -EINVAL;
7996
7997 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7998 nla_data(attr), nla_len(attr),
7999 nl80211_wowlan_tcp_policy);
8000 if (err)
8001 return err;
8002
8003 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8004 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8005 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8006 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8007 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8008 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8009 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8010 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8011 return -EINVAL;
8012
8013 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008014 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008015 return -EINVAL;
8016
8017 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008018 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008019 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008020 return -EINVAL;
8021
8022 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008023 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008024 return -EINVAL;
8025
8026 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8027 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8028 return -EINVAL;
8029
8030 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8031 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8032
8033 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8034 tokens_size = tokln - sizeof(*tok);
8035
8036 if (!tok->len || tokens_size % tok->len)
8037 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008038 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008039 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008040 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008041 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008042 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008043 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008044 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008045 return -EINVAL;
8046 if (tok->offset + tok->len > data_size)
8047 return -EINVAL;
8048 }
8049
8050 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8051 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008052 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008053 return -EINVAL;
8054 if (seq->len == 0 || seq->len > 4)
8055 return -EINVAL;
8056 if (seq->len + seq->offset > data_size)
8057 return -EINVAL;
8058 }
8059
8060 size = sizeof(*cfg);
8061 size += data_size;
8062 size += wake_size + wake_mask_size;
8063 size += tokens_size;
8064
8065 cfg = kzalloc(size, GFP_KERNEL);
8066 if (!cfg)
8067 return -ENOMEM;
8068 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8069 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8070 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8071 ETH_ALEN);
8072 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8073 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8074 else
8075 port = 0;
8076#ifdef CONFIG_INET
8077 /* allocate a socket and port for it and use it */
8078 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8079 IPPROTO_TCP, &cfg->sock, 1);
8080 if (err) {
8081 kfree(cfg);
8082 return err;
8083 }
8084 if (inet_csk_get_port(cfg->sock->sk, port)) {
8085 sock_release(cfg->sock);
8086 kfree(cfg);
8087 return -EADDRINUSE;
8088 }
8089 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8090#else
8091 if (!port) {
8092 kfree(cfg);
8093 return -EINVAL;
8094 }
8095 cfg->src_port = port;
8096#endif
8097
8098 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8099 cfg->payload_len = data_size;
8100 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8101 memcpy((void *)cfg->payload,
8102 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8103 data_size);
8104 if (seq)
8105 cfg->payload_seq = *seq;
8106 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8107 cfg->wake_len = wake_size;
8108 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8109 memcpy((void *)cfg->wake_data,
8110 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8111 wake_size);
8112 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8113 data_size + wake_size;
8114 memcpy((void *)cfg->wake_mask,
8115 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8116 wake_mask_size);
8117 if (tok) {
8118 cfg->tokens_size = tokens_size;
8119 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8120 }
8121
8122 trig->tcp = cfg;
8123
8124 return 0;
8125}
8126
Johannes Bergff1b6e62011-05-04 15:37:28 +02008127static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8128{
8129 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8130 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008131 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008132 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008133 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008134 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008135 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008136
Johannes Berg964dc9e2013-06-03 17:25:34 +02008137 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008138 return -EOPNOTSUPP;
8139
Johannes Bergae33bd82012-07-12 16:25:02 +02008140 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8141 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008142 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008143 goto set_wakeup;
8144 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008145
8146 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8147 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8148 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8149 nl80211_wowlan_policy);
8150 if (err)
8151 return err;
8152
8153 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8154 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8155 return -EINVAL;
8156 new_triggers.any = true;
8157 }
8158
8159 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8160 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8161 return -EINVAL;
8162 new_triggers.disconnect = true;
8163 }
8164
8165 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8166 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8167 return -EINVAL;
8168 new_triggers.magic_pkt = true;
8169 }
8170
Johannes Berg77dbbb12011-07-13 10:48:55 +02008171 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8172 return -EINVAL;
8173
8174 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8175 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8176 return -EINVAL;
8177 new_triggers.gtk_rekey_failure = true;
8178 }
8179
8180 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8181 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8182 return -EINVAL;
8183 new_triggers.eap_identity_req = true;
8184 }
8185
8186 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8187 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8188 return -EINVAL;
8189 new_triggers.four_way_handshake = true;
8190 }
8191
8192 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8193 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8194 return -EINVAL;
8195 new_triggers.rfkill_release = true;
8196 }
8197
Johannes Bergff1b6e62011-05-04 15:37:28 +02008198 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8199 struct nlattr *pat;
8200 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008201 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008202 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008203
8204 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8205 rem)
8206 n_patterns++;
8207 if (n_patterns > wowlan->n_patterns)
8208 return -EINVAL;
8209
8210 new_triggers.patterns = kcalloc(n_patterns,
8211 sizeof(new_triggers.patterns[0]),
8212 GFP_KERNEL);
8213 if (!new_triggers.patterns)
8214 return -ENOMEM;
8215
8216 new_triggers.n_patterns = n_patterns;
8217 i = 0;
8218
8219 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8220 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008221 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8222 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008223 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008224 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8225 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008226 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008227 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008228 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008229 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008230 goto error;
8231 if (pat_len > wowlan->pattern_max_len ||
8232 pat_len < wowlan->pattern_min_len)
8233 goto error;
8234
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008235 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008236 pkt_offset = 0;
8237 else
8238 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008239 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008240 if (pkt_offset > wowlan->max_pkt_offset)
8241 goto error;
8242 new_triggers.patterns[i].pkt_offset = pkt_offset;
8243
Johannes Bergff1b6e62011-05-04 15:37:28 +02008244 new_triggers.patterns[i].mask =
8245 kmalloc(mask_len + pat_len, GFP_KERNEL);
8246 if (!new_triggers.patterns[i].mask) {
8247 err = -ENOMEM;
8248 goto error;
8249 }
8250 new_triggers.patterns[i].pattern =
8251 new_triggers.patterns[i].mask + mask_len;
8252 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008253 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008254 mask_len);
8255 new_triggers.patterns[i].pattern_len = pat_len;
8256 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008257 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008258 pat_len);
8259 i++;
8260 }
8261 }
8262
Johannes Berg2a0e0472013-01-23 22:57:40 +01008263 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8264 err = nl80211_parse_wowlan_tcp(
8265 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8266 &new_triggers);
8267 if (err)
8268 goto error;
8269 }
8270
Johannes Bergae33bd82012-07-12 16:25:02 +02008271 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8272 if (!ntrig) {
8273 err = -ENOMEM;
8274 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008275 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008276 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008277 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008278
Johannes Bergae33bd82012-07-12 16:25:02 +02008279 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008280 if (rdev->ops->set_wakeup &&
8281 prev_enabled != !!rdev->wiphy.wowlan_config)
8282 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008283
Johannes Bergff1b6e62011-05-04 15:37:28 +02008284 return 0;
8285 error:
8286 for (i = 0; i < new_triggers.n_patterns; i++)
8287 kfree(new_triggers.patterns[i].mask);
8288 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008289 if (new_triggers.tcp && new_triggers.tcp->sock)
8290 sock_release(new_triggers.tcp->sock);
8291 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008292 return err;
8293}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008294#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008295
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008296static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8297 struct cfg80211_registered_device *rdev)
8298{
8299 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8300 int i, j, pat_len;
8301 struct cfg80211_coalesce_rules *rule;
8302
8303 if (!rdev->coalesce->n_rules)
8304 return 0;
8305
8306 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8307 if (!nl_rules)
8308 return -ENOBUFS;
8309
8310 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8311 nl_rule = nla_nest_start(msg, i + 1);
8312 if (!nl_rule)
8313 return -ENOBUFS;
8314
8315 rule = &rdev->coalesce->rules[i];
8316 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8317 rule->delay))
8318 return -ENOBUFS;
8319
8320 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8321 rule->condition))
8322 return -ENOBUFS;
8323
8324 nl_pats = nla_nest_start(msg,
8325 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8326 if (!nl_pats)
8327 return -ENOBUFS;
8328
8329 for (j = 0; j < rule->n_patterns; j++) {
8330 nl_pat = nla_nest_start(msg, j + 1);
8331 if (!nl_pat)
8332 return -ENOBUFS;
8333 pat_len = rule->patterns[j].pattern_len;
8334 if (nla_put(msg, NL80211_PKTPAT_MASK,
8335 DIV_ROUND_UP(pat_len, 8),
8336 rule->patterns[j].mask) ||
8337 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8338 rule->patterns[j].pattern) ||
8339 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8340 rule->patterns[j].pkt_offset))
8341 return -ENOBUFS;
8342 nla_nest_end(msg, nl_pat);
8343 }
8344 nla_nest_end(msg, nl_pats);
8345 nla_nest_end(msg, nl_rule);
8346 }
8347 nla_nest_end(msg, nl_rules);
8348
8349 return 0;
8350}
8351
8352static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8353{
8354 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8355 struct sk_buff *msg;
8356 void *hdr;
8357
8358 if (!rdev->wiphy.coalesce)
8359 return -EOPNOTSUPP;
8360
8361 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8362 if (!msg)
8363 return -ENOMEM;
8364
8365 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8366 NL80211_CMD_GET_COALESCE);
8367 if (!hdr)
8368 goto nla_put_failure;
8369
8370 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8371 goto nla_put_failure;
8372
8373 genlmsg_end(msg, hdr);
8374 return genlmsg_reply(msg, info);
8375
8376nla_put_failure:
8377 nlmsg_free(msg);
8378 return -ENOBUFS;
8379}
8380
8381void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8382{
8383 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8384 int i, j;
8385 struct cfg80211_coalesce_rules *rule;
8386
8387 if (!coalesce)
8388 return;
8389
8390 for (i = 0; i < coalesce->n_rules; i++) {
8391 rule = &coalesce->rules[i];
8392 for (j = 0; j < rule->n_patterns; j++)
8393 kfree(rule->patterns[j].mask);
8394 kfree(rule->patterns);
8395 }
8396 kfree(coalesce->rules);
8397 kfree(coalesce);
8398 rdev->coalesce = NULL;
8399}
8400
8401static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8402 struct nlattr *rule,
8403 struct cfg80211_coalesce_rules *new_rule)
8404{
8405 int err, i;
8406 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8407 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8408 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8409 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8410
8411 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8412 nla_len(rule), nl80211_coalesce_policy);
8413 if (err)
8414 return err;
8415
8416 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8417 new_rule->delay =
8418 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8419 if (new_rule->delay > coalesce->max_delay)
8420 return -EINVAL;
8421
8422 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8423 new_rule->condition =
8424 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8425 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8426 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8427 return -EINVAL;
8428
8429 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8430 return -EINVAL;
8431
8432 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8433 rem)
8434 n_patterns++;
8435 if (n_patterns > coalesce->n_patterns)
8436 return -EINVAL;
8437
8438 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8439 GFP_KERNEL);
8440 if (!new_rule->patterns)
8441 return -ENOMEM;
8442
8443 new_rule->n_patterns = n_patterns;
8444 i = 0;
8445
8446 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8447 rem) {
8448 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8449 nla_len(pat), NULL);
8450 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8451 !pat_tb[NL80211_PKTPAT_PATTERN])
8452 return -EINVAL;
8453 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8454 mask_len = DIV_ROUND_UP(pat_len, 8);
8455 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8456 return -EINVAL;
8457 if (pat_len > coalesce->pattern_max_len ||
8458 pat_len < coalesce->pattern_min_len)
8459 return -EINVAL;
8460
8461 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8462 pkt_offset = 0;
8463 else
8464 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8465 if (pkt_offset > coalesce->max_pkt_offset)
8466 return -EINVAL;
8467 new_rule->patterns[i].pkt_offset = pkt_offset;
8468
8469 new_rule->patterns[i].mask =
8470 kmalloc(mask_len + pat_len, GFP_KERNEL);
8471 if (!new_rule->patterns[i].mask)
8472 return -ENOMEM;
8473 new_rule->patterns[i].pattern =
8474 new_rule->patterns[i].mask + mask_len;
8475 memcpy(new_rule->patterns[i].mask,
8476 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8477 new_rule->patterns[i].pattern_len = pat_len;
8478 memcpy(new_rule->patterns[i].pattern,
8479 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8480 i++;
8481 }
8482
8483 return 0;
8484}
8485
8486static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8487{
8488 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8489 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8490 struct cfg80211_coalesce new_coalesce = {};
8491 struct cfg80211_coalesce *n_coalesce;
8492 int err, rem_rule, n_rules = 0, i, j;
8493 struct nlattr *rule;
8494 struct cfg80211_coalesce_rules *tmp_rule;
8495
8496 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8497 return -EOPNOTSUPP;
8498
8499 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8500 cfg80211_rdev_free_coalesce(rdev);
8501 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8502 return 0;
8503 }
8504
8505 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8506 rem_rule)
8507 n_rules++;
8508 if (n_rules > coalesce->n_rules)
8509 return -EINVAL;
8510
8511 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8512 GFP_KERNEL);
8513 if (!new_coalesce.rules)
8514 return -ENOMEM;
8515
8516 new_coalesce.n_rules = n_rules;
8517 i = 0;
8518
8519 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8520 rem_rule) {
8521 err = nl80211_parse_coalesce_rule(rdev, rule,
8522 &new_coalesce.rules[i]);
8523 if (err)
8524 goto error;
8525
8526 i++;
8527 }
8528
8529 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8530 if (err)
8531 goto error;
8532
8533 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8534 if (!n_coalesce) {
8535 err = -ENOMEM;
8536 goto error;
8537 }
8538 cfg80211_rdev_free_coalesce(rdev);
8539 rdev->coalesce = n_coalesce;
8540
8541 return 0;
8542error:
8543 for (i = 0; i < new_coalesce.n_rules; i++) {
8544 tmp_rule = &new_coalesce.rules[i];
8545 for (j = 0; j < tmp_rule->n_patterns; j++)
8546 kfree(tmp_rule->patterns[j].mask);
8547 kfree(tmp_rule->patterns);
8548 }
8549 kfree(new_coalesce.rules);
8550
8551 return err;
8552}
8553
Johannes Berge5497d72011-07-05 16:35:40 +02008554static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8555{
8556 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8557 struct net_device *dev = info->user_ptr[1];
8558 struct wireless_dev *wdev = dev->ieee80211_ptr;
8559 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8560 struct cfg80211_gtk_rekey_data rekey_data;
8561 int err;
8562
8563 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8564 return -EINVAL;
8565
8566 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8567 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8568 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8569 nl80211_rekey_policy);
8570 if (err)
8571 return err;
8572
8573 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8574 return -ERANGE;
8575 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8576 return -ERANGE;
8577 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8578 return -ERANGE;
8579
8580 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8581 NL80211_KEK_LEN);
8582 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8583 NL80211_KCK_LEN);
8584 memcpy(rekey_data.replay_ctr,
8585 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8586 NL80211_REPLAY_CTR_LEN);
8587
8588 wdev_lock(wdev);
8589 if (!wdev->current_bss) {
8590 err = -ENOTCONN;
8591 goto out;
8592 }
8593
8594 if (!rdev->ops->set_rekey_data) {
8595 err = -EOPNOTSUPP;
8596 goto out;
8597 }
8598
Hila Gonene35e4d22012-06-27 17:19:42 +03008599 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008600 out:
8601 wdev_unlock(wdev);
8602 return err;
8603}
8604
Johannes Berg28946da2011-11-04 11:18:12 +01008605static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8606 struct genl_info *info)
8607{
8608 struct net_device *dev = info->user_ptr[1];
8609 struct wireless_dev *wdev = dev->ieee80211_ptr;
8610
8611 if (wdev->iftype != NL80211_IFTYPE_AP &&
8612 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8613 return -EINVAL;
8614
Eric W. Biederman15e47302012-09-07 20:12:54 +00008615 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008616 return -EBUSY;
8617
Eric W. Biederman15e47302012-09-07 20:12:54 +00008618 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008619 return 0;
8620}
8621
Johannes Berg7f6cf312011-11-04 11:18:15 +01008622static int nl80211_probe_client(struct sk_buff *skb,
8623 struct genl_info *info)
8624{
8625 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8626 struct net_device *dev = info->user_ptr[1];
8627 struct wireless_dev *wdev = dev->ieee80211_ptr;
8628 struct sk_buff *msg;
8629 void *hdr;
8630 const u8 *addr;
8631 u64 cookie;
8632 int err;
8633
8634 if (wdev->iftype != NL80211_IFTYPE_AP &&
8635 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8636 return -EOPNOTSUPP;
8637
8638 if (!info->attrs[NL80211_ATTR_MAC])
8639 return -EINVAL;
8640
8641 if (!rdev->ops->probe_client)
8642 return -EOPNOTSUPP;
8643
8644 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8645 if (!msg)
8646 return -ENOMEM;
8647
Eric W. Biederman15e47302012-09-07 20:12:54 +00008648 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008649 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008650 if (!hdr) {
8651 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008652 goto free_msg;
8653 }
8654
8655 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8656
Hila Gonene35e4d22012-06-27 17:19:42 +03008657 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008658 if (err)
8659 goto free_msg;
8660
David S. Miller9360ffd2012-03-29 04:41:26 -04008661 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8662 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008663
8664 genlmsg_end(msg, hdr);
8665
8666 return genlmsg_reply(msg, info);
8667
8668 nla_put_failure:
8669 err = -ENOBUFS;
8670 free_msg:
8671 nlmsg_free(msg);
8672 return err;
8673}
8674
Johannes Berg5e760232011-11-04 11:18:17 +01008675static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8676{
8677 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008678 struct cfg80211_beacon_registration *reg, *nreg;
8679 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008680
8681 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8682 return -EOPNOTSUPP;
8683
Ben Greear37c73b52012-10-26 14:49:25 -07008684 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8685 if (!nreg)
8686 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008687
Ben Greear37c73b52012-10-26 14:49:25 -07008688 /* First, check if already registered. */
8689 spin_lock_bh(&rdev->beacon_registrations_lock);
8690 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8691 if (reg->nlportid == info->snd_portid) {
8692 rv = -EALREADY;
8693 goto out_err;
8694 }
8695 }
8696 /* Add it to the list */
8697 nreg->nlportid = info->snd_portid;
8698 list_add(&nreg->list, &rdev->beacon_registrations);
8699
8700 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008701
8702 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008703out_err:
8704 spin_unlock_bh(&rdev->beacon_registrations_lock);
8705 kfree(nreg);
8706 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008707}
8708
Johannes Berg98104fde2012-06-16 00:19:54 +02008709static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8710{
8711 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8712 struct wireless_dev *wdev = info->user_ptr[1];
8713 int err;
8714
8715 if (!rdev->ops->start_p2p_device)
8716 return -EOPNOTSUPP;
8717
8718 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8719 return -EOPNOTSUPP;
8720
8721 if (wdev->p2p_started)
8722 return 0;
8723
Johannes Berg98104fde2012-06-16 00:19:54 +02008724 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008725 if (err)
8726 return err;
8727
Johannes Bergeeb126e2012-10-23 15:16:50 +02008728 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008729 if (err)
8730 return err;
8731
8732 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02008733 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02008734
8735 return 0;
8736}
8737
8738static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8739{
8740 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8741 struct wireless_dev *wdev = info->user_ptr[1];
8742
8743 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8744 return -EOPNOTSUPP;
8745
8746 if (!rdev->ops->stop_p2p_device)
8747 return -EOPNOTSUPP;
8748
Johannes Bergf9f47522013-03-19 15:04:07 +01008749 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008750
8751 return 0;
8752}
8753
Johannes Berg3713b4e2013-02-14 16:19:38 +01008754static int nl80211_get_protocol_features(struct sk_buff *skb,
8755 struct genl_info *info)
8756{
8757 void *hdr;
8758 struct sk_buff *msg;
8759
8760 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8761 if (!msg)
8762 return -ENOMEM;
8763
8764 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8765 NL80211_CMD_GET_PROTOCOL_FEATURES);
8766 if (!hdr)
8767 goto nla_put_failure;
8768
8769 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8770 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8771 goto nla_put_failure;
8772
8773 genlmsg_end(msg, hdr);
8774 return genlmsg_reply(msg, info);
8775
8776 nla_put_failure:
8777 kfree_skb(msg);
8778 return -ENOBUFS;
8779}
8780
Jouni Malinen355199e2013-02-27 17:14:27 +02008781static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8782{
8783 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8784 struct cfg80211_update_ft_ies_params ft_params;
8785 struct net_device *dev = info->user_ptr[1];
8786
8787 if (!rdev->ops->update_ft_ies)
8788 return -EOPNOTSUPP;
8789
8790 if (!info->attrs[NL80211_ATTR_MDID] ||
8791 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8792 return -EINVAL;
8793
8794 memset(&ft_params, 0, sizeof(ft_params));
8795 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8796 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8797 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8798
8799 return rdev_update_ft_ies(rdev, dev, &ft_params);
8800}
8801
Arend van Spriel5de17982013-04-18 15:49:00 +02008802static int nl80211_crit_protocol_start(struct sk_buff *skb,
8803 struct genl_info *info)
8804{
8805 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8806 struct wireless_dev *wdev = info->user_ptr[1];
8807 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8808 u16 duration;
8809 int ret;
8810
8811 if (!rdev->ops->crit_proto_start)
8812 return -EOPNOTSUPP;
8813
8814 if (WARN_ON(!rdev->ops->crit_proto_stop))
8815 return -EINVAL;
8816
8817 if (rdev->crit_proto_nlportid)
8818 return -EBUSY;
8819
8820 /* determine protocol if provided */
8821 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8822 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8823
8824 if (proto >= NUM_NL80211_CRIT_PROTO)
8825 return -EINVAL;
8826
8827 /* timeout must be provided */
8828 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8829 return -EINVAL;
8830
8831 duration =
8832 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8833
8834 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8835 return -ERANGE;
8836
8837 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8838 if (!ret)
8839 rdev->crit_proto_nlportid = info->snd_portid;
8840
8841 return ret;
8842}
8843
8844static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8845 struct genl_info *info)
8846{
8847 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8848 struct wireless_dev *wdev = info->user_ptr[1];
8849
8850 if (!rdev->ops->crit_proto_stop)
8851 return -EOPNOTSUPP;
8852
8853 if (rdev->crit_proto_nlportid) {
8854 rdev->crit_proto_nlportid = 0;
8855 rdev_crit_proto_stop(rdev, wdev);
8856 }
8857 return 0;
8858}
8859
Johannes Bergad7e7182013-11-13 13:37:47 +01008860static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
8861{
8862 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8863 struct wireless_dev *wdev =
8864 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
8865 int i, err;
8866 u32 vid, subcmd;
8867
8868 if (!rdev->wiphy.vendor_commands)
8869 return -EOPNOTSUPP;
8870
8871 if (IS_ERR(wdev)) {
8872 err = PTR_ERR(wdev);
8873 if (err != -EINVAL)
8874 return err;
8875 wdev = NULL;
8876 } else if (wdev->wiphy != &rdev->wiphy) {
8877 return -EINVAL;
8878 }
8879
8880 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
8881 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
8882 return -EINVAL;
8883
8884 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
8885 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
8886 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
8887 const struct wiphy_vendor_command *vcmd;
8888 void *data = NULL;
8889 int len = 0;
8890
8891 vcmd = &rdev->wiphy.vendor_commands[i];
8892
8893 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
8894 continue;
8895
8896 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
8897 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
8898 if (!wdev)
8899 return -EINVAL;
8900 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
8901 !wdev->netdev)
8902 return -EINVAL;
8903
8904 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
8905 if (wdev->netdev &&
8906 !netif_running(wdev->netdev))
8907 return -ENETDOWN;
8908 if (!wdev->netdev && !wdev->p2p_started)
8909 return -ENETDOWN;
8910 }
8911 } else {
8912 wdev = NULL;
8913 }
8914
8915 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
8916 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
8917 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
8918 }
8919
8920 rdev->cur_cmd_info = info;
8921 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
8922 data, len);
8923 rdev->cur_cmd_info = NULL;
8924 return err;
8925 }
8926
8927 return -EOPNOTSUPP;
8928}
8929
8930struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
8931 enum nl80211_commands cmd,
8932 enum nl80211_attrs attr,
8933 int approxlen)
8934{
8935 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8936
8937 if (WARN_ON(!rdev->cur_cmd_info))
8938 return NULL;
8939
8940 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
8941 rdev->cur_cmd_info->snd_portid,
8942 rdev->cur_cmd_info->snd_seq,
8943 cmd, attr, GFP_KERNEL);
8944}
8945EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
8946
8947int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
8948{
8949 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
8950 void *hdr = ((void **)skb->cb)[1];
8951 struct nlattr *data = ((void **)skb->cb)[2];
8952
8953 if (WARN_ON(!rdev->cur_cmd_info)) {
8954 kfree_skb(skb);
8955 return -EINVAL;
8956 }
8957
8958 nla_nest_end(skb, data);
8959 genlmsg_end(skb, hdr);
8960 return genlmsg_reply(skb, rdev->cur_cmd_info);
8961}
8962EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
8963
8964
Johannes Berg4c476992010-10-04 21:36:35 +02008965#define NL80211_FLAG_NEED_WIPHY 0x01
8966#define NL80211_FLAG_NEED_NETDEV 0x02
8967#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008968#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8969#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8970 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008971#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008972/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008973#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8974 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008975
8976static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8977 struct genl_info *info)
8978{
8979 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008980 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008981 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008982 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8983
8984 if (rtnl)
8985 rtnl_lock();
8986
8987 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008988 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008989 if (IS_ERR(rdev)) {
8990 if (rtnl)
8991 rtnl_unlock();
8992 return PTR_ERR(rdev);
8993 }
8994 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008995 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8996 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02008997 ASSERT_RTNL();
8998
Johannes Berg89a54e42012-06-15 14:33:17 +02008999 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
9000 info->attrs);
9001 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009002 if (rtnl)
9003 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009004 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009005 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009006
Johannes Berg89a54e42012-06-15 14:33:17 +02009007 dev = wdev->netdev;
9008 rdev = wiphy_to_dev(wdev->wiphy);
9009
Johannes Berg1bf614e2012-06-15 15:23:36 +02009010 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9011 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009012 if (rtnl)
9013 rtnl_unlock();
9014 return -EINVAL;
9015 }
9016
9017 info->user_ptr[1] = dev;
9018 } else {
9019 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009020 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009021
Johannes Berg1bf614e2012-06-15 15:23:36 +02009022 if (dev) {
9023 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9024 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009025 if (rtnl)
9026 rtnl_unlock();
9027 return -ENETDOWN;
9028 }
9029
9030 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009031 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9032 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009033 if (rtnl)
9034 rtnl_unlock();
9035 return -ENETDOWN;
9036 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009037 }
9038
Johannes Berg4c476992010-10-04 21:36:35 +02009039 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009040 }
9041
9042 return 0;
9043}
9044
9045static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
9046 struct genl_info *info)
9047{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009048 if (info->user_ptr[1]) {
9049 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9050 struct wireless_dev *wdev = info->user_ptr[1];
9051
9052 if (wdev->netdev)
9053 dev_put(wdev->netdev);
9054 } else {
9055 dev_put(info->user_ptr[1]);
9056 }
9057 }
Johannes Berg4c476992010-10-04 21:36:35 +02009058 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9059 rtnl_unlock();
9060}
9061
Johannes Berg55682962007-09-20 13:09:35 -04009062static struct genl_ops nl80211_ops[] = {
9063 {
9064 .cmd = NL80211_CMD_GET_WIPHY,
9065 .doit = nl80211_get_wiphy,
9066 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009067 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009068 .policy = nl80211_policy,
9069 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009070 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9071 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009072 },
9073 {
9074 .cmd = NL80211_CMD_SET_WIPHY,
9075 .doit = nl80211_set_wiphy,
9076 .policy = nl80211_policy,
9077 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009078 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009079 },
9080 {
9081 .cmd = NL80211_CMD_GET_INTERFACE,
9082 .doit = nl80211_get_interface,
9083 .dumpit = nl80211_dump_interface,
9084 .policy = nl80211_policy,
9085 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009086 .internal_flags = NL80211_FLAG_NEED_WDEV |
9087 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009088 },
9089 {
9090 .cmd = NL80211_CMD_SET_INTERFACE,
9091 .doit = nl80211_set_interface,
9092 .policy = nl80211_policy,
9093 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009094 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9095 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009096 },
9097 {
9098 .cmd = NL80211_CMD_NEW_INTERFACE,
9099 .doit = nl80211_new_interface,
9100 .policy = nl80211_policy,
9101 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009102 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9103 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009104 },
9105 {
9106 .cmd = NL80211_CMD_DEL_INTERFACE,
9107 .doit = nl80211_del_interface,
9108 .policy = nl80211_policy,
9109 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009110 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009111 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009112 },
Johannes Berg41ade002007-12-19 02:03:29 +01009113 {
9114 .cmd = NL80211_CMD_GET_KEY,
9115 .doit = nl80211_get_key,
9116 .policy = nl80211_policy,
9117 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009118 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009119 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009120 },
9121 {
9122 .cmd = NL80211_CMD_SET_KEY,
9123 .doit = nl80211_set_key,
9124 .policy = nl80211_policy,
9125 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009126 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009127 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009128 },
9129 {
9130 .cmd = NL80211_CMD_NEW_KEY,
9131 .doit = nl80211_new_key,
9132 .policy = nl80211_policy,
9133 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009134 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009135 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009136 },
9137 {
9138 .cmd = NL80211_CMD_DEL_KEY,
9139 .doit = nl80211_del_key,
9140 .policy = nl80211_policy,
9141 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009142 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009143 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009144 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009145 {
9146 .cmd = NL80211_CMD_SET_BEACON,
9147 .policy = nl80211_policy,
9148 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009149 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009150 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009151 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009152 },
9153 {
Johannes Berg88600202012-02-13 15:17:18 +01009154 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009155 .policy = nl80211_policy,
9156 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009157 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009158 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009159 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009160 },
9161 {
Johannes Berg88600202012-02-13 15:17:18 +01009162 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009163 .policy = nl80211_policy,
9164 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009165 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009166 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009167 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009168 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009169 {
9170 .cmd = NL80211_CMD_GET_STATION,
9171 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009172 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009173 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009174 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9175 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009176 },
9177 {
9178 .cmd = NL80211_CMD_SET_STATION,
9179 .doit = nl80211_set_station,
9180 .policy = nl80211_policy,
9181 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009182 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009183 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009184 },
9185 {
9186 .cmd = NL80211_CMD_NEW_STATION,
9187 .doit = nl80211_new_station,
9188 .policy = nl80211_policy,
9189 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009190 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009191 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009192 },
9193 {
9194 .cmd = NL80211_CMD_DEL_STATION,
9195 .doit = nl80211_del_station,
9196 .policy = nl80211_policy,
9197 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009198 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009199 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009200 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009201 {
9202 .cmd = NL80211_CMD_GET_MPATH,
9203 .doit = nl80211_get_mpath,
9204 .dumpit = nl80211_dump_mpath,
9205 .policy = nl80211_policy,
9206 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009207 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009208 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009209 },
9210 {
9211 .cmd = NL80211_CMD_SET_MPATH,
9212 .doit = nl80211_set_mpath,
9213 .policy = nl80211_policy,
9214 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009215 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009216 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009217 },
9218 {
9219 .cmd = NL80211_CMD_NEW_MPATH,
9220 .doit = nl80211_new_mpath,
9221 .policy = nl80211_policy,
9222 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009223 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009224 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009225 },
9226 {
9227 .cmd = NL80211_CMD_DEL_MPATH,
9228 .doit = nl80211_del_mpath,
9229 .policy = nl80211_policy,
9230 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009231 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009232 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009233 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009234 {
9235 .cmd = NL80211_CMD_SET_BSS,
9236 .doit = nl80211_set_bss,
9237 .policy = nl80211_policy,
9238 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009239 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009240 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009241 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009242 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009243 .cmd = NL80211_CMD_GET_REG,
9244 .doit = nl80211_get_reg,
9245 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009246 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009247 /* can be retrieved by unprivileged users */
9248 },
9249 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009250 .cmd = NL80211_CMD_SET_REG,
9251 .doit = nl80211_set_reg,
9252 .policy = nl80211_policy,
9253 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009254 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009255 },
9256 {
9257 .cmd = NL80211_CMD_REQ_SET_REG,
9258 .doit = nl80211_req_set_reg,
9259 .policy = nl80211_policy,
9260 .flags = GENL_ADMIN_PERM,
9261 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009262 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009263 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9264 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009265 .policy = nl80211_policy,
9266 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009267 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009268 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009269 },
9270 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009271 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9272 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009273 .policy = nl80211_policy,
9274 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009275 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009276 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009277 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009278 {
Johannes Berg2a519312009-02-10 21:25:55 +01009279 .cmd = NL80211_CMD_TRIGGER_SCAN,
9280 .doit = nl80211_trigger_scan,
9281 .policy = nl80211_policy,
9282 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009283 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009284 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009285 },
9286 {
9287 .cmd = NL80211_CMD_GET_SCAN,
9288 .policy = nl80211_policy,
9289 .dumpit = nl80211_dump_scan,
9290 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009291 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009292 .cmd = NL80211_CMD_START_SCHED_SCAN,
9293 .doit = nl80211_start_sched_scan,
9294 .policy = nl80211_policy,
9295 .flags = GENL_ADMIN_PERM,
9296 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9297 NL80211_FLAG_NEED_RTNL,
9298 },
9299 {
9300 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9301 .doit = nl80211_stop_sched_scan,
9302 .policy = nl80211_policy,
9303 .flags = GENL_ADMIN_PERM,
9304 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9305 NL80211_FLAG_NEED_RTNL,
9306 },
9307 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009308 .cmd = NL80211_CMD_AUTHENTICATE,
9309 .doit = nl80211_authenticate,
9310 .policy = nl80211_policy,
9311 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009312 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009313 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009314 },
9315 {
9316 .cmd = NL80211_CMD_ASSOCIATE,
9317 .doit = nl80211_associate,
9318 .policy = nl80211_policy,
9319 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009320 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009321 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009322 },
9323 {
9324 .cmd = NL80211_CMD_DEAUTHENTICATE,
9325 .doit = nl80211_deauthenticate,
9326 .policy = nl80211_policy,
9327 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009328 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009329 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009330 },
9331 {
9332 .cmd = NL80211_CMD_DISASSOCIATE,
9333 .doit = nl80211_disassociate,
9334 .policy = nl80211_policy,
9335 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009336 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009337 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009338 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009339 {
9340 .cmd = NL80211_CMD_JOIN_IBSS,
9341 .doit = nl80211_join_ibss,
9342 .policy = nl80211_policy,
9343 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009344 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009345 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009346 },
9347 {
9348 .cmd = NL80211_CMD_LEAVE_IBSS,
9349 .doit = nl80211_leave_ibss,
9350 .policy = nl80211_policy,
9351 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009352 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009353 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009354 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009355#ifdef CONFIG_NL80211_TESTMODE
9356 {
9357 .cmd = NL80211_CMD_TESTMODE,
9358 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009359 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009360 .policy = nl80211_policy,
9361 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009362 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9363 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009364 },
9365#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009366 {
9367 .cmd = NL80211_CMD_CONNECT,
9368 .doit = nl80211_connect,
9369 .policy = nl80211_policy,
9370 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009371 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009372 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009373 },
9374 {
9375 .cmd = NL80211_CMD_DISCONNECT,
9376 .doit = nl80211_disconnect,
9377 .policy = nl80211_policy,
9378 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009379 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009380 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009381 },
Johannes Berg463d0182009-07-14 00:33:35 +02009382 {
9383 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9384 .doit = nl80211_wiphy_netns,
9385 .policy = nl80211_policy,
9386 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009387 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9388 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009389 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009390 {
9391 .cmd = NL80211_CMD_GET_SURVEY,
9392 .policy = nl80211_policy,
9393 .dumpit = nl80211_dump_survey,
9394 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009395 {
9396 .cmd = NL80211_CMD_SET_PMKSA,
9397 .doit = nl80211_setdel_pmksa,
9398 .policy = nl80211_policy,
9399 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009400 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009401 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009402 },
9403 {
9404 .cmd = NL80211_CMD_DEL_PMKSA,
9405 .doit = nl80211_setdel_pmksa,
9406 .policy = nl80211_policy,
9407 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009408 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009409 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009410 },
9411 {
9412 .cmd = NL80211_CMD_FLUSH_PMKSA,
9413 .doit = nl80211_flush_pmksa,
9414 .policy = nl80211_policy,
9415 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009416 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009417 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009418 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009419 {
9420 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9421 .doit = nl80211_remain_on_channel,
9422 .policy = nl80211_policy,
9423 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009424 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009425 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009426 },
9427 {
9428 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9429 .doit = nl80211_cancel_remain_on_channel,
9430 .policy = nl80211_policy,
9431 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009432 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009433 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009434 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009435 {
9436 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9437 .doit = nl80211_set_tx_bitrate_mask,
9438 .policy = nl80211_policy,
9439 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009440 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9441 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009442 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009443 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009444 .cmd = NL80211_CMD_REGISTER_FRAME,
9445 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009446 .policy = nl80211_policy,
9447 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009448 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009449 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009450 },
9451 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009452 .cmd = NL80211_CMD_FRAME,
9453 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009454 .policy = nl80211_policy,
9455 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009456 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009457 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009458 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009459 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009460 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9461 .doit = nl80211_tx_mgmt_cancel_wait,
9462 .policy = nl80211_policy,
9463 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009464 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009465 NL80211_FLAG_NEED_RTNL,
9466 },
9467 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009468 .cmd = NL80211_CMD_SET_POWER_SAVE,
9469 .doit = nl80211_set_power_save,
9470 .policy = nl80211_policy,
9471 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009472 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9473 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009474 },
9475 {
9476 .cmd = NL80211_CMD_GET_POWER_SAVE,
9477 .doit = nl80211_get_power_save,
9478 .policy = nl80211_policy,
9479 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009480 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9481 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009482 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009483 {
9484 .cmd = NL80211_CMD_SET_CQM,
9485 .doit = nl80211_set_cqm,
9486 .policy = nl80211_policy,
9487 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009488 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9489 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009490 },
Johannes Bergf444de02010-05-05 15:25:02 +02009491 {
9492 .cmd = NL80211_CMD_SET_CHANNEL,
9493 .doit = nl80211_set_channel,
9494 .policy = nl80211_policy,
9495 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009496 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9497 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009498 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009499 {
9500 .cmd = NL80211_CMD_SET_WDS_PEER,
9501 .doit = nl80211_set_wds_peer,
9502 .policy = nl80211_policy,
9503 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009504 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9505 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009506 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009507 {
9508 .cmd = NL80211_CMD_JOIN_MESH,
9509 .doit = nl80211_join_mesh,
9510 .policy = nl80211_policy,
9511 .flags = GENL_ADMIN_PERM,
9512 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9513 NL80211_FLAG_NEED_RTNL,
9514 },
9515 {
9516 .cmd = NL80211_CMD_LEAVE_MESH,
9517 .doit = nl80211_leave_mesh,
9518 .policy = nl80211_policy,
9519 .flags = GENL_ADMIN_PERM,
9520 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9521 NL80211_FLAG_NEED_RTNL,
9522 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009523#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009524 {
9525 .cmd = NL80211_CMD_GET_WOWLAN,
9526 .doit = nl80211_get_wowlan,
9527 .policy = nl80211_policy,
9528 /* can be retrieved by unprivileged users */
9529 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9530 NL80211_FLAG_NEED_RTNL,
9531 },
9532 {
9533 .cmd = NL80211_CMD_SET_WOWLAN,
9534 .doit = nl80211_set_wowlan,
9535 .policy = nl80211_policy,
9536 .flags = GENL_ADMIN_PERM,
9537 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9538 NL80211_FLAG_NEED_RTNL,
9539 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009540#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009541 {
9542 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9543 .doit = nl80211_set_rekey_data,
9544 .policy = nl80211_policy,
9545 .flags = GENL_ADMIN_PERM,
9546 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9547 NL80211_FLAG_NEED_RTNL,
9548 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009549 {
9550 .cmd = NL80211_CMD_TDLS_MGMT,
9551 .doit = nl80211_tdls_mgmt,
9552 .policy = nl80211_policy,
9553 .flags = GENL_ADMIN_PERM,
9554 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9555 NL80211_FLAG_NEED_RTNL,
9556 },
9557 {
9558 .cmd = NL80211_CMD_TDLS_OPER,
9559 .doit = nl80211_tdls_oper,
9560 .policy = nl80211_policy,
9561 .flags = GENL_ADMIN_PERM,
9562 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9563 NL80211_FLAG_NEED_RTNL,
9564 },
Johannes Berg28946da2011-11-04 11:18:12 +01009565 {
9566 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9567 .doit = nl80211_register_unexpected_frame,
9568 .policy = nl80211_policy,
9569 .flags = GENL_ADMIN_PERM,
9570 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9571 NL80211_FLAG_NEED_RTNL,
9572 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009573 {
9574 .cmd = NL80211_CMD_PROBE_CLIENT,
9575 .doit = nl80211_probe_client,
9576 .policy = nl80211_policy,
9577 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009578 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009579 NL80211_FLAG_NEED_RTNL,
9580 },
Johannes Berg5e760232011-11-04 11:18:17 +01009581 {
9582 .cmd = NL80211_CMD_REGISTER_BEACONS,
9583 .doit = nl80211_register_beacons,
9584 .policy = nl80211_policy,
9585 .flags = GENL_ADMIN_PERM,
9586 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9587 NL80211_FLAG_NEED_RTNL,
9588 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009589 {
9590 .cmd = NL80211_CMD_SET_NOACK_MAP,
9591 .doit = nl80211_set_noack_map,
9592 .policy = nl80211_policy,
9593 .flags = GENL_ADMIN_PERM,
9594 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9595 NL80211_FLAG_NEED_RTNL,
9596 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009597 {
9598 .cmd = NL80211_CMD_START_P2P_DEVICE,
9599 .doit = nl80211_start_p2p_device,
9600 .policy = nl80211_policy,
9601 .flags = GENL_ADMIN_PERM,
9602 .internal_flags = NL80211_FLAG_NEED_WDEV |
9603 NL80211_FLAG_NEED_RTNL,
9604 },
9605 {
9606 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9607 .doit = nl80211_stop_p2p_device,
9608 .policy = nl80211_policy,
9609 .flags = GENL_ADMIN_PERM,
9610 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9611 NL80211_FLAG_NEED_RTNL,
9612 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009613 {
9614 .cmd = NL80211_CMD_SET_MCAST_RATE,
9615 .doit = nl80211_set_mcast_rate,
9616 .policy = nl80211_policy,
9617 .flags = GENL_ADMIN_PERM,
9618 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9619 NL80211_FLAG_NEED_RTNL,
9620 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309621 {
9622 .cmd = NL80211_CMD_SET_MAC_ACL,
9623 .doit = nl80211_set_mac_acl,
9624 .policy = nl80211_policy,
9625 .flags = GENL_ADMIN_PERM,
9626 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9627 NL80211_FLAG_NEED_RTNL,
9628 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009629 {
9630 .cmd = NL80211_CMD_RADAR_DETECT,
9631 .doit = nl80211_start_radar_detection,
9632 .policy = nl80211_policy,
9633 .flags = GENL_ADMIN_PERM,
9634 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9635 NL80211_FLAG_NEED_RTNL,
9636 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009637 {
9638 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9639 .doit = nl80211_get_protocol_features,
9640 .policy = nl80211_policy,
9641 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009642 {
9643 .cmd = NL80211_CMD_UPDATE_FT_IES,
9644 .doit = nl80211_update_ft_ies,
9645 .policy = nl80211_policy,
9646 .flags = GENL_ADMIN_PERM,
9647 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9648 NL80211_FLAG_NEED_RTNL,
9649 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009650 {
9651 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9652 .doit = nl80211_crit_protocol_start,
9653 .policy = nl80211_policy,
9654 .flags = GENL_ADMIN_PERM,
9655 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9656 NL80211_FLAG_NEED_RTNL,
9657 },
9658 {
9659 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9660 .doit = nl80211_crit_protocol_stop,
9661 .policy = nl80211_policy,
9662 .flags = GENL_ADMIN_PERM,
9663 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9664 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009665 },
9666 {
9667 .cmd = NL80211_CMD_GET_COALESCE,
9668 .doit = nl80211_get_coalesce,
9669 .policy = nl80211_policy,
9670 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9671 NL80211_FLAG_NEED_RTNL,
9672 },
9673 {
9674 .cmd = NL80211_CMD_SET_COALESCE,
9675 .doit = nl80211_set_coalesce,
9676 .policy = nl80211_policy,
9677 .flags = GENL_ADMIN_PERM,
9678 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9679 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02009680 },
9681 {
9682 .cmd = NL80211_CMD_CHANNEL_SWITCH,
9683 .doit = nl80211_channel_switch,
9684 .policy = nl80211_policy,
9685 .flags = GENL_ADMIN_PERM,
9686 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9687 NL80211_FLAG_NEED_RTNL,
9688 },
Johannes Bergad7e7182013-11-13 13:37:47 +01009689 {
9690 .cmd = NL80211_CMD_VENDOR,
9691 .doit = nl80211_vendor_cmd,
9692 .policy = nl80211_policy,
9693 .flags = GENL_ADMIN_PERM,
9694 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9695 NL80211_FLAG_NEED_RTNL,
9696 },
Johannes Berg55682962007-09-20 13:09:35 -04009697};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009698
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009699static struct genl_multicast_group nl80211_mlme_mcgrp = {
9700 .name = "mlme",
9701};
Johannes Berg55682962007-09-20 13:09:35 -04009702
9703/* multicast groups */
9704static struct genl_multicast_group nl80211_config_mcgrp = {
9705 .name = "config",
9706};
Johannes Berg2a519312009-02-10 21:25:55 +01009707static struct genl_multicast_group nl80211_scan_mcgrp = {
9708 .name = "scan",
9709};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009710static struct genl_multicast_group nl80211_regulatory_mcgrp = {
9711 .name = "regulatory",
9712};
Johannes Berg55682962007-09-20 13:09:35 -04009713
9714/* notification functions */
9715
9716void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9717{
9718 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +02009719 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04009720
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009721 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009722 if (!msg)
9723 return;
9724
Johannes Berg86e8cf92013-06-19 10:57:22 +02009725 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04009726 nlmsg_free(msg);
9727 return;
9728 }
9729
Johannes Berg463d0182009-07-14 00:33:35 +02009730 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9731 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009732}
9733
Johannes Berg362a4152009-05-24 16:43:15 +02009734static int nl80211_add_scan_req(struct sk_buff *msg,
9735 struct cfg80211_registered_device *rdev)
9736{
9737 struct cfg80211_scan_request *req = rdev->scan_req;
9738 struct nlattr *nest;
9739 int i;
9740
9741 if (WARN_ON(!req))
9742 return 0;
9743
9744 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9745 if (!nest)
9746 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009747 for (i = 0; i < req->n_ssids; i++) {
9748 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9749 goto nla_put_failure;
9750 }
Johannes Berg362a4152009-05-24 16:43:15 +02009751 nla_nest_end(msg, nest);
9752
9753 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9754 if (!nest)
9755 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009756 for (i = 0; i < req->n_channels; i++) {
9757 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9758 goto nla_put_failure;
9759 }
Johannes Berg362a4152009-05-24 16:43:15 +02009760 nla_nest_end(msg, nest);
9761
David S. Miller9360ffd2012-03-29 04:41:26 -04009762 if (req->ie &&
9763 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9764 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02009765
Sam Lefflered4737712012-10-11 21:03:31 -07009766 if (req->flags)
9767 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
9768
Johannes Berg362a4152009-05-24 16:43:15 +02009769 return 0;
9770 nla_put_failure:
9771 return -ENOBUFS;
9772}
9773
Johannes Berga538e2d2009-06-16 19:56:42 +02009774static int nl80211_send_scan_msg(struct sk_buff *msg,
9775 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009776 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009777 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02009778 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01009779{
9780 void *hdr;
9781
Eric W. Biederman15e47302012-09-07 20:12:54 +00009782 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01009783 if (!hdr)
9784 return -1;
9785
David S. Miller9360ffd2012-03-29 04:41:26 -04009786 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009787 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9788 wdev->netdev->ifindex)) ||
9789 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009790 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009791
Johannes Berg362a4152009-05-24 16:43:15 +02009792 /* ignore errors and send incomplete event anyway */
9793 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009794
9795 return genlmsg_end(msg, hdr);
9796
9797 nla_put_failure:
9798 genlmsg_cancel(msg, hdr);
9799 return -EMSGSIZE;
9800}
9801
Luciano Coelho807f8a82011-05-11 17:09:35 +03009802static int
9803nl80211_send_sched_scan_msg(struct sk_buff *msg,
9804 struct cfg80211_registered_device *rdev,
9805 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009806 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009807{
9808 void *hdr;
9809
Eric W. Biederman15e47302012-09-07 20:12:54 +00009810 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009811 if (!hdr)
9812 return -1;
9813
David S. Miller9360ffd2012-03-29 04:41:26 -04009814 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9815 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9816 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009817
9818 return genlmsg_end(msg, hdr);
9819
9820 nla_put_failure:
9821 genlmsg_cancel(msg, hdr);
9822 return -EMSGSIZE;
9823}
9824
Johannes Berga538e2d2009-06-16 19:56:42 +02009825void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009826 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009827{
9828 struct sk_buff *msg;
9829
Thomas Graf58050fc2012-06-28 03:57:45 +00009830 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009831 if (!msg)
9832 return;
9833
Johannes Bergfd014282012-06-18 19:17:03 +02009834 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009835 NL80211_CMD_TRIGGER_SCAN) < 0) {
9836 nlmsg_free(msg);
9837 return;
9838 }
9839
Johannes Berg463d0182009-07-14 00:33:35 +02009840 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9841 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009842}
9843
Johannes Berg2a519312009-02-10 21:25:55 +01009844void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009845 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009846{
9847 struct sk_buff *msg;
9848
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009849 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009850 if (!msg)
9851 return;
9852
Johannes Bergfd014282012-06-18 19:17:03 +02009853 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009854 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009855 nlmsg_free(msg);
9856 return;
9857 }
9858
Johannes Berg463d0182009-07-14 00:33:35 +02009859 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9860 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009861}
9862
9863void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009864 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009865{
9866 struct sk_buff *msg;
9867
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009868 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009869 if (!msg)
9870 return;
9871
Johannes Bergfd014282012-06-18 19:17:03 +02009872 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009873 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009874 nlmsg_free(msg);
9875 return;
9876 }
9877
Johannes Berg463d0182009-07-14 00:33:35 +02009878 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9879 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009880}
9881
Luciano Coelho807f8a82011-05-11 17:09:35 +03009882void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9883 struct net_device *netdev)
9884{
9885 struct sk_buff *msg;
9886
9887 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9888 if (!msg)
9889 return;
9890
9891 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9892 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9893 nlmsg_free(msg);
9894 return;
9895 }
9896
9897 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9898 nl80211_scan_mcgrp.id, GFP_KERNEL);
9899}
9900
9901void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9902 struct net_device *netdev, u32 cmd)
9903{
9904 struct sk_buff *msg;
9905
Thomas Graf58050fc2012-06-28 03:57:45 +00009906 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009907 if (!msg)
9908 return;
9909
9910 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9911 nlmsg_free(msg);
9912 return;
9913 }
9914
9915 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9916 nl80211_scan_mcgrp.id, GFP_KERNEL);
9917}
9918
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009919/*
9920 * This can happen on global regulatory changes or device specific settings
9921 * based on custom world regulatory domains.
9922 */
9923void nl80211_send_reg_change_event(struct regulatory_request *request)
9924{
9925 struct sk_buff *msg;
9926 void *hdr;
9927
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009928 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009929 if (!msg)
9930 return;
9931
9932 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9933 if (!hdr) {
9934 nlmsg_free(msg);
9935 return;
9936 }
9937
9938 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04009939 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9940 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009941
David S. Miller9360ffd2012-03-29 04:41:26 -04009942 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9943 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9944 NL80211_REGDOM_TYPE_WORLD))
9945 goto nla_put_failure;
9946 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9947 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9948 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9949 goto nla_put_failure;
9950 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9951 request->intersect) {
9952 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9953 NL80211_REGDOM_TYPE_INTERSECTION))
9954 goto nla_put_failure;
9955 } else {
9956 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9957 NL80211_REGDOM_TYPE_COUNTRY) ||
9958 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9959 request->alpha2))
9960 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009961 }
9962
Johannes Bergf4173762012-12-03 18:23:37 +01009963 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04009964 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9965 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009966
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009967 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009968
Johannes Bergbc43b282009-07-25 10:54:13 +02009969 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009970 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009971 GFP_ATOMIC);
9972 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009973
9974 return;
9975
9976nla_put_failure:
9977 genlmsg_cancel(msg, hdr);
9978 nlmsg_free(msg);
9979}
9980
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009981static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9982 struct net_device *netdev,
9983 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009984 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009985{
9986 struct sk_buff *msg;
9987 void *hdr;
9988
Johannes Berge6d6e342009-07-01 21:26:47 +02009989 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009990 if (!msg)
9991 return;
9992
9993 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9994 if (!hdr) {
9995 nlmsg_free(msg);
9996 return;
9997 }
9998
David S. Miller9360ffd2012-03-29 04:41:26 -04009999 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10000 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10001 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10002 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010003
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010004 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010005
Johannes Berg463d0182009-07-14 00:33:35 +020010006 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10007 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010008 return;
10009
10010 nla_put_failure:
10011 genlmsg_cancel(msg, hdr);
10012 nlmsg_free(msg);
10013}
10014
10015void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010016 struct net_device *netdev, const u8 *buf,
10017 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010018{
10019 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010020 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010021}
10022
10023void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10024 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010025 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010026{
Johannes Berge6d6e342009-07-01 21:26:47 +020010027 nl80211_send_mlme_event(rdev, netdev, buf, len,
10028 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010029}
10030
Jouni Malinen53b46b82009-03-27 20:53:56 +020010031void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010032 struct net_device *netdev, const u8 *buf,
10033 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010034{
10035 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010036 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010037}
10038
Jouni Malinen53b46b82009-03-27 20:53:56 +020010039void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10040 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010041 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010042{
10043 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010044 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010045}
10046
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010047void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10048 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010049{
Johannes Berg947add32013-02-22 22:05:20 +010010050 struct wireless_dev *wdev = dev->ieee80211_ptr;
10051 struct wiphy *wiphy = wdev->wiphy;
10052 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010053 const struct ieee80211_mgmt *mgmt = (void *)buf;
10054 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010055
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010056 if (WARN_ON(len < 2))
10057 return;
10058
10059 if (ieee80211_is_deauth(mgmt->frame_control))
10060 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10061 else
10062 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10063
10064 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10065 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010066}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010067EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010068
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010069static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10070 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010071 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010072{
10073 struct sk_buff *msg;
10074 void *hdr;
10075
Johannes Berge6d6e342009-07-01 21:26:47 +020010076 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010077 if (!msg)
10078 return;
10079
10080 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10081 if (!hdr) {
10082 nlmsg_free(msg);
10083 return;
10084 }
10085
David S. Miller9360ffd2012-03-29 04:41:26 -040010086 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10087 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10088 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10089 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10090 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010091
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010092 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010093
Johannes Berg463d0182009-07-14 00:33:35 +020010094 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10095 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010096 return;
10097
10098 nla_put_failure:
10099 genlmsg_cancel(msg, hdr);
10100 nlmsg_free(msg);
10101}
10102
10103void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010104 struct net_device *netdev, const u8 *addr,
10105 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010106{
10107 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010108 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010109}
10110
10111void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010112 struct net_device *netdev, const u8 *addr,
10113 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010114{
Johannes Berge6d6e342009-07-01 21:26:47 +020010115 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10116 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010117}
10118
Samuel Ortizb23aa672009-07-01 21:26:54 +020010119void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10120 struct net_device *netdev, const u8 *bssid,
10121 const u8 *req_ie, size_t req_ie_len,
10122 const u8 *resp_ie, size_t resp_ie_len,
10123 u16 status, gfp_t gfp)
10124{
10125 struct sk_buff *msg;
10126 void *hdr;
10127
Thomas Graf58050fc2012-06-28 03:57:45 +000010128 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010129 if (!msg)
10130 return;
10131
10132 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10133 if (!hdr) {
10134 nlmsg_free(msg);
10135 return;
10136 }
10137
David S. Miller9360ffd2012-03-29 04:41:26 -040010138 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10139 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10140 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10141 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10142 (req_ie &&
10143 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10144 (resp_ie &&
10145 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10146 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010147
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010148 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010149
Johannes Berg463d0182009-07-14 00:33:35 +020010150 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10151 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010152 return;
10153
10154 nla_put_failure:
10155 genlmsg_cancel(msg, hdr);
10156 nlmsg_free(msg);
10157
10158}
10159
10160void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10161 struct net_device *netdev, const u8 *bssid,
10162 const u8 *req_ie, size_t req_ie_len,
10163 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10164{
10165 struct sk_buff *msg;
10166 void *hdr;
10167
Thomas Graf58050fc2012-06-28 03:57:45 +000010168 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010169 if (!msg)
10170 return;
10171
10172 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10173 if (!hdr) {
10174 nlmsg_free(msg);
10175 return;
10176 }
10177
David S. Miller9360ffd2012-03-29 04:41:26 -040010178 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10179 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10180 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10181 (req_ie &&
10182 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10183 (resp_ie &&
10184 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10185 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010186
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010187 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010188
Johannes Berg463d0182009-07-14 00:33:35 +020010189 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10190 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010191 return;
10192
10193 nla_put_failure:
10194 genlmsg_cancel(msg, hdr);
10195 nlmsg_free(msg);
10196
10197}
10198
10199void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10200 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +020010201 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010202{
10203 struct sk_buff *msg;
10204 void *hdr;
10205
Thomas Graf58050fc2012-06-28 03:57:45 +000010206 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010207 if (!msg)
10208 return;
10209
10210 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10211 if (!hdr) {
10212 nlmsg_free(msg);
10213 return;
10214 }
10215
David S. Miller9360ffd2012-03-29 04:41:26 -040010216 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10217 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10218 (from_ap && reason &&
10219 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10220 (from_ap &&
10221 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10222 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10223 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010224
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010225 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010226
Johannes Berg463d0182009-07-14 00:33:35 +020010227 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10228 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010229 return;
10230
10231 nla_put_failure:
10232 genlmsg_cancel(msg, hdr);
10233 nlmsg_free(msg);
10234
10235}
10236
Johannes Berg04a773a2009-04-19 21:24:32 +020010237void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10238 struct net_device *netdev, const u8 *bssid,
10239 gfp_t gfp)
10240{
10241 struct sk_buff *msg;
10242 void *hdr;
10243
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010244 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010245 if (!msg)
10246 return;
10247
10248 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10249 if (!hdr) {
10250 nlmsg_free(msg);
10251 return;
10252 }
10253
David S. Miller9360ffd2012-03-29 04:41:26 -040010254 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10255 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10256 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10257 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010258
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010259 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010260
Johannes Berg463d0182009-07-14 00:33:35 +020010261 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10262 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010263 return;
10264
10265 nla_put_failure:
10266 genlmsg_cancel(msg, hdr);
10267 nlmsg_free(msg);
10268}
10269
Johannes Berg947add32013-02-22 22:05:20 +010010270void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10271 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010272{
Johannes Berg947add32013-02-22 22:05:20 +010010273 struct wireless_dev *wdev = dev->ieee80211_ptr;
10274 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010275 struct sk_buff *msg;
10276 void *hdr;
10277
Johannes Berg947add32013-02-22 22:05:20 +010010278 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10279 return;
10280
10281 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10282
Javier Cardonac93b5e72011-04-07 15:08:34 -070010283 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10284 if (!msg)
10285 return;
10286
10287 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10288 if (!hdr) {
10289 nlmsg_free(msg);
10290 return;
10291 }
10292
David S. Miller9360ffd2012-03-29 04:41:26 -040010293 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010294 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10295 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010296 (ie_len && ie &&
10297 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10298 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010299
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010300 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010301
10302 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10303 nl80211_mlme_mcgrp.id, gfp);
10304 return;
10305
10306 nla_put_failure:
10307 genlmsg_cancel(msg, hdr);
10308 nlmsg_free(msg);
10309}
Johannes Berg947add32013-02-22 22:05:20 +010010310EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010311
Jouni Malinena3b8b052009-03-27 21:59:49 +020010312void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10313 struct net_device *netdev, const u8 *addr,
10314 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010315 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010316{
10317 struct sk_buff *msg;
10318 void *hdr;
10319
Johannes Berge6d6e342009-07-01 21:26:47 +020010320 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010321 if (!msg)
10322 return;
10323
10324 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10325 if (!hdr) {
10326 nlmsg_free(msg);
10327 return;
10328 }
10329
David S. Miller9360ffd2012-03-29 04:41:26 -040010330 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10331 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10332 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10333 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10334 (key_id != -1 &&
10335 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10336 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10337 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010338
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010339 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010340
Johannes Berg463d0182009-07-14 00:33:35 +020010341 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10342 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010343 return;
10344
10345 nla_put_failure:
10346 genlmsg_cancel(msg, hdr);
10347 nlmsg_free(msg);
10348}
10349
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010350void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10351 struct ieee80211_channel *channel_before,
10352 struct ieee80211_channel *channel_after)
10353{
10354 struct sk_buff *msg;
10355 void *hdr;
10356 struct nlattr *nl_freq;
10357
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010358 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010359 if (!msg)
10360 return;
10361
10362 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10363 if (!hdr) {
10364 nlmsg_free(msg);
10365 return;
10366 }
10367
10368 /*
10369 * Since we are applying the beacon hint to a wiphy we know its
10370 * wiphy_idx is valid
10371 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010372 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10373 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010374
10375 /* Before */
10376 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10377 if (!nl_freq)
10378 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010379 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010380 goto nla_put_failure;
10381 nla_nest_end(msg, nl_freq);
10382
10383 /* After */
10384 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10385 if (!nl_freq)
10386 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010387 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010388 goto nla_put_failure;
10389 nla_nest_end(msg, nl_freq);
10390
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010391 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010392
Johannes Berg463d0182009-07-14 00:33:35 +020010393 rcu_read_lock();
10394 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
10395 GFP_ATOMIC);
10396 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010397
10398 return;
10399
10400nla_put_failure:
10401 genlmsg_cancel(msg, hdr);
10402 nlmsg_free(msg);
10403}
10404
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010405static void nl80211_send_remain_on_chan_event(
10406 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010407 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010408 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010409 unsigned int duration, gfp_t gfp)
10410{
10411 struct sk_buff *msg;
10412 void *hdr;
10413
10414 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10415 if (!msg)
10416 return;
10417
10418 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10419 if (!hdr) {
10420 nlmsg_free(msg);
10421 return;
10422 }
10423
David S. Miller9360ffd2012-03-29 04:41:26 -040010424 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010425 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10426 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010427 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010428 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010429 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10430 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010431 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10432 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010433
David S. Miller9360ffd2012-03-29 04:41:26 -040010434 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10435 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10436 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010437
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010438 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010439
10440 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10441 nl80211_mlme_mcgrp.id, gfp);
10442 return;
10443
10444 nla_put_failure:
10445 genlmsg_cancel(msg, hdr);
10446 nlmsg_free(msg);
10447}
10448
Johannes Berg947add32013-02-22 22:05:20 +010010449void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10450 struct ieee80211_channel *chan,
10451 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010452{
Johannes Berg947add32013-02-22 22:05:20 +010010453 struct wiphy *wiphy = wdev->wiphy;
10454 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10455
10456 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010457 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010458 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010459 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010460}
Johannes Berg947add32013-02-22 22:05:20 +010010461EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010462
Johannes Berg947add32013-02-22 22:05:20 +010010463void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10464 struct ieee80211_channel *chan,
10465 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010466{
Johannes Berg947add32013-02-22 22:05:20 +010010467 struct wiphy *wiphy = wdev->wiphy;
10468 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10469
10470 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010471 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010472 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010473}
Johannes Berg947add32013-02-22 22:05:20 +010010474EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010475
Johannes Berg947add32013-02-22 22:05:20 +010010476void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10477 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010478{
Johannes Berg947add32013-02-22 22:05:20 +010010479 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10480 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010481 struct sk_buff *msg;
10482
Johannes Berg947add32013-02-22 22:05:20 +010010483 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10484
Thomas Graf58050fc2012-06-28 03:57:45 +000010485 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010486 if (!msg)
10487 return;
10488
John W. Linville66266b32012-03-15 13:25:41 -040010489 if (nl80211_send_station(msg, 0, 0, 0,
10490 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010491 nlmsg_free(msg);
10492 return;
10493 }
10494
10495 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10496 nl80211_mlme_mcgrp.id, gfp);
10497}
Johannes Berg947add32013-02-22 22:05:20 +010010498EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010499
Johannes Berg947add32013-02-22 22:05:20 +010010500void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010501{
Johannes Berg947add32013-02-22 22:05:20 +010010502 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10503 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010504 struct sk_buff *msg;
10505 void *hdr;
10506
Johannes Berg947add32013-02-22 22:05:20 +010010507 trace_cfg80211_del_sta(dev, mac_addr);
10508
Thomas Graf58050fc2012-06-28 03:57:45 +000010509 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010510 if (!msg)
10511 return;
10512
10513 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10514 if (!hdr) {
10515 nlmsg_free(msg);
10516 return;
10517 }
10518
David S. Miller9360ffd2012-03-29 04:41:26 -040010519 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10520 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10521 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010522
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010523 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010524
10525 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10526 nl80211_mlme_mcgrp.id, gfp);
10527 return;
10528
10529 nla_put_failure:
10530 genlmsg_cancel(msg, hdr);
10531 nlmsg_free(msg);
10532}
Johannes Berg947add32013-02-22 22:05:20 +010010533EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010534
Johannes Berg947add32013-02-22 22:05:20 +010010535void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10536 enum nl80211_connect_failed_reason reason,
10537 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010538{
Johannes Berg947add32013-02-22 22:05:20 +010010539 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10540 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010541 struct sk_buff *msg;
10542 void *hdr;
10543
10544 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10545 if (!msg)
10546 return;
10547
10548 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10549 if (!hdr) {
10550 nlmsg_free(msg);
10551 return;
10552 }
10553
10554 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10555 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10556 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10557 goto nla_put_failure;
10558
10559 genlmsg_end(msg, hdr);
10560
10561 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10562 nl80211_mlme_mcgrp.id, gfp);
10563 return;
10564
10565 nla_put_failure:
10566 genlmsg_cancel(msg, hdr);
10567 nlmsg_free(msg);
10568}
Johannes Berg947add32013-02-22 22:05:20 +010010569EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010570
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010571static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10572 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010573{
10574 struct wireless_dev *wdev = dev->ieee80211_ptr;
10575 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10576 struct sk_buff *msg;
10577 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010578 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010579
Eric W. Biederman15e47302012-09-07 20:12:54 +000010580 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010581 return false;
10582
10583 msg = nlmsg_new(100, gfp);
10584 if (!msg)
10585 return true;
10586
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010587 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010588 if (!hdr) {
10589 nlmsg_free(msg);
10590 return true;
10591 }
10592
David S. Miller9360ffd2012-03-29 04:41:26 -040010593 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10594 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10595 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10596 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010597
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010598 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010599 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010600 return true;
10601
10602 nla_put_failure:
10603 genlmsg_cancel(msg, hdr);
10604 nlmsg_free(msg);
10605 return true;
10606}
10607
Johannes Berg947add32013-02-22 22:05:20 +010010608bool cfg80211_rx_spurious_frame(struct net_device *dev,
10609 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010610{
Johannes Berg947add32013-02-22 22:05:20 +010010611 struct wireless_dev *wdev = dev->ieee80211_ptr;
10612 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010613
Johannes Berg947add32013-02-22 22:05:20 +010010614 trace_cfg80211_rx_spurious_frame(dev, addr);
10615
10616 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10617 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10618 trace_cfg80211_return_bool(false);
10619 return false;
10620 }
10621 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10622 addr, gfp);
10623 trace_cfg80211_return_bool(ret);
10624 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010625}
Johannes Berg947add32013-02-22 22:05:20 +010010626EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10627
10628bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10629 const u8 *addr, gfp_t gfp)
10630{
10631 struct wireless_dev *wdev = dev->ieee80211_ptr;
10632 bool ret;
10633
10634 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10635
10636 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10637 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10638 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10639 trace_cfg80211_return_bool(false);
10640 return false;
10641 }
10642 ret = __nl80211_unexpected_frame(dev,
10643 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10644 addr, gfp);
10645 trace_cfg80211_return_bool(ret);
10646 return ret;
10647}
10648EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010649
Johannes Berg2e161f72010-08-12 15:38:38 +020010650int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010651 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010652 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010653 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010654{
Johannes Berg71bbc992012-06-15 15:30:18 +020010655 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010656 struct sk_buff *msg;
10657 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010658
10659 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10660 if (!msg)
10661 return -ENOMEM;
10662
Johannes Berg2e161f72010-08-12 15:38:38 +020010663 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010664 if (!hdr) {
10665 nlmsg_free(msg);
10666 return -ENOMEM;
10667 }
10668
David S. Miller9360ffd2012-03-29 04:41:26 -040010669 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010670 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10671 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010672 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010673 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10674 (sig_dbm &&
10675 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010676 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10677 (flags &&
10678 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010679 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010680
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010681 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010682
Eric W. Biederman15e47302012-09-07 20:12:54 +000010683 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010684
10685 nla_put_failure:
10686 genlmsg_cancel(msg, hdr);
10687 nlmsg_free(msg);
10688 return -ENOBUFS;
10689}
10690
Johannes Berg947add32013-02-22 22:05:20 +010010691void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10692 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010693{
Johannes Berg947add32013-02-22 22:05:20 +010010694 struct wiphy *wiphy = wdev->wiphy;
10695 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010696 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010697 struct sk_buff *msg;
10698 void *hdr;
10699
Johannes Berg947add32013-02-22 22:05:20 +010010700 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10701
Jouni Malinen026331c2010-02-15 12:53:10 +020010702 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10703 if (!msg)
10704 return;
10705
Johannes Berg2e161f72010-08-12 15:38:38 +020010706 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010707 if (!hdr) {
10708 nlmsg_free(msg);
10709 return;
10710 }
10711
David S. Miller9360ffd2012-03-29 04:41:26 -040010712 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010713 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10714 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010715 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010716 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10717 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10718 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10719 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010720
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010721 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010722
Michal Kaziora0ec5702013-06-25 09:17:17 +020010723 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10724 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020010725 return;
10726
10727 nla_put_failure:
10728 genlmsg_cancel(msg, hdr);
10729 nlmsg_free(msg);
10730}
Johannes Berg947add32013-02-22 22:05:20 +010010731EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020010732
Johannes Berg947add32013-02-22 22:05:20 +010010733void cfg80211_cqm_rssi_notify(struct net_device *dev,
10734 enum nl80211_cqm_rssi_threshold_event rssi_event,
10735 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010736{
Johannes Berg947add32013-02-22 22:05:20 +010010737 struct wireless_dev *wdev = dev->ieee80211_ptr;
10738 struct wiphy *wiphy = wdev->wiphy;
10739 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010740 struct sk_buff *msg;
10741 struct nlattr *pinfoattr;
10742 void *hdr;
10743
Johannes Berg947add32013-02-22 22:05:20 +010010744 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10745
Thomas Graf58050fc2012-06-28 03:57:45 +000010746 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010747 if (!msg)
10748 return;
10749
10750 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10751 if (!hdr) {
10752 nlmsg_free(msg);
10753 return;
10754 }
10755
David S. Miller9360ffd2012-03-29 04:41:26 -040010756 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010757 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040010758 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010759
10760 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10761 if (!pinfoattr)
10762 goto nla_put_failure;
10763
David S. Miller9360ffd2012-03-29 04:41:26 -040010764 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10765 rssi_event))
10766 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010767
10768 nla_nest_end(msg, pinfoattr);
10769
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010770 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010771
10772 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10773 nl80211_mlme_mcgrp.id, gfp);
10774 return;
10775
10776 nla_put_failure:
10777 genlmsg_cancel(msg, hdr);
10778 nlmsg_free(msg);
10779}
Johannes Berg947add32013-02-22 22:05:20 +010010780EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010781
Johannes Berg947add32013-02-22 22:05:20 +010010782static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10783 struct net_device *netdev, const u8 *bssid,
10784 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010785{
10786 struct sk_buff *msg;
10787 struct nlattr *rekey_attr;
10788 void *hdr;
10789
Thomas Graf58050fc2012-06-28 03:57:45 +000010790 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010791 if (!msg)
10792 return;
10793
10794 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10795 if (!hdr) {
10796 nlmsg_free(msg);
10797 return;
10798 }
10799
David S. Miller9360ffd2012-03-29 04:41:26 -040010800 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10801 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10802 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10803 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010804
10805 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10806 if (!rekey_attr)
10807 goto nla_put_failure;
10808
David S. Miller9360ffd2012-03-29 04:41:26 -040010809 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10810 NL80211_REPLAY_CTR_LEN, replay_ctr))
10811 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010812
10813 nla_nest_end(msg, rekey_attr);
10814
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010815 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010816
10817 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10818 nl80211_mlme_mcgrp.id, gfp);
10819 return;
10820
10821 nla_put_failure:
10822 genlmsg_cancel(msg, hdr);
10823 nlmsg_free(msg);
10824}
10825
Johannes Berg947add32013-02-22 22:05:20 +010010826void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10827 const u8 *replay_ctr, gfp_t gfp)
10828{
10829 struct wireless_dev *wdev = dev->ieee80211_ptr;
10830 struct wiphy *wiphy = wdev->wiphy;
10831 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10832
10833 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10834 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10835}
10836EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10837
10838static void
10839nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10840 struct net_device *netdev, int index,
10841 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010842{
10843 struct sk_buff *msg;
10844 struct nlattr *attr;
10845 void *hdr;
10846
Thomas Graf58050fc2012-06-28 03:57:45 +000010847 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010848 if (!msg)
10849 return;
10850
10851 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10852 if (!hdr) {
10853 nlmsg_free(msg);
10854 return;
10855 }
10856
David S. Miller9360ffd2012-03-29 04:41:26 -040010857 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10858 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10859 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010860
10861 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10862 if (!attr)
10863 goto nla_put_failure;
10864
David S. Miller9360ffd2012-03-29 04:41:26 -040010865 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10866 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10867 (preauth &&
10868 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10869 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010870
10871 nla_nest_end(msg, attr);
10872
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010873 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010874
10875 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10876 nl80211_mlme_mcgrp.id, gfp);
10877 return;
10878
10879 nla_put_failure:
10880 genlmsg_cancel(msg, hdr);
10881 nlmsg_free(msg);
10882}
10883
Johannes Berg947add32013-02-22 22:05:20 +010010884void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10885 const u8 *bssid, bool preauth, gfp_t gfp)
10886{
10887 struct wireless_dev *wdev = dev->ieee80211_ptr;
10888 struct wiphy *wiphy = wdev->wiphy;
10889 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10890
10891 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10892 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10893}
10894EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10895
10896static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10897 struct net_device *netdev,
10898 struct cfg80211_chan_def *chandef,
10899 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070010900{
10901 struct sk_buff *msg;
10902 void *hdr;
10903
Thomas Graf58050fc2012-06-28 03:57:45 +000010904 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070010905 if (!msg)
10906 return;
10907
10908 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10909 if (!hdr) {
10910 nlmsg_free(msg);
10911 return;
10912 }
10913
Johannes Berg683b6d32012-11-08 21:25:48 +010010914 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10915 goto nla_put_failure;
10916
10917 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040010918 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070010919
10920 genlmsg_end(msg, hdr);
10921
10922 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10923 nl80211_mlme_mcgrp.id, gfp);
10924 return;
10925
10926 nla_put_failure:
10927 genlmsg_cancel(msg, hdr);
10928 nlmsg_free(msg);
10929}
10930
Johannes Berg947add32013-02-22 22:05:20 +010010931void cfg80211_ch_switch_notify(struct net_device *dev,
10932 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070010933{
Johannes Berg947add32013-02-22 22:05:20 +010010934 struct wireless_dev *wdev = dev->ieee80211_ptr;
10935 struct wiphy *wiphy = wdev->wiphy;
10936 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10937
Simon Wunderliche487eae2013-11-21 18:19:51 +010010938 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010010939
Simon Wunderliche487eae2013-11-21 18:19:51 +010010940 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010010941
10942 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020010943 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070010944 wdev->iftype != NL80211_IFTYPE_ADHOC &&
10945 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010010946 return;
Johannes Berg947add32013-02-22 22:05:20 +010010947
10948 wdev->channel = chandef->chan;
10949 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010010950}
10951EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10952
10953void cfg80211_cqm_txe_notify(struct net_device *dev,
10954 const u8 *peer, u32 num_packets,
10955 u32 rate, u32 intvl, gfp_t gfp)
10956{
10957 struct wireless_dev *wdev = dev->ieee80211_ptr;
10958 struct wiphy *wiphy = wdev->wiphy;
10959 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010960 struct sk_buff *msg;
10961 struct nlattr *pinfoattr;
10962 void *hdr;
10963
10964 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10965 if (!msg)
10966 return;
10967
10968 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10969 if (!hdr) {
10970 nlmsg_free(msg);
10971 return;
10972 }
10973
10974 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010975 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070010976 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10977 goto nla_put_failure;
10978
10979 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10980 if (!pinfoattr)
10981 goto nla_put_failure;
10982
10983 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10984 goto nla_put_failure;
10985
10986 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10987 goto nla_put_failure;
10988
10989 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10990 goto nla_put_failure;
10991
10992 nla_nest_end(msg, pinfoattr);
10993
10994 genlmsg_end(msg, hdr);
10995
10996 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10997 nl80211_mlme_mcgrp.id, gfp);
10998 return;
10999
11000 nla_put_failure:
11001 genlmsg_cancel(msg, hdr);
11002 nlmsg_free(msg);
11003}
Johannes Berg947add32013-02-22 22:05:20 +010011004EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011005
11006void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011007nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011008 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011009 enum nl80211_radar_event event,
11010 struct net_device *netdev, gfp_t gfp)
11011{
11012 struct sk_buff *msg;
11013 void *hdr;
11014
11015 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11016 if (!msg)
11017 return;
11018
11019 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11020 if (!hdr) {
11021 nlmsg_free(msg);
11022 return;
11023 }
11024
11025 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11026 goto nla_put_failure;
11027
11028 /* NOP and radar events don't need a netdev parameter */
11029 if (netdev) {
11030 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11031
11032 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11033 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11034 goto nla_put_failure;
11035 }
11036
11037 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11038 goto nla_put_failure;
11039
11040 if (nl80211_send_chandef(msg, chandef))
11041 goto nla_put_failure;
11042
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011043 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011044
11045 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11046 nl80211_mlme_mcgrp.id, gfp);
11047 return;
11048
11049 nla_put_failure:
11050 genlmsg_cancel(msg, hdr);
11051 nlmsg_free(msg);
11052}
11053
Johannes Berg947add32013-02-22 22:05:20 +010011054void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11055 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011056{
Johannes Berg947add32013-02-22 22:05:20 +010011057 struct wireless_dev *wdev = dev->ieee80211_ptr;
11058 struct wiphy *wiphy = wdev->wiphy;
11059 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011060 struct sk_buff *msg;
11061 struct nlattr *pinfoattr;
11062 void *hdr;
11063
Johannes Berg947add32013-02-22 22:05:20 +010011064 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11065
Thomas Graf58050fc2012-06-28 03:57:45 +000011066 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011067 if (!msg)
11068 return;
11069
11070 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11071 if (!hdr) {
11072 nlmsg_free(msg);
11073 return;
11074 }
11075
David S. Miller9360ffd2012-03-29 04:41:26 -040011076 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011077 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011078 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11079 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011080
11081 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11082 if (!pinfoattr)
11083 goto nla_put_failure;
11084
David S. Miller9360ffd2012-03-29 04:41:26 -040011085 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11086 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011087
11088 nla_nest_end(msg, pinfoattr);
11089
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011090 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011091
11092 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11093 nl80211_mlme_mcgrp.id, gfp);
11094 return;
11095
11096 nla_put_failure:
11097 genlmsg_cancel(msg, hdr);
11098 nlmsg_free(msg);
11099}
Johannes Berg947add32013-02-22 22:05:20 +010011100EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011101
Johannes Berg7f6cf312011-11-04 11:18:15 +010011102void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11103 u64 cookie, bool acked, gfp_t gfp)
11104{
11105 struct wireless_dev *wdev = dev->ieee80211_ptr;
11106 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11107 struct sk_buff *msg;
11108 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011109
Beni Lev4ee3e062012-08-27 12:49:39 +030011110 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11111
Thomas Graf58050fc2012-06-28 03:57:45 +000011112 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011113
Johannes Berg7f6cf312011-11-04 11:18:15 +010011114 if (!msg)
11115 return;
11116
11117 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11118 if (!hdr) {
11119 nlmsg_free(msg);
11120 return;
11121 }
11122
David S. Miller9360ffd2012-03-29 04:41:26 -040011123 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11124 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11125 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11126 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11127 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11128 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011129
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011130 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011131
11132 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11133 nl80211_mlme_mcgrp.id, gfp);
11134 return;
11135
11136 nla_put_failure:
11137 genlmsg_cancel(msg, hdr);
11138 nlmsg_free(msg);
11139}
11140EXPORT_SYMBOL(cfg80211_probe_status);
11141
Johannes Berg5e760232011-11-04 11:18:17 +010011142void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11143 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011144 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010011145{
11146 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11147 struct sk_buff *msg;
11148 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011149 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010011150
Beni Lev4ee3e062012-08-27 12:49:39 +030011151 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11152
Ben Greear37c73b52012-10-26 14:49:25 -070011153 spin_lock_bh(&rdev->beacon_registrations_lock);
11154 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11155 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11156 if (!msg) {
11157 spin_unlock_bh(&rdev->beacon_registrations_lock);
11158 return;
11159 }
Johannes Berg5e760232011-11-04 11:18:17 +010011160
Ben Greear37c73b52012-10-26 14:49:25 -070011161 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11162 if (!hdr)
11163 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010011164
Ben Greear37c73b52012-10-26 14:49:25 -070011165 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11166 (freq &&
11167 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11168 (sig_dbm &&
11169 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11170 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11171 goto nla_put_failure;
11172
11173 genlmsg_end(msg, hdr);
11174
11175 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010011176 }
Ben Greear37c73b52012-10-26 14:49:25 -070011177 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010011178 return;
11179
11180 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011181 spin_unlock_bh(&rdev->beacon_registrations_lock);
11182 if (hdr)
11183 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010011184 nlmsg_free(msg);
11185}
11186EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11187
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011188#ifdef CONFIG_PM
11189void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11190 struct cfg80211_wowlan_wakeup *wakeup,
11191 gfp_t gfp)
11192{
11193 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11194 struct sk_buff *msg;
11195 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011196 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011197
11198 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11199
11200 if (wakeup)
11201 size += wakeup->packet_present_len;
11202
11203 msg = nlmsg_new(size, gfp);
11204 if (!msg)
11205 return;
11206
11207 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11208 if (!hdr)
11209 goto free_msg;
11210
11211 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11212 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11213 goto free_msg;
11214
11215 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11216 wdev->netdev->ifindex))
11217 goto free_msg;
11218
11219 if (wakeup) {
11220 struct nlattr *reasons;
11221
11222 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
11223
11224 if (wakeup->disconnect &&
11225 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11226 goto free_msg;
11227 if (wakeup->magic_pkt &&
11228 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11229 goto free_msg;
11230 if (wakeup->gtk_rekey_failure &&
11231 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11232 goto free_msg;
11233 if (wakeup->eap_identity_req &&
11234 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11235 goto free_msg;
11236 if (wakeup->four_way_handshake &&
11237 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11238 goto free_msg;
11239 if (wakeup->rfkill_release &&
11240 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11241 goto free_msg;
11242
11243 if (wakeup->pattern_idx >= 0 &&
11244 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11245 wakeup->pattern_idx))
11246 goto free_msg;
11247
Johannes Berg2a0e0472013-01-23 22:57:40 +010011248 if (wakeup->tcp_match)
11249 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
11250
11251 if (wakeup->tcp_connlost)
11252 nla_put_flag(msg,
11253 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
11254
11255 if (wakeup->tcp_nomoretokens)
11256 nla_put_flag(msg,
11257 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
11258
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011259 if (wakeup->packet) {
11260 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11261 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11262
11263 if (!wakeup->packet_80211) {
11264 pkt_attr =
11265 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11266 len_attr =
11267 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11268 }
11269
11270 if (wakeup->packet_len &&
11271 nla_put_u32(msg, len_attr, wakeup->packet_len))
11272 goto free_msg;
11273
11274 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11275 wakeup->packet))
11276 goto free_msg;
11277 }
11278
11279 nla_nest_end(msg, reasons);
11280 }
11281
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011282 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011283
11284 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11285 nl80211_mlme_mcgrp.id, gfp);
11286 return;
11287
11288 free_msg:
11289 nlmsg_free(msg);
11290}
11291EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11292#endif
11293
Jouni Malinen3475b092012-11-16 22:49:57 +020011294void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11295 enum nl80211_tdls_operation oper,
11296 u16 reason_code, gfp_t gfp)
11297{
11298 struct wireless_dev *wdev = dev->ieee80211_ptr;
11299 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11300 struct sk_buff *msg;
11301 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011302
11303 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11304 reason_code);
11305
11306 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11307 if (!msg)
11308 return;
11309
11310 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11311 if (!hdr) {
11312 nlmsg_free(msg);
11313 return;
11314 }
11315
11316 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11317 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11318 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11319 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11320 (reason_code > 0 &&
11321 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11322 goto nla_put_failure;
11323
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011324 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011325
11326 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11327 nl80211_mlme_mcgrp.id, gfp);
11328 return;
11329
11330 nla_put_failure:
11331 genlmsg_cancel(msg, hdr);
11332 nlmsg_free(msg);
11333}
11334EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11335
Jouni Malinen026331c2010-02-15 12:53:10 +020011336static int nl80211_netlink_notify(struct notifier_block * nb,
11337 unsigned long state,
11338 void *_notify)
11339{
11340 struct netlink_notify *notify = _notify;
11341 struct cfg80211_registered_device *rdev;
11342 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011343 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011344
11345 if (state != NETLINK_URELEASE)
11346 return NOTIFY_DONE;
11347
11348 rcu_read_lock();
11349
Johannes Berg5e760232011-11-04 11:18:17 +010011350 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020011351 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000011352 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011353
11354 spin_lock_bh(&rdev->beacon_registrations_lock);
11355 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11356 list) {
11357 if (reg->nlportid == notify->portid) {
11358 list_del(&reg->list);
11359 kfree(reg);
11360 break;
11361 }
11362 }
11363 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010011364 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011365
11366 rcu_read_unlock();
11367
11368 return NOTIFY_DONE;
11369}
11370
11371static struct notifier_block nl80211_netlink_notifier = {
11372 .notifier_call = nl80211_netlink_notify,
11373};
11374
Jouni Malinen355199e2013-02-27 17:14:27 +020011375void cfg80211_ft_event(struct net_device *netdev,
11376 struct cfg80211_ft_event_params *ft_event)
11377{
11378 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11379 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11380 struct sk_buff *msg;
11381 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011382
11383 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11384
11385 if (!ft_event->target_ap)
11386 return;
11387
11388 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11389 if (!msg)
11390 return;
11391
11392 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
11393 if (!hdr) {
11394 nlmsg_free(msg);
11395 return;
11396 }
11397
11398 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
11399 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
11400 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
11401 if (ft_event->ies)
11402 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
11403 if (ft_event->ric_ies)
11404 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11405 ft_event->ric_ies);
11406
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011407 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011408
11409 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11410 nl80211_mlme_mcgrp.id, GFP_KERNEL);
11411}
11412EXPORT_SYMBOL(cfg80211_ft_event);
11413
Arend van Spriel5de17982013-04-18 15:49:00 +020011414void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11415{
11416 struct cfg80211_registered_device *rdev;
11417 struct sk_buff *msg;
11418 void *hdr;
11419 u32 nlportid;
11420
11421 rdev = wiphy_to_dev(wdev->wiphy);
11422 if (!rdev->crit_proto_nlportid)
11423 return;
11424
11425 nlportid = rdev->crit_proto_nlportid;
11426 rdev->crit_proto_nlportid = 0;
11427
11428 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11429 if (!msg)
11430 return;
11431
11432 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11433 if (!hdr)
11434 goto nla_put_failure;
11435
11436 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11437 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11438 goto nla_put_failure;
11439
11440 genlmsg_end(msg, hdr);
11441
11442 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11443 return;
11444
11445 nla_put_failure:
11446 if (hdr)
11447 genlmsg_cancel(msg, hdr);
11448 nlmsg_free(msg);
11449
11450}
11451EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11452
Johannes Berg55682962007-09-20 13:09:35 -040011453/* initialisation/exit functions */
11454
11455int nl80211_init(void)
11456{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011457 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011458
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011459 err = genl_register_family_with_ops(&nl80211_fam,
11460 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040011461 if (err)
11462 return err;
11463
Johannes Berg55682962007-09-20 13:09:35 -040011464 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
11465 if (err)
11466 goto err_out;
11467
Johannes Berg2a519312009-02-10 21:25:55 +010011468 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
11469 if (err)
11470 goto err_out;
11471
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040011472 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
11473 if (err)
11474 goto err_out;
11475
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011476 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
11477 if (err)
11478 goto err_out;
11479
Johannes Bergaff89a92009-07-01 21:26:51 +020011480#ifdef CONFIG_NL80211_TESTMODE
11481 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
11482 if (err)
11483 goto err_out;
11484#endif
11485
Jouni Malinen026331c2010-02-15 12:53:10 +020011486 err = netlink_register_notifier(&nl80211_netlink_notifier);
11487 if (err)
11488 goto err_out;
11489
Johannes Berg55682962007-09-20 13:09:35 -040011490 return 0;
11491 err_out:
11492 genl_unregister_family(&nl80211_fam);
11493 return err;
11494}
11495
11496void nl80211_exit(void)
11497{
Jouni Malinen026331c2010-02-15 12:53:10 +020011498 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011499 genl_unregister_family(&nl80211_fam);
11500}