blob: 444f5effb77f4a5975898f1659885260aee570a5 [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 Malinendc6382c2009-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 },
Johannes Berg55682962007-09-20 13:09:35 -0400352};
353
Johannes Berge31b8212010-10-05 19:39:30 +0200354/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000355static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200356 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200357 [NL80211_KEY_IDX] = { .type = NLA_U8 },
358 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200359 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200360 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
361 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200362 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100363 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
364};
365
366/* policy for the key default flags */
367static const struct nla_policy
368nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
369 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
370 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200371};
372
Johannes Bergff1b6e62011-05-04 15:37:28 +0200373/* policy for WoWLAN attributes */
374static const struct nla_policy
375nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
376 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
377 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
378 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
379 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200380 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
381 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
382 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
383 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100384 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
385};
386
387static const struct nla_policy
388nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
389 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
390 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
391 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
392 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
393 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
394 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
395 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
396 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
397 },
398 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
399 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
400 },
401 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
402 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
403 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200404};
405
Johannes Berge5497d72011-07-05 16:35:40 +0200406/* policy for GTK rekey offload attributes */
407static const struct nla_policy
408nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
409 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
410 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
411 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
412};
413
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300414static const struct nla_policy
415nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200416 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300417 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700418 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300419};
420
Johannes Berg97990a02013-04-19 01:02:55 +0200421static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
422 struct netlink_callback *cb,
423 struct cfg80211_registered_device **rdev,
424 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100425{
Johannes Berg67748892010-10-04 21:14:06 +0200426 int err;
427
Johannes Berg67748892010-10-04 21:14:06 +0200428 rtnl_lock();
429
Johannes Berg97990a02013-04-19 01:02:55 +0200430 if (!cb->args[0]) {
431 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
432 nl80211_fam.attrbuf, nl80211_fam.maxattr,
433 nl80211_policy);
434 if (err)
435 goto out_unlock;
436
437 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
438 nl80211_fam.attrbuf);
439 if (IS_ERR(*wdev)) {
440 err = PTR_ERR(*wdev);
441 goto out_unlock;
442 }
443 *rdev = wiphy_to_dev((*wdev)->wiphy);
444 cb->args[0] = (*rdev)->wiphy_idx;
445 cb->args[1] = (*wdev)->identifier;
446 } else {
447 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0]);
448 struct wireless_dev *tmp;
449
450 if (!wiphy) {
451 err = -ENODEV;
452 goto out_unlock;
453 }
454 *rdev = wiphy_to_dev(wiphy);
455 *wdev = NULL;
456
Johannes Berg97990a02013-04-19 01:02:55 +0200457 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
458 if (tmp->identifier == cb->args[1]) {
459 *wdev = tmp;
460 break;
461 }
462 }
Johannes Berg97990a02013-04-19 01:02:55 +0200463
464 if (!*wdev) {
465 err = -ENODEV;
466 goto out_unlock;
467 }
Johannes Berg67748892010-10-04 21:14:06 +0200468 }
469
Johannes Berg67748892010-10-04 21:14:06 +0200470 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200471 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200472 rtnl_unlock();
473 return err;
474}
475
Johannes Berg97990a02013-04-19 01:02:55 +0200476static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200477{
Johannes Berg67748892010-10-04 21:14:06 +0200478 rtnl_unlock();
479}
480
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100481/* IE validation */
482static bool is_valid_ie_attr(const struct nlattr *attr)
483{
484 const u8 *pos;
485 int len;
486
487 if (!attr)
488 return true;
489
490 pos = nla_data(attr);
491 len = nla_len(attr);
492
493 while (len) {
494 u8 elemlen;
495
496 if (len < 2)
497 return false;
498 len -= 2;
499
500 elemlen = pos[1];
501 if (elemlen > len)
502 return false;
503
504 len -= elemlen;
505 pos += 2 + elemlen;
506 }
507
508 return true;
509}
510
Johannes Berg55682962007-09-20 13:09:35 -0400511/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000512static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400513 int flags, u8 cmd)
514{
515 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000516 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400517}
518
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400519static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100520 struct ieee80211_channel *chan,
521 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400522{
David S. Miller9360ffd2012-03-29 04:41:26 -0400523 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
524 chan->center_freq))
525 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400526
David S. Miller9360ffd2012-03-29 04:41:26 -0400527 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
528 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
529 goto nla_put_failure;
530 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
531 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
532 goto nla_put_failure;
533 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
534 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
535 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100536 if (chan->flags & IEEE80211_CHAN_RADAR) {
537 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
538 goto nla_put_failure;
539 if (large) {
540 u32 time;
541
542 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
543
544 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
545 chan->dfs_state))
546 goto nla_put_failure;
547 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
548 time))
549 goto nla_put_failure;
550 }
551 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400552
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100553 if (large) {
554 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
555 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
556 goto nla_put_failure;
557 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
558 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
559 goto nla_put_failure;
560 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
561 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
562 goto nla_put_failure;
563 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
564 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
565 goto nla_put_failure;
566 }
567
David S. Miller9360ffd2012-03-29 04:41:26 -0400568 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
569 DBM_TO_MBM(chan->max_power)))
570 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400571
572 return 0;
573
574 nla_put_failure:
575 return -ENOBUFS;
576}
577
Johannes Berg55682962007-09-20 13:09:35 -0400578/* netlink command implementations */
579
Johannes Bergb9454e82009-07-08 13:29:08 +0200580struct key_parse {
581 struct key_params p;
582 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200583 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200584 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100585 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200586};
587
588static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
589{
590 struct nlattr *tb[NL80211_KEY_MAX + 1];
591 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
592 nl80211_key_policy);
593 if (err)
594 return err;
595
596 k->def = !!tb[NL80211_KEY_DEFAULT];
597 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
598
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100599 if (k->def) {
600 k->def_uni = true;
601 k->def_multi = true;
602 }
603 if (k->defmgmt)
604 k->def_multi = true;
605
Johannes Bergb9454e82009-07-08 13:29:08 +0200606 if (tb[NL80211_KEY_IDX])
607 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
608
609 if (tb[NL80211_KEY_DATA]) {
610 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
611 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
612 }
613
614 if (tb[NL80211_KEY_SEQ]) {
615 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
616 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
617 }
618
619 if (tb[NL80211_KEY_CIPHER])
620 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
621
Johannes Berge31b8212010-10-05 19:39:30 +0200622 if (tb[NL80211_KEY_TYPE]) {
623 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
624 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
625 return -EINVAL;
626 }
627
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100628 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
629 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100630 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
631 tb[NL80211_KEY_DEFAULT_TYPES],
632 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100633 if (err)
634 return err;
635
636 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
637 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
638 }
639
Johannes Bergb9454e82009-07-08 13:29:08 +0200640 return 0;
641}
642
643static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
644{
645 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
646 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
647 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
648 }
649
650 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
651 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
652 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
653 }
654
655 if (info->attrs[NL80211_ATTR_KEY_IDX])
656 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
657
658 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
659 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
660
661 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
662 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
663
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100664 if (k->def) {
665 k->def_uni = true;
666 k->def_multi = true;
667 }
668 if (k->defmgmt)
669 k->def_multi = true;
670
Johannes Berge31b8212010-10-05 19:39:30 +0200671 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
672 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
673 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
674 return -EINVAL;
675 }
676
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100677 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
678 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
679 int err = nla_parse_nested(
680 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
681 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
682 nl80211_key_default_policy);
683 if (err)
684 return err;
685
686 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
687 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
688 }
689
Johannes Bergb9454e82009-07-08 13:29:08 +0200690 return 0;
691}
692
693static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
694{
695 int err;
696
697 memset(k, 0, sizeof(*k));
698 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200699 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200700
701 if (info->attrs[NL80211_ATTR_KEY])
702 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
703 else
704 err = nl80211_parse_key_old(info, k);
705
706 if (err)
707 return err;
708
709 if (k->def && k->defmgmt)
710 return -EINVAL;
711
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100712 if (k->defmgmt) {
713 if (k->def_uni || !k->def_multi)
714 return -EINVAL;
715 }
716
Johannes Bergb9454e82009-07-08 13:29:08 +0200717 if (k->idx != -1) {
718 if (k->defmgmt) {
719 if (k->idx < 4 || k->idx > 5)
720 return -EINVAL;
721 } else if (k->def) {
722 if (k->idx < 0 || k->idx > 3)
723 return -EINVAL;
724 } else {
725 if (k->idx < 0 || k->idx > 5)
726 return -EINVAL;
727 }
728 }
729
730 return 0;
731}
732
Johannes Bergfffd0932009-07-08 14:22:54 +0200733static struct cfg80211_cached_keys *
734nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530735 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200736{
737 struct key_parse parse;
738 struct nlattr *key;
739 struct cfg80211_cached_keys *result;
740 int rem, err, def = 0;
741
742 result = kzalloc(sizeof(*result), GFP_KERNEL);
743 if (!result)
744 return ERR_PTR(-ENOMEM);
745
746 result->def = -1;
747 result->defmgmt = -1;
748
749 nla_for_each_nested(key, keys, rem) {
750 memset(&parse, 0, sizeof(parse));
751 parse.idx = -1;
752
753 err = nl80211_parse_key_new(key, &parse);
754 if (err)
755 goto error;
756 err = -EINVAL;
757 if (!parse.p.key)
758 goto error;
759 if (parse.idx < 0 || parse.idx > 4)
760 goto error;
761 if (parse.def) {
762 if (def)
763 goto error;
764 def = 1;
765 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100766 if (!parse.def_uni || !parse.def_multi)
767 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200768 } else if (parse.defmgmt)
769 goto error;
770 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200771 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200772 if (err)
773 goto error;
774 result->params[parse.idx].cipher = parse.p.cipher;
775 result->params[parse.idx].key_len = parse.p.key_len;
776 result->params[parse.idx].key = result->data[parse.idx];
777 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530778
779 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
780 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
781 if (no_ht)
782 *no_ht = true;
783 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200784 }
785
786 return result;
787 error:
788 kfree(result);
789 return ERR_PTR(err);
790}
791
792static int nl80211_key_allowed(struct wireless_dev *wdev)
793{
794 ASSERT_WDEV_LOCK(wdev);
795
Johannes Bergfffd0932009-07-08 14:22:54 +0200796 switch (wdev->iftype) {
797 case NL80211_IFTYPE_AP:
798 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200799 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700800 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200801 break;
802 case NL80211_IFTYPE_ADHOC:
803 if (!wdev->current_bss)
804 return -ENOLINK;
805 break;
806 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200807 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200808 if (wdev->sme_state != CFG80211_SME_CONNECTED)
809 return -ENOLINK;
810 break;
811 default:
812 return -EINVAL;
813 }
814
815 return 0;
816}
817
Johannes Berg7527a782011-05-13 10:58:57 +0200818static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
819{
820 struct nlattr *nl_modes = nla_nest_start(msg, attr);
821 int i;
822
823 if (!nl_modes)
824 goto nla_put_failure;
825
826 i = 0;
827 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400828 if ((ifmodes & 1) && nla_put_flag(msg, i))
829 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200830 ifmodes >>= 1;
831 i++;
832 }
833
834 nla_nest_end(msg, nl_modes);
835 return 0;
836
837nla_put_failure:
838 return -ENOBUFS;
839}
840
841static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100842 struct sk_buff *msg,
843 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200844{
845 struct nlattr *nl_combis;
846 int i, j;
847
848 nl_combis = nla_nest_start(msg,
849 NL80211_ATTR_INTERFACE_COMBINATIONS);
850 if (!nl_combis)
851 goto nla_put_failure;
852
853 for (i = 0; i < wiphy->n_iface_combinations; i++) {
854 const struct ieee80211_iface_combination *c;
855 struct nlattr *nl_combi, *nl_limits;
856
857 c = &wiphy->iface_combinations[i];
858
859 nl_combi = nla_nest_start(msg, i + 1);
860 if (!nl_combi)
861 goto nla_put_failure;
862
863 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
864 if (!nl_limits)
865 goto nla_put_failure;
866
867 for (j = 0; j < c->n_limits; j++) {
868 struct nlattr *nl_limit;
869
870 nl_limit = nla_nest_start(msg, j + 1);
871 if (!nl_limit)
872 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400873 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
874 c->limits[j].max))
875 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200876 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
877 c->limits[j].types))
878 goto nla_put_failure;
879 nla_nest_end(msg, nl_limit);
880 }
881
882 nla_nest_end(msg, nl_limits);
883
David S. Miller9360ffd2012-03-29 04:41:26 -0400884 if (c->beacon_int_infra_match &&
885 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
886 goto nla_put_failure;
887 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
888 c->num_different_channels) ||
889 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
890 c->max_interfaces))
891 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100892 if (large &&
893 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
894 c->radar_detect_widths))
895 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200896
897 nla_nest_end(msg, nl_combi);
898 }
899
900 nla_nest_end(msg, nl_combis);
901
902 return 0;
903nla_put_failure:
904 return -ENOBUFS;
905}
906
Johannes Berg3713b4e2013-02-14 16:19:38 +0100907#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100908static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
909 struct sk_buff *msg)
910{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200911 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100912 struct nlattr *nl_tcp;
913
914 if (!tcp)
915 return 0;
916
917 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
918 if (!nl_tcp)
919 return -ENOBUFS;
920
921 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
922 tcp->data_payload_max))
923 return -ENOBUFS;
924
925 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
926 tcp->data_payload_max))
927 return -ENOBUFS;
928
929 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
930 return -ENOBUFS;
931
932 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
933 sizeof(*tcp->tok), tcp->tok))
934 return -ENOBUFS;
935
936 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
937 tcp->data_interval_max))
938 return -ENOBUFS;
939
940 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
941 tcp->wake_payload_max))
942 return -ENOBUFS;
943
944 nla_nest_end(msg, nl_tcp);
945 return 0;
946}
947
Johannes Berg3713b4e2013-02-14 16:19:38 +0100948static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +0100949 struct cfg80211_registered_device *dev,
950 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100951{
952 struct nlattr *nl_wowlan;
953
Johannes Berg964dc9e2013-06-03 17:25:34 +0200954 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100955 return 0;
956
957 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
958 if (!nl_wowlan)
959 return -ENOBUFS;
960
Johannes Berg964dc9e2013-06-03 17:25:34 +0200961 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100962 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200963 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100964 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200965 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100966 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200967 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100968 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200969 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100970 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200971 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100972 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200973 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100974 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200975 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100976 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
977 return -ENOBUFS;
978
Johannes Berg964dc9e2013-06-03 17:25:34 +0200979 if (dev->wiphy.wowlan->n_patterns) {
Johannes Berg3713b4e2013-02-14 16:19:38 +0100980 struct nl80211_wowlan_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +0200981 .max_patterns = dev->wiphy.wowlan->n_patterns,
982 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
983 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
984 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +0100985 };
986
987 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
988 sizeof(pat), &pat))
989 return -ENOBUFS;
990 }
991
Johannes Bergb56cf722013-02-20 01:02:38 +0100992 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
993 return -ENOBUFS;
994
Johannes Berg3713b4e2013-02-14 16:19:38 +0100995 nla_nest_end(msg, nl_wowlan);
996
997 return 0;
998}
999#endif
1000
1001static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1002 struct ieee80211_supported_band *sband)
1003{
1004 struct nlattr *nl_rates, *nl_rate;
1005 struct ieee80211_rate *rate;
1006 int i;
1007
1008 /* add HT info */
1009 if (sband->ht_cap.ht_supported &&
1010 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1011 sizeof(sband->ht_cap.mcs),
1012 &sband->ht_cap.mcs) ||
1013 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1014 sband->ht_cap.cap) ||
1015 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1016 sband->ht_cap.ampdu_factor) ||
1017 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1018 sband->ht_cap.ampdu_density)))
1019 return -ENOBUFS;
1020
1021 /* add VHT info */
1022 if (sband->vht_cap.vht_supported &&
1023 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1024 sizeof(sband->vht_cap.vht_mcs),
1025 &sband->vht_cap.vht_mcs) ||
1026 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1027 sband->vht_cap.cap)))
1028 return -ENOBUFS;
1029
1030 /* add bitrates */
1031 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1032 if (!nl_rates)
1033 return -ENOBUFS;
1034
1035 for (i = 0; i < sband->n_bitrates; i++) {
1036 nl_rate = nla_nest_start(msg, i);
1037 if (!nl_rate)
1038 return -ENOBUFS;
1039
1040 rate = &sband->bitrates[i];
1041 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1042 rate->bitrate))
1043 return -ENOBUFS;
1044 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1045 nla_put_flag(msg,
1046 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1047 return -ENOBUFS;
1048
1049 nla_nest_end(msg, nl_rate);
1050 }
1051
1052 nla_nest_end(msg, nl_rates);
1053
1054 return 0;
1055}
1056
1057static int
1058nl80211_send_mgmt_stypes(struct sk_buff *msg,
1059 const struct ieee80211_txrx_stypes *mgmt_stypes)
1060{
1061 u16 stypes;
1062 struct nlattr *nl_ftypes, *nl_ifs;
1063 enum nl80211_iftype ift;
1064 int i;
1065
1066 if (!mgmt_stypes)
1067 return 0;
1068
1069 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1070 if (!nl_ifs)
1071 return -ENOBUFS;
1072
1073 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1074 nl_ftypes = nla_nest_start(msg, ift);
1075 if (!nl_ftypes)
1076 return -ENOBUFS;
1077 i = 0;
1078 stypes = mgmt_stypes[ift].tx;
1079 while (stypes) {
1080 if ((stypes & 1) &&
1081 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1082 (i << 4) | IEEE80211_FTYPE_MGMT))
1083 return -ENOBUFS;
1084 stypes >>= 1;
1085 i++;
1086 }
1087 nla_nest_end(msg, nl_ftypes);
1088 }
1089
1090 nla_nest_end(msg, nl_ifs);
1091
1092 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1093 if (!nl_ifs)
1094 return -ENOBUFS;
1095
1096 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1097 nl_ftypes = nla_nest_start(msg, ift);
1098 if (!nl_ftypes)
1099 return -ENOBUFS;
1100 i = 0;
1101 stypes = mgmt_stypes[ift].rx;
1102 while (stypes) {
1103 if ((stypes & 1) &&
1104 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1105 (i << 4) | IEEE80211_FTYPE_MGMT))
1106 return -ENOBUFS;
1107 stypes >>= 1;
1108 i++;
1109 }
1110 nla_nest_end(msg, nl_ftypes);
1111 }
1112 nla_nest_end(msg, nl_ifs);
1113
1114 return 0;
1115}
1116
1117static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1118 struct sk_buff *msg, u32 portid, u32 seq,
1119 int flags, bool split, long *split_start,
1120 long *band_start, long *chan_start)
Johannes Berg55682962007-09-20 13:09:35 -04001121{
1122 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001123 struct nlattr *nl_bands, *nl_band;
1124 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001125 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001126 enum ieee80211_band band;
1127 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001128 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001129 const struct ieee80211_txrx_stypes *mgmt_stypes =
1130 dev->wiphy.mgmt_stypes;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001131 long start = 0, start_chan = 0, start_band = 0;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001132 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001133
Eric W. Biederman15e47302012-09-07 20:12:54 +00001134 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001135 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001136 return -ENOBUFS;
1137
1138 /* allow always using the variables */
1139 if (!split) {
1140 split_start = &start;
1141 band_start = &start_band;
1142 chan_start = &start_chan;
1143 }
Johannes Berg55682962007-09-20 13:09:35 -04001144
David S. Miller9360ffd2012-03-29 04:41:26 -04001145 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001146 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1147 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001148 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001149 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001150 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001151
Johannes Berg3713b4e2013-02-14 16:19:38 +01001152 switch (*split_start) {
1153 case 0:
1154 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1155 dev->wiphy.retry_short) ||
1156 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1157 dev->wiphy.retry_long) ||
1158 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1159 dev->wiphy.frag_threshold) ||
1160 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1161 dev->wiphy.rts_threshold) ||
1162 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1163 dev->wiphy.coverage_class) ||
1164 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1165 dev->wiphy.max_scan_ssids) ||
1166 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1167 dev->wiphy.max_sched_scan_ssids) ||
1168 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1169 dev->wiphy.max_scan_ie_len) ||
1170 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1171 dev->wiphy.max_sched_scan_ie_len) ||
1172 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1173 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001174 goto nla_put_failure;
1175
Johannes Berg3713b4e2013-02-14 16:19:38 +01001176 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1177 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1178 goto nla_put_failure;
1179 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1180 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1181 goto nla_put_failure;
1182 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1183 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1184 goto nla_put_failure;
1185 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1186 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1187 goto nla_put_failure;
1188 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1189 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1190 goto nla_put_failure;
1191 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1192 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001193 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +02001194
Johannes Berg3713b4e2013-02-14 16:19:38 +01001195 (*split_start)++;
1196 if (split)
1197 break;
1198 case 1:
1199 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1200 sizeof(u32) * dev->wiphy.n_cipher_suites,
1201 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001202 goto nla_put_failure;
1203
Johannes Berg3713b4e2013-02-14 16:19:38 +01001204 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1205 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001206 goto nla_put_failure;
1207
Johannes Berg3713b4e2013-02-14 16:19:38 +01001208 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1209 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1210 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001211
Johannes Berg3713b4e2013-02-14 16:19:38 +01001212 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1213 dev->wiphy.available_antennas_tx) ||
1214 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1215 dev->wiphy.available_antennas_rx))
1216 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001217
Johannes Berg3713b4e2013-02-14 16:19:38 +01001218 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1219 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1220 dev->wiphy.probe_resp_offload))
1221 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001222
Johannes Berg3713b4e2013-02-14 16:19:38 +01001223 if ((dev->wiphy.available_antennas_tx ||
1224 dev->wiphy.available_antennas_rx) &&
1225 dev->ops->get_antenna) {
1226 u32 tx_ant = 0, rx_ant = 0;
1227 int res;
1228 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1229 if (!res) {
1230 if (nla_put_u32(msg,
1231 NL80211_ATTR_WIPHY_ANTENNA_TX,
1232 tx_ant) ||
1233 nla_put_u32(msg,
1234 NL80211_ATTR_WIPHY_ANTENNA_RX,
1235 rx_ant))
1236 goto nla_put_failure;
1237 }
Johannes Bergee688b002008-01-24 19:38:39 +01001238 }
1239
Johannes Berg3713b4e2013-02-14 16:19:38 +01001240 (*split_start)++;
1241 if (split)
1242 break;
1243 case 2:
1244 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1245 dev->wiphy.interface_modes))
1246 goto nla_put_failure;
1247 (*split_start)++;
1248 if (split)
1249 break;
1250 case 3:
1251 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1252 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001253 goto nla_put_failure;
1254
Johannes Berg3713b4e2013-02-14 16:19:38 +01001255 for (band = *band_start; band < IEEE80211_NUM_BANDS; band++) {
1256 struct ieee80211_supported_band *sband;
1257
1258 sband = dev->wiphy.bands[band];
1259
1260 if (!sband)
1261 continue;
1262
1263 nl_band = nla_nest_start(msg, band);
1264 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001265 goto nla_put_failure;
1266
Johannes Berg3713b4e2013-02-14 16:19:38 +01001267 switch (*chan_start) {
1268 case 0:
1269 if (nl80211_send_band_rateinfo(msg, sband))
1270 goto nla_put_failure;
1271 (*chan_start)++;
1272 if (split)
1273 break;
1274 default:
1275 /* add frequencies */
1276 nl_freqs = nla_nest_start(
1277 msg, NL80211_BAND_ATTR_FREQS);
1278 if (!nl_freqs)
1279 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001280
Johannes Berg3713b4e2013-02-14 16:19:38 +01001281 for (i = *chan_start - 1;
1282 i < sband->n_channels;
1283 i++) {
1284 nl_freq = nla_nest_start(msg, i);
1285 if (!nl_freq)
1286 goto nla_put_failure;
1287
1288 chan = &sband->channels[i];
1289
Johannes Bergcdc89b92013-02-18 23:54:36 +01001290 if (nl80211_msg_put_channel(msg, chan,
1291 split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001292 goto nla_put_failure;
1293
1294 nla_nest_end(msg, nl_freq);
1295 if (split)
1296 break;
1297 }
1298 if (i < sband->n_channels)
1299 *chan_start = i + 2;
1300 else
1301 *chan_start = 0;
1302 nla_nest_end(msg, nl_freqs);
1303 }
1304
1305 nla_nest_end(msg, nl_band);
1306
1307 if (split) {
1308 /* start again here */
1309 if (*chan_start)
1310 band--;
1311 break;
1312 }
Johannes Bergee688b002008-01-24 19:38:39 +01001313 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001314 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001315
Johannes Berg3713b4e2013-02-14 16:19:38 +01001316 if (band < IEEE80211_NUM_BANDS)
1317 *band_start = band + 1;
1318 else
1319 *band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001320
Johannes Berg3713b4e2013-02-14 16:19:38 +01001321 /* if bands & channels are done, continue outside */
1322 if (*band_start == 0 && *chan_start == 0)
1323 (*split_start)++;
1324 if (split)
1325 break;
1326 case 4:
1327 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1328 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001329 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001330
1331 i = 0;
1332#define CMD(op, n) \
1333 do { \
1334 if (dev->ops->op) { \
1335 i++; \
1336 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1337 goto nla_put_failure; \
1338 } \
1339 } while (0)
1340
1341 CMD(add_virtual_intf, NEW_INTERFACE);
1342 CMD(change_virtual_intf, SET_INTERFACE);
1343 CMD(add_key, NEW_KEY);
1344 CMD(start_ap, START_AP);
1345 CMD(add_station, NEW_STATION);
1346 CMD(add_mpath, NEW_MPATH);
1347 CMD(update_mesh_config, SET_MESH_CONFIG);
1348 CMD(change_bss, SET_BSS);
1349 CMD(auth, AUTHENTICATE);
1350 CMD(assoc, ASSOCIATE);
1351 CMD(deauth, DEAUTHENTICATE);
1352 CMD(disassoc, DISASSOCIATE);
1353 CMD(join_ibss, JOIN_IBSS);
1354 CMD(join_mesh, JOIN_MESH);
1355 CMD(set_pmksa, SET_PMKSA);
1356 CMD(del_pmksa, DEL_PMKSA);
1357 CMD(flush_pmksa, FLUSH_PMKSA);
1358 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1359 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1360 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1361 CMD(mgmt_tx, FRAME);
1362 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1363 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1364 i++;
1365 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1366 goto nla_put_failure;
1367 }
1368 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1369 dev->ops->join_mesh) {
1370 i++;
1371 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1372 goto nla_put_failure;
1373 }
1374 CMD(set_wds_peer, SET_WDS_PEER);
1375 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1376 CMD(tdls_mgmt, TDLS_MGMT);
1377 CMD(tdls_oper, TDLS_OPER);
1378 }
1379 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1380 CMD(sched_scan_start, START_SCHED_SCAN);
1381 CMD(probe_client, PROBE_CLIENT);
1382 CMD(set_noack_map, SET_NOACK_MAP);
1383 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1384 i++;
1385 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1386 goto nla_put_failure;
1387 }
1388 CMD(start_p2p_device, START_P2P_DEVICE);
1389 CMD(set_mcast_rate, SET_MCAST_RATE);
Arend van Spriel5de17982013-04-18 15:49:00 +02001390 if (split) {
1391 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1392 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
1393 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001394
Kalle Valo4745fc02011-11-17 19:06:10 +02001395#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001396 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001397#endif
1398
Johannes Berg8fdc6212009-03-14 09:34:01 +01001399#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001400
Johannes Berg3713b4e2013-02-14 16:19:38 +01001401 if (dev->ops->connect || dev->ops->auth) {
1402 i++;
1403 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001404 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001405 }
1406
Johannes Berg3713b4e2013-02-14 16:19:38 +01001407 if (dev->ops->disconnect || dev->ops->deauth) {
1408 i++;
1409 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1410 goto nla_put_failure;
1411 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001412
Johannes Berg3713b4e2013-02-14 16:19:38 +01001413 nla_nest_end(msg, nl_cmds);
1414 (*split_start)++;
1415 if (split)
1416 break;
1417 case 5:
1418 if (dev->ops->remain_on_channel &&
1419 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1420 nla_put_u32(msg,
1421 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1422 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001423 goto nla_put_failure;
1424
Johannes Berg3713b4e2013-02-14 16:19:38 +01001425 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1426 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1427 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001428
Johannes Berg3713b4e2013-02-14 16:19:38 +01001429 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1430 goto nla_put_failure;
1431 (*split_start)++;
1432 if (split)
1433 break;
1434 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001435#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +01001436 if (nl80211_send_wowlan(msg, dev, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001437 goto nla_put_failure;
1438 (*split_start)++;
1439 if (split)
1440 break;
1441#else
1442 (*split_start)++;
1443#endif
1444 case 7:
1445 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1446 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001447 goto nla_put_failure;
1448
Johannes Bergcdc89b92013-02-18 23:54:36 +01001449 if (nl80211_put_iface_combinations(&dev->wiphy, msg, split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001450 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001451
Johannes Berg3713b4e2013-02-14 16:19:38 +01001452 (*split_start)++;
1453 if (split)
1454 break;
1455 case 8:
1456 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1457 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1458 dev->wiphy.ap_sme_capa))
1459 goto nla_put_failure;
1460
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001461 features = dev->wiphy.features;
1462 /*
1463 * We can only add the per-channel limit information if the
1464 * dump is split, otherwise it makes it too big. Therefore
1465 * only advertise it in that case.
1466 */
1467 if (split)
1468 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1469 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001470 goto nla_put_failure;
1471
1472 if (dev->wiphy.ht_capa_mod_mask &&
1473 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1474 sizeof(*dev->wiphy.ht_capa_mod_mask),
1475 dev->wiphy.ht_capa_mod_mask))
1476 goto nla_put_failure;
1477
1478 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1479 dev->wiphy.max_acl_mac_addrs &&
1480 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1481 dev->wiphy.max_acl_mac_addrs))
1482 goto nla_put_failure;
1483
1484 /*
1485 * Any information below this point is only available to
1486 * applications that can deal with it being split. This
1487 * helps ensure that newly added capabilities don't break
1488 * older tools by overrunning their buffers.
1489 *
1490 * We still increment split_start so that in the split
1491 * case we'll continue with more data in the next round,
1492 * but break unconditionally so unsplit data stops here.
1493 */
1494 (*split_start)++;
1495 break;
1496 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001497 if (dev->wiphy.extended_capabilities &&
1498 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1499 dev->wiphy.extended_capabilities_len,
1500 dev->wiphy.extended_capabilities) ||
1501 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1502 dev->wiphy.extended_capabilities_len,
1503 dev->wiphy.extended_capabilities_mask)))
1504 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001505
Johannes Bergee2aca32013-02-21 17:36:01 +01001506 if (dev->wiphy.vht_capa_mod_mask &&
1507 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1508 sizeof(*dev->wiphy.vht_capa_mod_mask),
1509 dev->wiphy.vht_capa_mod_mask))
1510 goto nla_put_failure;
1511
Johannes Berg3713b4e2013-02-14 16:19:38 +01001512 /* done */
1513 *split_start = 0;
1514 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001515 }
Johannes Berg55682962007-09-20 13:09:35 -04001516 return genlmsg_end(msg, hdr);
1517
1518 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001519 genlmsg_cancel(msg, hdr);
1520 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001521}
1522
1523static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1524{
Johannes Berg645e77d2013-03-01 14:03:49 +01001525 int idx = 0, ret;
Johannes Berg55682962007-09-20 13:09:35 -04001526 int start = cb->args[0];
1527 struct cfg80211_registered_device *dev;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001528 s64 filter_wiphy = -1;
1529 bool split = false;
1530 struct nlattr **tb = nl80211_fam.attrbuf;
1531 int res;
Johannes Berg55682962007-09-20 13:09:35 -04001532
Johannes Berg5fe231e2013-05-08 21:45:15 +02001533 rtnl_lock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001534 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1535 tb, nl80211_fam.maxattr, nl80211_policy);
1536 if (res == 0) {
1537 split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1538 if (tb[NL80211_ATTR_WIPHY])
1539 filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1540 if (tb[NL80211_ATTR_WDEV])
1541 filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1542 if (tb[NL80211_ATTR_IFINDEX]) {
1543 struct net_device *netdev;
1544 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1545
1546 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001547 if (!netdev)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001548 return -ENODEV;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001549 if (netdev->ieee80211_ptr) {
1550 dev = wiphy_to_dev(
1551 netdev->ieee80211_ptr->wiphy);
1552 filter_wiphy = dev->wiphy_idx;
1553 }
1554 dev_put(netdev);
1555 }
1556 }
1557
Johannes Berg79c97e92009-07-07 03:56:12 +02001558 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001559 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1560 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001561 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001562 continue;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001563 if (filter_wiphy != -1 && dev->wiphy_idx != filter_wiphy)
1564 continue;
1565 /* attempt to fit multiple wiphy data chunks into the skb */
1566 do {
1567 ret = nl80211_send_wiphy(dev, skb,
1568 NETLINK_CB(cb->skb).portid,
1569 cb->nlh->nlmsg_seq,
1570 NLM_F_MULTI,
1571 split, &cb->args[1],
1572 &cb->args[2],
1573 &cb->args[3]);
1574 if (ret < 0) {
1575 /*
1576 * If sending the wiphy data didn't fit (ENOBUFS
1577 * or EMSGSIZE returned), this SKB is still
1578 * empty (so it's not too big because another
1579 * wiphy dataset is already in the skb) and
1580 * we've not tried to adjust the dump allocation
1581 * yet ... then adjust the alloc size to be
1582 * bigger, and return 1 but with the empty skb.
1583 * This results in an empty message being RX'ed
1584 * in userspace, but that is ignored.
1585 *
1586 * We can then retry with the larger buffer.
1587 */
1588 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1589 !skb->len &&
1590 cb->min_dump_alloc < 4096) {
1591 cb->min_dump_alloc = 4096;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001592 return 1;
1593 }
1594 idx--;
1595 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001596 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001597 } while (cb->args[1] > 0);
1598 break;
Johannes Berg55682962007-09-20 13:09:35 -04001599 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001600 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001601
1602 cb->args[0] = idx;
1603
1604 return skb->len;
1605}
1606
1607static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1608{
1609 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001610 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001611
Johannes Berg645e77d2013-03-01 14:03:49 +01001612 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001613 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001614 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001615
Johannes Berg3713b4e2013-02-14 16:19:38 +01001616 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
1617 false, NULL, NULL, NULL) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001618 nlmsg_free(msg);
1619 return -ENOBUFS;
1620 }
Johannes Berg55682962007-09-20 13:09:35 -04001621
Johannes Berg134e6372009-07-10 09:51:34 +00001622 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001623}
1624
Jouni Malinen31888482008-10-30 16:59:24 +02001625static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1626 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1627 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1628 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1629 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1630 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1631};
1632
1633static int parse_txq_params(struct nlattr *tb[],
1634 struct ieee80211_txq_params *txq_params)
1635{
Johannes Berga3304b02012-03-28 11:04:24 +02001636 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001637 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1638 !tb[NL80211_TXQ_ATTR_AIFS])
1639 return -EINVAL;
1640
Johannes Berga3304b02012-03-28 11:04:24 +02001641 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001642 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1643 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1644 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1645 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1646
Johannes Berga3304b02012-03-28 11:04:24 +02001647 if (txq_params->ac >= NL80211_NUM_ACS)
1648 return -EINVAL;
1649
Jouni Malinen31888482008-10-30 16:59:24 +02001650 return 0;
1651}
1652
Johannes Bergf444de02010-05-05 15:25:02 +02001653static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1654{
1655 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001656 * You can only set the channel explicitly for WDS interfaces,
1657 * all others have their channel managed via their respective
1658 * "establish a connection" command (connect, join, ...)
1659 *
1660 * For AP/GO and mesh mode, the channel can be set with the
1661 * channel userspace API, but is only stored and passed to the
1662 * low-level driver when the AP starts or the mesh is joined.
1663 * This is for backward compatibility, userspace can also give
1664 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001665 *
1666 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001667 * whatever else is going on, so they have their own special
1668 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001669 */
1670 return !wdev ||
1671 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001672 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001673 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1674 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001675}
1676
Johannes Berg683b6d32012-11-08 21:25:48 +01001677static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1678 struct genl_info *info,
1679 struct cfg80211_chan_def *chandef)
1680{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301681 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001682
1683 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1684 return -EINVAL;
1685
1686 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1687
1688 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001689 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1690 chandef->center_freq1 = control_freq;
1691 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001692
1693 /* Primary channel not allowed */
1694 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1695 return -EINVAL;
1696
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001697 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1698 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001699
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001700 chantype = nla_get_u32(
1701 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1702
1703 switch (chantype) {
1704 case NL80211_CHAN_NO_HT:
1705 case NL80211_CHAN_HT20:
1706 case NL80211_CHAN_HT40PLUS:
1707 case NL80211_CHAN_HT40MINUS:
1708 cfg80211_chandef_create(chandef, chandef->chan,
1709 chantype);
1710 break;
1711 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001712 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001713 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001714 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1715 chandef->width =
1716 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1717 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1718 chandef->center_freq1 =
1719 nla_get_u32(
1720 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1721 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1722 chandef->center_freq2 =
1723 nla_get_u32(
1724 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1725 }
1726
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001727 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001728 return -EINVAL;
1729
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001730 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1731 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001732 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001733
Johannes Berg683b6d32012-11-08 21:25:48 +01001734 return 0;
1735}
1736
Johannes Bergf444de02010-05-05 15:25:02 +02001737static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1738 struct wireless_dev *wdev,
1739 struct genl_info *info)
1740{
Johannes Berg683b6d32012-11-08 21:25:48 +01001741 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001742 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001743 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1744
1745 if (wdev)
1746 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001747
Johannes Bergf444de02010-05-05 15:25:02 +02001748 if (!nl80211_can_set_dev_channel(wdev))
1749 return -EOPNOTSUPP;
1750
Johannes Berg683b6d32012-11-08 21:25:48 +01001751 result = nl80211_parse_chandef(rdev, info, &chandef);
1752 if (result)
1753 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001754
Johannes Berge8c9bd52012-06-06 08:18:22 +02001755 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001756 case NL80211_IFTYPE_AP:
1757 case NL80211_IFTYPE_P2P_GO:
1758 if (wdev->beacon_interval) {
1759 result = -EBUSY;
1760 break;
1761 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001762 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001763 result = -EINVAL;
1764 break;
1765 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001766 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001767 result = 0;
1768 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001769 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001770 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001771 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001772 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001773 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001774 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001775 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001776 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001777 }
Johannes Bergf444de02010-05-05 15:25:02 +02001778
1779 return result;
1780}
1781
1782static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1783{
Johannes Berg4c476992010-10-04 21:36:35 +02001784 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1785 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001786
Johannes Berg4c476992010-10-04 21:36:35 +02001787 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001788}
1789
Bill Jordane8347eb2010-10-01 13:54:28 -04001790static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1791{
Johannes Berg43b19952010-10-07 13:10:30 +02001792 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1793 struct net_device *dev = info->user_ptr[1];
1794 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001795 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001796
1797 if (!info->attrs[NL80211_ATTR_MAC])
1798 return -EINVAL;
1799
Johannes Berg43b19952010-10-07 13:10:30 +02001800 if (netif_running(dev))
1801 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001802
Johannes Berg43b19952010-10-07 13:10:30 +02001803 if (!rdev->ops->set_wds_peer)
1804 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001805
Johannes Berg43b19952010-10-07 13:10:30 +02001806 if (wdev->iftype != NL80211_IFTYPE_WDS)
1807 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001808
1809 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001810 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001811}
1812
1813
Johannes Berg55682962007-09-20 13:09:35 -04001814static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1815{
1816 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001817 struct net_device *netdev = NULL;
1818 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001819 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001820 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001821 u32 changed;
1822 u8 retry_short = 0, retry_long = 0;
1823 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001824 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001825
Johannes Berg5fe231e2013-05-08 21:45:15 +02001826 ASSERT_RTNL();
1827
Johannes Bergf444de02010-05-05 15:25:02 +02001828 /*
1829 * Try to find the wiphy and netdev. Normally this
1830 * function shouldn't need the netdev, but this is
1831 * done for backward compatibility -- previously
1832 * setting the channel was done per wiphy, but now
1833 * it is per netdev. Previous userland like hostapd
1834 * also passed a netdev to set_wiphy, so that it is
1835 * possible to let that go to the right netdev!
1836 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001837
Johannes Bergf444de02010-05-05 15:25:02 +02001838 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1839 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1840
1841 netdev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001842 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02001843 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001844 else
Johannes Bergf444de02010-05-05 15:25:02 +02001845 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001846 }
1847
Johannes Bergf444de02010-05-05 15:25:02 +02001848 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001849 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1850 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001851 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02001852 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001853 wdev = NULL;
1854 netdev = NULL;
1855 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02001856 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001857 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001858
1859 /*
1860 * end workaround code, by now the rdev is available
1861 * and locked, and wdev may or may not be NULL.
1862 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001863
1864 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001865 result = cfg80211_dev_rename(
1866 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001867
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001868 if (result)
1869 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001870
Jouni Malinen31888482008-10-30 16:59:24 +02001871 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1872 struct ieee80211_txq_params txq_params;
1873 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1874
1875 if (!rdev->ops->set_txq_params) {
1876 result = -EOPNOTSUPP;
1877 goto bad_res;
1878 }
1879
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001880 if (!netdev) {
1881 result = -EINVAL;
1882 goto bad_res;
1883 }
1884
Johannes Berg133a3ff2011-11-03 14:50:13 +01001885 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1886 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1887 result = -EINVAL;
1888 goto bad_res;
1889 }
1890
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001891 if (!netif_running(netdev)) {
1892 result = -ENETDOWN;
1893 goto bad_res;
1894 }
1895
Jouni Malinen31888482008-10-30 16:59:24 +02001896 nla_for_each_nested(nl_txq_params,
1897 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1898 rem_txq_params) {
1899 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1900 nla_data(nl_txq_params),
1901 nla_len(nl_txq_params),
1902 txq_params_policy);
1903 result = parse_txq_params(tb, &txq_params);
1904 if (result)
1905 goto bad_res;
1906
Hila Gonene35e4d22012-06-27 17:19:42 +03001907 result = rdev_set_txq_params(rdev, netdev,
1908 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02001909 if (result)
1910 goto bad_res;
1911 }
1912 }
1913
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001914 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02001915 result = __nl80211_set_channel(rdev,
1916 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
1917 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001918 if (result)
1919 goto bad_res;
1920 }
1921
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001922 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02001923 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001924 enum nl80211_tx_power_setting type;
1925 int idx, mbm = 0;
1926
Johannes Bergc8442112012-10-24 10:17:18 +02001927 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
1928 txp_wdev = NULL;
1929
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001930 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001931 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001932 goto bad_res;
1933 }
1934
1935 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1936 type = nla_get_u32(info->attrs[idx]);
1937
1938 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1939 (type != NL80211_TX_POWER_AUTOMATIC)) {
1940 result = -EINVAL;
1941 goto bad_res;
1942 }
1943
1944 if (type != NL80211_TX_POWER_AUTOMATIC) {
1945 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1946 mbm = nla_get_u32(info->attrs[idx]);
1947 }
1948
Johannes Bergc8442112012-10-24 10:17:18 +02001949 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001950 if (result)
1951 goto bad_res;
1952 }
1953
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001954 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1955 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1956 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001957 if ((!rdev->wiphy.available_antennas_tx &&
1958 !rdev->wiphy.available_antennas_rx) ||
1959 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001960 result = -EOPNOTSUPP;
1961 goto bad_res;
1962 }
1963
1964 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1965 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1966
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001967 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001968 * available antenna masks, except for the "all" mask */
1969 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1970 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001971 result = -EINVAL;
1972 goto bad_res;
1973 }
1974
Bruno Randolf7f531e02010-12-16 11:30:22 +09001975 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1976 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001977
Hila Gonene35e4d22012-06-27 17:19:42 +03001978 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001979 if (result)
1980 goto bad_res;
1981 }
1982
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001983 changed = 0;
1984
1985 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1986 retry_short = nla_get_u8(
1987 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1988 if (retry_short == 0) {
1989 result = -EINVAL;
1990 goto bad_res;
1991 }
1992 changed |= WIPHY_PARAM_RETRY_SHORT;
1993 }
1994
1995 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1996 retry_long = nla_get_u8(
1997 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1998 if (retry_long == 0) {
1999 result = -EINVAL;
2000 goto bad_res;
2001 }
2002 changed |= WIPHY_PARAM_RETRY_LONG;
2003 }
2004
2005 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2006 frag_threshold = nla_get_u32(
2007 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2008 if (frag_threshold < 256) {
2009 result = -EINVAL;
2010 goto bad_res;
2011 }
2012 if (frag_threshold != (u32) -1) {
2013 /*
2014 * Fragments (apart from the last one) are required to
2015 * have even length. Make the fragmentation code
2016 * simpler by stripping LSB should someone try to use
2017 * odd threshold value.
2018 */
2019 frag_threshold &= ~0x1;
2020 }
2021 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2022 }
2023
2024 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2025 rts_threshold = nla_get_u32(
2026 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2027 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2028 }
2029
Lukáš Turek81077e82009-12-21 22:50:47 +01002030 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2031 coverage_class = nla_get_u8(
2032 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2033 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2034 }
2035
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002036 if (changed) {
2037 u8 old_retry_short, old_retry_long;
2038 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002039 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002040
2041 if (!rdev->ops->set_wiphy_params) {
2042 result = -EOPNOTSUPP;
2043 goto bad_res;
2044 }
2045
2046 old_retry_short = rdev->wiphy.retry_short;
2047 old_retry_long = rdev->wiphy.retry_long;
2048 old_frag_threshold = rdev->wiphy.frag_threshold;
2049 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002050 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002051
2052 if (changed & WIPHY_PARAM_RETRY_SHORT)
2053 rdev->wiphy.retry_short = retry_short;
2054 if (changed & WIPHY_PARAM_RETRY_LONG)
2055 rdev->wiphy.retry_long = retry_long;
2056 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2057 rdev->wiphy.frag_threshold = frag_threshold;
2058 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2059 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002060 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2061 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002062
Hila Gonene35e4d22012-06-27 17:19:42 +03002063 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002064 if (result) {
2065 rdev->wiphy.retry_short = old_retry_short;
2066 rdev->wiphy.retry_long = old_retry_long;
2067 rdev->wiphy.frag_threshold = old_frag_threshold;
2068 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002069 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002070 }
2071 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002072
Johannes Berg306d6112008-12-08 12:39:04 +01002073 bad_res:
Johannes Bergf444de02010-05-05 15:25:02 +02002074 if (netdev)
2075 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002076 return result;
2077}
2078
Johannes Berg71bbc992012-06-15 15:30:18 +02002079static inline u64 wdev_id(struct wireless_dev *wdev)
2080{
2081 return (u64)wdev->identifier |
2082 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2083}
Johannes Berg55682962007-09-20 13:09:35 -04002084
Johannes Berg683b6d32012-11-08 21:25:48 +01002085static int nl80211_send_chandef(struct sk_buff *msg,
2086 struct cfg80211_chan_def *chandef)
2087{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002088 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002089
Johannes Berg683b6d32012-11-08 21:25:48 +01002090 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2091 chandef->chan->center_freq))
2092 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002093 switch (chandef->width) {
2094 case NL80211_CHAN_WIDTH_20_NOHT:
2095 case NL80211_CHAN_WIDTH_20:
2096 case NL80211_CHAN_WIDTH_40:
2097 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2098 cfg80211_get_chandef_type(chandef)))
2099 return -ENOBUFS;
2100 break;
2101 default:
2102 break;
2103 }
2104 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2105 return -ENOBUFS;
2106 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2107 return -ENOBUFS;
2108 if (chandef->center_freq2 &&
2109 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002110 return -ENOBUFS;
2111 return 0;
2112}
2113
Eric W. Biederman15e47302012-09-07 20:12:54 +00002114static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002115 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002116 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002117{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002118 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002119 void *hdr;
2120
Eric W. Biederman15e47302012-09-07 20:12:54 +00002121 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002122 if (!hdr)
2123 return -1;
2124
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002125 if (dev &&
2126 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002127 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002128 goto nla_put_failure;
2129
2130 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2131 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002132 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002133 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002134 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2135 rdev->devlist_generation ^
2136 (cfg80211_rdev_list_generation << 2)))
2137 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002138
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002139 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002140 int ret;
2141 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002142
Johannes Berg683b6d32012-11-08 21:25:48 +01002143 ret = rdev_get_channel(rdev, wdev, &chandef);
2144 if (ret == 0) {
2145 if (nl80211_send_chandef(msg, &chandef))
2146 goto nla_put_failure;
2147 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002148 }
2149
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002150 if (wdev->ssid_len) {
2151 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2152 goto nla_put_failure;
2153 }
2154
Johannes Berg55682962007-09-20 13:09:35 -04002155 return genlmsg_end(msg, hdr);
2156
2157 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002158 genlmsg_cancel(msg, hdr);
2159 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002160}
2161
2162static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2163{
2164 int wp_idx = 0;
2165 int if_idx = 0;
2166 int wp_start = cb->args[0];
2167 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002168 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002169 struct wireless_dev *wdev;
2170
Johannes Berg5fe231e2013-05-08 21:45:15 +02002171 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002172 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2173 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002174 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002175 if (wp_idx < wp_start) {
2176 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002177 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002178 }
Johannes Berg55682962007-09-20 13:09:35 -04002179 if_idx = 0;
2180
Johannes Berg89a54e42012-06-15 14:33:17 +02002181 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002182 if (if_idx < if_start) {
2183 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002184 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002185 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002186 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002187 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002188 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002189 goto out;
2190 }
2191 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002192 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002193
2194 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002195 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002196 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002197 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002198
2199 cb->args[0] = wp_idx;
2200 cb->args[1] = if_idx;
2201
2202 return skb->len;
2203}
2204
2205static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2206{
2207 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002208 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002209 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002210
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002212 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002213 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002214
Eric W. Biederman15e47302012-09-07 20:12:54 +00002215 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002216 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002217 nlmsg_free(msg);
2218 return -ENOBUFS;
2219 }
Johannes Berg55682962007-09-20 13:09:35 -04002220
Johannes Berg134e6372009-07-10 09:51:34 +00002221 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002222}
2223
Michael Wu66f7ac52008-01-31 19:48:22 +01002224static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2225 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2226 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2227 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2228 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2229 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002230 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002231};
2232
2233static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2234{
2235 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2236 int flag;
2237
2238 *mntrflags = 0;
2239
2240 if (!nla)
2241 return -EINVAL;
2242
2243 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2244 nla, mntr_flags_policy))
2245 return -EINVAL;
2246
2247 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2248 if (flags[flag])
2249 *mntrflags |= (1<<flag);
2250
2251 return 0;
2252}
2253
Johannes Berg9bc383d2009-11-19 11:55:19 +01002254static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002255 struct net_device *netdev, u8 use_4addr,
2256 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002257{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002258 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002259 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002260 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002261 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002262 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002263
2264 switch (iftype) {
2265 case NL80211_IFTYPE_AP_VLAN:
2266 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2267 return 0;
2268 break;
2269 case NL80211_IFTYPE_STATION:
2270 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2271 return 0;
2272 break;
2273 default:
2274 break;
2275 }
2276
2277 return -EOPNOTSUPP;
2278}
2279
Johannes Berg55682962007-09-20 13:09:35 -04002280static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2281{
Johannes Berg4c476992010-10-04 21:36:35 +02002282 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002283 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002284 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002285 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002286 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002287 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002288 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002289
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002290 memset(&params, 0, sizeof(params));
2291
Johannes Berg04a773a2009-04-19 21:24:32 +02002292 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002293
Johannes Berg723b0382008-09-16 20:22:09 +02002294 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002295 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002296 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002297 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002298 if (ntype > NL80211_IFTYPE_MAX)
2299 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002300 }
2301
Johannes Berg92ffe052008-09-16 20:39:36 +02002302 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002303 struct wireless_dev *wdev = dev->ieee80211_ptr;
2304
Johannes Berg4c476992010-10-04 21:36:35 +02002305 if (ntype != NL80211_IFTYPE_MESH_POINT)
2306 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002307 if (netif_running(dev))
2308 return -EBUSY;
2309
2310 wdev_lock(wdev);
2311 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2312 IEEE80211_MAX_MESH_ID_LEN);
2313 wdev->mesh_id_up_len =
2314 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2315 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2316 wdev->mesh_id_up_len);
2317 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002318 }
2319
Felix Fietkau8b787642009-11-10 18:53:10 +01002320 if (info->attrs[NL80211_ATTR_4ADDR]) {
2321 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2322 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002323 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002324 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002325 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002326 } else {
2327 params.use_4addr = -1;
2328 }
2329
Johannes Berg92ffe052008-09-16 20:39:36 +02002330 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002331 if (ntype != NL80211_IFTYPE_MONITOR)
2332 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002333 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2334 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002335 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002336 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002337
2338 flags = &_flags;
2339 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002340 }
Johannes Berg3b858752009-03-12 09:55:09 +01002341
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002342 if (flags && (*flags & NL80211_MNTR_FLAG_ACTIVE) &&
2343 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2344 return -EOPNOTSUPP;
2345
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002346 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002347 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002348 else
2349 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002350
Johannes Berg9bc383d2009-11-19 11:55:19 +01002351 if (!err && params.use_4addr != -1)
2352 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2353
Johannes Berg55682962007-09-20 13:09:35 -04002354 return err;
2355}
2356
2357static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2358{
Johannes Berg4c476992010-10-04 21:36:35 +02002359 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002360 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002361 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002362 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002363 int err;
2364 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002365 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002366
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002367 memset(&params, 0, sizeof(params));
2368
Johannes Berg55682962007-09-20 13:09:35 -04002369 if (!info->attrs[NL80211_ATTR_IFNAME])
2370 return -EINVAL;
2371
2372 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2373 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2374 if (type > NL80211_IFTYPE_MAX)
2375 return -EINVAL;
2376 }
2377
Johannes Berg79c97e92009-07-07 03:56:12 +02002378 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002379 !(rdev->wiphy.interface_modes & (1 << type)))
2380 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002381
Arend van Spriel1c18f142013-01-08 10:17:27 +01002382 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2383 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2384 ETH_ALEN);
2385 if (!is_valid_ether_addr(params.macaddr))
2386 return -EADDRNOTAVAIL;
2387 }
2388
Johannes Berg9bc383d2009-11-19 11:55:19 +01002389 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002390 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002391 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002392 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002393 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002394 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002395
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002396 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2397 if (!msg)
2398 return -ENOMEM;
2399
Michael Wu66f7ac52008-01-31 19:48:22 +01002400 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2401 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2402 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002403
2404 if (!err && (flags & NL80211_MNTR_FLAG_ACTIVE) &&
2405 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2406 return -EOPNOTSUPP;
2407
Hila Gonene35e4d22012-06-27 17:19:42 +03002408 wdev = rdev_add_virtual_intf(rdev,
2409 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2410 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002411 if (IS_ERR(wdev)) {
2412 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002413 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002414 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002415
Johannes Berg98104fde2012-06-16 00:19:54 +02002416 switch (type) {
2417 case NL80211_IFTYPE_MESH_POINT:
2418 if (!info->attrs[NL80211_ATTR_MESH_ID])
2419 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002420 wdev_lock(wdev);
2421 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2422 IEEE80211_MAX_MESH_ID_LEN);
2423 wdev->mesh_id_up_len =
2424 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2425 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2426 wdev->mesh_id_up_len);
2427 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002428 break;
2429 case NL80211_IFTYPE_P2P_DEVICE:
2430 /*
2431 * P2P Device doesn't have a netdev, so doesn't go
2432 * through the netdev notifier and must be added here
2433 */
2434 mutex_init(&wdev->mtx);
2435 INIT_LIST_HEAD(&wdev->event_list);
2436 spin_lock_init(&wdev->event_lock);
2437 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2438 spin_lock_init(&wdev->mgmt_registrations_lock);
2439
Johannes Berg98104fde2012-06-16 00:19:54 +02002440 wdev->identifier = ++rdev->wdev_id;
2441 list_add_rcu(&wdev->list, &rdev->wdev_list);
2442 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002443 break;
2444 default:
2445 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002446 }
2447
Eric W. Biederman15e47302012-09-07 20:12:54 +00002448 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002449 rdev, wdev) < 0) {
2450 nlmsg_free(msg);
2451 return -ENOBUFS;
2452 }
2453
2454 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002455}
2456
2457static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2458{
Johannes Berg4c476992010-10-04 21:36:35 +02002459 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002460 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002461
Johannes Berg4c476992010-10-04 21:36:35 +02002462 if (!rdev->ops->del_virtual_intf)
2463 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002464
Johannes Berg84efbb82012-06-16 00:00:26 +02002465 /*
2466 * If we remove a wireless device without a netdev then clear
2467 * user_ptr[1] so that nl80211_post_doit won't dereference it
2468 * to check if it needs to do dev_put(). Otherwise it crashes
2469 * since the wdev has been freed, unlike with a netdev where
2470 * we need the dev_put() for the netdev to really be freed.
2471 */
2472 if (!wdev->netdev)
2473 info->user_ptr[1] = NULL;
2474
Hila Gonene35e4d22012-06-27 17:19:42 +03002475 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002476}
2477
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002478static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2479{
2480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2481 struct net_device *dev = info->user_ptr[1];
2482 u16 noack_map;
2483
2484 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2485 return -EINVAL;
2486
2487 if (!rdev->ops->set_noack_map)
2488 return -EOPNOTSUPP;
2489
2490 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2491
Hila Gonene35e4d22012-06-27 17:19:42 +03002492 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002493}
2494
Johannes Berg41ade002007-12-19 02:03:29 +01002495struct get_key_cookie {
2496 struct sk_buff *msg;
2497 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002498 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002499};
2500
2501static void get_key_callback(void *c, struct key_params *params)
2502{
Johannes Bergb9454e82009-07-08 13:29:08 +02002503 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002504 struct get_key_cookie *cookie = c;
2505
David S. Miller9360ffd2012-03-29 04:41:26 -04002506 if ((params->key &&
2507 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2508 params->key_len, params->key)) ||
2509 (params->seq &&
2510 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2511 params->seq_len, params->seq)) ||
2512 (params->cipher &&
2513 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2514 params->cipher)))
2515 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002516
Johannes Bergb9454e82009-07-08 13:29:08 +02002517 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2518 if (!key)
2519 goto nla_put_failure;
2520
David S. Miller9360ffd2012-03-29 04:41:26 -04002521 if ((params->key &&
2522 nla_put(cookie->msg, NL80211_KEY_DATA,
2523 params->key_len, params->key)) ||
2524 (params->seq &&
2525 nla_put(cookie->msg, NL80211_KEY_SEQ,
2526 params->seq_len, params->seq)) ||
2527 (params->cipher &&
2528 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2529 params->cipher)))
2530 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002531
David S. Miller9360ffd2012-03-29 04:41:26 -04002532 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2533 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002534
2535 nla_nest_end(cookie->msg, key);
2536
Johannes Berg41ade002007-12-19 02:03:29 +01002537 return;
2538 nla_put_failure:
2539 cookie->error = 1;
2540}
2541
2542static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2543{
Johannes Berg4c476992010-10-04 21:36:35 +02002544 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002545 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002546 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002547 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002548 const u8 *mac_addr = NULL;
2549 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002550 struct get_key_cookie cookie = {
2551 .error = 0,
2552 };
2553 void *hdr;
2554 struct sk_buff *msg;
2555
2556 if (info->attrs[NL80211_ATTR_KEY_IDX])
2557 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2558
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002559 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002560 return -EINVAL;
2561
2562 if (info->attrs[NL80211_ATTR_MAC])
2563 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2564
Johannes Berge31b8212010-10-05 19:39:30 +02002565 pairwise = !!mac_addr;
2566 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2567 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2568 if (kt >= NUM_NL80211_KEYTYPES)
2569 return -EINVAL;
2570 if (kt != NL80211_KEYTYPE_GROUP &&
2571 kt != NL80211_KEYTYPE_PAIRWISE)
2572 return -EINVAL;
2573 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2574 }
2575
Johannes Berg4c476992010-10-04 21:36:35 +02002576 if (!rdev->ops->get_key)
2577 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002578
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002579 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002580 if (!msg)
2581 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002582
Eric W. Biederman15e47302012-09-07 20:12:54 +00002583 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002584 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002585 if (IS_ERR(hdr))
2586 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002587
2588 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002589 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002590
David S. Miller9360ffd2012-03-29 04:41:26 -04002591 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2592 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2593 goto nla_put_failure;
2594 if (mac_addr &&
2595 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2596 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002597
Johannes Berge31b8212010-10-05 19:39:30 +02002598 if (pairwise && mac_addr &&
2599 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2600 return -ENOENT;
2601
Hila Gonene35e4d22012-06-27 17:19:42 +03002602 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2603 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002604
2605 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002606 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002607
2608 if (cookie.error)
2609 goto nla_put_failure;
2610
2611 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002612 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002613
2614 nla_put_failure:
2615 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002616 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002617 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002618 return err;
2619}
2620
2621static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2622{
Johannes Berg4c476992010-10-04 21:36:35 +02002623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002624 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002625 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002626 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002627
Johannes Bergb9454e82009-07-08 13:29:08 +02002628 err = nl80211_parse_key(info, &key);
2629 if (err)
2630 return err;
2631
2632 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002633 return -EINVAL;
2634
Johannes Bergb9454e82009-07-08 13:29:08 +02002635 /* only support setting default key */
2636 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002637 return -EINVAL;
2638
Johannes Bergfffd0932009-07-08 14:22:54 +02002639 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002640
2641 if (key.def) {
2642 if (!rdev->ops->set_default_key) {
2643 err = -EOPNOTSUPP;
2644 goto out;
2645 }
2646
2647 err = nl80211_key_allowed(dev->ieee80211_ptr);
2648 if (err)
2649 goto out;
2650
Hila Gonene35e4d22012-06-27 17:19:42 +03002651 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002652 key.def_uni, key.def_multi);
2653
2654 if (err)
2655 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002656
Johannes Berg3d23e342009-09-29 23:27:28 +02002657#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002658 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002659#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002660 } else {
2661 if (key.def_uni || !key.def_multi) {
2662 err = -EINVAL;
2663 goto out;
2664 }
2665
2666 if (!rdev->ops->set_default_mgmt_key) {
2667 err = -EOPNOTSUPP;
2668 goto out;
2669 }
2670
2671 err = nl80211_key_allowed(dev->ieee80211_ptr);
2672 if (err)
2673 goto out;
2674
Hila Gonene35e4d22012-06-27 17:19:42 +03002675 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002676 if (err)
2677 goto out;
2678
2679#ifdef CONFIG_CFG80211_WEXT
2680 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2681#endif
2682 }
2683
2684 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002685 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002686
Johannes Berg41ade002007-12-19 02:03:29 +01002687 return err;
2688}
2689
2690static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2691{
Johannes Berg4c476992010-10-04 21:36:35 +02002692 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002693 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002694 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002695 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002696 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002697
Johannes Bergb9454e82009-07-08 13:29:08 +02002698 err = nl80211_parse_key(info, &key);
2699 if (err)
2700 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002701
Johannes Bergb9454e82009-07-08 13:29:08 +02002702 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002703 return -EINVAL;
2704
Johannes Berg41ade002007-12-19 02:03:29 +01002705 if (info->attrs[NL80211_ATTR_MAC])
2706 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2707
Johannes Berge31b8212010-10-05 19:39:30 +02002708 if (key.type == -1) {
2709 if (mac_addr)
2710 key.type = NL80211_KEYTYPE_PAIRWISE;
2711 else
2712 key.type = NL80211_KEYTYPE_GROUP;
2713 }
2714
2715 /* for now */
2716 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2717 key.type != NL80211_KEYTYPE_GROUP)
2718 return -EINVAL;
2719
Johannes Berg4c476992010-10-04 21:36:35 +02002720 if (!rdev->ops->add_key)
2721 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002722
Johannes Berge31b8212010-10-05 19:39:30 +02002723 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2724 key.type == NL80211_KEYTYPE_PAIRWISE,
2725 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002726 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002727
2728 wdev_lock(dev->ieee80211_ptr);
2729 err = nl80211_key_allowed(dev->ieee80211_ptr);
2730 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002731 err = rdev_add_key(rdev, dev, key.idx,
2732 key.type == NL80211_KEYTYPE_PAIRWISE,
2733 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002734 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002735
Johannes Berg41ade002007-12-19 02:03:29 +01002736 return err;
2737}
2738
2739static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2740{
Johannes Berg4c476992010-10-04 21:36:35 +02002741 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002742 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002743 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002744 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002745 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002746
Johannes Bergb9454e82009-07-08 13:29:08 +02002747 err = nl80211_parse_key(info, &key);
2748 if (err)
2749 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002750
2751 if (info->attrs[NL80211_ATTR_MAC])
2752 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2753
Johannes Berge31b8212010-10-05 19:39:30 +02002754 if (key.type == -1) {
2755 if (mac_addr)
2756 key.type = NL80211_KEYTYPE_PAIRWISE;
2757 else
2758 key.type = NL80211_KEYTYPE_GROUP;
2759 }
2760
2761 /* for now */
2762 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2763 key.type != NL80211_KEYTYPE_GROUP)
2764 return -EINVAL;
2765
Johannes Berg4c476992010-10-04 21:36:35 +02002766 if (!rdev->ops->del_key)
2767 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002768
Johannes Bergfffd0932009-07-08 14:22:54 +02002769 wdev_lock(dev->ieee80211_ptr);
2770 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002771
2772 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2773 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2774 err = -ENOENT;
2775
Johannes Bergfffd0932009-07-08 14:22:54 +02002776 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002777 err = rdev_del_key(rdev, dev, key.idx,
2778 key.type == NL80211_KEYTYPE_PAIRWISE,
2779 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002780
Johannes Berg3d23e342009-09-29 23:27:28 +02002781#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002782 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002783 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002784 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002785 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002786 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2787 }
2788#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002789 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002790
Johannes Berg41ade002007-12-19 02:03:29 +01002791 return err;
2792}
2793
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302794/* This function returns an error or the number of nested attributes */
2795static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2796{
2797 struct nlattr *attr;
2798 int n_entries = 0, tmp;
2799
2800 nla_for_each_nested(attr, nl_attr, tmp) {
2801 if (nla_len(attr) != ETH_ALEN)
2802 return -EINVAL;
2803
2804 n_entries++;
2805 }
2806
2807 return n_entries;
2808}
2809
2810/*
2811 * This function parses ACL information and allocates memory for ACL data.
2812 * On successful return, the calling function is responsible to free the
2813 * ACL buffer returned by this function.
2814 */
2815static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2816 struct genl_info *info)
2817{
2818 enum nl80211_acl_policy acl_policy;
2819 struct nlattr *attr;
2820 struct cfg80211_acl_data *acl;
2821 int i = 0, n_entries, tmp;
2822
2823 if (!wiphy->max_acl_mac_addrs)
2824 return ERR_PTR(-EOPNOTSUPP);
2825
2826 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2827 return ERR_PTR(-EINVAL);
2828
2829 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2830 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2831 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2832 return ERR_PTR(-EINVAL);
2833
2834 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2835 return ERR_PTR(-EINVAL);
2836
2837 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2838 if (n_entries < 0)
2839 return ERR_PTR(n_entries);
2840
2841 if (n_entries > wiphy->max_acl_mac_addrs)
2842 return ERR_PTR(-ENOTSUPP);
2843
2844 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2845 GFP_KERNEL);
2846 if (!acl)
2847 return ERR_PTR(-ENOMEM);
2848
2849 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2850 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2851 i++;
2852 }
2853
2854 acl->n_acl_entries = n_entries;
2855 acl->acl_policy = acl_policy;
2856
2857 return acl;
2858}
2859
2860static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2861{
2862 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2863 struct net_device *dev = info->user_ptr[1];
2864 struct cfg80211_acl_data *acl;
2865 int err;
2866
2867 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2868 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2869 return -EOPNOTSUPP;
2870
2871 if (!dev->ieee80211_ptr->beacon_interval)
2872 return -EINVAL;
2873
2874 acl = parse_acl_data(&rdev->wiphy, info);
2875 if (IS_ERR(acl))
2876 return PTR_ERR(acl);
2877
2878 err = rdev_set_mac_acl(rdev, dev, acl);
2879
2880 kfree(acl);
2881
2882 return err;
2883}
2884
Johannes Berg88600202012-02-13 15:17:18 +01002885static int nl80211_parse_beacon(struct genl_info *info,
2886 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002887{
Johannes Berg88600202012-02-13 15:17:18 +01002888 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002889
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002890 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2891 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2892 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2893 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002894 return -EINVAL;
2895
Johannes Berg88600202012-02-13 15:17:18 +01002896 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002897
Johannes Berged1b6cc2007-12-19 02:03:32 +01002898 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002899 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2900 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2901 if (!bcn->head_len)
2902 return -EINVAL;
2903 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002904 }
2905
2906 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002907 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2908 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002909 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002910 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002911 }
2912
Johannes Berg4c476992010-10-04 21:36:35 +02002913 if (!haveinfo)
2914 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002915
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002916 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002917 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2918 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002919 }
2920
2921 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002922 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002923 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002924 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002925 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2926 }
2927
2928 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002929 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002930 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002931 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002932 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2933 }
2934
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002935 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002936 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002937 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002938 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002939 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2940 }
2941
Johannes Berg88600202012-02-13 15:17:18 +01002942 return 0;
2943}
2944
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002945static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2946 struct cfg80211_ap_settings *params)
2947{
2948 struct wireless_dev *wdev;
2949 bool ret = false;
2950
Johannes Berg89a54e42012-06-15 14:33:17 +02002951 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002952 if (wdev->iftype != NL80211_IFTYPE_AP &&
2953 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2954 continue;
2955
Johannes Berg683b6d32012-11-08 21:25:48 +01002956 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002957 continue;
2958
Johannes Berg683b6d32012-11-08 21:25:48 +01002959 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002960 ret = true;
2961 break;
2962 }
2963
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002964 return ret;
2965}
2966
Jouni Malinene39e5b52012-09-30 19:29:39 +03002967static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2968 enum nl80211_auth_type auth_type,
2969 enum nl80211_commands cmd)
2970{
2971 if (auth_type > NL80211_AUTHTYPE_MAX)
2972 return false;
2973
2974 switch (cmd) {
2975 case NL80211_CMD_AUTHENTICATE:
2976 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2977 auth_type == NL80211_AUTHTYPE_SAE)
2978 return false;
2979 return true;
2980 case NL80211_CMD_CONNECT:
2981 case NL80211_CMD_START_AP:
2982 /* SAE not supported yet */
2983 if (auth_type == NL80211_AUTHTYPE_SAE)
2984 return false;
2985 return true;
2986 default:
2987 return false;
2988 }
2989}
2990
Johannes Berg88600202012-02-13 15:17:18 +01002991static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2992{
2993 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2994 struct net_device *dev = info->user_ptr[1];
2995 struct wireless_dev *wdev = dev->ieee80211_ptr;
2996 struct cfg80211_ap_settings params;
2997 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01002998 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01002999
3000 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3001 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3002 return -EOPNOTSUPP;
3003
3004 if (!rdev->ops->start_ap)
3005 return -EOPNOTSUPP;
3006
3007 if (wdev->beacon_interval)
3008 return -EALREADY;
3009
3010 memset(&params, 0, sizeof(params));
3011
3012 /* these are required for START_AP */
3013 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3014 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3015 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3016 return -EINVAL;
3017
3018 err = nl80211_parse_beacon(info, &params.beacon);
3019 if (err)
3020 return err;
3021
3022 params.beacon_interval =
3023 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3024 params.dtim_period =
3025 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3026
3027 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3028 if (err)
3029 return err;
3030
3031 /*
3032 * In theory, some of these attributes should be required here
3033 * but since they were not used when the command was originally
3034 * added, keep them optional for old user space programs to let
3035 * them continue to work with drivers that do not need the
3036 * additional information -- drivers must check!
3037 */
3038 if (info->attrs[NL80211_ATTR_SSID]) {
3039 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3040 params.ssid_len =
3041 nla_len(info->attrs[NL80211_ATTR_SSID]);
3042 if (params.ssid_len == 0 ||
3043 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3044 return -EINVAL;
3045 }
3046
3047 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3048 params.hidden_ssid = nla_get_u32(
3049 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3050 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3051 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3052 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3053 return -EINVAL;
3054 }
3055
3056 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3057
3058 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3059 params.auth_type = nla_get_u32(
3060 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003061 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3062 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003063 return -EINVAL;
3064 } else
3065 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3066
3067 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3068 NL80211_MAX_NR_CIPHER_SUITES);
3069 if (err)
3070 return err;
3071
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303072 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3073 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3074 return -EOPNOTSUPP;
3075 params.inactivity_timeout = nla_get_u16(
3076 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3077 }
3078
Johannes Berg53cabad2012-11-14 15:17:28 +01003079 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3080 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3081 return -EINVAL;
3082 params.p2p_ctwindow =
3083 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3084 if (params.p2p_ctwindow > 127)
3085 return -EINVAL;
3086 if (params.p2p_ctwindow != 0 &&
3087 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3088 return -EINVAL;
3089 }
3090
3091 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3092 u8 tmp;
3093
3094 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3095 return -EINVAL;
3096 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3097 if (tmp > 1)
3098 return -EINVAL;
3099 params.p2p_opp_ps = tmp;
3100 if (params.p2p_opp_ps != 0 &&
3101 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3102 return -EINVAL;
3103 }
3104
Johannes Bergaa430da2012-05-16 23:50:18 +02003105 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003106 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3107 if (err)
3108 return err;
3109 } else if (wdev->preset_chandef.chan) {
3110 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003111 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003112 return -EINVAL;
3113
Johannes Berg683b6d32012-11-08 21:25:48 +01003114 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003115 return -EINVAL;
3116
Simon Wunderlich04f39042013-02-08 18:16:19 +01003117 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3118 if (err < 0)
3119 return err;
3120 if (err) {
3121 radar_detect_width = BIT(params.chandef.width);
3122 params.radar_required = true;
3123 }
3124
Simon Wunderlich04f39042013-02-08 18:16:19 +01003125 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3126 params.chandef.chan,
3127 CHAN_MODE_SHARED,
3128 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003129 if (err)
3130 return err;
3131
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303132 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3133 params.acl = parse_acl_data(&rdev->wiphy, info);
3134 if (IS_ERR(params.acl))
3135 return PTR_ERR(params.acl);
3136 }
3137
Hila Gonene35e4d22012-06-27 17:19:42 +03003138 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003139 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003140 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003141 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003142 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003143 wdev->ssid_len = params.ssid_len;
3144 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003145 }
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303146
3147 kfree(params.acl);
3148
Johannes Berg56d18932011-05-09 18:41:15 +02003149 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003150}
3151
Johannes Berg88600202012-02-13 15:17:18 +01003152static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3153{
3154 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3155 struct net_device *dev = info->user_ptr[1];
3156 struct wireless_dev *wdev = dev->ieee80211_ptr;
3157 struct cfg80211_beacon_data params;
3158 int err;
3159
3160 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3161 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3162 return -EOPNOTSUPP;
3163
3164 if (!rdev->ops->change_beacon)
3165 return -EOPNOTSUPP;
3166
3167 if (!wdev->beacon_interval)
3168 return -EINVAL;
3169
3170 err = nl80211_parse_beacon(info, &params);
3171 if (err)
3172 return err;
3173
Hila Gonene35e4d22012-06-27 17:19:42 +03003174 return rdev_change_beacon(rdev, dev, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003175}
3176
3177static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003178{
Johannes Berg4c476992010-10-04 21:36:35 +02003179 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3180 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003181
Michal Kazior60771782012-06-29 12:46:56 +02003182 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003183}
3184
Johannes Berg5727ef12007-12-19 02:03:34 +01003185static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3186 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3187 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3188 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003189 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003190 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003191 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003192};
3193
Johannes Bergeccb8e82009-05-11 21:57:56 +03003194static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003195 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003196 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003197{
3198 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003199 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003200 int flag;
3201
Johannes Bergeccb8e82009-05-11 21:57:56 +03003202 /*
3203 * Try parsing the new attribute first so userspace
3204 * can specify both for older kernels.
3205 */
3206 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3207 if (nla) {
3208 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003209
Johannes Bergeccb8e82009-05-11 21:57:56 +03003210 sta_flags = nla_data(nla);
3211 params->sta_flags_mask = sta_flags->mask;
3212 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003213 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003214 if ((params->sta_flags_mask |
3215 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3216 return -EINVAL;
3217 return 0;
3218 }
3219
3220 /* if present, parse the old attribute */
3221
3222 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003223 if (!nla)
3224 return 0;
3225
3226 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3227 nla, sta_flags_policy))
3228 return -EINVAL;
3229
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003230 /*
3231 * Only allow certain flags for interface types so that
3232 * other attributes are silently ignored. Remember that
3233 * this is backward compatibility code with old userspace
3234 * and shouldn't be hit in other cases anyway.
3235 */
3236 switch (iftype) {
3237 case NL80211_IFTYPE_AP:
3238 case NL80211_IFTYPE_AP_VLAN:
3239 case NL80211_IFTYPE_P2P_GO:
3240 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3241 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3242 BIT(NL80211_STA_FLAG_WME) |
3243 BIT(NL80211_STA_FLAG_MFP);
3244 break;
3245 case NL80211_IFTYPE_P2P_CLIENT:
3246 case NL80211_IFTYPE_STATION:
3247 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3248 BIT(NL80211_STA_FLAG_TDLS_PEER);
3249 break;
3250 case NL80211_IFTYPE_MESH_POINT:
3251 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3252 BIT(NL80211_STA_FLAG_MFP) |
3253 BIT(NL80211_STA_FLAG_AUTHORIZED);
3254 default:
3255 return -EINVAL;
3256 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003257
Johannes Berg3383b5a2012-05-10 20:14:43 +02003258 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3259 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003260 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003261
Johannes Berg3383b5a2012-05-10 20:14:43 +02003262 /* no longer support new API additions in old API */
3263 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3264 return -EINVAL;
3265 }
3266 }
3267
Johannes Berg5727ef12007-12-19 02:03:34 +01003268 return 0;
3269}
3270
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003271static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3272 int attr)
3273{
3274 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003275 u32 bitrate;
3276 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003277
3278 rate = nla_nest_start(msg, attr);
3279 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003280 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003281
3282 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3283 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003284 /* report 16-bit bitrate only if we can */
3285 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003286 if (bitrate > 0 &&
3287 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3288 return false;
3289 if (bitrate_compat > 0 &&
3290 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3291 return false;
3292
3293 if (info->flags & RATE_INFO_FLAGS_MCS) {
3294 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3295 return false;
3296 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3297 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3298 return false;
3299 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3300 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3301 return false;
3302 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3303 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3304 return false;
3305 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3306 return false;
3307 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3308 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3309 return false;
3310 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3311 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3312 return false;
3313 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3314 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3315 return false;
3316 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3317 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3318 return false;
3319 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3320 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3321 return false;
3322 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003323
3324 nla_nest_end(msg, rate);
3325 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003326}
3327
Felix Fietkau119363c2013-04-22 16:29:30 +02003328static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3329 int id)
3330{
3331 void *attr;
3332 int i = 0;
3333
3334 if (!mask)
3335 return true;
3336
3337 attr = nla_nest_start(msg, id);
3338 if (!attr)
3339 return false;
3340
3341 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3342 if (!(mask & BIT(i)))
3343 continue;
3344
3345 if (nla_put_u8(msg, i, signal[i]))
3346 return false;
3347 }
3348
3349 nla_nest_end(msg, attr);
3350
3351 return true;
3352}
3353
Eric W. Biederman15e47302012-09-07 20:12:54 +00003354static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003355 int flags,
3356 struct cfg80211_registered_device *rdev,
3357 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003358 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003359{
3360 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003361 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003362
Eric W. Biederman15e47302012-09-07 20:12:54 +00003363 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003364 if (!hdr)
3365 return -1;
3366
David S. Miller9360ffd2012-03-29 04:41:26 -04003367 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3368 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3369 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3370 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003371
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003372 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3373 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003374 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003375 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3376 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3377 sinfo->connected_time))
3378 goto nla_put_failure;
3379 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3380 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3381 sinfo->inactive_time))
3382 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003383 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3384 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003385 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003386 (u32)sinfo->rx_bytes))
3387 goto nla_put_failure;
3388 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003389 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003390 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3391 (u32)sinfo->tx_bytes))
3392 goto nla_put_failure;
3393 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3394 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003395 sinfo->rx_bytes))
3396 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003397 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3398 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003399 sinfo->tx_bytes))
3400 goto nla_put_failure;
3401 if ((sinfo->filled & STATION_INFO_LLID) &&
3402 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3403 goto nla_put_failure;
3404 if ((sinfo->filled & STATION_INFO_PLID) &&
3405 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3406 goto nla_put_failure;
3407 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3408 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3409 sinfo->plink_state))
3410 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003411 switch (rdev->wiphy.signal_type) {
3412 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003413 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3414 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3415 sinfo->signal))
3416 goto nla_put_failure;
3417 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3418 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3419 sinfo->signal_avg))
3420 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003421 break;
3422 default:
3423 break;
3424 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003425 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3426 if (!nl80211_put_signal(msg, sinfo->chains,
3427 sinfo->chain_signal,
3428 NL80211_STA_INFO_CHAIN_SIGNAL))
3429 goto nla_put_failure;
3430 }
3431 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3432 if (!nl80211_put_signal(msg, sinfo->chains,
3433 sinfo->chain_signal_avg,
3434 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3435 goto nla_put_failure;
3436 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003437 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003438 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3439 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003440 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003441 }
3442 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3443 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3444 NL80211_STA_INFO_RX_BITRATE))
3445 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003446 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003447 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3448 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3449 sinfo->rx_packets))
3450 goto nla_put_failure;
3451 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3452 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3453 sinfo->tx_packets))
3454 goto nla_put_failure;
3455 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3456 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3457 sinfo->tx_retries))
3458 goto nla_put_failure;
3459 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3460 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3461 sinfo->tx_failed))
3462 goto nla_put_failure;
3463 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3464 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3465 sinfo->beacon_loss_count))
3466 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003467 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3468 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3469 sinfo->local_pm))
3470 goto nla_put_failure;
3471 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3472 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3473 sinfo->peer_pm))
3474 goto nla_put_failure;
3475 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3476 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3477 sinfo->nonpeer_pm))
3478 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003479 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3480 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3481 if (!bss_param)
3482 goto nla_put_failure;
3483
David S. Miller9360ffd2012-03-29 04:41:26 -04003484 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3485 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3486 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3487 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3488 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3489 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3490 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3491 sinfo->bss_param.dtim_period) ||
3492 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3493 sinfo->bss_param.beacon_interval))
3494 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003495
3496 nla_nest_end(msg, bss_param);
3497 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003498 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3499 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3500 sizeof(struct nl80211_sta_flag_update),
3501 &sinfo->sta_flags))
3502 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003503 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3504 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3505 sinfo->t_offset))
3506 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003507 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003508
David S. Miller9360ffd2012-03-29 04:41:26 -04003509 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3510 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3511 sinfo->assoc_req_ies))
3512 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003513
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003514 return genlmsg_end(msg, hdr);
3515
3516 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003517 genlmsg_cancel(msg, hdr);
3518 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003519}
3520
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003521static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003522 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003523{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003524 struct station_info sinfo;
3525 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003526 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003527 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003528 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003529 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003530
Johannes Berg97990a02013-04-19 01:02:55 +02003531 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003532 if (err)
3533 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003534
Johannes Berg97990a02013-04-19 01:02:55 +02003535 if (!wdev->netdev) {
3536 err = -EINVAL;
3537 goto out_err;
3538 }
3539
Johannes Bergbba95fe2008-07-29 13:22:51 +02003540 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003541 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003542 goto out_err;
3543 }
3544
Johannes Bergbba95fe2008-07-29 13:22:51 +02003545 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003546 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003547 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003548 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003549 if (err == -ENOENT)
3550 break;
3551 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003552 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003553
3554 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003555 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003556 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003557 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003558 &sinfo) < 0)
3559 goto out;
3560
3561 sta_idx++;
3562 }
3563
3564
3565 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003566 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003567 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003568 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003569 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003570
3571 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003572}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003573
Johannes Berg5727ef12007-12-19 02:03:34 +01003574static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3575{
Johannes Berg4c476992010-10-04 21:36:35 +02003576 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3577 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003578 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003579 struct sk_buff *msg;
3580 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003581 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003582
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003583 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003584
3585 if (!info->attrs[NL80211_ATTR_MAC])
3586 return -EINVAL;
3587
3588 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3589
Johannes Berg4c476992010-10-04 21:36:35 +02003590 if (!rdev->ops->get_station)
3591 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003592
Hila Gonene35e4d22012-06-27 17:19:42 +03003593 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003594 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003595 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003596
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003597 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003598 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003599 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003600
Eric W. Biederman15e47302012-09-07 20:12:54 +00003601 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003602 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003603 nlmsg_free(msg);
3604 return -ENOBUFS;
3605 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003606
Johannes Berg4c476992010-10-04 21:36:35 +02003607 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003608}
3609
Johannes Berg77ee7c82013-02-15 00:48:33 +01003610int cfg80211_check_station_change(struct wiphy *wiphy,
3611 struct station_parameters *params,
3612 enum cfg80211_station_type statype)
3613{
3614 if (params->listen_interval != -1)
3615 return -EINVAL;
3616 if (params->aid)
3617 return -EINVAL;
3618
3619 /* When you run into this, adjust the code below for the new flag */
3620 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3621
3622 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003623 case CFG80211_STA_MESH_PEER_KERNEL:
3624 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003625 /*
3626 * No ignoring the TDLS flag here -- the userspace mesh
3627 * code doesn't have the bug of including TDLS in the
3628 * mask everywhere.
3629 */
3630 if (params->sta_flags_mask &
3631 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3632 BIT(NL80211_STA_FLAG_MFP) |
3633 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3634 return -EINVAL;
3635 break;
3636 case CFG80211_STA_TDLS_PEER_SETUP:
3637 case CFG80211_STA_TDLS_PEER_ACTIVE:
3638 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3639 return -EINVAL;
3640 /* ignore since it can't change */
3641 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3642 break;
3643 default:
3644 /* disallow mesh-specific things */
3645 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3646 return -EINVAL;
3647 if (params->local_pm)
3648 return -EINVAL;
3649 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3650 return -EINVAL;
3651 }
3652
3653 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3654 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3655 /* TDLS can't be set, ... */
3656 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3657 return -EINVAL;
3658 /*
3659 * ... but don't bother the driver with it. This works around
3660 * a hostapd/wpa_supplicant issue -- it always includes the
3661 * TLDS_PEER flag in the mask even for AP mode.
3662 */
3663 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3664 }
3665
3666 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3667 /* reject other things that can't change */
3668 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3669 return -EINVAL;
3670 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3671 return -EINVAL;
3672 if (params->supported_rates)
3673 return -EINVAL;
3674 if (params->ext_capab || params->ht_capa || params->vht_capa)
3675 return -EINVAL;
3676 }
3677
3678 if (statype != CFG80211_STA_AP_CLIENT) {
3679 if (params->vlan)
3680 return -EINVAL;
3681 }
3682
3683 switch (statype) {
3684 case CFG80211_STA_AP_MLME_CLIENT:
3685 /* Use this only for authorizing/unauthorizing a station */
3686 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3687 return -EOPNOTSUPP;
3688 break;
3689 case CFG80211_STA_AP_CLIENT:
3690 /* accept only the listed bits */
3691 if (params->sta_flags_mask &
3692 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3693 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3694 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3695 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3696 BIT(NL80211_STA_FLAG_WME) |
3697 BIT(NL80211_STA_FLAG_MFP)))
3698 return -EINVAL;
3699
3700 /* but authenticated/associated only if driver handles it */
3701 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3702 params->sta_flags_mask &
3703 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3704 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3705 return -EINVAL;
3706 break;
3707 case CFG80211_STA_IBSS:
3708 case CFG80211_STA_AP_STA:
3709 /* reject any changes other than AUTHORIZED */
3710 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3711 return -EINVAL;
3712 break;
3713 case CFG80211_STA_TDLS_PEER_SETUP:
3714 /* reject any changes other than AUTHORIZED or WME */
3715 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3716 BIT(NL80211_STA_FLAG_WME)))
3717 return -EINVAL;
3718 /* force (at least) rates when authorizing */
3719 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3720 !params->supported_rates)
3721 return -EINVAL;
3722 break;
3723 case CFG80211_STA_TDLS_PEER_ACTIVE:
3724 /* reject any changes */
3725 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003726 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003727 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3728 return -EINVAL;
3729 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003730 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003731 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3732 return -EINVAL;
3733 break;
3734 }
3735
3736 return 0;
3737}
3738EXPORT_SYMBOL(cfg80211_check_station_change);
3739
Johannes Berg5727ef12007-12-19 02:03:34 +01003740/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003741 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003742 */
Johannes Berg80b99892011-11-18 16:23:01 +01003743static struct net_device *get_vlan(struct genl_info *info,
3744 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003745{
Johannes Berg463d0182009-07-14 00:33:35 +02003746 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003747 struct net_device *v;
3748 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003749
Johannes Berg80b99892011-11-18 16:23:01 +01003750 if (!vlanattr)
3751 return NULL;
3752
3753 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3754 if (!v)
3755 return ERR_PTR(-ENODEV);
3756
3757 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3758 ret = -EINVAL;
3759 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003760 }
Johannes Berg80b99892011-11-18 16:23:01 +01003761
Johannes Berg77ee7c82013-02-15 00:48:33 +01003762 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3763 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3764 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3765 ret = -EINVAL;
3766 goto error;
3767 }
3768
Johannes Berg80b99892011-11-18 16:23:01 +01003769 if (!netif_running(v)) {
3770 ret = -ENETDOWN;
3771 goto error;
3772 }
3773
3774 return v;
3775 error:
3776 dev_put(v);
3777 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003778}
3779
Jouni Malinendf881292013-02-14 21:10:54 +02003780static struct nla_policy
3781nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3782 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3783 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3784};
3785
Johannes Bergff276692013-02-15 00:09:01 +01003786static int nl80211_parse_sta_wme(struct genl_info *info,
3787 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003788{
Jouni Malinendf881292013-02-14 21:10:54 +02003789 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3790 struct nlattr *nla;
3791 int err;
3792
Jouni Malinendf881292013-02-14 21:10:54 +02003793 /* parse WME attributes if present */
3794 if (!info->attrs[NL80211_ATTR_STA_WME])
3795 return 0;
3796
3797 nla = info->attrs[NL80211_ATTR_STA_WME];
3798 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3799 nl80211_sta_wme_policy);
3800 if (err)
3801 return err;
3802
3803 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3804 params->uapsd_queues = nla_get_u8(
3805 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3806 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3807 return -EINVAL;
3808
3809 if (tb[NL80211_STA_WME_MAX_SP])
3810 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3811
3812 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3813 return -EINVAL;
3814
3815 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3816
3817 return 0;
3818}
3819
Johannes Bergff276692013-02-15 00:09:01 +01003820static int nl80211_set_station_tdls(struct genl_info *info,
3821 struct station_parameters *params)
3822{
3823 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03003824 if (info->attrs[NL80211_ATTR_PEER_AID])
3825 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01003826 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3827 params->ht_capa =
3828 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3829 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3830 params->vht_capa =
3831 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3832
3833 return nl80211_parse_sta_wme(info, params);
3834}
3835
Johannes Berg5727ef12007-12-19 02:03:34 +01003836static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3837{
Johannes Berg4c476992010-10-04 21:36:35 +02003838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003839 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003840 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003841 u8 *mac_addr;
3842 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003843
3844 memset(&params, 0, sizeof(params));
3845
3846 params.listen_interval = -1;
3847
Johannes Berg77ee7c82013-02-15 00:48:33 +01003848 if (!rdev->ops->change_station)
3849 return -EOPNOTSUPP;
3850
Johannes Berg5727ef12007-12-19 02:03:34 +01003851 if (info->attrs[NL80211_ATTR_STA_AID])
3852 return -EINVAL;
3853
3854 if (!info->attrs[NL80211_ATTR_MAC])
3855 return -EINVAL;
3856
3857 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3858
3859 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3860 params.supported_rates =
3861 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3862 params.supported_rates_len =
3863 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3864 }
3865
Jouni Malinen9d62a982013-02-14 21:10:13 +02003866 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3867 params.capability =
3868 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3869 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3870 }
3871
3872 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3873 params.ext_capab =
3874 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3875 params.ext_capab_len =
3876 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3877 }
3878
Jouni Malinendf881292013-02-14 21:10:54 +02003879 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01003880 return -EINVAL;
Jouni Malinen36aedc92008-08-25 11:58:58 +03003881
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003882 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003883 return -EINVAL;
3884
Johannes Bergf8bacc22013-02-14 23:27:01 +01003885 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003886 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003887 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3888 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
3889 return -EINVAL;
3890 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003891
Johannes Bergf8bacc22013-02-14 23:27:01 +01003892 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07003893 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01003894 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3895 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
3896 return -EINVAL;
3897 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
3898 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07003899
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003900 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
3901 enum nl80211_mesh_power_mode pm = nla_get_u32(
3902 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
3903
3904 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
3905 pm > NL80211_MESH_POWER_MAX)
3906 return -EINVAL;
3907
3908 params.local_pm = pm;
3909 }
3910
Johannes Berg77ee7c82013-02-15 00:48:33 +01003911 /* Include parameters for TDLS peer (will check later) */
3912 err = nl80211_set_station_tdls(info, &params);
3913 if (err)
3914 return err;
3915
3916 params.vlan = get_vlan(info, rdev);
3917 if (IS_ERR(params.vlan))
3918 return PTR_ERR(params.vlan);
3919
Johannes Berga97f4422009-06-18 17:23:43 +02003920 switch (dev->ieee80211_ptr->iftype) {
3921 case NL80211_IFTYPE_AP:
3922 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003923 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003924 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003925 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01003926 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02003927 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02003928 break;
3929 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003930 err = -EOPNOTSUPP;
3931 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02003932 }
3933
Johannes Berg77ee7c82013-02-15 00:48:33 +01003934 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03003935 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003936
Johannes Berg77ee7c82013-02-15 00:48:33 +01003937 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01003938 if (params.vlan)
3939 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003940
Johannes Berg5727ef12007-12-19 02:03:34 +01003941 return err;
3942}
3943
3944static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3945{
Johannes Berg4c476992010-10-04 21:36:35 +02003946 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003947 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003948 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003949 struct station_parameters params;
3950 u8 *mac_addr = NULL;
3951
3952 memset(&params, 0, sizeof(params));
3953
Johannes Berg984c3112013-02-14 23:43:25 +01003954 if (!rdev->ops->add_station)
3955 return -EOPNOTSUPP;
3956
Johannes Berg5727ef12007-12-19 02:03:34 +01003957 if (!info->attrs[NL80211_ATTR_MAC])
3958 return -EINVAL;
3959
Johannes Berg5727ef12007-12-19 02:03:34 +01003960 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3961 return -EINVAL;
3962
3963 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3964 return -EINVAL;
3965
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03003966 if (!info->attrs[NL80211_ATTR_STA_AID] &&
3967 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003968 return -EINVAL;
3969
Johannes Berg5727ef12007-12-19 02:03:34 +01003970 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3971 params.supported_rates =
3972 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3973 params.supported_rates_len =
3974 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3975 params.listen_interval =
3976 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003977
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03003978 if (info->attrs[NL80211_ATTR_STA_AID])
3979 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3980 else
3981 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003982 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3983 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003984
Jouni Malinen9d62a982013-02-14 21:10:13 +02003985 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
3986 params.capability =
3987 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
3988 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
3989 }
3990
3991 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
3992 params.ext_capab =
3993 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3994 params.ext_capab_len =
3995 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
3996 }
3997
Jouni Malinen36aedc92008-08-25 11:58:58 +03003998 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3999 params.ht_capa =
4000 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004001
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004002 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4003 params.vht_capa =
4004 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4005
Johannes Bergf8bacc22013-02-14 23:27:01 +01004006 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004007 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004008 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4009 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4010 return -EINVAL;
4011 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004012
Johannes Bergff276692013-02-15 00:09:01 +01004013 err = nl80211_parse_sta_wme(info, &params);
4014 if (err)
4015 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004016
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004017 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004018 return -EINVAL;
4019
Johannes Berg77ee7c82013-02-15 00:48:33 +01004020 /* When you run into this, adjust the code below for the new flag */
4021 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4022
Johannes Bergbdd90d52011-12-14 12:20:27 +01004023 switch (dev->ieee80211_ptr->iftype) {
4024 case NL80211_IFTYPE_AP:
4025 case NL80211_IFTYPE_AP_VLAN:
4026 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004027 /* ignore WME attributes if iface/sta is not capable */
4028 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4029 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4030 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004031
Johannes Bergbdd90d52011-12-14 12:20:27 +01004032 /* TDLS peers cannot be added */
4033 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004034 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004035 /* but don't bother the driver with it */
4036 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004037
Johannes Bergd582cff2012-10-26 17:53:44 +02004038 /* allow authenticated/associated only if driver handles it */
4039 if (!(rdev->wiphy.features &
4040 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4041 params.sta_flags_mask &
4042 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4043 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4044 return -EINVAL;
4045
Johannes Bergbdd90d52011-12-14 12:20:27 +01004046 /* must be last in here for error handling */
4047 params.vlan = get_vlan(info, rdev);
4048 if (IS_ERR(params.vlan))
4049 return PTR_ERR(params.vlan);
4050 break;
4051 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004052 /* ignore uAPSD data */
4053 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4054
Johannes Bergd582cff2012-10-26 17:53:44 +02004055 /* associated is disallowed */
4056 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4057 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004058 /* TDLS peers cannot be added */
4059 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02004060 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004061 break;
4062 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004063 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004064 /* ignore uAPSD data */
4065 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4066
Johannes Berg77ee7c82013-02-15 00:48:33 +01004067 /* these are disallowed */
4068 if (params.sta_flags_mask &
4069 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4070 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004071 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004072 /* Only TDLS peers can be added */
4073 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4074 return -EINVAL;
4075 /* Can only add if TDLS ... */
4076 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4077 return -EOPNOTSUPP;
4078 /* ... with external setup is supported */
4079 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4080 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004081 /*
4082 * Older wpa_supplicant versions always mark the TDLS peer
4083 * as authorized, but it shouldn't yet be.
4084 */
4085 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004086 break;
4087 default:
4088 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004089 }
4090
Johannes Bergbdd90d52011-12-14 12:20:27 +01004091 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004092
Hila Gonene35e4d22012-06-27 17:19:42 +03004093 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004094
Johannes Berg5727ef12007-12-19 02:03:34 +01004095 if (params.vlan)
4096 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004097 return err;
4098}
4099
4100static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4101{
Johannes Berg4c476992010-10-04 21:36:35 +02004102 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4103 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004104 u8 *mac_addr = NULL;
4105
4106 if (info->attrs[NL80211_ATTR_MAC])
4107 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4108
Johannes Berge80cf852009-05-11 14:43:13 +02004109 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004110 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004111 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004112 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4113 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004114
Johannes Berg4c476992010-10-04 21:36:35 +02004115 if (!rdev->ops->del_station)
4116 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004117
Hila Gonene35e4d22012-06-27 17:19:42 +03004118 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004119}
4120
Eric W. Biederman15e47302012-09-07 20:12:54 +00004121static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004122 int flags, struct net_device *dev,
4123 u8 *dst, u8 *next_hop,
4124 struct mpath_info *pinfo)
4125{
4126 void *hdr;
4127 struct nlattr *pinfoattr;
4128
Eric W. Biederman15e47302012-09-07 20:12:54 +00004129 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004130 if (!hdr)
4131 return -1;
4132
David S. Miller9360ffd2012-03-29 04:41:26 -04004133 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4134 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4135 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4136 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4137 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004138
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004139 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4140 if (!pinfoattr)
4141 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004142 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4143 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4144 pinfo->frame_qlen))
4145 goto nla_put_failure;
4146 if (((pinfo->filled & MPATH_INFO_SN) &&
4147 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4148 ((pinfo->filled & MPATH_INFO_METRIC) &&
4149 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4150 pinfo->metric)) ||
4151 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4152 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4153 pinfo->exptime)) ||
4154 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4155 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4156 pinfo->flags)) ||
4157 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4158 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4159 pinfo->discovery_timeout)) ||
4160 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4161 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4162 pinfo->discovery_retries)))
4163 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004164
4165 nla_nest_end(msg, pinfoattr);
4166
4167 return genlmsg_end(msg, hdr);
4168
4169 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004170 genlmsg_cancel(msg, hdr);
4171 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004172}
4173
4174static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004175 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004176{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004177 struct mpath_info pinfo;
4178 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004179 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004180 u8 dst[ETH_ALEN];
4181 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004182 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004183 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004184
Johannes Berg97990a02013-04-19 01:02:55 +02004185 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004186 if (err)
4187 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004188
4189 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004190 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004191 goto out_err;
4192 }
4193
Johannes Berg97990a02013-04-19 01:02:55 +02004194 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004195 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004196 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004197 }
4198
Johannes Bergbba95fe2008-07-29 13:22:51 +02004199 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004200 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4201 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004202 if (err == -ENOENT)
4203 break;
4204 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004205 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004206
Eric W. Biederman15e47302012-09-07 20:12:54 +00004207 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004208 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004209 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004210 &pinfo) < 0)
4211 goto out;
4212
4213 path_idx++;
4214 }
4215
4216
4217 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004218 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004219 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004220 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004221 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004222 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004223}
4224
4225static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4226{
Johannes Berg4c476992010-10-04 21:36:35 +02004227 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004228 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004229 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004230 struct mpath_info pinfo;
4231 struct sk_buff *msg;
4232 u8 *dst = NULL;
4233 u8 next_hop[ETH_ALEN];
4234
4235 memset(&pinfo, 0, sizeof(pinfo));
4236
4237 if (!info->attrs[NL80211_ATTR_MAC])
4238 return -EINVAL;
4239
4240 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4241
Johannes Berg4c476992010-10-04 21:36:35 +02004242 if (!rdev->ops->get_mpath)
4243 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004244
Johannes Berg4c476992010-10-04 21:36:35 +02004245 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4246 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004247
Hila Gonene35e4d22012-06-27 17:19:42 +03004248 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004249 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004250 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004251
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004252 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004253 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004254 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004255
Eric W. Biederman15e47302012-09-07 20:12:54 +00004256 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004257 dev, dst, next_hop, &pinfo) < 0) {
4258 nlmsg_free(msg);
4259 return -ENOBUFS;
4260 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004261
Johannes Berg4c476992010-10-04 21:36:35 +02004262 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004263}
4264
4265static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4266{
Johannes Berg4c476992010-10-04 21:36:35 +02004267 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4268 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004269 u8 *dst = NULL;
4270 u8 *next_hop = NULL;
4271
4272 if (!info->attrs[NL80211_ATTR_MAC])
4273 return -EINVAL;
4274
4275 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4276 return -EINVAL;
4277
4278 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4279 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4280
Johannes Berg4c476992010-10-04 21:36:35 +02004281 if (!rdev->ops->change_mpath)
4282 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004283
Johannes Berg4c476992010-10-04 21:36:35 +02004284 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4285 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004286
Hila Gonene35e4d22012-06-27 17:19:42 +03004287 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004288}
Johannes Berg4c476992010-10-04 21:36:35 +02004289
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004290static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4291{
Johannes Berg4c476992010-10-04 21:36:35 +02004292 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4293 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004294 u8 *dst = NULL;
4295 u8 *next_hop = NULL;
4296
4297 if (!info->attrs[NL80211_ATTR_MAC])
4298 return -EINVAL;
4299
4300 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4301 return -EINVAL;
4302
4303 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4304 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4305
Johannes Berg4c476992010-10-04 21:36:35 +02004306 if (!rdev->ops->add_mpath)
4307 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004308
Johannes Berg4c476992010-10-04 21:36:35 +02004309 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4310 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004311
Hila Gonene35e4d22012-06-27 17:19:42 +03004312 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004313}
4314
4315static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4316{
Johannes Berg4c476992010-10-04 21:36:35 +02004317 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4318 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004319 u8 *dst = NULL;
4320
4321 if (info->attrs[NL80211_ATTR_MAC])
4322 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4323
Johannes Berg4c476992010-10-04 21:36:35 +02004324 if (!rdev->ops->del_mpath)
4325 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004326
Hila Gonene35e4d22012-06-27 17:19:42 +03004327 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004328}
4329
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004330static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4331{
Johannes Berg4c476992010-10-04 21:36:35 +02004332 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4333 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004334 struct bss_parameters params;
4335
4336 memset(&params, 0, sizeof(params));
4337 /* default to not changing parameters */
4338 params.use_cts_prot = -1;
4339 params.use_short_preamble = -1;
4340 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004341 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004342 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004343 params.p2p_ctwindow = -1;
4344 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004345
4346 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4347 params.use_cts_prot =
4348 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4349 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4350 params.use_short_preamble =
4351 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4352 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4353 params.use_short_slot_time =
4354 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004355 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4356 params.basic_rates =
4357 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4358 params.basic_rates_len =
4359 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4360 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004361 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4362 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004363 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4364 params.ht_opmode =
4365 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004366
Johannes Berg53cabad2012-11-14 15:17:28 +01004367 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4368 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4369 return -EINVAL;
4370 params.p2p_ctwindow =
4371 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4372 if (params.p2p_ctwindow < 0)
4373 return -EINVAL;
4374 if (params.p2p_ctwindow != 0 &&
4375 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4376 return -EINVAL;
4377 }
4378
4379 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4380 u8 tmp;
4381
4382 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4383 return -EINVAL;
4384 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4385 if (tmp > 1)
4386 return -EINVAL;
4387 params.p2p_opp_ps = tmp;
4388 if (params.p2p_opp_ps &&
4389 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4390 return -EINVAL;
4391 }
4392
Johannes Berg4c476992010-10-04 21:36:35 +02004393 if (!rdev->ops->change_bss)
4394 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004395
Johannes Berg074ac8d2010-09-16 14:58:22 +02004396 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004397 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4398 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004399
Hila Gonene35e4d22012-06-27 17:19:42 +03004400 return rdev_change_bss(rdev, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004401}
4402
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004403static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004404 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4405 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4406 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4407 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4408 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4409 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4410};
4411
4412static int parse_reg_rule(struct nlattr *tb[],
4413 struct ieee80211_reg_rule *reg_rule)
4414{
4415 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4416 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4417
4418 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4419 return -EINVAL;
4420 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4421 return -EINVAL;
4422 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4423 return -EINVAL;
4424 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4425 return -EINVAL;
4426 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4427 return -EINVAL;
4428
4429 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4430
4431 freq_range->start_freq_khz =
4432 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4433 freq_range->end_freq_khz =
4434 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4435 freq_range->max_bandwidth_khz =
4436 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4437
4438 power_rule->max_eirp =
4439 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4440
4441 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4442 power_rule->max_antenna_gain =
4443 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4444
4445 return 0;
4446}
4447
4448static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4449{
4450 int r;
4451 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004452 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004453
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004454 /*
4455 * You should only get this when cfg80211 hasn't yet initialized
4456 * completely when built-in to the kernel right between the time
4457 * window between nl80211_init() and regulatory_init(), if that is
4458 * even possible.
4459 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004460 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004461 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004462
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004463 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4464 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004465
4466 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4467
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004468 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4469 user_reg_hint_type =
4470 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4471 else
4472 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4473
4474 switch (user_reg_hint_type) {
4475 case NL80211_USER_REG_HINT_USER:
4476 case NL80211_USER_REG_HINT_CELL_BASE:
4477 break;
4478 default:
4479 return -EINVAL;
4480 }
4481
4482 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004483
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004484 return r;
4485}
4486
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004487static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004488 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004489{
Johannes Berg4c476992010-10-04 21:36:35 +02004490 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004491 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004492 struct wireless_dev *wdev = dev->ieee80211_ptr;
4493 struct mesh_config cur_params;
4494 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004495 void *hdr;
4496 struct nlattr *pinfoattr;
4497 struct sk_buff *msg;
4498
Johannes Berg29cbe682010-12-03 09:20:44 +01004499 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4500 return -EOPNOTSUPP;
4501
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004502 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004503 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004504
Johannes Berg29cbe682010-12-03 09:20:44 +01004505 wdev_lock(wdev);
4506 /* If not connected, get default parameters */
4507 if (!wdev->mesh_id_len)
4508 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4509 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004510 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004511 wdev_unlock(wdev);
4512
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004513 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004514 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004515
4516 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004517 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004518 if (!msg)
4519 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004520 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004521 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004522 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004523 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004524 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004525 if (!pinfoattr)
4526 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004527 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4528 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4529 cur_params.dot11MeshRetryTimeout) ||
4530 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4531 cur_params.dot11MeshConfirmTimeout) ||
4532 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4533 cur_params.dot11MeshHoldingTimeout) ||
4534 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4535 cur_params.dot11MeshMaxPeerLinks) ||
4536 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4537 cur_params.dot11MeshMaxRetries) ||
4538 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4539 cur_params.dot11MeshTTL) ||
4540 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4541 cur_params.element_ttl) ||
4542 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4543 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004544 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4545 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004546 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4547 cur_params.dot11MeshHWMPmaxPREQretries) ||
4548 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4549 cur_params.path_refresh_time) ||
4550 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4551 cur_params.min_discovery_timeout) ||
4552 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4553 cur_params.dot11MeshHWMPactivePathTimeout) ||
4554 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4555 cur_params.dot11MeshHWMPpreqMinInterval) ||
4556 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4557 cur_params.dot11MeshHWMPperrMinInterval) ||
4558 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4559 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4560 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4561 cur_params.dot11MeshHWMPRootMode) ||
4562 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4563 cur_params.dot11MeshHWMPRannInterval) ||
4564 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4565 cur_params.dot11MeshGateAnnouncementProtocol) ||
4566 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4567 cur_params.dot11MeshForwarding) ||
4568 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004569 cur_params.rssi_threshold) ||
4570 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004571 cur_params.ht_opmode) ||
4572 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4573 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4574 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004575 cur_params.dot11MeshHWMProotInterval) ||
4576 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004577 cur_params.dot11MeshHWMPconfirmationInterval) ||
4578 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4579 cur_params.power_mode) ||
4580 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
4581 cur_params.dot11MeshAwakeWindowDuration))
David S. Miller9360ffd2012-03-29 04:41:26 -04004582 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004583 nla_nest_end(msg, pinfoattr);
4584 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004585 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004586
Johannes Berg3b858752009-03-12 09:55:09 +01004587 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004588 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004589 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004590 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004591 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004592}
4593
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004594static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004595 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4596 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4597 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4598 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4599 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4600 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004601 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004602 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004603 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004604 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4605 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4606 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4607 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4608 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004609 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004610 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004611 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004612 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004613 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004614 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004615 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4616 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004617 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4618 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004619 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004620 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4621 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004622};
4623
Javier Cardonac80d5452010-12-16 17:37:49 -08004624static const struct nla_policy
4625 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004626 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004627 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4628 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004629 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004630 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004631 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004632 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004633 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004634 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004635};
4636
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004637static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004638 struct mesh_config *cfg,
4639 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004640{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004641 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004642 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004643
Marco Porschea54fba2013-01-07 16:04:48 +01004644#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4645do { \
4646 if (tb[attr]) { \
4647 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4648 return -EINVAL; \
4649 cfg->param = fn(tb[attr]); \
4650 mask |= (1 << (attr - 1)); \
4651 } \
4652} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004653
4654
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004655 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004656 return -EINVAL;
4657 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004658 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004659 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004660 return -EINVAL;
4661
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004662 /* This makes sure that there aren't more than 32 mesh config
4663 * parameters (otherwise our bitfield scheme would not work.) */
4664 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4665
4666 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004667 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004668 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4669 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004670 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004671 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4672 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004673 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004674 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4675 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004676 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004677 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4678 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004679 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004680 mask, NL80211_MESHCONF_MAX_RETRIES,
4681 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004682 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004683 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004684 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004685 mask, NL80211_MESHCONF_ELEMENT_TTL,
4686 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004687 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004688 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4689 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004690 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4691 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004692 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4693 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004694 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004695 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4696 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004697 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004698 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4699 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004700 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004701 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4702 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004703 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4704 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004705 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4706 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004707 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004708 1, 65535, mask,
4709 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004710 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004712 1, 65535, mask,
4713 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004714 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004715 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004716 dot11MeshHWMPnetDiameterTraversalTime,
4717 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004718 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4719 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004720 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4721 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4722 nla_get_u8);
4723 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4724 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004725 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004726 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004727 dot11MeshGateAnnouncementProtocol, 0, 1,
4728 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004729 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004730 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004731 mask, NL80211_MESHCONF_FORWARDING,
4732 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004733 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004734 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
4735 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004736 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004737 mask, NL80211_MESHCONF_HT_OPMODE,
4738 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004739 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004740 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004741 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4742 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004743 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004744 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4745 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004746 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004747 dot11MeshHWMPconfirmationInterval,
4748 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004749 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4750 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004751 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4752 NL80211_MESH_POWER_ACTIVE,
4753 NL80211_MESH_POWER_MAX,
4754 mask, NL80211_MESHCONF_POWER_MODE,
4755 nla_get_u32);
4756 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4757 0, 65535, mask,
4758 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004759 if (mask_out)
4760 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004761
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004762 return 0;
4763
4764#undef FILL_IN_MESH_PARAM_IF_SET
4765}
4766
Javier Cardonac80d5452010-12-16 17:37:49 -08004767static int nl80211_parse_mesh_setup(struct genl_info *info,
4768 struct mesh_setup *setup)
4769{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004770 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004771 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4772
4773 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4774 return -EINVAL;
4775 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4776 info->attrs[NL80211_ATTR_MESH_SETUP],
4777 nl80211_mesh_setup_params_policy))
4778 return -EINVAL;
4779
Javier Cardonad299a1f2012-03-31 11:31:33 -07004780 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4781 setup->sync_method =
4782 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4783 IEEE80211_SYNC_METHOD_VENDOR :
4784 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4785
Javier Cardonac80d5452010-12-16 17:37:49 -08004786 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4787 setup->path_sel_proto =
4788 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4789 IEEE80211_PATH_PROTOCOL_VENDOR :
4790 IEEE80211_PATH_PROTOCOL_HWMP;
4791
4792 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4793 setup->path_metric =
4794 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4795 IEEE80211_PATH_METRIC_VENDOR :
4796 IEEE80211_PATH_METRIC_AIRTIME;
4797
Javier Cardona581a8b02011-04-07 15:08:27 -07004798
4799 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004800 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004801 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004802 if (!is_valid_ie_attr(ieattr))
4803 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004804 setup->ie = nla_data(ieattr);
4805 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004806 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004807 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4808 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4809 return -EINVAL;
4810 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004811 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4812 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004813 if (setup->is_secure)
4814 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08004815
Colleen Twitty6e16d902013-05-08 11:45:59 -07004816 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
4817 if (!setup->user_mpm)
4818 return -EINVAL;
4819 setup->auth_id =
4820 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
4821 }
4822
Javier Cardonac80d5452010-12-16 17:37:49 -08004823 return 0;
4824}
4825
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004826static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004827 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004828{
4829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4830 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004831 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004832 struct mesh_config cfg;
4833 u32 mask;
4834 int err;
4835
Johannes Berg29cbe682010-12-03 09:20:44 +01004836 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4837 return -EOPNOTSUPP;
4838
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004839 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004840 return -EOPNOTSUPP;
4841
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004842 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004843 if (err)
4844 return err;
4845
Johannes Berg29cbe682010-12-03 09:20:44 +01004846 wdev_lock(wdev);
4847 if (!wdev->mesh_id_len)
4848 err = -ENOLINK;
4849
4850 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03004851 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004852
4853 wdev_unlock(wdev);
4854
4855 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004856}
4857
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004858static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4859{
Johannes Berg458f4f92012-12-06 15:47:38 +01004860 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004861 struct sk_buff *msg;
4862 void *hdr = NULL;
4863 struct nlattr *nl_reg_rules;
4864 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004865
4866 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02004867 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004868
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004869 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02004870 if (!msg)
4871 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004872
Eric W. Biederman15e47302012-09-07 20:12:54 +00004873 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004874 NL80211_CMD_GET_REG);
4875 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004876 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004877
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004878 if (reg_last_request_cell_base() &&
4879 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4880 NL80211_USER_REG_HINT_CELL_BASE))
4881 goto nla_put_failure;
4882
Johannes Berg458f4f92012-12-06 15:47:38 +01004883 rcu_read_lock();
4884 regdom = rcu_dereference(cfg80211_regdomain);
4885
4886 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
4887 (regdom->dfs_region &&
4888 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
4889 goto nla_put_failure_rcu;
4890
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004891 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4892 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01004893 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004894
Johannes Berg458f4f92012-12-06 15:47:38 +01004895 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004896 struct nlattr *nl_reg_rule;
4897 const struct ieee80211_reg_rule *reg_rule;
4898 const struct ieee80211_freq_range *freq_range;
4899 const struct ieee80211_power_rule *power_rule;
4900
Johannes Berg458f4f92012-12-06 15:47:38 +01004901 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004902 freq_range = &reg_rule->freq_range;
4903 power_rule = &reg_rule->power_rule;
4904
4905 nl_reg_rule = nla_nest_start(msg, i);
4906 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01004907 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004908
David S. Miller9360ffd2012-03-29 04:41:26 -04004909 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4910 reg_rule->flags) ||
4911 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4912 freq_range->start_freq_khz) ||
4913 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4914 freq_range->end_freq_khz) ||
4915 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4916 freq_range->max_bandwidth_khz) ||
4917 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4918 power_rule->max_antenna_gain) ||
4919 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4920 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01004921 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004922
4923 nla_nest_end(msg, nl_reg_rule);
4924 }
Johannes Berg458f4f92012-12-06 15:47:38 +01004925 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004926
4927 nla_nest_end(msg, nl_reg_rules);
4928
4929 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02004930 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004931
Johannes Berg458f4f92012-12-06 15:47:38 +01004932nla_put_failure_rcu:
4933 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004934nla_put_failure:
4935 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004936put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004937 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02004938 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004939}
4940
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004941static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4942{
4943 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4944 struct nlattr *nl_reg_rule;
4945 char *alpha2 = NULL;
4946 int rem_reg_rules = 0, r = 0;
4947 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004948 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004949 struct ieee80211_regdomain *rd = NULL;
4950
4951 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4952 return -EINVAL;
4953
4954 if (!info->attrs[NL80211_ATTR_REG_RULES])
4955 return -EINVAL;
4956
4957 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4958
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004959 if (info->attrs[NL80211_ATTR_DFS_REGION])
4960 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4961
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004962 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004963 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004964 num_rules++;
4965 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004966 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004967 }
4968
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004969 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01004970 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004971
4972 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01004973 if (!rd)
4974 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004975
4976 rd->n_reg_rules = num_rules;
4977 rd->alpha2[0] = alpha2[0];
4978 rd->alpha2[1] = alpha2[1];
4979
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004980 /*
4981 * Disable DFS master mode if the DFS region was
4982 * not supported or known on this kernel.
4983 */
4984 if (reg_supported_dfs_region(dfs_region))
4985 rd->dfs_region = dfs_region;
4986
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004987 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01004988 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004989 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01004990 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4991 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004992 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4993 if (r)
4994 goto bad_reg;
4995
4996 rule_idx++;
4997
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004998 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4999 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005000 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005001 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005002 }
5003
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005004 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005005 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005006 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005007
Johannes Bergd2372b32008-10-24 20:32:20 +02005008 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005009 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005010 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005011}
5012
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005013static int validate_scan_freqs(struct nlattr *freqs)
5014{
5015 struct nlattr *attr1, *attr2;
5016 int n_channels = 0, tmp1, tmp2;
5017
5018 nla_for_each_nested(attr1, freqs, tmp1) {
5019 n_channels++;
5020 /*
5021 * Some hardware has a limited channel list for
5022 * scanning, and it is pretty much nonsensical
5023 * to scan for a channel twice, so disallow that
5024 * and don't require drivers to check that the
5025 * channel list they get isn't longer than what
5026 * they can scan, as long as they can scan all
5027 * the channels they registered at once.
5028 */
5029 nla_for_each_nested(attr2, freqs, tmp2)
5030 if (attr1 != attr2 &&
5031 nla_get_u32(attr1) == nla_get_u32(attr2))
5032 return 0;
5033 }
5034
5035 return n_channels;
5036}
5037
Johannes Berg2a519312009-02-10 21:25:55 +01005038static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5039{
Johannes Berg4c476992010-10-04 21:36:35 +02005040 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005041 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005042 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005043 struct nlattr *attr;
5044 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005045 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005046 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005047
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005048 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5049 return -EINVAL;
5050
Johannes Berg79c97e92009-07-07 03:56:12 +02005051 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005052
Johannes Berg4c476992010-10-04 21:36:35 +02005053 if (!rdev->ops->scan)
5054 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005055
Johannes Bergf9f47522013-03-19 15:04:07 +01005056 if (rdev->scan_req) {
5057 err = -EBUSY;
5058 goto unlock;
5059 }
Johannes Berg2a519312009-02-10 21:25:55 +01005060
5061 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005062 n_channels = validate_scan_freqs(
5063 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005064 if (!n_channels) {
5065 err = -EINVAL;
5066 goto unlock;
5067 }
Johannes Berg2a519312009-02-10 21:25:55 +01005068 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005069 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005070 n_channels = 0;
5071
Johannes Berg2a519312009-02-10 21:25:55 +01005072 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5073 if (wiphy->bands[band])
5074 n_channels += wiphy->bands[band]->n_channels;
5075 }
5076
5077 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5078 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5079 n_ssids++;
5080
Johannes Bergf9f47522013-03-19 15:04:07 +01005081 if (n_ssids > wiphy->max_scan_ssids) {
5082 err = -EINVAL;
5083 goto unlock;
5084 }
Johannes Berg2a519312009-02-10 21:25:55 +01005085
Jouni Malinen70692ad2009-02-16 19:39:13 +02005086 if (info->attrs[NL80211_ATTR_IE])
5087 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5088 else
5089 ie_len = 0;
5090
Johannes Bergf9f47522013-03-19 15:04:07 +01005091 if (ie_len > wiphy->max_scan_ie_len) {
5092 err = -EINVAL;
5093 goto unlock;
5094 }
Johannes Berg18a83652009-03-31 12:12:05 +02005095
Johannes Berg2a519312009-02-10 21:25:55 +01005096 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005097 + sizeof(*request->ssids) * n_ssids
5098 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005099 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005100 if (!request) {
5101 err = -ENOMEM;
5102 goto unlock;
5103 }
Johannes Berg2a519312009-02-10 21:25:55 +01005104
Johannes Berg2a519312009-02-10 21:25:55 +01005105 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005106 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005107 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005108 if (ie_len) {
5109 if (request->ssids)
5110 request->ie = (void *)(request->ssids + n_ssids);
5111 else
5112 request->ie = (void *)(request->channels + n_channels);
5113 }
Johannes Berg2a519312009-02-10 21:25:55 +01005114
Johannes Berg584991d2009-11-02 13:32:03 +01005115 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005116 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5117 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005118 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005119 struct ieee80211_channel *chan;
5120
5121 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5122
5123 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005124 err = -EINVAL;
5125 goto out_free;
5126 }
Johannes Berg584991d2009-11-02 13:32:03 +01005127
5128 /* ignore disabled channels */
5129 if (chan->flags & IEEE80211_CHAN_DISABLED)
5130 continue;
5131
5132 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005133 i++;
5134 }
5135 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005136 enum ieee80211_band band;
5137
Johannes Berg2a519312009-02-10 21:25:55 +01005138 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005139 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5140 int j;
5141 if (!wiphy->bands[band])
5142 continue;
5143 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005144 struct ieee80211_channel *chan;
5145
5146 chan = &wiphy->bands[band]->channels[j];
5147
5148 if (chan->flags & IEEE80211_CHAN_DISABLED)
5149 continue;
5150
5151 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005152 i++;
5153 }
5154 }
5155 }
5156
Johannes Berg584991d2009-11-02 13:32:03 +01005157 if (!i) {
5158 err = -EINVAL;
5159 goto out_free;
5160 }
5161
5162 request->n_channels = i;
5163
Johannes Berg2a519312009-02-10 21:25:55 +01005164 i = 0;
5165 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5166 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005167 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005168 err = -EINVAL;
5169 goto out_free;
5170 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005171 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005172 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005173 i++;
5174 }
5175 }
5176
Jouni Malinen70692ad2009-02-16 19:39:13 +02005177 if (info->attrs[NL80211_ATTR_IE]) {
5178 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005179 memcpy((void *)request->ie,
5180 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005181 request->ie_len);
5182 }
5183
Johannes Berg34850ab2011-07-18 18:08:35 +02005184 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005185 if (wiphy->bands[i])
5186 request->rates[i] =
5187 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005188
5189 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5190 nla_for_each_nested(attr,
5191 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5192 tmp) {
5193 enum ieee80211_band band = nla_type(attr);
5194
Dan Carpenter84404622011-07-29 11:52:18 +03005195 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005196 err = -EINVAL;
5197 goto out_free;
5198 }
5199 err = ieee80211_get_ratemask(wiphy->bands[band],
5200 nla_data(attr),
5201 nla_len(attr),
5202 &request->rates[band]);
5203 if (err)
5204 goto out_free;
5205 }
5206 }
5207
Sam Leffler46856bb2012-10-11 21:03:32 -07005208 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005209 request->flags = nla_get_u32(
5210 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005211 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5212 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5213 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5214 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005215 err = -EOPNOTSUPP;
5216 goto out_free;
5217 }
5218 }
Sam Lefflered4737712012-10-11 21:03:31 -07005219
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305220 request->no_cck =
5221 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5222
Johannes Bergfd014282012-06-18 19:17:03 +02005223 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005224 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005225 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005226
Johannes Berg79c97e92009-07-07 03:56:12 +02005227 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005228 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005229
Johannes Berg463d0182009-07-14 00:33:35 +02005230 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005231 nl80211_send_scan_start(rdev, wdev);
5232 if (wdev->netdev)
5233 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005234 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005235 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005236 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005237 kfree(request);
5238 }
Johannes Berg3b858752009-03-12 09:55:09 +01005239
Johannes Bergf9f47522013-03-19 15:04:07 +01005240 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005241 return err;
5242}
5243
Luciano Coelho807f8a82011-05-11 17:09:35 +03005244static int nl80211_start_sched_scan(struct sk_buff *skb,
5245 struct genl_info *info)
5246{
5247 struct cfg80211_sched_scan_request *request;
5248 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5249 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005250 struct nlattr *attr;
5251 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005252 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005253 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005254 enum ieee80211_band band;
5255 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005256 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005257
5258 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5259 !rdev->ops->sched_scan_start)
5260 return -EOPNOTSUPP;
5261
5262 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5263 return -EINVAL;
5264
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005265 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5266 return -EINVAL;
5267
5268 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5269 if (interval == 0)
5270 return -EINVAL;
5271
Luciano Coelho807f8a82011-05-11 17:09:35 +03005272 wiphy = &rdev->wiphy;
5273
5274 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5275 n_channels = validate_scan_freqs(
5276 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5277 if (!n_channels)
5278 return -EINVAL;
5279 } else {
5280 n_channels = 0;
5281
5282 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5283 if (wiphy->bands[band])
5284 n_channels += wiphy->bands[band]->n_channels;
5285 }
5286
5287 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5288 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5289 tmp)
5290 n_ssids++;
5291
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005292 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005293 return -EINVAL;
5294
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005295 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5296 nla_for_each_nested(attr,
5297 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5298 tmp)
5299 n_match_sets++;
5300
5301 if (n_match_sets > wiphy->max_match_sets)
5302 return -EINVAL;
5303
Luciano Coelho807f8a82011-05-11 17:09:35 +03005304 if (info->attrs[NL80211_ATTR_IE])
5305 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5306 else
5307 ie_len = 0;
5308
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005309 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005310 return -EINVAL;
5311
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005312 if (rdev->sched_scan_req) {
5313 err = -EINPROGRESS;
5314 goto out;
5315 }
5316
Luciano Coelho807f8a82011-05-11 17:09:35 +03005317 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005318 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005319 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005320 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005321 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005322 if (!request) {
5323 err = -ENOMEM;
5324 goto out;
5325 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005326
5327 if (n_ssids)
5328 request->ssids = (void *)&request->channels[n_channels];
5329 request->n_ssids = n_ssids;
5330 if (ie_len) {
5331 if (request->ssids)
5332 request->ie = (void *)(request->ssids + n_ssids);
5333 else
5334 request->ie = (void *)(request->channels + n_channels);
5335 }
5336
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005337 if (n_match_sets) {
5338 if (request->ie)
5339 request->match_sets = (void *)(request->ie + ie_len);
5340 else if (request->ssids)
5341 request->match_sets =
5342 (void *)(request->ssids + n_ssids);
5343 else
5344 request->match_sets =
5345 (void *)(request->channels + n_channels);
5346 }
5347 request->n_match_sets = n_match_sets;
5348
Luciano Coelho807f8a82011-05-11 17:09:35 +03005349 i = 0;
5350 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5351 /* user specified, bail out if channel not found */
5352 nla_for_each_nested(attr,
5353 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5354 tmp) {
5355 struct ieee80211_channel *chan;
5356
5357 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5358
5359 if (!chan) {
5360 err = -EINVAL;
5361 goto out_free;
5362 }
5363
5364 /* ignore disabled channels */
5365 if (chan->flags & IEEE80211_CHAN_DISABLED)
5366 continue;
5367
5368 request->channels[i] = chan;
5369 i++;
5370 }
5371 } else {
5372 /* all channels */
5373 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5374 int j;
5375 if (!wiphy->bands[band])
5376 continue;
5377 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5378 struct ieee80211_channel *chan;
5379
5380 chan = &wiphy->bands[band]->channels[j];
5381
5382 if (chan->flags & IEEE80211_CHAN_DISABLED)
5383 continue;
5384
5385 request->channels[i] = chan;
5386 i++;
5387 }
5388 }
5389 }
5390
5391 if (!i) {
5392 err = -EINVAL;
5393 goto out_free;
5394 }
5395
5396 request->n_channels = i;
5397
5398 i = 0;
5399 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5400 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5401 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005402 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005403 err = -EINVAL;
5404 goto out_free;
5405 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005406 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005407 memcpy(request->ssids[i].ssid, nla_data(attr),
5408 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005409 i++;
5410 }
5411 }
5412
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005413 i = 0;
5414 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5415 nla_for_each_nested(attr,
5416 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5417 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005418 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005419
5420 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5421 nla_data(attr), nla_len(attr),
5422 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005423 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005424 if (ssid) {
5425 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5426 err = -EINVAL;
5427 goto out_free;
5428 }
5429 memcpy(request->match_sets[i].ssid.ssid,
5430 nla_data(ssid), nla_len(ssid));
5431 request->match_sets[i].ssid.ssid_len =
5432 nla_len(ssid);
5433 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005434 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5435 if (rssi)
5436 request->rssi_thold = nla_get_u32(rssi);
5437 else
5438 request->rssi_thold =
5439 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005440 i++;
5441 }
5442 }
5443
Luciano Coelho807f8a82011-05-11 17:09:35 +03005444 if (info->attrs[NL80211_ATTR_IE]) {
5445 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5446 memcpy((void *)request->ie,
5447 nla_data(info->attrs[NL80211_ATTR_IE]),
5448 request->ie_len);
5449 }
5450
Sam Leffler46856bb2012-10-11 21:03:32 -07005451 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005452 request->flags = nla_get_u32(
5453 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07005454 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5455 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
5456 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
5457 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005458 err = -EOPNOTSUPP;
5459 goto out_free;
5460 }
5461 }
Sam Lefflered4737712012-10-11 21:03:31 -07005462
Luciano Coelho807f8a82011-05-11 17:09:35 +03005463 request->dev = dev;
5464 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005465 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005466 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005467
Hila Gonene35e4d22012-06-27 17:19:42 +03005468 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005469 if (!err) {
5470 rdev->sched_scan_req = request;
5471 nl80211_send_sched_scan(rdev, dev,
5472 NL80211_CMD_START_SCHED_SCAN);
5473 goto out;
5474 }
5475
5476out_free:
5477 kfree(request);
5478out:
5479 return err;
5480}
5481
5482static int nl80211_stop_sched_scan(struct sk_buff *skb,
5483 struct genl_info *info)
5484{
5485 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5486
5487 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5488 !rdev->ops->sched_scan_stop)
5489 return -EOPNOTSUPP;
5490
Johannes Berg5fe231e2013-05-08 21:45:15 +02005491 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005492}
5493
Simon Wunderlich04f39042013-02-08 18:16:19 +01005494static int nl80211_start_radar_detection(struct sk_buff *skb,
5495 struct genl_info *info)
5496{
5497 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5498 struct net_device *dev = info->user_ptr[1];
5499 struct wireless_dev *wdev = dev->ieee80211_ptr;
5500 struct cfg80211_chan_def chandef;
5501 int err;
5502
5503 err = nl80211_parse_chandef(rdev, info, &chandef);
5504 if (err)
5505 return err;
5506
5507 if (wdev->cac_started)
5508 return -EBUSY;
5509
5510 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5511 if (err < 0)
5512 return err;
5513
5514 if (err == 0)
5515 return -EINVAL;
5516
5517 if (chandef.chan->dfs_state != NL80211_DFS_USABLE)
5518 return -EINVAL;
5519
5520 if (!rdev->ops->start_radar_detection)
5521 return -EOPNOTSUPP;
5522
Simon Wunderlich04f39042013-02-08 18:16:19 +01005523 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5524 chandef.chan, CHAN_MODE_SHARED,
5525 BIT(chandef.width));
5526 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005527 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005528
5529 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5530 if (!err) {
5531 wdev->channel = chandef.chan;
5532 wdev->cac_started = true;
5533 wdev->cac_start_time = jiffies;
5534 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005535 return err;
5536}
5537
Johannes Berg9720bb32011-06-21 09:45:33 +02005538static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5539 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005540 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005541 struct wireless_dev *wdev,
5542 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005543{
Johannes Berg48ab9052009-07-10 18:42:31 +02005544 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005545 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005546 void *hdr;
5547 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005548 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005549
5550 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005551
Eric W. Biederman15e47302012-09-07 20:12:54 +00005552 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005553 NL80211_CMD_NEW_SCAN_RESULTS);
5554 if (!hdr)
5555 return -1;
5556
Johannes Berg9720bb32011-06-21 09:45:33 +02005557 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5558
Johannes Berg97990a02013-04-19 01:02:55 +02005559 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5560 goto nla_put_failure;
5561 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005562 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5563 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005564 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5565 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005566
5567 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5568 if (!bss)
5569 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005570 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005571 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005572 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005573
5574 rcu_read_lock();
5575 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005576 if (ies) {
5577 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5578 goto fail_unlock_rcu;
5579 tsf = true;
5580 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5581 ies->len, ies->data))
5582 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005583 }
5584 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005585 if (ies) {
5586 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5587 goto fail_unlock_rcu;
5588 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5589 ies->len, ies->data))
5590 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005591 }
5592 rcu_read_unlock();
5593
David S. Miller9360ffd2012-03-29 04:41:26 -04005594 if (res->beacon_interval &&
5595 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5596 goto nla_put_failure;
5597 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5598 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
5599 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5600 jiffies_to_msecs(jiffies - intbss->ts)))
5601 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005602
Johannes Berg77965c92009-02-18 18:45:06 +01005603 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005604 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005605 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5606 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005607 break;
5608 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005609 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5610 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005611 break;
5612 default:
5613 break;
5614 }
5615
Johannes Berg48ab9052009-07-10 18:42:31 +02005616 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005617 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005618 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005619 if (intbss == wdev->current_bss &&
5620 nla_put_u32(msg, NL80211_BSS_STATUS,
5621 NL80211_BSS_STATUS_ASSOCIATED))
5622 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005623 break;
5624 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005625 if (intbss == wdev->current_bss &&
5626 nla_put_u32(msg, NL80211_BSS_STATUS,
5627 NL80211_BSS_STATUS_IBSS_JOINED))
5628 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005629 break;
5630 default:
5631 break;
5632 }
5633
Johannes Berg2a519312009-02-10 21:25:55 +01005634 nla_nest_end(msg, bss);
5635
5636 return genlmsg_end(msg, hdr);
5637
Johannes Berg8cef2c92013-02-05 16:54:31 +01005638 fail_unlock_rcu:
5639 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005640 nla_put_failure:
5641 genlmsg_cancel(msg, hdr);
5642 return -EMSGSIZE;
5643}
5644
Johannes Berg97990a02013-04-19 01:02:55 +02005645static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005646{
Johannes Berg48ab9052009-07-10 18:42:31 +02005647 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005648 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005649 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005650 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005651 int err;
5652
Johannes Berg97990a02013-04-19 01:02:55 +02005653 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005654 if (err)
5655 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005656
Johannes Berg48ab9052009-07-10 18:42:31 +02005657 wdev_lock(wdev);
5658 spin_lock_bh(&rdev->bss_lock);
5659 cfg80211_bss_expire(rdev);
5660
Johannes Berg9720bb32011-06-21 09:45:33 +02005661 cb->seq = rdev->bss_generation;
5662
Johannes Berg48ab9052009-07-10 18:42:31 +02005663 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005664 if (++idx <= start)
5665 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005666 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005667 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005668 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005669 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005670 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005671 }
5672 }
5673
Johannes Berg48ab9052009-07-10 18:42:31 +02005674 spin_unlock_bh(&rdev->bss_lock);
5675 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005676
Johannes Berg97990a02013-04-19 01:02:55 +02005677 cb->args[2] = idx;
5678 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005679
Johannes Berg67748892010-10-04 21:14:06 +02005680 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005681}
5682
Eric W. Biederman15e47302012-09-07 20:12:54 +00005683static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005684 int flags, struct net_device *dev,
5685 struct survey_info *survey)
5686{
5687 void *hdr;
5688 struct nlattr *infoattr;
5689
Eric W. Biederman15e47302012-09-07 20:12:54 +00005690 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005691 NL80211_CMD_NEW_SURVEY_RESULTS);
5692 if (!hdr)
5693 return -ENOMEM;
5694
David S. Miller9360ffd2012-03-29 04:41:26 -04005695 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
5696 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005697
5698 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
5699 if (!infoattr)
5700 goto nla_put_failure;
5701
David S. Miller9360ffd2012-03-29 04:41:26 -04005702 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
5703 survey->channel->center_freq))
5704 goto nla_put_failure;
5705
5706 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
5707 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
5708 goto nla_put_failure;
5709 if ((survey->filled & SURVEY_INFO_IN_USE) &&
5710 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
5711 goto nla_put_failure;
5712 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
5713 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
5714 survey->channel_time))
5715 goto nla_put_failure;
5716 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
5717 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
5718 survey->channel_time_busy))
5719 goto nla_put_failure;
5720 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
5721 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
5722 survey->channel_time_ext_busy))
5723 goto nla_put_failure;
5724 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
5725 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
5726 survey->channel_time_rx))
5727 goto nla_put_failure;
5728 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
5729 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
5730 survey->channel_time_tx))
5731 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01005732
5733 nla_nest_end(msg, infoattr);
5734
5735 return genlmsg_end(msg, hdr);
5736
5737 nla_put_failure:
5738 genlmsg_cancel(msg, hdr);
5739 return -EMSGSIZE;
5740}
5741
5742static int nl80211_dump_survey(struct sk_buff *skb,
5743 struct netlink_callback *cb)
5744{
5745 struct survey_info survey;
5746 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02005747 struct wireless_dev *wdev;
5748 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01005749 int res;
5750
Johannes Berg97990a02013-04-19 01:02:55 +02005751 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005752 if (res)
5753 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01005754
Johannes Berg97990a02013-04-19 01:02:55 +02005755 if (!wdev->netdev) {
5756 res = -EINVAL;
5757 goto out_err;
5758 }
5759
Holger Schurig61fa7132009-11-11 12:25:40 +01005760 if (!dev->ops->dump_survey) {
5761 res = -EOPNOTSUPP;
5762 goto out_err;
5763 }
5764
5765 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005766 struct ieee80211_channel *chan;
5767
Johannes Berg97990a02013-04-19 01:02:55 +02005768 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01005769 if (res == -ENOENT)
5770 break;
5771 if (res)
5772 goto out_err;
5773
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07005774 /* Survey without a channel doesn't make sense */
5775 if (!survey.channel) {
5776 res = -EINVAL;
5777 goto out;
5778 }
5779
5780 chan = ieee80211_get_channel(&dev->wiphy,
5781 survey.channel->center_freq);
5782 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
5783 survey_idx++;
5784 continue;
5785 }
5786
Holger Schurig61fa7132009-11-11 12:25:40 +01005787 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005788 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01005789 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02005790 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01005791 goto out;
5792 survey_idx++;
5793 }
5794
5795 out:
Johannes Berg97990a02013-04-19 01:02:55 +02005796 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01005797 res = skb->len;
5798 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02005799 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01005800 return res;
5801}
5802
Samuel Ortizb23aa672009-07-01 21:26:54 +02005803static bool nl80211_valid_wpa_versions(u32 wpa_versions)
5804{
5805 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
5806 NL80211_WPA_VERSION_2));
5807}
5808
Jouni Malinen636a5d32009-03-19 13:39:22 +02005809static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
5810{
Johannes Berg4c476992010-10-04 21:36:35 +02005811 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5812 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005813 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03005814 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
5815 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005816 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02005817 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005818 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005819
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005820 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5821 return -EINVAL;
5822
5823 if (!info->attrs[NL80211_ATTR_MAC])
5824 return -EINVAL;
5825
Jouni Malinen17780922009-03-27 20:52:47 +02005826 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
5827 return -EINVAL;
5828
Johannes Berg19957bb2009-07-02 17:20:43 +02005829 if (!info->attrs[NL80211_ATTR_SSID])
5830 return -EINVAL;
5831
5832 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
5833 return -EINVAL;
5834
Johannes Bergfffd0932009-07-08 14:22:54 +02005835 err = nl80211_parse_key(info, &key);
5836 if (err)
5837 return err;
5838
5839 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02005840 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
5841 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02005842 if (!key.p.key || !key.p.key_len)
5843 return -EINVAL;
5844 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
5845 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
5846 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
5847 key.p.key_len != WLAN_KEY_LEN_WEP104))
5848 return -EINVAL;
5849 if (key.idx > 4)
5850 return -EINVAL;
5851 } else {
5852 key.p.key_len = 0;
5853 key.p.key = NULL;
5854 }
5855
Johannes Bergafea0b72010-08-10 09:46:42 +02005856 if (key.idx >= 0) {
5857 int i;
5858 bool ok = false;
5859 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
5860 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
5861 ok = true;
5862 break;
5863 }
5864 }
Johannes Berg4c476992010-10-04 21:36:35 +02005865 if (!ok)
5866 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02005867 }
5868
Johannes Berg4c476992010-10-04 21:36:35 +02005869 if (!rdev->ops->auth)
5870 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005871
Johannes Berg074ac8d2010-09-16 14:58:22 +02005872 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005873 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5874 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005875
Johannes Berg19957bb2009-07-02 17:20:43 +02005876 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02005877 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02005878 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005879 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5880 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005881
Johannes Berg19957bb2009-07-02 17:20:43 +02005882 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5883 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5884
5885 if (info->attrs[NL80211_ATTR_IE]) {
5886 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5887 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5888 }
5889
5890 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005891 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005892 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005893
Jouni Malinene39e5b52012-09-30 19:29:39 +03005894 if (auth_type == NL80211_AUTHTYPE_SAE &&
5895 !info->attrs[NL80211_ATTR_SAE_DATA])
5896 return -EINVAL;
5897
5898 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5899 if (auth_type != NL80211_AUTHTYPE_SAE)
5900 return -EINVAL;
5901 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5902 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5903 /* need to include at least Auth Transaction and Status Code */
5904 if (sae_data_len < 4)
5905 return -EINVAL;
5906 }
5907
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005908 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5909
Johannes Berg95de8172012-01-20 13:55:25 +01005910 /*
5911 * Since we no longer track auth state, ignore
5912 * requests to only change local state.
5913 */
5914 if (local_state_change)
5915 return 0;
5916
Johannes Berg91bf9b22013-05-15 17:44:01 +02005917 wdev_lock(dev->ieee80211_ptr);
5918 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5919 ssid, ssid_len, ie, ie_len,
5920 key.p.key, key.p.key_len, key.idx,
5921 sae_data, sae_data_len);
5922 wdev_unlock(dev->ieee80211_ptr);
5923 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005924}
5925
Johannes Bergc0692b82010-08-27 14:26:53 +03005926static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5927 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005928 struct cfg80211_crypto_settings *settings,
5929 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005930{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005931 memset(settings, 0, sizeof(*settings));
5932
Samuel Ortizb23aa672009-07-01 21:26:54 +02005933 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5934
Johannes Bergc0692b82010-08-27 14:26:53 +03005935 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5936 u16 proto;
5937 proto = nla_get_u16(
5938 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5939 settings->control_port_ethertype = cpu_to_be16(proto);
5940 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5941 proto != ETH_P_PAE)
5942 return -EINVAL;
5943 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5944 settings->control_port_no_encrypt = true;
5945 } else
5946 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5947
Samuel Ortizb23aa672009-07-01 21:26:54 +02005948 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5949 void *data;
5950 int len, i;
5951
5952 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5953 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5954 settings->n_ciphers_pairwise = len / sizeof(u32);
5955
5956 if (len % sizeof(u32))
5957 return -EINVAL;
5958
Johannes Berg3dc27d22009-07-02 21:36:37 +02005959 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005960 return -EINVAL;
5961
5962 memcpy(settings->ciphers_pairwise, data, len);
5963
5964 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005965 if (!cfg80211_supported_cipher_suite(
5966 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005967 settings->ciphers_pairwise[i]))
5968 return -EINVAL;
5969 }
5970
5971 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5972 settings->cipher_group =
5973 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005974 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5975 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005976 return -EINVAL;
5977 }
5978
5979 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5980 settings->wpa_versions =
5981 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5982 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5983 return -EINVAL;
5984 }
5985
5986 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5987 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005988 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005989
5990 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5991 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5992 settings->n_akm_suites = len / sizeof(u32);
5993
5994 if (len % sizeof(u32))
5995 return -EINVAL;
5996
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005997 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5998 return -EINVAL;
5999
Samuel Ortizb23aa672009-07-01 21:26:54 +02006000 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006001 }
6002
6003 return 0;
6004}
6005
Jouni Malinen636a5d32009-03-19 13:39:22 +02006006static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6007{
Johannes Berg4c476992010-10-04 21:36:35 +02006008 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6009 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006010 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006011 struct cfg80211_assoc_request req = {};
6012 const u8 *bssid, *ssid;
6013 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006014
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006015 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6016 return -EINVAL;
6017
6018 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006019 !info->attrs[NL80211_ATTR_SSID] ||
6020 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006021 return -EINVAL;
6022
Johannes Berg4c476992010-10-04 21:36:35 +02006023 if (!rdev->ops->assoc)
6024 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006025
Johannes Berg074ac8d2010-09-16 14:58:22 +02006026 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006027 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6028 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006029
Johannes Berg19957bb2009-07-02 17:20:43 +02006030 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006031
Johannes Berg19957bb2009-07-02 17:20:43 +02006032 chan = ieee80211_get_channel(&rdev->wiphy,
6033 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006034 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6035 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006036
Johannes Berg19957bb2009-07-02 17:20:43 +02006037 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6038 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006039
6040 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006041 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6042 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006043 }
6044
Jouni Malinendc6382c2009-05-06 22:09:37 +03006045 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006046 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006047 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006048 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006049 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006050 else if (mfp != NL80211_MFP_NO)
6051 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006052 }
6053
Johannes Berg3e5d7642009-07-07 14:37:26 +02006054 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006055 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006056
Ben Greear7e7c8922011-11-18 11:31:59 -08006057 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006058 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006059
6060 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006061 memcpy(&req.ht_capa_mask,
6062 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6063 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006064
6065 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006066 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006067 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006068 memcpy(&req.ht_capa,
6069 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6070 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006071 }
6072
Johannes Bergee2aca32013-02-21 17:36:01 +01006073 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006074 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006075
6076 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006077 memcpy(&req.vht_capa_mask,
6078 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6079 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006080
6081 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006082 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006083 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006084 memcpy(&req.vht_capa,
6085 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6086 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006087 }
6088
Johannes Bergf62fab72013-02-21 20:09:09 +01006089 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006090 if (!err) {
6091 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006092 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6093 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006094 wdev_unlock(dev->ieee80211_ptr);
6095 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006096
Jouni Malinen636a5d32009-03-19 13:39:22 +02006097 return err;
6098}
6099
6100static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6101{
Johannes Berg4c476992010-10-04 21:36:35 +02006102 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6103 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006104 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006105 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006106 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006107 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006108
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006109 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6110 return -EINVAL;
6111
6112 if (!info->attrs[NL80211_ATTR_MAC])
6113 return -EINVAL;
6114
6115 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6116 return -EINVAL;
6117
Johannes Berg4c476992010-10-04 21:36:35 +02006118 if (!rdev->ops->deauth)
6119 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006120
Johannes Berg074ac8d2010-09-16 14:58:22 +02006121 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006122 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6123 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006124
Johannes Berg19957bb2009-07-02 17:20:43 +02006125 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006126
Johannes Berg19957bb2009-07-02 17:20:43 +02006127 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6128 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006129 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006130 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006131 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006132
6133 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006134 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6135 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006136 }
6137
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006138 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6139
Johannes Berg91bf9b22013-05-15 17:44:01 +02006140 wdev_lock(dev->ieee80211_ptr);
6141 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6142 local_state_change);
6143 wdev_unlock(dev->ieee80211_ptr);
6144 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006145}
6146
6147static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6148{
Johannes Berg4c476992010-10-04 21:36:35 +02006149 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6150 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006151 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006152 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006153 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006154 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006155
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006156 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6157 return -EINVAL;
6158
6159 if (!info->attrs[NL80211_ATTR_MAC])
6160 return -EINVAL;
6161
6162 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6163 return -EINVAL;
6164
Johannes Berg4c476992010-10-04 21:36:35 +02006165 if (!rdev->ops->disassoc)
6166 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006167
Johannes Berg074ac8d2010-09-16 14:58:22 +02006168 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006169 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6170 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006171
Johannes Berg19957bb2009-07-02 17:20:43 +02006172 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006173
Johannes Berg19957bb2009-07-02 17:20:43 +02006174 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6175 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006176 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006177 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006178 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006179
6180 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006181 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6182 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006183 }
6184
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006185 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6186
Johannes Berg91bf9b22013-05-15 17:44:01 +02006187 wdev_lock(dev->ieee80211_ptr);
6188 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6189 local_state_change);
6190 wdev_unlock(dev->ieee80211_ptr);
6191 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006192}
6193
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006194static bool
6195nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6196 int mcast_rate[IEEE80211_NUM_BANDS],
6197 int rateval)
6198{
6199 struct wiphy *wiphy = &rdev->wiphy;
6200 bool found = false;
6201 int band, i;
6202
6203 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6204 struct ieee80211_supported_band *sband;
6205
6206 sband = wiphy->bands[band];
6207 if (!sband)
6208 continue;
6209
6210 for (i = 0; i < sband->n_bitrates; i++) {
6211 if (sband->bitrates[i].bitrate == rateval) {
6212 mcast_rate[band] = i + 1;
6213 found = true;
6214 break;
6215 }
6216 }
6217 }
6218
6219 return found;
6220}
6221
Johannes Berg04a773a2009-04-19 21:24:32 +02006222static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6223{
Johannes Berg4c476992010-10-04 21:36:35 +02006224 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6225 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006226 struct cfg80211_ibss_params ibss;
6227 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006228 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006229 int err;
6230
Johannes Berg8e30bc52009-04-22 17:45:38 +02006231 memset(&ibss, 0, sizeof(ibss));
6232
Johannes Berg04a773a2009-04-19 21:24:32 +02006233 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6234 return -EINVAL;
6235
Johannes Berg683b6d32012-11-08 21:25:48 +01006236 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006237 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6238 return -EINVAL;
6239
Johannes Berg8e30bc52009-04-22 17:45:38 +02006240 ibss.beacon_interval = 100;
6241
6242 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6243 ibss.beacon_interval =
6244 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6245 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6246 return -EINVAL;
6247 }
6248
Johannes Berg4c476992010-10-04 21:36:35 +02006249 if (!rdev->ops->join_ibss)
6250 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006251
Johannes Berg4c476992010-10-04 21:36:35 +02006252 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6253 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006254
Johannes Berg79c97e92009-07-07 03:56:12 +02006255 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006256
Johannes Berg39193492011-09-16 13:45:25 +02006257 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006258 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006259
6260 if (!is_valid_ether_addr(ibss.bssid))
6261 return -EINVAL;
6262 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006263 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6264 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6265
6266 if (info->attrs[NL80211_ATTR_IE]) {
6267 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6268 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6269 }
6270
Johannes Berg683b6d32012-11-08 21:25:48 +01006271 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6272 if (err)
6273 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006274
Johannes Berg683b6d32012-11-08 21:25:48 +01006275 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006276 return -EINVAL;
6277
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006278 if (ibss.chandef.width > NL80211_CHAN_WIDTH_40)
6279 return -EINVAL;
6280 if (ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
6281 !(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
Simon Wunderlichc04d6152012-11-29 18:37:22 +01006282 return -EINVAL;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006283
Johannes Berg04a773a2009-04-19 21:24:32 +02006284 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006285 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006286
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006287 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6288 u8 *rates =
6289 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6290 int n_rates =
6291 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6292 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006293 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006294
Johannes Berg34850ab2011-07-18 18:08:35 +02006295 err = ieee80211_get_ratemask(sband, rates, n_rates,
6296 &ibss.basic_rates);
6297 if (err)
6298 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006299 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006300
6301 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6302 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6303 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6304 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006305
Johannes Berg4c476992010-10-04 21:36:35 +02006306 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306307 bool no_ht = false;
6308
Johannes Berg4c476992010-10-04 21:36:35 +02006309 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306310 info->attrs[NL80211_ATTR_KEYS],
6311 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006312 if (IS_ERR(connkeys))
6313 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306314
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006315 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6316 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306317 kfree(connkeys);
6318 return -EINVAL;
6319 }
Johannes Berg4c476992010-10-04 21:36:35 +02006320 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006321
Antonio Quartulli267335d2012-01-31 20:25:47 +01006322 ibss.control_port =
6323 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6324
Johannes Berg4c476992010-10-04 21:36:35 +02006325 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006326 if (err)
6327 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006328 return err;
6329}
6330
6331static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6332{
Johannes Berg4c476992010-10-04 21:36:35 +02006333 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6334 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006335
Johannes Berg4c476992010-10-04 21:36:35 +02006336 if (!rdev->ops->leave_ibss)
6337 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006338
Johannes Berg4c476992010-10-04 21:36:35 +02006339 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6340 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006341
Johannes Berg4c476992010-10-04 21:36:35 +02006342 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006343}
6344
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006345static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6346{
6347 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6348 struct net_device *dev = info->user_ptr[1];
6349 int mcast_rate[IEEE80211_NUM_BANDS];
6350 u32 nla_rate;
6351 int err;
6352
6353 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6354 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6355 return -EOPNOTSUPP;
6356
6357 if (!rdev->ops->set_mcast_rate)
6358 return -EOPNOTSUPP;
6359
6360 memset(mcast_rate, 0, sizeof(mcast_rate));
6361
6362 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6363 return -EINVAL;
6364
6365 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6366 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6367 return -EINVAL;
6368
6369 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6370
6371 return err;
6372}
6373
6374
Johannes Bergaff89a92009-07-01 21:26:51 +02006375#ifdef CONFIG_NL80211_TESTMODE
6376static struct genl_multicast_group nl80211_testmode_mcgrp = {
6377 .name = "testmode",
6378};
6379
6380static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6381{
Johannes Berg4c476992010-10-04 21:36:35 +02006382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006383 int err;
6384
6385 if (!info->attrs[NL80211_ATTR_TESTDATA])
6386 return -EINVAL;
6387
Johannes Bergaff89a92009-07-01 21:26:51 +02006388 err = -EOPNOTSUPP;
6389 if (rdev->ops->testmode_cmd) {
6390 rdev->testmode_info = info;
Hila Gonene35e4d22012-06-27 17:19:42 +03006391 err = rdev_testmode_cmd(rdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006392 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6393 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
6394 rdev->testmode_info = NULL;
6395 }
6396
Johannes Bergaff89a92009-07-01 21:26:51 +02006397 return err;
6398}
6399
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006400static int nl80211_testmode_dump(struct sk_buff *skb,
6401 struct netlink_callback *cb)
6402{
Johannes Berg00918d32011-12-13 17:22:05 +01006403 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006404 int err;
6405 long phy_idx;
6406 void *data = NULL;
6407 int data_len = 0;
6408
Johannes Berg5fe231e2013-05-08 21:45:15 +02006409 rtnl_lock();
6410
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006411 if (cb->args[0]) {
6412 /*
6413 * 0 is a valid index, but not valid for args[0],
6414 * so we need to offset by 1.
6415 */
6416 phy_idx = cb->args[0] - 1;
6417 } else {
6418 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6419 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6420 nl80211_policy);
6421 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006422 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006423
Johannes Berg2bd7e352012-06-15 14:23:16 +02006424 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6425 nl80211_fam.attrbuf);
6426 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006427 err = PTR_ERR(rdev);
6428 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006429 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006430 phy_idx = rdev->wiphy_idx;
6431 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02006432
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006433 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6434 cb->args[1] =
6435 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6436 }
6437
6438 if (cb->args[1]) {
6439 data = nla_data((void *)cb->args[1]);
6440 data_len = nla_len((void *)cb->args[1]);
6441 }
6442
Johannes Berg00918d32011-12-13 17:22:05 +01006443 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6444 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006445 err = -ENOENT;
6446 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006447 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006448
Johannes Berg00918d32011-12-13 17:22:05 +01006449 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006450 err = -EOPNOTSUPP;
6451 goto out_err;
6452 }
6453
6454 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006455 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006456 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6457 NL80211_CMD_TESTMODE);
6458 struct nlattr *tmdata;
6459
David S. Miller9360ffd2012-03-29 04:41:26 -04006460 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006461 genlmsg_cancel(skb, hdr);
6462 break;
6463 }
6464
6465 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6466 if (!tmdata) {
6467 genlmsg_cancel(skb, hdr);
6468 break;
6469 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006470 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006471 nla_nest_end(skb, tmdata);
6472
6473 if (err == -ENOBUFS || err == -ENOENT) {
6474 genlmsg_cancel(skb, hdr);
6475 break;
6476 } else if (err) {
6477 genlmsg_cancel(skb, hdr);
6478 goto out_err;
6479 }
6480
6481 genlmsg_end(skb, hdr);
6482 }
6483
6484 err = skb->len;
6485 /* see above */
6486 cb->args[0] = phy_idx + 1;
6487 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02006488 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006489 return err;
6490}
6491
Johannes Bergaff89a92009-07-01 21:26:51 +02006492static struct sk_buff *
6493__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006494 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02006495{
6496 struct sk_buff *skb;
6497 void *hdr;
6498 struct nlattr *data;
6499
6500 skb = nlmsg_new(approxlen + 100, gfp);
6501 if (!skb)
6502 return NULL;
6503
Eric W. Biederman15e47302012-09-07 20:12:54 +00006504 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02006505 if (!hdr) {
6506 kfree_skb(skb);
6507 return NULL;
6508 }
6509
David S. Miller9360ffd2012-03-29 04:41:26 -04006510 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6511 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02006512 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6513
6514 ((void **)skb->cb)[0] = rdev;
6515 ((void **)skb->cb)[1] = hdr;
6516 ((void **)skb->cb)[2] = data;
6517
6518 return skb;
6519
6520 nla_put_failure:
6521 kfree_skb(skb);
6522 return NULL;
6523}
6524
6525struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
6526 int approxlen)
6527{
6528 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6529
6530 if (WARN_ON(!rdev->testmode_info))
6531 return NULL;
6532
6533 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006534 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02006535 rdev->testmode_info->snd_seq,
6536 GFP_KERNEL);
6537}
6538EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
6539
6540int cfg80211_testmode_reply(struct sk_buff *skb)
6541{
6542 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
6543 void *hdr = ((void **)skb->cb)[1];
6544 struct nlattr *data = ((void **)skb->cb)[2];
6545
6546 if (WARN_ON(!rdev->testmode_info)) {
6547 kfree_skb(skb);
6548 return -EINVAL;
6549 }
6550
6551 nla_nest_end(skb, data);
6552 genlmsg_end(skb, hdr);
6553 return genlmsg_reply(skb, rdev->testmode_info);
6554}
6555EXPORT_SYMBOL(cfg80211_testmode_reply);
6556
6557struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6558 int approxlen, gfp_t gfp)
6559{
6560 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6561
6562 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
6563}
6564EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6565
6566void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6567{
6568 void *hdr = ((void **)skb->cb)[1];
6569 struct nlattr *data = ((void **)skb->cb)[2];
6570
6571 nla_nest_end(skb, data);
6572 genlmsg_end(skb, hdr);
6573 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
6574}
6575EXPORT_SYMBOL(cfg80211_testmode_event);
6576#endif
6577
Samuel Ortizb23aa672009-07-01 21:26:54 +02006578static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6579{
Johannes Berg4c476992010-10-04 21:36:35 +02006580 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6581 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006582 struct cfg80211_connect_params connect;
6583 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006584 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006585 int err;
6586
6587 memset(&connect, 0, sizeof(connect));
6588
6589 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6590 return -EINVAL;
6591
6592 if (!info->attrs[NL80211_ATTR_SSID] ||
6593 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6594 return -EINVAL;
6595
6596 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6597 connect.auth_type =
6598 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006599 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6600 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006601 return -EINVAL;
6602 } else
6603 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6604
6605 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6606
Johannes Bergc0692b82010-08-27 14:26:53 +03006607 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006608 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006609 if (err)
6610 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006611
Johannes Berg074ac8d2010-09-16 14:58:22 +02006612 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006613 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6614 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006615
Johannes Berg79c97e92009-07-07 03:56:12 +02006616 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006617
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306618 connect.bg_scan_period = -1;
6619 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6620 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6621 connect.bg_scan_period =
6622 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6623 }
6624
Samuel Ortizb23aa672009-07-01 21:26:54 +02006625 if (info->attrs[NL80211_ATTR_MAC])
6626 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6627 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6628 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6629
6630 if (info->attrs[NL80211_ATTR_IE]) {
6631 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6632 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6633 }
6634
Jouni Malinencee00a92013-01-15 17:15:57 +02006635 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6636 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6637 if (connect.mfp != NL80211_MFP_REQUIRED &&
6638 connect.mfp != NL80211_MFP_NO)
6639 return -EINVAL;
6640 } else {
6641 connect.mfp = NL80211_MFP_NO;
6642 }
6643
Samuel Ortizb23aa672009-07-01 21:26:54 +02006644 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6645 connect.channel =
6646 ieee80211_get_channel(wiphy,
6647 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6648 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006649 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6650 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006651 }
6652
Johannes Bergfffd0932009-07-08 14:22:54 +02006653 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6654 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306655 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006656 if (IS_ERR(connkeys))
6657 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006658 }
6659
Ben Greear7e7c8922011-11-18 11:31:59 -08006660 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6661 connect.flags |= ASSOC_REQ_DISABLE_HT;
6662
6663 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6664 memcpy(&connect.ht_capa_mask,
6665 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6666 sizeof(connect.ht_capa_mask));
6667
6668 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006669 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6670 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006671 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006672 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006673 memcpy(&connect.ht_capa,
6674 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6675 sizeof(connect.ht_capa));
6676 }
6677
Johannes Bergee2aca32013-02-21 17:36:01 +01006678 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6679 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6680
6681 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6682 memcpy(&connect.vht_capa_mask,
6683 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6684 sizeof(connect.vht_capa_mask));
6685
6686 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
6687 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
6688 kfree(connkeys);
6689 return -EINVAL;
6690 }
6691 memcpy(&connect.vht_capa,
6692 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6693 sizeof(connect.vht_capa));
6694 }
6695
Johannes Berg83739b02013-05-15 17:44:01 +02006696 wdev_lock(dev->ieee80211_ptr);
6697 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
6698 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02006699 if (err)
6700 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006701 return err;
6702}
6703
6704static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
6705{
Johannes Berg4c476992010-10-04 21:36:35 +02006706 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6707 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006708 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02006709 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006710
6711 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6712 reason = WLAN_REASON_DEAUTH_LEAVING;
6713 else
6714 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6715
6716 if (reason == 0)
6717 return -EINVAL;
6718
Johannes Berg074ac8d2010-09-16 14:58:22 +02006719 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006720 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6721 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006722
Johannes Berg83739b02013-05-15 17:44:01 +02006723 wdev_lock(dev->ieee80211_ptr);
6724 ret = cfg80211_disconnect(rdev, dev, reason, true);
6725 wdev_unlock(dev->ieee80211_ptr);
6726 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006727}
6728
Johannes Berg463d0182009-07-14 00:33:35 +02006729static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
6730{
Johannes Berg4c476992010-10-04 21:36:35 +02006731 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02006732 struct net *net;
6733 int err;
6734 u32 pid;
6735
6736 if (!info->attrs[NL80211_ATTR_PID])
6737 return -EINVAL;
6738
6739 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
6740
Johannes Berg463d0182009-07-14 00:33:35 +02006741 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02006742 if (IS_ERR(net))
6743 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006744
6745 err = 0;
6746
6747 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02006748 if (!net_eq(wiphy_net(&rdev->wiphy), net))
6749 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02006750
Johannes Berg463d0182009-07-14 00:33:35 +02006751 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02006752 return err;
6753}
6754
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006755static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
6756{
Johannes Berg4c476992010-10-04 21:36:35 +02006757 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006758 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
6759 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02006760 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006761 struct cfg80211_pmksa pmksa;
6762
6763 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
6764
6765 if (!info->attrs[NL80211_ATTR_MAC])
6766 return -EINVAL;
6767
6768 if (!info->attrs[NL80211_ATTR_PMKID])
6769 return -EINVAL;
6770
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006771 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
6772 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6773
Johannes Berg074ac8d2010-09-16 14:58:22 +02006774 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006775 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6776 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006777
6778 switch (info->genlhdr->cmd) {
6779 case NL80211_CMD_SET_PMKSA:
6780 rdev_ops = rdev->ops->set_pmksa;
6781 break;
6782 case NL80211_CMD_DEL_PMKSA:
6783 rdev_ops = rdev->ops->del_pmksa;
6784 break;
6785 default:
6786 WARN_ON(1);
6787 break;
6788 }
6789
Johannes Berg4c476992010-10-04 21:36:35 +02006790 if (!rdev_ops)
6791 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006792
Johannes Berg4c476992010-10-04 21:36:35 +02006793 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006794}
6795
6796static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
6797{
Johannes Berg4c476992010-10-04 21:36:35 +02006798 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6799 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006800
Johannes Berg074ac8d2010-09-16 14:58:22 +02006801 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006802 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6803 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006804
Johannes Berg4c476992010-10-04 21:36:35 +02006805 if (!rdev->ops->flush_pmksa)
6806 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006807
Hila Gonene35e4d22012-06-27 17:19:42 +03006808 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006809}
6810
Arik Nemtsov109086c2011-09-28 14:12:50 +03006811static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
6812{
6813 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6814 struct net_device *dev = info->user_ptr[1];
6815 u8 action_code, dialog_token;
6816 u16 status_code;
6817 u8 *peer;
6818
6819 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6820 !rdev->ops->tdls_mgmt)
6821 return -EOPNOTSUPP;
6822
6823 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
6824 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
6825 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
6826 !info->attrs[NL80211_ATTR_IE] ||
6827 !info->attrs[NL80211_ATTR_MAC])
6828 return -EINVAL;
6829
6830 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6831 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
6832 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
6833 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
6834
Hila Gonene35e4d22012-06-27 17:19:42 +03006835 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
6836 dialog_token, status_code,
6837 nla_data(info->attrs[NL80211_ATTR_IE]),
6838 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03006839}
6840
6841static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
6842{
6843 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6844 struct net_device *dev = info->user_ptr[1];
6845 enum nl80211_tdls_operation operation;
6846 u8 *peer;
6847
6848 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
6849 !rdev->ops->tdls_oper)
6850 return -EOPNOTSUPP;
6851
6852 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
6853 !info->attrs[NL80211_ATTR_MAC])
6854 return -EINVAL;
6855
6856 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
6857 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
6858
Hila Gonene35e4d22012-06-27 17:19:42 +03006859 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03006860}
6861
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006862static int nl80211_remain_on_channel(struct sk_buff *skb,
6863 struct genl_info *info)
6864{
Johannes Berg4c476992010-10-04 21:36:35 +02006865 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006866 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01006867 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006868 struct sk_buff *msg;
6869 void *hdr;
6870 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01006871 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006872 int err;
6873
6874 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
6875 !info->attrs[NL80211_ATTR_DURATION])
6876 return -EINVAL;
6877
6878 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
6879
Johannes Berg7c4ef712011-11-18 15:33:48 +01006880 if (!rdev->ops->remain_on_channel ||
6881 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02006882 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006883
Johannes Bergebf348f2012-06-01 12:50:54 +02006884 /*
6885 * We should be on that channel for at least a minimum amount of
6886 * time (10ms) but no longer than the driver supports.
6887 */
6888 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6889 duration > rdev->wiphy.max_remain_on_channel_duration)
6890 return -EINVAL;
6891
Johannes Berg683b6d32012-11-08 21:25:48 +01006892 err = nl80211_parse_chandef(rdev, info, &chandef);
6893 if (err)
6894 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006895
6896 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006897 if (!msg)
6898 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006899
Eric W. Biederman15e47302012-09-07 20:12:54 +00006900 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006901 NL80211_CMD_REMAIN_ON_CHANNEL);
6902
6903 if (IS_ERR(hdr)) {
6904 err = PTR_ERR(hdr);
6905 goto free_msg;
6906 }
6907
Johannes Berg683b6d32012-11-08 21:25:48 +01006908 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
6909 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006910
6911 if (err)
6912 goto free_msg;
6913
David S. Miller9360ffd2012-03-29 04:41:26 -04006914 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6915 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006916
6917 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006918
6919 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006920
6921 nla_put_failure:
6922 err = -ENOBUFS;
6923 free_msg:
6924 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006925 return err;
6926}
6927
6928static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
6929 struct genl_info *info)
6930{
Johannes Berg4c476992010-10-04 21:36:35 +02006931 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006932 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006933 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006934
6935 if (!info->attrs[NL80211_ATTR_COOKIE])
6936 return -EINVAL;
6937
Johannes Berg4c476992010-10-04 21:36:35 +02006938 if (!rdev->ops->cancel_remain_on_channel)
6939 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006940
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006941 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6942
Hila Gonene35e4d22012-06-27 17:19:42 +03006943 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006944}
6945
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006946static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6947 u8 *rates, u8 rates_len)
6948{
6949 u8 i;
6950 u32 mask = 0;
6951
6952 for (i = 0; i < rates_len; i++) {
6953 int rate = (rates[i] & 0x7f) * 5;
6954 int ridx;
6955 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6956 struct ieee80211_rate *srate =
6957 &sband->bitrates[ridx];
6958 if (rate == srate->bitrate) {
6959 mask |= 1 << ridx;
6960 break;
6961 }
6962 }
6963 if (ridx == sband->n_bitrates)
6964 return 0; /* rate not found */
6965 }
6966
6967 return mask;
6968}
6969
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006970static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6971 u8 *rates, u8 rates_len,
6972 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6973{
6974 u8 i;
6975
6976 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6977
6978 for (i = 0; i < rates_len; i++) {
6979 int ridx, rbit;
6980
6981 ridx = rates[i] / 8;
6982 rbit = BIT(rates[i] % 8);
6983
6984 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006985 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006986 return false;
6987
6988 /* check availability */
6989 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6990 mcs[ridx] |= rbit;
6991 else
6992 return false;
6993 }
6994
6995 return true;
6996}
6997
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006998static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006999 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7000 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007001 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
7002 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007003};
7004
7005static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7006 struct genl_info *info)
7007{
7008 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007009 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007010 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007011 int rem, i;
7012 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007013 struct nlattr *tx_rates;
7014 struct ieee80211_supported_band *sband;
7015
7016 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7017 return -EINVAL;
7018
Johannes Berg4c476992010-10-04 21:36:35 +02007019 if (!rdev->ops->set_bitrate_mask)
7020 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007021
7022 memset(&mask, 0, sizeof(mask));
7023 /* Default to all rates enabled */
7024 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7025 sband = rdev->wiphy.bands[i];
7026 mask.control[i].legacy =
7027 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007028 if (sband)
7029 memcpy(mask.control[i].mcs,
7030 sband->ht_cap.mcs.rx_mask,
7031 sizeof(mask.control[i].mcs));
7032 else
7033 memset(mask.control[i].mcs, 0,
7034 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007035 }
7036
7037 /*
7038 * The nested attribute uses enum nl80211_band as the index. This maps
7039 * directly to the enum ieee80211_band values used in cfg80211.
7040 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007041 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007042 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7043 {
7044 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007045 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7046 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007047 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007048 if (sband == NULL)
7049 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007050 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7051 nla_len(tx_rates), nl80211_txattr_policy);
7052 if (tb[NL80211_TXRATE_LEGACY]) {
7053 mask.control[band].legacy = rateset_to_mask(
7054 sband,
7055 nla_data(tb[NL80211_TXRATE_LEGACY]),
7056 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307057 if ((mask.control[band].legacy == 0) &&
7058 nla_len(tb[NL80211_TXRATE_LEGACY]))
7059 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007060 }
7061 if (tb[NL80211_TXRATE_MCS]) {
7062 if (!ht_rateset_to_mask(
7063 sband,
7064 nla_data(tb[NL80211_TXRATE_MCS]),
7065 nla_len(tb[NL80211_TXRATE_MCS]),
7066 mask.control[band].mcs))
7067 return -EINVAL;
7068 }
7069
7070 if (mask.control[band].legacy == 0) {
7071 /* don't allow empty legacy rates if HT
7072 * is not even supported. */
7073 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7074 return -EINVAL;
7075
7076 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7077 if (mask.control[band].mcs[i])
7078 break;
7079
7080 /* legacy and mcs rates may not be both empty */
7081 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02007082 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007083 }
7084 }
7085
Hila Gonene35e4d22012-06-27 17:19:42 +03007086 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007087}
7088
Johannes Berg2e161f72010-08-12 15:38:38 +02007089static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007090{
Johannes Berg4c476992010-10-04 21:36:35 +02007091 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007092 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007093 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007094
7095 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7096 return -EINVAL;
7097
Johannes Berg2e161f72010-08-12 15:38:38 +02007098 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7099 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007100
Johannes Berg71bbc992012-06-15 15:30:18 +02007101 switch (wdev->iftype) {
7102 case NL80211_IFTYPE_STATION:
7103 case NL80211_IFTYPE_ADHOC:
7104 case NL80211_IFTYPE_P2P_CLIENT:
7105 case NL80211_IFTYPE_AP:
7106 case NL80211_IFTYPE_AP_VLAN:
7107 case NL80211_IFTYPE_MESH_POINT:
7108 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007109 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007110 break;
7111 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007112 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007113 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007114
7115 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007116 if (!rdev->ops->mgmt_tx)
7117 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007118
Eric W. Biederman15e47302012-09-07 20:12:54 +00007119 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007120 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7121 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007122}
7123
Johannes Berg2e161f72010-08-12 15:38:38 +02007124static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007125{
Johannes Berg4c476992010-10-04 21:36:35 +02007126 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007127 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007128 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007129 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007130 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007131 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007132 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007133 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01007134 bool offchan, no_cck, dont_wait_for_ack;
7135
7136 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02007137
Johannes Berg683b6d32012-11-08 21:25:48 +01007138 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007139 return -EINVAL;
7140
Johannes Berg4c476992010-10-04 21:36:35 +02007141 if (!rdev->ops->mgmt_tx)
7142 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007143
Johannes Berg71bbc992012-06-15 15:30:18 +02007144 switch (wdev->iftype) {
7145 case NL80211_IFTYPE_STATION:
7146 case NL80211_IFTYPE_ADHOC:
7147 case NL80211_IFTYPE_P2P_CLIENT:
7148 case NL80211_IFTYPE_AP:
7149 case NL80211_IFTYPE_AP_VLAN:
7150 case NL80211_IFTYPE_MESH_POINT:
7151 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007152 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007153 break;
7154 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007155 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007156 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007157
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007158 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007159 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007160 return -EINVAL;
7161 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007162
7163 /*
7164 * We should wait on the channel for at least a minimum amount
7165 * of time (10ms) but no longer than the driver supports.
7166 */
7167 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7168 wait > rdev->wiphy.max_remain_on_channel_duration)
7169 return -EINVAL;
7170
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007171 }
7172
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007173 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
7174
Johannes Berg7c4ef712011-11-18 15:33:48 +01007175 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
7176 return -EINVAL;
7177
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307178 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
7179
Johannes Berg683b6d32012-11-08 21:25:48 +01007180 err = nl80211_parse_chandef(rdev, info, &chandef);
7181 if (err)
7182 return err;
Jouni Malinen026331c2010-02-15 12:53:10 +02007183
Johannes Berge247bd902011-11-04 11:18:21 +01007184 if (!dont_wait_for_ack) {
7185 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7186 if (!msg)
7187 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007188
Eric W. Biederman15e47302012-09-07 20:12:54 +00007189 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007190 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007191
Johannes Berge247bd902011-11-04 11:18:21 +01007192 if (IS_ERR(hdr)) {
7193 err = PTR_ERR(hdr);
7194 goto free_msg;
7195 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007196 }
Johannes Berge247bd902011-11-04 11:18:21 +01007197
Johannes Berg683b6d32012-11-08 21:25:48 +01007198 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chandef.chan, offchan, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02007199 nla_data(info->attrs[NL80211_ATTR_FRAME]),
7200 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01007201 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007202 if (err)
7203 goto free_msg;
7204
Johannes Berge247bd902011-11-04 11:18:21 +01007205 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007206 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7207 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007208
Johannes Berge247bd902011-11-04 11:18:21 +01007209 genlmsg_end(msg, hdr);
7210 return genlmsg_reply(msg, info);
7211 }
7212
7213 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007214
7215 nla_put_failure:
7216 err = -ENOBUFS;
7217 free_msg:
7218 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007219 return err;
7220}
7221
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007222static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7223{
7224 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007225 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007226 u64 cookie;
7227
7228 if (!info->attrs[NL80211_ATTR_COOKIE])
7229 return -EINVAL;
7230
7231 if (!rdev->ops->mgmt_tx_cancel_wait)
7232 return -EOPNOTSUPP;
7233
Johannes Berg71bbc992012-06-15 15:30:18 +02007234 switch (wdev->iftype) {
7235 case NL80211_IFTYPE_STATION:
7236 case NL80211_IFTYPE_ADHOC:
7237 case NL80211_IFTYPE_P2P_CLIENT:
7238 case NL80211_IFTYPE_AP:
7239 case NL80211_IFTYPE_AP_VLAN:
7240 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007241 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007242 break;
7243 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007244 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007245 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007246
7247 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7248
Hila Gonene35e4d22012-06-27 17:19:42 +03007249 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007250}
7251
Kalle Valoffb9eb32010-02-17 17:58:10 +02007252static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7253{
Johannes Berg4c476992010-10-04 21:36:35 +02007254 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007255 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007256 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007257 u8 ps_state;
7258 bool state;
7259 int err;
7260
Johannes Berg4c476992010-10-04 21:36:35 +02007261 if (!info->attrs[NL80211_ATTR_PS_STATE])
7262 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007263
7264 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7265
Johannes Berg4c476992010-10-04 21:36:35 +02007266 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7267 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007268
7269 wdev = dev->ieee80211_ptr;
7270
Johannes Berg4c476992010-10-04 21:36:35 +02007271 if (!rdev->ops->set_power_mgmt)
7272 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007273
7274 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7275
7276 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007277 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007278
Hila Gonene35e4d22012-06-27 17:19:42 +03007279 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007280 if (!err)
7281 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007282 return err;
7283}
7284
7285static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7286{
Johannes Berg4c476992010-10-04 21:36:35 +02007287 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007288 enum nl80211_ps_state ps_state;
7289 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007290 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007291 struct sk_buff *msg;
7292 void *hdr;
7293 int err;
7294
Kalle Valoffb9eb32010-02-17 17:58:10 +02007295 wdev = dev->ieee80211_ptr;
7296
Johannes Berg4c476992010-10-04 21:36:35 +02007297 if (!rdev->ops->set_power_mgmt)
7298 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007299
7300 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007301 if (!msg)
7302 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007303
Eric W. Biederman15e47302012-09-07 20:12:54 +00007304 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007305 NL80211_CMD_GET_POWER_SAVE);
7306 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007307 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007308 goto free_msg;
7309 }
7310
7311 if (wdev->ps)
7312 ps_state = NL80211_PS_ENABLED;
7313 else
7314 ps_state = NL80211_PS_DISABLED;
7315
David S. Miller9360ffd2012-03-29 04:41:26 -04007316 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7317 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007318
7319 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007320 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007321
Johannes Berg4c476992010-10-04 21:36:35 +02007322 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007323 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007324 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007325 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007326 return err;
7327}
7328
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007329static struct nla_policy
7330nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7331 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7332 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7333 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007334 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7335 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7336 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007337};
7338
Thomas Pedersen84f10702012-07-12 16:17:33 -07007339static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007340 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007341{
7342 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7343 struct wireless_dev *wdev;
7344 struct net_device *dev = info->user_ptr[1];
7345
Johannes Bergd9d8b012012-11-26 12:51:52 +01007346 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007347 return -EINVAL;
7348
7349 wdev = dev->ieee80211_ptr;
7350
7351 if (!rdev->ops->set_cqm_txe_config)
7352 return -EOPNOTSUPP;
7353
7354 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7355 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7356 return -EOPNOTSUPP;
7357
Hila Gonene35e4d22012-06-27 17:19:42 +03007358 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007359}
7360
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007361static int nl80211_set_cqm_rssi(struct genl_info *info,
7362 s32 threshold, u32 hysteresis)
7363{
Johannes Berg4c476992010-10-04 21:36:35 +02007364 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007365 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007366 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007367
7368 if (threshold > 0)
7369 return -EINVAL;
7370
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007371 wdev = dev->ieee80211_ptr;
7372
Johannes Berg4c476992010-10-04 21:36:35 +02007373 if (!rdev->ops->set_cqm_rssi_config)
7374 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007375
Johannes Berg074ac8d2010-09-16 14:58:22 +02007376 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007377 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7378 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007379
Hila Gonene35e4d22012-06-27 17:19:42 +03007380 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007381}
7382
7383static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7384{
7385 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7386 struct nlattr *cqm;
7387 int err;
7388
7389 cqm = info->attrs[NL80211_ATTR_CQM];
7390 if (!cqm) {
7391 err = -EINVAL;
7392 goto out;
7393 }
7394
7395 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7396 nl80211_attr_cqm_policy);
7397 if (err)
7398 goto out;
7399
7400 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7401 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
7402 s32 threshold;
7403 u32 hysteresis;
7404 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7405 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
7406 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007407 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7408 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7409 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7410 u32 rate, pkts, intvl;
7411 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7412 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7413 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7414 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007415 } else
7416 err = -EINVAL;
7417
7418out:
7419 return err;
7420}
7421
Johannes Berg29cbe682010-12-03 09:20:44 +01007422static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7423{
7424 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7425 struct net_device *dev = info->user_ptr[1];
7426 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007427 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007428 int err;
7429
7430 /* start with default */
7431 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007432 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007433
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007434 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007435 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007436 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007437 if (err)
7438 return err;
7439 }
7440
7441 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7442 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7443 return -EINVAL;
7444
Javier Cardonac80d5452010-12-16 17:37:49 -08007445 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7446 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7447
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007448 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7449 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7450 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7451 return -EINVAL;
7452
Marco Porsch9bdbf042013-01-07 16:04:51 +01007453 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7454 setup.beacon_interval =
7455 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7456 if (setup.beacon_interval < 10 ||
7457 setup.beacon_interval > 10000)
7458 return -EINVAL;
7459 }
7460
7461 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7462 setup.dtim_period =
7463 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7464 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7465 return -EINVAL;
7466 }
7467
Javier Cardonac80d5452010-12-16 17:37:49 -08007468 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7469 /* parse additional setup parameters if given */
7470 err = nl80211_parse_mesh_setup(info, &setup);
7471 if (err)
7472 return err;
7473 }
7474
Thomas Pedersend37bb182013-03-04 13:06:13 -08007475 if (setup.user_mpm)
7476 cfg.auto_open_plinks = false;
7477
Johannes Bergcc1d2802012-05-16 23:50:20 +02007478 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007479 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7480 if (err)
7481 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007482 } else {
7483 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007484 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007485 }
7486
Javier Cardonac80d5452010-12-16 17:37:49 -08007487 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007488}
7489
7490static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7491{
7492 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7493 struct net_device *dev = info->user_ptr[1];
7494
7495 return cfg80211_leave_mesh(rdev, dev);
7496}
7497
Johannes Bergdfb89c52012-06-27 09:23:48 +02007498#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007499static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7500 struct cfg80211_registered_device *rdev)
7501{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007502 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007503 struct nlattr *nl_pats, *nl_pat;
7504 int i, pat_len;
7505
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007506 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007507 return 0;
7508
7509 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7510 if (!nl_pats)
7511 return -ENOBUFS;
7512
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007513 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007514 nl_pat = nla_nest_start(msg, i + 1);
7515 if (!nl_pat)
7516 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007517 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007518 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
7519 DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007520 wowlan->patterns[i].mask) ||
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007521 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007522 pat_len, wowlan->patterns[i].pattern) ||
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007523 nla_put_u32(msg, NL80211_WOWLAN_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007524 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007525 return -ENOBUFS;
7526 nla_nest_end(msg, nl_pat);
7527 }
7528 nla_nest_end(msg, nl_pats);
7529
7530 return 0;
7531}
7532
Johannes Berg2a0e0472013-01-23 22:57:40 +01007533static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7534 struct cfg80211_wowlan_tcp *tcp)
7535{
7536 struct nlattr *nl_tcp;
7537
7538 if (!tcp)
7539 return 0;
7540
7541 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7542 if (!nl_tcp)
7543 return -ENOBUFS;
7544
7545 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7546 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7547 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7548 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7549 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7550 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7551 tcp->payload_len, tcp->payload) ||
7552 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7553 tcp->data_interval) ||
7554 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7555 tcp->wake_len, tcp->wake_data) ||
7556 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7557 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7558 return -ENOBUFS;
7559
7560 if (tcp->payload_seq.len &&
7561 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7562 sizeof(tcp->payload_seq), &tcp->payload_seq))
7563 return -ENOBUFS;
7564
7565 if (tcp->payload_tok.len &&
7566 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7567 sizeof(tcp->payload_tok) + tcp->tokens_size,
7568 &tcp->payload_tok))
7569 return -ENOBUFS;
7570
Johannes Berge248ad32013-05-16 10:24:28 +02007571 nla_nest_end(msg, nl_tcp);
7572
Johannes Berg2a0e0472013-01-23 22:57:40 +01007573 return 0;
7574}
7575
Johannes Bergff1b6e62011-05-04 15:37:28 +02007576static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7577{
7578 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7579 struct sk_buff *msg;
7580 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007581 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007582
Johannes Berg964dc9e2013-06-03 17:25:34 +02007583 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007584 return -EOPNOTSUPP;
7585
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007586 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01007587 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007588 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
7589 rdev->wiphy.wowlan_config->tcp->payload_len +
7590 rdev->wiphy.wowlan_config->tcp->wake_len +
7591 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007592 }
7593
7594 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007595 if (!msg)
7596 return -ENOMEM;
7597
Eric W. Biederman15e47302012-09-07 20:12:54 +00007598 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007599 NL80211_CMD_GET_WOWLAN);
7600 if (!hdr)
7601 goto nla_put_failure;
7602
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007603 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02007604 struct nlattr *nl_wowlan;
7605
7606 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7607 if (!nl_wowlan)
7608 goto nla_put_failure;
7609
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007610 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007611 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007612 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007613 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007614 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007615 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007616 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007617 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007618 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007619 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007620 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007621 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007622 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007623 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7624 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007625
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007626 if (nl80211_send_wowlan_patterns(msg, rdev))
7627 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007628
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007629 if (nl80211_send_wowlan_tcp(msg,
7630 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01007631 goto nla_put_failure;
7632
Johannes Bergff1b6e62011-05-04 15:37:28 +02007633 nla_nest_end(msg, nl_wowlan);
7634 }
7635
7636 genlmsg_end(msg, hdr);
7637 return genlmsg_reply(msg, info);
7638
7639nla_put_failure:
7640 nlmsg_free(msg);
7641 return -ENOBUFS;
7642}
7643
Johannes Berg2a0e0472013-01-23 22:57:40 +01007644static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7645 struct nlattr *attr,
7646 struct cfg80211_wowlan *trig)
7647{
7648 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7649 struct cfg80211_wowlan_tcp *cfg;
7650 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7651 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7652 u32 size;
7653 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7654 int err, port;
7655
Johannes Berg964dc9e2013-06-03 17:25:34 +02007656 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007657 return -EINVAL;
7658
7659 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7660 nla_data(attr), nla_len(attr),
7661 nl80211_wowlan_tcp_policy);
7662 if (err)
7663 return err;
7664
7665 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
7666 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
7667 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
7668 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
7669 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
7670 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
7671 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
7672 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
7673 return -EINVAL;
7674
7675 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02007676 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007677 return -EINVAL;
7678
7679 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02007680 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01007681 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007682 return -EINVAL;
7683
7684 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02007685 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007686 return -EINVAL;
7687
7688 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
7689 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
7690 return -EINVAL;
7691
7692 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
7693 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7694
7695 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
7696 tokens_size = tokln - sizeof(*tok);
7697
7698 if (!tok->len || tokens_size % tok->len)
7699 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02007700 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007701 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02007702 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007703 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02007704 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007705 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02007706 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007707 return -EINVAL;
7708 if (tok->offset + tok->len > data_size)
7709 return -EINVAL;
7710 }
7711
7712 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
7713 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02007714 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007715 return -EINVAL;
7716 if (seq->len == 0 || seq->len > 4)
7717 return -EINVAL;
7718 if (seq->len + seq->offset > data_size)
7719 return -EINVAL;
7720 }
7721
7722 size = sizeof(*cfg);
7723 size += data_size;
7724 size += wake_size + wake_mask_size;
7725 size += tokens_size;
7726
7727 cfg = kzalloc(size, GFP_KERNEL);
7728 if (!cfg)
7729 return -ENOMEM;
7730 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
7731 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
7732 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
7733 ETH_ALEN);
7734 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
7735 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
7736 else
7737 port = 0;
7738#ifdef CONFIG_INET
7739 /* allocate a socket and port for it and use it */
7740 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
7741 IPPROTO_TCP, &cfg->sock, 1);
7742 if (err) {
7743 kfree(cfg);
7744 return err;
7745 }
7746 if (inet_csk_get_port(cfg->sock->sk, port)) {
7747 sock_release(cfg->sock);
7748 kfree(cfg);
7749 return -EADDRINUSE;
7750 }
7751 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
7752#else
7753 if (!port) {
7754 kfree(cfg);
7755 return -EINVAL;
7756 }
7757 cfg->src_port = port;
7758#endif
7759
7760 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
7761 cfg->payload_len = data_size;
7762 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
7763 memcpy((void *)cfg->payload,
7764 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
7765 data_size);
7766 if (seq)
7767 cfg->payload_seq = *seq;
7768 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
7769 cfg->wake_len = wake_size;
7770 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
7771 memcpy((void *)cfg->wake_data,
7772 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
7773 wake_size);
7774 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
7775 data_size + wake_size;
7776 memcpy((void *)cfg->wake_mask,
7777 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
7778 wake_mask_size);
7779 if (tok) {
7780 cfg->tokens_size = tokens_size;
7781 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
7782 }
7783
7784 trig->tcp = cfg;
7785
7786 return 0;
7787}
7788
Johannes Bergff1b6e62011-05-04 15:37:28 +02007789static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
7790{
7791 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7792 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02007793 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02007794 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02007795 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007796 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007797 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007798
Johannes Berg964dc9e2013-06-03 17:25:34 +02007799 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007800 return -EOPNOTSUPP;
7801
Johannes Bergae33bd82012-07-12 16:25:02 +02007802 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
7803 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007804 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02007805 goto set_wakeup;
7806 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02007807
7808 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
7809 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7810 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
7811 nl80211_wowlan_policy);
7812 if (err)
7813 return err;
7814
7815 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
7816 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
7817 return -EINVAL;
7818 new_triggers.any = true;
7819 }
7820
7821 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
7822 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
7823 return -EINVAL;
7824 new_triggers.disconnect = true;
7825 }
7826
7827 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
7828 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
7829 return -EINVAL;
7830 new_triggers.magic_pkt = true;
7831 }
7832
Johannes Berg77dbbb12011-07-13 10:48:55 +02007833 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
7834 return -EINVAL;
7835
7836 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
7837 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
7838 return -EINVAL;
7839 new_triggers.gtk_rekey_failure = true;
7840 }
7841
7842 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
7843 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
7844 return -EINVAL;
7845 new_triggers.eap_identity_req = true;
7846 }
7847
7848 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
7849 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
7850 return -EINVAL;
7851 new_triggers.four_way_handshake = true;
7852 }
7853
7854 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
7855 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
7856 return -EINVAL;
7857 new_triggers.rfkill_release = true;
7858 }
7859
Johannes Bergff1b6e62011-05-04 15:37:28 +02007860 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
7861 struct nlattr *pat;
7862 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007863 int rem, pat_len, mask_len, pkt_offset;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007864 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
7865
7866 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7867 rem)
7868 n_patterns++;
7869 if (n_patterns > wowlan->n_patterns)
7870 return -EINVAL;
7871
7872 new_triggers.patterns = kcalloc(n_patterns,
7873 sizeof(new_triggers.patterns[0]),
7874 GFP_KERNEL);
7875 if (!new_triggers.patterns)
7876 return -ENOMEM;
7877
7878 new_triggers.n_patterns = n_patterns;
7879 i = 0;
7880
7881 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
7882 rem) {
7883 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
7884 nla_data(pat), nla_len(pat), NULL);
7885 err = -EINVAL;
7886 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
7887 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
7888 goto error;
7889 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
7890 mask_len = DIV_ROUND_UP(pat_len, 8);
7891 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
7892 mask_len)
7893 goto error;
7894 if (pat_len > wowlan->pattern_max_len ||
7895 pat_len < wowlan->pattern_min_len)
7896 goto error;
7897
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007898 if (!pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET])
7899 pkt_offset = 0;
7900 else
7901 pkt_offset = nla_get_u32(
7902 pat_tb[NL80211_WOWLAN_PKTPAT_OFFSET]);
7903 if (pkt_offset > wowlan->max_pkt_offset)
7904 goto error;
7905 new_triggers.patterns[i].pkt_offset = pkt_offset;
7906
Johannes Bergff1b6e62011-05-04 15:37:28 +02007907 new_triggers.patterns[i].mask =
7908 kmalloc(mask_len + pat_len, GFP_KERNEL);
7909 if (!new_triggers.patterns[i].mask) {
7910 err = -ENOMEM;
7911 goto error;
7912 }
7913 new_triggers.patterns[i].pattern =
7914 new_triggers.patterns[i].mask + mask_len;
7915 memcpy(new_triggers.patterns[i].mask,
7916 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
7917 mask_len);
7918 new_triggers.patterns[i].pattern_len = pat_len;
7919 memcpy(new_triggers.patterns[i].pattern,
7920 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
7921 pat_len);
7922 i++;
7923 }
7924 }
7925
Johannes Berg2a0e0472013-01-23 22:57:40 +01007926 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
7927 err = nl80211_parse_wowlan_tcp(
7928 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
7929 &new_triggers);
7930 if (err)
7931 goto error;
7932 }
7933
Johannes Bergae33bd82012-07-12 16:25:02 +02007934 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
7935 if (!ntrig) {
7936 err = -ENOMEM;
7937 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007938 }
Johannes Bergae33bd82012-07-12 16:25:02 +02007939 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007940 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007941
Johannes Bergae33bd82012-07-12 16:25:02 +02007942 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007943 if (rdev->ops->set_wakeup &&
7944 prev_enabled != !!rdev->wiphy.wowlan_config)
7945 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02007946
Johannes Bergff1b6e62011-05-04 15:37:28 +02007947 return 0;
7948 error:
7949 for (i = 0; i < new_triggers.n_patterns; i++)
7950 kfree(new_triggers.patterns[i].mask);
7951 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01007952 if (new_triggers.tcp && new_triggers.tcp->sock)
7953 sock_release(new_triggers.tcp->sock);
7954 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007955 return err;
7956}
Johannes Bergdfb89c52012-06-27 09:23:48 +02007957#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02007958
Johannes Berge5497d72011-07-05 16:35:40 +02007959static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
7960{
7961 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7962 struct net_device *dev = info->user_ptr[1];
7963 struct wireless_dev *wdev = dev->ieee80211_ptr;
7964 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
7965 struct cfg80211_gtk_rekey_data rekey_data;
7966 int err;
7967
7968 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
7969 return -EINVAL;
7970
7971 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
7972 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
7973 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
7974 nl80211_rekey_policy);
7975 if (err)
7976 return err;
7977
7978 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
7979 return -ERANGE;
7980 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
7981 return -ERANGE;
7982 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
7983 return -ERANGE;
7984
7985 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
7986 NL80211_KEK_LEN);
7987 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
7988 NL80211_KCK_LEN);
7989 memcpy(rekey_data.replay_ctr,
7990 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
7991 NL80211_REPLAY_CTR_LEN);
7992
7993 wdev_lock(wdev);
7994 if (!wdev->current_bss) {
7995 err = -ENOTCONN;
7996 goto out;
7997 }
7998
7999 if (!rdev->ops->set_rekey_data) {
8000 err = -EOPNOTSUPP;
8001 goto out;
8002 }
8003
Hila Gonene35e4d22012-06-27 17:19:42 +03008004 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008005 out:
8006 wdev_unlock(wdev);
8007 return err;
8008}
8009
Johannes Berg28946da2011-11-04 11:18:12 +01008010static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8011 struct genl_info *info)
8012{
8013 struct net_device *dev = info->user_ptr[1];
8014 struct wireless_dev *wdev = dev->ieee80211_ptr;
8015
8016 if (wdev->iftype != NL80211_IFTYPE_AP &&
8017 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8018 return -EINVAL;
8019
Eric W. Biederman15e47302012-09-07 20:12:54 +00008020 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008021 return -EBUSY;
8022
Eric W. Biederman15e47302012-09-07 20:12:54 +00008023 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008024 return 0;
8025}
8026
Johannes Berg7f6cf312011-11-04 11:18:15 +01008027static int nl80211_probe_client(struct sk_buff *skb,
8028 struct genl_info *info)
8029{
8030 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8031 struct net_device *dev = info->user_ptr[1];
8032 struct wireless_dev *wdev = dev->ieee80211_ptr;
8033 struct sk_buff *msg;
8034 void *hdr;
8035 const u8 *addr;
8036 u64 cookie;
8037 int err;
8038
8039 if (wdev->iftype != NL80211_IFTYPE_AP &&
8040 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8041 return -EOPNOTSUPP;
8042
8043 if (!info->attrs[NL80211_ATTR_MAC])
8044 return -EINVAL;
8045
8046 if (!rdev->ops->probe_client)
8047 return -EOPNOTSUPP;
8048
8049 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8050 if (!msg)
8051 return -ENOMEM;
8052
Eric W. Biederman15e47302012-09-07 20:12:54 +00008053 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008054 NL80211_CMD_PROBE_CLIENT);
8055
8056 if (IS_ERR(hdr)) {
8057 err = PTR_ERR(hdr);
8058 goto free_msg;
8059 }
8060
8061 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8062
Hila Gonene35e4d22012-06-27 17:19:42 +03008063 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008064 if (err)
8065 goto free_msg;
8066
David S. Miller9360ffd2012-03-29 04:41:26 -04008067 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8068 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008069
8070 genlmsg_end(msg, hdr);
8071
8072 return genlmsg_reply(msg, info);
8073
8074 nla_put_failure:
8075 err = -ENOBUFS;
8076 free_msg:
8077 nlmsg_free(msg);
8078 return err;
8079}
8080
Johannes Berg5e760232011-11-04 11:18:17 +01008081static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8082{
8083 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008084 struct cfg80211_beacon_registration *reg, *nreg;
8085 int rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008086
8087 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8088 return -EOPNOTSUPP;
8089
Ben Greear37c73b52012-10-26 14:49:25 -07008090 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8091 if (!nreg)
8092 return -ENOMEM;
Johannes Berg5e760232011-11-04 11:18:17 +01008093
Ben Greear37c73b52012-10-26 14:49:25 -07008094 /* First, check if already registered. */
8095 spin_lock_bh(&rdev->beacon_registrations_lock);
8096 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8097 if (reg->nlportid == info->snd_portid) {
8098 rv = -EALREADY;
8099 goto out_err;
8100 }
8101 }
8102 /* Add it to the list */
8103 nreg->nlportid = info->snd_portid;
8104 list_add(&nreg->list, &rdev->beacon_registrations);
8105
8106 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +01008107
8108 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008109out_err:
8110 spin_unlock_bh(&rdev->beacon_registrations_lock);
8111 kfree(nreg);
8112 return rv;
Johannes Berg5e760232011-11-04 11:18:17 +01008113}
8114
Johannes Berg98104fde2012-06-16 00:19:54 +02008115static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8116{
8117 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8118 struct wireless_dev *wdev = info->user_ptr[1];
8119 int err;
8120
8121 if (!rdev->ops->start_p2p_device)
8122 return -EOPNOTSUPP;
8123
8124 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8125 return -EOPNOTSUPP;
8126
8127 if (wdev->p2p_started)
8128 return 0;
8129
Johannes Berg98104fde2012-06-16 00:19:54 +02008130 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008131 if (err)
8132 return err;
8133
Johannes Bergeeb126e2012-10-23 15:16:50 +02008134 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008135 if (err)
8136 return err;
8137
8138 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02008139 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02008140
8141 return 0;
8142}
8143
8144static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8145{
8146 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8147 struct wireless_dev *wdev = info->user_ptr[1];
8148
8149 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8150 return -EOPNOTSUPP;
8151
8152 if (!rdev->ops->stop_p2p_device)
8153 return -EOPNOTSUPP;
8154
Johannes Bergf9f47522013-03-19 15:04:07 +01008155 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008156
8157 return 0;
8158}
8159
Johannes Berg3713b4e2013-02-14 16:19:38 +01008160static int nl80211_get_protocol_features(struct sk_buff *skb,
8161 struct genl_info *info)
8162{
8163 void *hdr;
8164 struct sk_buff *msg;
8165
8166 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8167 if (!msg)
8168 return -ENOMEM;
8169
8170 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8171 NL80211_CMD_GET_PROTOCOL_FEATURES);
8172 if (!hdr)
8173 goto nla_put_failure;
8174
8175 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8176 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8177 goto nla_put_failure;
8178
8179 genlmsg_end(msg, hdr);
8180 return genlmsg_reply(msg, info);
8181
8182 nla_put_failure:
8183 kfree_skb(msg);
8184 return -ENOBUFS;
8185}
8186
Jouni Malinen355199e2013-02-27 17:14:27 +02008187static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8188{
8189 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8190 struct cfg80211_update_ft_ies_params ft_params;
8191 struct net_device *dev = info->user_ptr[1];
8192
8193 if (!rdev->ops->update_ft_ies)
8194 return -EOPNOTSUPP;
8195
8196 if (!info->attrs[NL80211_ATTR_MDID] ||
8197 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8198 return -EINVAL;
8199
8200 memset(&ft_params, 0, sizeof(ft_params));
8201 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8202 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8203 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8204
8205 return rdev_update_ft_ies(rdev, dev, &ft_params);
8206}
8207
Arend van Spriel5de17982013-04-18 15:49:00 +02008208static int nl80211_crit_protocol_start(struct sk_buff *skb,
8209 struct genl_info *info)
8210{
8211 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8212 struct wireless_dev *wdev = info->user_ptr[1];
8213 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8214 u16 duration;
8215 int ret;
8216
8217 if (!rdev->ops->crit_proto_start)
8218 return -EOPNOTSUPP;
8219
8220 if (WARN_ON(!rdev->ops->crit_proto_stop))
8221 return -EINVAL;
8222
8223 if (rdev->crit_proto_nlportid)
8224 return -EBUSY;
8225
8226 /* determine protocol if provided */
8227 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8228 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8229
8230 if (proto >= NUM_NL80211_CRIT_PROTO)
8231 return -EINVAL;
8232
8233 /* timeout must be provided */
8234 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8235 return -EINVAL;
8236
8237 duration =
8238 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8239
8240 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8241 return -ERANGE;
8242
8243 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8244 if (!ret)
8245 rdev->crit_proto_nlportid = info->snd_portid;
8246
8247 return ret;
8248}
8249
8250static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8251 struct genl_info *info)
8252{
8253 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8254 struct wireless_dev *wdev = info->user_ptr[1];
8255
8256 if (!rdev->ops->crit_proto_stop)
8257 return -EOPNOTSUPP;
8258
8259 if (rdev->crit_proto_nlportid) {
8260 rdev->crit_proto_nlportid = 0;
8261 rdev_crit_proto_stop(rdev, wdev);
8262 }
8263 return 0;
8264}
8265
Johannes Berg4c476992010-10-04 21:36:35 +02008266#define NL80211_FLAG_NEED_WIPHY 0x01
8267#define NL80211_FLAG_NEED_NETDEV 0x02
8268#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008269#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8270#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8271 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008272#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008273/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008274#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8275 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008276
8277static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8278 struct genl_info *info)
8279{
8280 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008281 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008282 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008283 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8284
8285 if (rtnl)
8286 rtnl_lock();
8287
8288 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008289 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008290 if (IS_ERR(rdev)) {
8291 if (rtnl)
8292 rtnl_unlock();
8293 return PTR_ERR(rdev);
8294 }
8295 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008296 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8297 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02008298 ASSERT_RTNL();
8299
Johannes Berg89a54e42012-06-15 14:33:17 +02008300 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8301 info->attrs);
8302 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02008303 if (rtnl)
8304 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02008305 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02008306 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008307
Johannes Berg89a54e42012-06-15 14:33:17 +02008308 dev = wdev->netdev;
8309 rdev = wiphy_to_dev(wdev->wiphy);
8310
Johannes Berg1bf614e2012-06-15 15:23:36 +02008311 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
8312 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02008313 if (rtnl)
8314 rtnl_unlock();
8315 return -EINVAL;
8316 }
8317
8318 info->user_ptr[1] = dev;
8319 } else {
8320 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02008321 }
Johannes Berg89a54e42012-06-15 14:33:17 +02008322
Johannes Berg1bf614e2012-06-15 15:23:36 +02008323 if (dev) {
8324 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
8325 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02008326 if (rtnl)
8327 rtnl_unlock();
8328 return -ENETDOWN;
8329 }
8330
8331 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008332 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
8333 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02008334 if (rtnl)
8335 rtnl_unlock();
8336 return -ENETDOWN;
8337 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02008338 }
8339
Johannes Berg4c476992010-10-04 21:36:35 +02008340 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008341 }
8342
8343 return 0;
8344}
8345
8346static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8347 struct genl_info *info)
8348{
Johannes Berg1bf614e2012-06-15 15:23:36 +02008349 if (info->user_ptr[1]) {
8350 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
8351 struct wireless_dev *wdev = info->user_ptr[1];
8352
8353 if (wdev->netdev)
8354 dev_put(wdev->netdev);
8355 } else {
8356 dev_put(info->user_ptr[1]);
8357 }
8358 }
Johannes Berg4c476992010-10-04 21:36:35 +02008359 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
8360 rtnl_unlock();
8361}
8362
Johannes Berg55682962007-09-20 13:09:35 -04008363static struct genl_ops nl80211_ops[] = {
8364 {
8365 .cmd = NL80211_CMD_GET_WIPHY,
8366 .doit = nl80211_get_wiphy,
8367 .dumpit = nl80211_dump_wiphy,
8368 .policy = nl80211_policy,
8369 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02008370 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8371 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008372 },
8373 {
8374 .cmd = NL80211_CMD_SET_WIPHY,
8375 .doit = nl80211_set_wiphy,
8376 .policy = nl80211_policy,
8377 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008378 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008379 },
8380 {
8381 .cmd = NL80211_CMD_GET_INTERFACE,
8382 .doit = nl80211_get_interface,
8383 .dumpit = nl80211_dump_interface,
8384 .policy = nl80211_policy,
8385 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02008386 .internal_flags = NL80211_FLAG_NEED_WDEV |
8387 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008388 },
8389 {
8390 .cmd = NL80211_CMD_SET_INTERFACE,
8391 .doit = nl80211_set_interface,
8392 .policy = nl80211_policy,
8393 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008394 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8395 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008396 },
8397 {
8398 .cmd = NL80211_CMD_NEW_INTERFACE,
8399 .doit = nl80211_new_interface,
8400 .policy = nl80211_policy,
8401 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008402 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8403 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008404 },
8405 {
8406 .cmd = NL80211_CMD_DEL_INTERFACE,
8407 .doit = nl80211_del_interface,
8408 .policy = nl80211_policy,
8409 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02008410 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008411 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04008412 },
Johannes Berg41ade002007-12-19 02:03:29 +01008413 {
8414 .cmd = NL80211_CMD_GET_KEY,
8415 .doit = nl80211_get_key,
8416 .policy = nl80211_policy,
8417 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008418 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008419 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008420 },
8421 {
8422 .cmd = NL80211_CMD_SET_KEY,
8423 .doit = nl80211_set_key,
8424 .policy = nl80211_policy,
8425 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008426 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008427 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008428 },
8429 {
8430 .cmd = NL80211_CMD_NEW_KEY,
8431 .doit = nl80211_new_key,
8432 .policy = nl80211_policy,
8433 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008434 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008435 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008436 },
8437 {
8438 .cmd = NL80211_CMD_DEL_KEY,
8439 .doit = nl80211_del_key,
8440 .policy = nl80211_policy,
8441 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008442 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008443 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01008444 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01008445 {
8446 .cmd = NL80211_CMD_SET_BEACON,
8447 .policy = nl80211_policy,
8448 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008449 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008450 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008451 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008452 },
8453 {
Johannes Berg88600202012-02-13 15:17:18 +01008454 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008455 .policy = nl80211_policy,
8456 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008457 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008458 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008459 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008460 },
8461 {
Johannes Berg88600202012-02-13 15:17:18 +01008462 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008463 .policy = nl80211_policy,
8464 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01008465 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008466 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008467 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01008468 },
Johannes Berg5727ef12007-12-19 02:03:34 +01008469 {
8470 .cmd = NL80211_CMD_GET_STATION,
8471 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008472 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01008473 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02008474 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8475 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008476 },
8477 {
8478 .cmd = NL80211_CMD_SET_STATION,
8479 .doit = nl80211_set_station,
8480 .policy = nl80211_policy,
8481 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008482 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008483 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008484 },
8485 {
8486 .cmd = NL80211_CMD_NEW_STATION,
8487 .doit = nl80211_new_station,
8488 .policy = nl80211_policy,
8489 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008490 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008491 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008492 },
8493 {
8494 .cmd = NL80211_CMD_DEL_STATION,
8495 .doit = nl80211_del_station,
8496 .policy = nl80211_policy,
8497 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008498 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008499 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01008500 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008501 {
8502 .cmd = NL80211_CMD_GET_MPATH,
8503 .doit = nl80211_get_mpath,
8504 .dumpit = nl80211_dump_mpath,
8505 .policy = nl80211_policy,
8506 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008507 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008508 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008509 },
8510 {
8511 .cmd = NL80211_CMD_SET_MPATH,
8512 .doit = nl80211_set_mpath,
8513 .policy = nl80211_policy,
8514 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008515 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008516 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008517 },
8518 {
8519 .cmd = NL80211_CMD_NEW_MPATH,
8520 .doit = nl80211_new_mpath,
8521 .policy = nl80211_policy,
8522 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008523 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008524 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008525 },
8526 {
8527 .cmd = NL80211_CMD_DEL_MPATH,
8528 .doit = nl80211_del_mpath,
8529 .policy = nl80211_policy,
8530 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008531 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008532 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01008533 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008534 {
8535 .cmd = NL80211_CMD_SET_BSS,
8536 .doit = nl80211_set_bss,
8537 .policy = nl80211_policy,
8538 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008539 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008540 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03008541 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008542 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008543 .cmd = NL80211_CMD_GET_REG,
8544 .doit = nl80211_get_reg,
8545 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02008546 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08008547 /* can be retrieved by unprivileged users */
8548 },
8549 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008550 .cmd = NL80211_CMD_SET_REG,
8551 .doit = nl80211_set_reg,
8552 .policy = nl80211_policy,
8553 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02008554 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07008555 },
8556 {
8557 .cmd = NL80211_CMD_REQ_SET_REG,
8558 .doit = nl80211_req_set_reg,
8559 .policy = nl80211_policy,
8560 .flags = GENL_ADMIN_PERM,
8561 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008562 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008563 .cmd = NL80211_CMD_GET_MESH_CONFIG,
8564 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008565 .policy = nl80211_policy,
8566 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008567 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008568 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008569 },
8570 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08008571 .cmd = NL80211_CMD_SET_MESH_CONFIG,
8572 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008573 .policy = nl80211_policy,
8574 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01008575 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008576 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07008577 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02008578 {
Johannes Berg2a519312009-02-10 21:25:55 +01008579 .cmd = NL80211_CMD_TRIGGER_SCAN,
8580 .doit = nl80211_trigger_scan,
8581 .policy = nl80211_policy,
8582 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02008583 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008584 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01008585 },
8586 {
8587 .cmd = NL80211_CMD_GET_SCAN,
8588 .policy = nl80211_policy,
8589 .dumpit = nl80211_dump_scan,
8590 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02008591 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03008592 .cmd = NL80211_CMD_START_SCHED_SCAN,
8593 .doit = nl80211_start_sched_scan,
8594 .policy = nl80211_policy,
8595 .flags = GENL_ADMIN_PERM,
8596 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8597 NL80211_FLAG_NEED_RTNL,
8598 },
8599 {
8600 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
8601 .doit = nl80211_stop_sched_scan,
8602 .policy = nl80211_policy,
8603 .flags = GENL_ADMIN_PERM,
8604 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8605 NL80211_FLAG_NEED_RTNL,
8606 },
8607 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02008608 .cmd = NL80211_CMD_AUTHENTICATE,
8609 .doit = nl80211_authenticate,
8610 .policy = nl80211_policy,
8611 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008612 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008613 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008614 },
8615 {
8616 .cmd = NL80211_CMD_ASSOCIATE,
8617 .doit = nl80211_associate,
8618 .policy = nl80211_policy,
8619 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008620 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008621 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008622 },
8623 {
8624 .cmd = NL80211_CMD_DEAUTHENTICATE,
8625 .doit = nl80211_deauthenticate,
8626 .policy = nl80211_policy,
8627 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008628 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008629 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008630 },
8631 {
8632 .cmd = NL80211_CMD_DISASSOCIATE,
8633 .doit = nl80211_disassociate,
8634 .policy = nl80211_policy,
8635 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008636 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008637 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02008638 },
Johannes Berg04a773a2009-04-19 21:24:32 +02008639 {
8640 .cmd = NL80211_CMD_JOIN_IBSS,
8641 .doit = nl80211_join_ibss,
8642 .policy = nl80211_policy,
8643 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008644 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008645 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008646 },
8647 {
8648 .cmd = NL80211_CMD_LEAVE_IBSS,
8649 .doit = nl80211_leave_ibss,
8650 .policy = nl80211_policy,
8651 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008652 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008653 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02008654 },
Johannes Bergaff89a92009-07-01 21:26:51 +02008655#ifdef CONFIG_NL80211_TESTMODE
8656 {
8657 .cmd = NL80211_CMD_TESTMODE,
8658 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07008659 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02008660 .policy = nl80211_policy,
8661 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008662 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8663 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02008664 },
8665#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02008666 {
8667 .cmd = NL80211_CMD_CONNECT,
8668 .doit = nl80211_connect,
8669 .policy = nl80211_policy,
8670 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008671 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008672 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008673 },
8674 {
8675 .cmd = NL80211_CMD_DISCONNECT,
8676 .doit = nl80211_disconnect,
8677 .policy = nl80211_policy,
8678 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02008679 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008680 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02008681 },
Johannes Berg463d0182009-07-14 00:33:35 +02008682 {
8683 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
8684 .doit = nl80211_wiphy_netns,
8685 .policy = nl80211_policy,
8686 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008687 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8688 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02008689 },
Holger Schurig61fa7132009-11-11 12:25:40 +01008690 {
8691 .cmd = NL80211_CMD_GET_SURVEY,
8692 .policy = nl80211_policy,
8693 .dumpit = nl80211_dump_survey,
8694 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008695 {
8696 .cmd = NL80211_CMD_SET_PMKSA,
8697 .doit = nl80211_setdel_pmksa,
8698 .policy = nl80211_policy,
8699 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008700 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008701 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008702 },
8703 {
8704 .cmd = NL80211_CMD_DEL_PMKSA,
8705 .doit = nl80211_setdel_pmksa,
8706 .policy = nl80211_policy,
8707 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008708 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008709 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008710 },
8711 {
8712 .cmd = NL80211_CMD_FLUSH_PMKSA,
8713 .doit = nl80211_flush_pmksa,
8714 .policy = nl80211_policy,
8715 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008716 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008717 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01008718 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008719 {
8720 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
8721 .doit = nl80211_remain_on_channel,
8722 .policy = nl80211_policy,
8723 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008724 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008725 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008726 },
8727 {
8728 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
8729 .doit = nl80211_cancel_remain_on_channel,
8730 .policy = nl80211_policy,
8731 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008732 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008733 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008734 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008735 {
8736 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
8737 .doit = nl80211_set_tx_bitrate_mask,
8738 .policy = nl80211_policy,
8739 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008740 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8741 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02008742 },
Jouni Malinen026331c2010-02-15 12:53:10 +02008743 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008744 .cmd = NL80211_CMD_REGISTER_FRAME,
8745 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008746 .policy = nl80211_policy,
8747 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008748 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02008749 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008750 },
8751 {
Johannes Berg2e161f72010-08-12 15:38:38 +02008752 .cmd = NL80211_CMD_FRAME,
8753 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02008754 .policy = nl80211_policy,
8755 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008756 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02008757 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02008758 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02008759 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008760 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
8761 .doit = nl80211_tx_mgmt_cancel_wait,
8762 .policy = nl80211_policy,
8763 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02008764 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01008765 NL80211_FLAG_NEED_RTNL,
8766 },
8767 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02008768 .cmd = NL80211_CMD_SET_POWER_SAVE,
8769 .doit = nl80211_set_power_save,
8770 .policy = nl80211_policy,
8771 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008772 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8773 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008774 },
8775 {
8776 .cmd = NL80211_CMD_GET_POWER_SAVE,
8777 .doit = nl80211_get_power_save,
8778 .policy = nl80211_policy,
8779 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02008780 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8781 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02008782 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008783 {
8784 .cmd = NL80211_CMD_SET_CQM,
8785 .doit = nl80211_set_cqm,
8786 .policy = nl80211_policy,
8787 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008788 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8789 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008790 },
Johannes Bergf444de02010-05-05 15:25:02 +02008791 {
8792 .cmd = NL80211_CMD_SET_CHANNEL,
8793 .doit = nl80211_set_channel,
8794 .policy = nl80211_policy,
8795 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02008796 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8797 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02008798 },
Bill Jordane8347eb2010-10-01 13:54:28 -04008799 {
8800 .cmd = NL80211_CMD_SET_WDS_PEER,
8801 .doit = nl80211_set_wds_peer,
8802 .policy = nl80211_policy,
8803 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02008804 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8805 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04008806 },
Johannes Berg29cbe682010-12-03 09:20:44 +01008807 {
8808 .cmd = NL80211_CMD_JOIN_MESH,
8809 .doit = nl80211_join_mesh,
8810 .policy = nl80211_policy,
8811 .flags = GENL_ADMIN_PERM,
8812 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8813 NL80211_FLAG_NEED_RTNL,
8814 },
8815 {
8816 .cmd = NL80211_CMD_LEAVE_MESH,
8817 .doit = nl80211_leave_mesh,
8818 .policy = nl80211_policy,
8819 .flags = GENL_ADMIN_PERM,
8820 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8821 NL80211_FLAG_NEED_RTNL,
8822 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008823#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02008824 {
8825 .cmd = NL80211_CMD_GET_WOWLAN,
8826 .doit = nl80211_get_wowlan,
8827 .policy = nl80211_policy,
8828 /* can be retrieved by unprivileged users */
8829 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8830 NL80211_FLAG_NEED_RTNL,
8831 },
8832 {
8833 .cmd = NL80211_CMD_SET_WOWLAN,
8834 .doit = nl80211_set_wowlan,
8835 .policy = nl80211_policy,
8836 .flags = GENL_ADMIN_PERM,
8837 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8838 NL80211_FLAG_NEED_RTNL,
8839 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02008840#endif
Johannes Berge5497d72011-07-05 16:35:40 +02008841 {
8842 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
8843 .doit = nl80211_set_rekey_data,
8844 .policy = nl80211_policy,
8845 .flags = GENL_ADMIN_PERM,
8846 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8847 NL80211_FLAG_NEED_RTNL,
8848 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03008849 {
8850 .cmd = NL80211_CMD_TDLS_MGMT,
8851 .doit = nl80211_tdls_mgmt,
8852 .policy = nl80211_policy,
8853 .flags = GENL_ADMIN_PERM,
8854 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8855 NL80211_FLAG_NEED_RTNL,
8856 },
8857 {
8858 .cmd = NL80211_CMD_TDLS_OPER,
8859 .doit = nl80211_tdls_oper,
8860 .policy = nl80211_policy,
8861 .flags = GENL_ADMIN_PERM,
8862 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8863 NL80211_FLAG_NEED_RTNL,
8864 },
Johannes Berg28946da2011-11-04 11:18:12 +01008865 {
8866 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
8867 .doit = nl80211_register_unexpected_frame,
8868 .policy = nl80211_policy,
8869 .flags = GENL_ADMIN_PERM,
8870 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8871 NL80211_FLAG_NEED_RTNL,
8872 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01008873 {
8874 .cmd = NL80211_CMD_PROBE_CLIENT,
8875 .doit = nl80211_probe_client,
8876 .policy = nl80211_policy,
8877 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02008878 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01008879 NL80211_FLAG_NEED_RTNL,
8880 },
Johannes Berg5e760232011-11-04 11:18:17 +01008881 {
8882 .cmd = NL80211_CMD_REGISTER_BEACONS,
8883 .doit = nl80211_register_beacons,
8884 .policy = nl80211_policy,
8885 .flags = GENL_ADMIN_PERM,
8886 .internal_flags = NL80211_FLAG_NEED_WIPHY |
8887 NL80211_FLAG_NEED_RTNL,
8888 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01008889 {
8890 .cmd = NL80211_CMD_SET_NOACK_MAP,
8891 .doit = nl80211_set_noack_map,
8892 .policy = nl80211_policy,
8893 .flags = GENL_ADMIN_PERM,
8894 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8895 NL80211_FLAG_NEED_RTNL,
8896 },
Johannes Berg98104fde2012-06-16 00:19:54 +02008897 {
8898 .cmd = NL80211_CMD_START_P2P_DEVICE,
8899 .doit = nl80211_start_p2p_device,
8900 .policy = nl80211_policy,
8901 .flags = GENL_ADMIN_PERM,
8902 .internal_flags = NL80211_FLAG_NEED_WDEV |
8903 NL80211_FLAG_NEED_RTNL,
8904 },
8905 {
8906 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
8907 .doit = nl80211_stop_p2p_device,
8908 .policy = nl80211_policy,
8909 .flags = GENL_ADMIN_PERM,
8910 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8911 NL80211_FLAG_NEED_RTNL,
8912 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01008913 {
8914 .cmd = NL80211_CMD_SET_MCAST_RATE,
8915 .doit = nl80211_set_mcast_rate,
8916 .policy = nl80211_policy,
8917 .flags = GENL_ADMIN_PERM,
8918 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8919 NL80211_FLAG_NEED_RTNL,
8920 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05308921 {
8922 .cmd = NL80211_CMD_SET_MAC_ACL,
8923 .doit = nl80211_set_mac_acl,
8924 .policy = nl80211_policy,
8925 .flags = GENL_ADMIN_PERM,
8926 .internal_flags = NL80211_FLAG_NEED_NETDEV |
8927 NL80211_FLAG_NEED_RTNL,
8928 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01008929 {
8930 .cmd = NL80211_CMD_RADAR_DETECT,
8931 .doit = nl80211_start_radar_detection,
8932 .policy = nl80211_policy,
8933 .flags = GENL_ADMIN_PERM,
8934 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8935 NL80211_FLAG_NEED_RTNL,
8936 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01008937 {
8938 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
8939 .doit = nl80211_get_protocol_features,
8940 .policy = nl80211_policy,
8941 },
Jouni Malinen355199e2013-02-27 17:14:27 +02008942 {
8943 .cmd = NL80211_CMD_UPDATE_FT_IES,
8944 .doit = nl80211_update_ft_ies,
8945 .policy = nl80211_policy,
8946 .flags = GENL_ADMIN_PERM,
8947 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
8948 NL80211_FLAG_NEED_RTNL,
8949 },
Arend van Spriel5de17982013-04-18 15:49:00 +02008950 {
8951 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
8952 .doit = nl80211_crit_protocol_start,
8953 .policy = nl80211_policy,
8954 .flags = GENL_ADMIN_PERM,
8955 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8956 NL80211_FLAG_NEED_RTNL,
8957 },
8958 {
8959 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
8960 .doit = nl80211_crit_protocol_stop,
8961 .policy = nl80211_policy,
8962 .flags = GENL_ADMIN_PERM,
8963 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
8964 NL80211_FLAG_NEED_RTNL,
8965 }
Johannes Berg55682962007-09-20 13:09:35 -04008966};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008967
Jouni Malinen6039f6d2009-03-19 13:39:21 +02008968static struct genl_multicast_group nl80211_mlme_mcgrp = {
8969 .name = "mlme",
8970};
Johannes Berg55682962007-09-20 13:09:35 -04008971
8972/* multicast groups */
8973static struct genl_multicast_group nl80211_config_mcgrp = {
8974 .name = "config",
8975};
Johannes Berg2a519312009-02-10 21:25:55 +01008976static struct genl_multicast_group nl80211_scan_mcgrp = {
8977 .name = "scan",
8978};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008979static struct genl_multicast_group nl80211_regulatory_mcgrp = {
8980 .name = "regulatory",
8981};
Johannes Berg55682962007-09-20 13:09:35 -04008982
8983/* notification functions */
8984
8985void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
8986{
8987 struct sk_buff *msg;
8988
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008989 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04008990 if (!msg)
8991 return;
8992
Johannes Berg3713b4e2013-02-14 16:19:38 +01008993 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0,
8994 false, NULL, NULL, NULL) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04008995 nlmsg_free(msg);
8996 return;
8997 }
8998
Johannes Berg463d0182009-07-14 00:33:35 +02008999 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9000 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009001}
9002
Johannes Berg362a4152009-05-24 16:43:15 +02009003static int nl80211_add_scan_req(struct sk_buff *msg,
9004 struct cfg80211_registered_device *rdev)
9005{
9006 struct cfg80211_scan_request *req = rdev->scan_req;
9007 struct nlattr *nest;
9008 int i;
9009
9010 if (WARN_ON(!req))
9011 return 0;
9012
9013 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9014 if (!nest)
9015 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009016 for (i = 0; i < req->n_ssids; i++) {
9017 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9018 goto nla_put_failure;
9019 }
Johannes Berg362a4152009-05-24 16:43:15 +02009020 nla_nest_end(msg, nest);
9021
9022 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9023 if (!nest)
9024 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009025 for (i = 0; i < req->n_channels; i++) {
9026 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9027 goto nla_put_failure;
9028 }
Johannes Berg362a4152009-05-24 16:43:15 +02009029 nla_nest_end(msg, nest);
9030
David S. Miller9360ffd2012-03-29 04:41:26 -04009031 if (req->ie &&
9032 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9033 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02009034
Sam Lefflered4737712012-10-11 21:03:31 -07009035 if (req->flags)
9036 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
9037
Johannes Berg362a4152009-05-24 16:43:15 +02009038 return 0;
9039 nla_put_failure:
9040 return -ENOBUFS;
9041}
9042
Johannes Berga538e2d2009-06-16 19:56:42 +02009043static int nl80211_send_scan_msg(struct sk_buff *msg,
9044 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009045 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009046 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02009047 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01009048{
9049 void *hdr;
9050
Eric W. Biederman15e47302012-09-07 20:12:54 +00009051 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01009052 if (!hdr)
9053 return -1;
9054
David S. Miller9360ffd2012-03-29 04:41:26 -04009055 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009056 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9057 wdev->netdev->ifindex)) ||
9058 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009059 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009060
Johannes Berg362a4152009-05-24 16:43:15 +02009061 /* ignore errors and send incomplete event anyway */
9062 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009063
9064 return genlmsg_end(msg, hdr);
9065
9066 nla_put_failure:
9067 genlmsg_cancel(msg, hdr);
9068 return -EMSGSIZE;
9069}
9070
Luciano Coelho807f8a82011-05-11 17:09:35 +03009071static int
9072nl80211_send_sched_scan_msg(struct sk_buff *msg,
9073 struct cfg80211_registered_device *rdev,
9074 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009075 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009076{
9077 void *hdr;
9078
Eric W. Biederman15e47302012-09-07 20:12:54 +00009079 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009080 if (!hdr)
9081 return -1;
9082
David S. Miller9360ffd2012-03-29 04:41:26 -04009083 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9084 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9085 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009086
9087 return genlmsg_end(msg, hdr);
9088
9089 nla_put_failure:
9090 genlmsg_cancel(msg, hdr);
9091 return -EMSGSIZE;
9092}
9093
Johannes Berga538e2d2009-06-16 19:56:42 +02009094void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009095 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009096{
9097 struct sk_buff *msg;
9098
Thomas Graf58050fc2012-06-28 03:57:45 +00009099 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009100 if (!msg)
9101 return;
9102
Johannes Bergfd014282012-06-18 19:17:03 +02009103 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009104 NL80211_CMD_TRIGGER_SCAN) < 0) {
9105 nlmsg_free(msg);
9106 return;
9107 }
9108
Johannes Berg463d0182009-07-14 00:33:35 +02009109 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9110 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009111}
9112
Johannes Berg2a519312009-02-10 21:25:55 +01009113void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009114 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009115{
9116 struct sk_buff *msg;
9117
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009118 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009119 if (!msg)
9120 return;
9121
Johannes Bergfd014282012-06-18 19:17:03 +02009122 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009123 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009124 nlmsg_free(msg);
9125 return;
9126 }
9127
Johannes Berg463d0182009-07-14 00:33:35 +02009128 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9129 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009130}
9131
9132void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009133 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009134{
9135 struct sk_buff *msg;
9136
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009137 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009138 if (!msg)
9139 return;
9140
Johannes Bergfd014282012-06-18 19:17:03 +02009141 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009142 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009143 nlmsg_free(msg);
9144 return;
9145 }
9146
Johannes Berg463d0182009-07-14 00:33:35 +02009147 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9148 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009149}
9150
Luciano Coelho807f8a82011-05-11 17:09:35 +03009151void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9152 struct net_device *netdev)
9153{
9154 struct sk_buff *msg;
9155
9156 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9157 if (!msg)
9158 return;
9159
9160 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9161 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9162 nlmsg_free(msg);
9163 return;
9164 }
9165
9166 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9167 nl80211_scan_mcgrp.id, GFP_KERNEL);
9168}
9169
9170void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9171 struct net_device *netdev, u32 cmd)
9172{
9173 struct sk_buff *msg;
9174
Thomas Graf58050fc2012-06-28 03:57:45 +00009175 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009176 if (!msg)
9177 return;
9178
9179 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9180 nlmsg_free(msg);
9181 return;
9182 }
9183
9184 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9185 nl80211_scan_mcgrp.id, GFP_KERNEL);
9186}
9187
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009188/*
9189 * This can happen on global regulatory changes or device specific settings
9190 * based on custom world regulatory domains.
9191 */
9192void nl80211_send_reg_change_event(struct regulatory_request *request)
9193{
9194 struct sk_buff *msg;
9195 void *hdr;
9196
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009197 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009198 if (!msg)
9199 return;
9200
9201 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9202 if (!hdr) {
9203 nlmsg_free(msg);
9204 return;
9205 }
9206
9207 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04009208 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9209 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009210
David S. Miller9360ffd2012-03-29 04:41:26 -04009211 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9212 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9213 NL80211_REGDOM_TYPE_WORLD))
9214 goto nla_put_failure;
9215 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9216 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9217 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9218 goto nla_put_failure;
9219 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9220 request->intersect) {
9221 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9222 NL80211_REGDOM_TYPE_INTERSECTION))
9223 goto nla_put_failure;
9224 } else {
9225 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9226 NL80211_REGDOM_TYPE_COUNTRY) ||
9227 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9228 request->alpha2))
9229 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009230 }
9231
Johannes Bergf4173762012-12-03 18:23:37 +01009232 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04009233 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9234 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009235
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009236 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009237
Johannes Bergbc43b282009-07-25 10:54:13 +02009238 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009239 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009240 GFP_ATOMIC);
9241 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009242
9243 return;
9244
9245nla_put_failure:
9246 genlmsg_cancel(msg, hdr);
9247 nlmsg_free(msg);
9248}
9249
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009250static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9251 struct net_device *netdev,
9252 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009253 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009254{
9255 struct sk_buff *msg;
9256 void *hdr;
9257
Johannes Berge6d6e342009-07-01 21:26:47 +02009258 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009259 if (!msg)
9260 return;
9261
9262 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9263 if (!hdr) {
9264 nlmsg_free(msg);
9265 return;
9266 }
9267
David S. Miller9360ffd2012-03-29 04:41:26 -04009268 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9269 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9270 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9271 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009272
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009273 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009274
Johannes Berg463d0182009-07-14 00:33:35 +02009275 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9276 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009277 return;
9278
9279 nla_put_failure:
9280 genlmsg_cancel(msg, hdr);
9281 nlmsg_free(msg);
9282}
9283
9284void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009285 struct net_device *netdev, const u8 *buf,
9286 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009287{
9288 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009289 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009290}
9291
9292void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
9293 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009294 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009295{
Johannes Berge6d6e342009-07-01 21:26:47 +02009296 nl80211_send_mlme_event(rdev, netdev, buf, len,
9297 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009298}
9299
Jouni Malinen53b46b82009-03-27 20:53:56 +02009300void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009301 struct net_device *netdev, const u8 *buf,
9302 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009303{
9304 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009305 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009306}
9307
Jouni Malinen53b46b82009-03-27 20:53:56 +02009308void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
9309 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02009310 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009311{
9312 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009313 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009314}
9315
Johannes Berg6ff57cf2013-05-16 00:55:00 +02009316void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
9317 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +02009318{
Johannes Berg947add32013-02-22 22:05:20 +01009319 struct wireless_dev *wdev = dev->ieee80211_ptr;
9320 struct wiphy *wiphy = wdev->wiphy;
9321 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +02009322 const struct ieee80211_mgmt *mgmt = (void *)buf;
9323 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +02009324
Johannes Berg6ff57cf2013-05-16 00:55:00 +02009325 if (WARN_ON(len < 2))
9326 return;
9327
9328 if (ieee80211_is_deauth(mgmt->frame_control))
9329 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
9330 else
9331 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
9332
9333 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
9334 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009335}
Johannes Berg6ff57cf2013-05-16 00:55:00 +02009336EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +02009337
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04009338static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9339 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02009340 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009341{
9342 struct sk_buff *msg;
9343 void *hdr;
9344
Johannes Berge6d6e342009-07-01 21:26:47 +02009345 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009346 if (!msg)
9347 return;
9348
9349 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9350 if (!hdr) {
9351 nlmsg_free(msg);
9352 return;
9353 }
9354
David S. Miller9360ffd2012-03-29 04:41:26 -04009355 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9356 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9357 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
9358 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9359 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03009360
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009361 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03009362
Johannes Berg463d0182009-07-14 00:33:35 +02009363 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9364 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009365 return;
9366
9367 nla_put_failure:
9368 genlmsg_cancel(msg, hdr);
9369 nlmsg_free(msg);
9370}
9371
9372void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009373 struct net_device *netdev, const u8 *addr,
9374 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009375{
9376 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02009377 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009378}
9379
9380void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02009381 struct net_device *netdev, const u8 *addr,
9382 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03009383{
Johannes Berge6d6e342009-07-01 21:26:47 +02009384 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
9385 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03009386}
9387
Samuel Ortizb23aa672009-07-01 21:26:54 +02009388void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9389 struct net_device *netdev, const u8 *bssid,
9390 const u8 *req_ie, size_t req_ie_len,
9391 const u8 *resp_ie, size_t resp_ie_len,
9392 u16 status, gfp_t gfp)
9393{
9394 struct sk_buff *msg;
9395 void *hdr;
9396
Thomas Graf58050fc2012-06-28 03:57:45 +00009397 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009398 if (!msg)
9399 return;
9400
9401 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
9402 if (!hdr) {
9403 nlmsg_free(msg);
9404 return;
9405 }
9406
David S. Miller9360ffd2012-03-29 04:41:26 -04009407 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9408 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9409 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
9410 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
9411 (req_ie &&
9412 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9413 (resp_ie &&
9414 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9415 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009416
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009417 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009418
Johannes Berg463d0182009-07-14 00:33:35 +02009419 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9420 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009421 return;
9422
9423 nla_put_failure:
9424 genlmsg_cancel(msg, hdr);
9425 nlmsg_free(msg);
9426
9427}
9428
9429void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9430 struct net_device *netdev, const u8 *bssid,
9431 const u8 *req_ie, size_t req_ie_len,
9432 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
9433{
9434 struct sk_buff *msg;
9435 void *hdr;
9436
Thomas Graf58050fc2012-06-28 03:57:45 +00009437 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009438 if (!msg)
9439 return;
9440
9441 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
9442 if (!hdr) {
9443 nlmsg_free(msg);
9444 return;
9445 }
9446
David S. Miller9360ffd2012-03-29 04:41:26 -04009447 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9448 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9449 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
9450 (req_ie &&
9451 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
9452 (resp_ie &&
9453 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
9454 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009455
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009456 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009457
Johannes Berg463d0182009-07-14 00:33:35 +02009458 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9459 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009460 return;
9461
9462 nla_put_failure:
9463 genlmsg_cancel(msg, hdr);
9464 nlmsg_free(msg);
9465
9466}
9467
9468void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
9469 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02009470 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02009471{
9472 struct sk_buff *msg;
9473 void *hdr;
9474
Thomas Graf58050fc2012-06-28 03:57:45 +00009475 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009476 if (!msg)
9477 return;
9478
9479 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
9480 if (!hdr) {
9481 nlmsg_free(msg);
9482 return;
9483 }
9484
David S. Miller9360ffd2012-03-29 04:41:26 -04009485 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9486 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9487 (from_ap && reason &&
9488 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
9489 (from_ap &&
9490 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
9491 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
9492 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02009493
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009494 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009495
Johannes Berg463d0182009-07-14 00:33:35 +02009496 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9497 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02009498 return;
9499
9500 nla_put_failure:
9501 genlmsg_cancel(msg, hdr);
9502 nlmsg_free(msg);
9503
9504}
9505
Johannes Berg04a773a2009-04-19 21:24:32 +02009506void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
9507 struct net_device *netdev, const u8 *bssid,
9508 gfp_t gfp)
9509{
9510 struct sk_buff *msg;
9511 void *hdr;
9512
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009513 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009514 if (!msg)
9515 return;
9516
9517 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
9518 if (!hdr) {
9519 nlmsg_free(msg);
9520 return;
9521 }
9522
David S. Miller9360ffd2012-03-29 04:41:26 -04009523 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9524 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9525 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
9526 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02009527
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009528 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02009529
Johannes Berg463d0182009-07-14 00:33:35 +02009530 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9531 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02009532 return;
9533
9534 nla_put_failure:
9535 genlmsg_cancel(msg, hdr);
9536 nlmsg_free(msg);
9537}
9538
Johannes Berg947add32013-02-22 22:05:20 +01009539void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
9540 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -07009541{
Johannes Berg947add32013-02-22 22:05:20 +01009542 struct wireless_dev *wdev = dev->ieee80211_ptr;
9543 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009544 struct sk_buff *msg;
9545 void *hdr;
9546
Johannes Berg947add32013-02-22 22:05:20 +01009547 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
9548 return;
9549
9550 trace_cfg80211_notify_new_peer_candidate(dev, addr);
9551
Javier Cardonac93b5e72011-04-07 15:08:34 -07009552 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9553 if (!msg)
9554 return;
9555
9556 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
9557 if (!hdr) {
9558 nlmsg_free(msg);
9559 return;
9560 }
9561
David S. Miller9360ffd2012-03-29 04:41:26 -04009562 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +01009563 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9564 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009565 (ie_len && ie &&
9566 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
9567 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07009568
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009569 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009570
9571 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9572 nl80211_mlme_mcgrp.id, gfp);
9573 return;
9574
9575 nla_put_failure:
9576 genlmsg_cancel(msg, hdr);
9577 nlmsg_free(msg);
9578}
Johannes Berg947add32013-02-22 22:05:20 +01009579EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -07009580
Jouni Malinena3b8b052009-03-27 21:59:49 +02009581void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
9582 struct net_device *netdev, const u8 *addr,
9583 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02009584 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02009585{
9586 struct sk_buff *msg;
9587 void *hdr;
9588
Johannes Berge6d6e342009-07-01 21:26:47 +02009589 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009590 if (!msg)
9591 return;
9592
9593 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
9594 if (!hdr) {
9595 nlmsg_free(msg);
9596 return;
9597 }
9598
David S. Miller9360ffd2012-03-29 04:41:26 -04009599 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9600 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9601 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
9602 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
9603 (key_id != -1 &&
9604 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
9605 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
9606 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02009607
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009608 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009609
Johannes Berg463d0182009-07-14 00:33:35 +02009610 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9611 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02009612 return;
9613
9614 nla_put_failure:
9615 genlmsg_cancel(msg, hdr);
9616 nlmsg_free(msg);
9617}
9618
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009619void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
9620 struct ieee80211_channel *channel_before,
9621 struct ieee80211_channel *channel_after)
9622{
9623 struct sk_buff *msg;
9624 void *hdr;
9625 struct nlattr *nl_freq;
9626
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009627 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009628 if (!msg)
9629 return;
9630
9631 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
9632 if (!hdr) {
9633 nlmsg_free(msg);
9634 return;
9635 }
9636
9637 /*
9638 * Since we are applying the beacon hint to a wiphy we know its
9639 * wiphy_idx is valid
9640 */
David S. Miller9360ffd2012-03-29 04:41:26 -04009641 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
9642 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009643
9644 /* Before */
9645 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
9646 if (!nl_freq)
9647 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009648 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009649 goto nla_put_failure;
9650 nla_nest_end(msg, nl_freq);
9651
9652 /* After */
9653 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
9654 if (!nl_freq)
9655 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +01009656 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009657 goto nla_put_failure;
9658 nla_nest_end(msg, nl_freq);
9659
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009660 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009661
Johannes Berg463d0182009-07-14 00:33:35 +02009662 rcu_read_lock();
9663 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
9664 GFP_ATOMIC);
9665 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04009666
9667 return;
9668
9669nla_put_failure:
9670 genlmsg_cancel(msg, hdr);
9671 nlmsg_free(msg);
9672}
9673
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009674static void nl80211_send_remain_on_chan_event(
9675 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02009676 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009677 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009678 unsigned int duration, gfp_t gfp)
9679{
9680 struct sk_buff *msg;
9681 void *hdr;
9682
9683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9684 if (!msg)
9685 return;
9686
9687 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9688 if (!hdr) {
9689 nlmsg_free(msg);
9690 return;
9691 }
9692
David S. Miller9360ffd2012-03-29 04:41:26 -04009693 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009694 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9695 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02009696 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009697 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +01009698 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
9699 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009700 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
9701 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009702
David S. Miller9360ffd2012-03-29 04:41:26 -04009703 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
9704 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
9705 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009706
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009707 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009708
9709 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9710 nl80211_mlme_mcgrp.id, gfp);
9711 return;
9712
9713 nla_put_failure:
9714 genlmsg_cancel(msg, hdr);
9715 nlmsg_free(msg);
9716}
9717
Johannes Berg947add32013-02-22 22:05:20 +01009718void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
9719 struct ieee80211_channel *chan,
9720 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009721{
Johannes Berg947add32013-02-22 22:05:20 +01009722 struct wiphy *wiphy = wdev->wiphy;
9723 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9724
9725 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009726 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02009727 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +01009728 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009729}
Johannes Berg947add32013-02-22 22:05:20 +01009730EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009731
Johannes Berg947add32013-02-22 22:05:20 +01009732void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
9733 struct ieee80211_channel *chan,
9734 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009735{
Johannes Berg947add32013-02-22 22:05:20 +01009736 struct wiphy *wiphy = wdev->wiphy;
9737 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
9738
9739 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009740 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +01009741 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009742}
Johannes Berg947add32013-02-22 22:05:20 +01009743EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009744
Johannes Berg947add32013-02-22 22:05:20 +01009745void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
9746 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +01009747{
Johannes Berg947add32013-02-22 22:05:20 +01009748 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9749 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +01009750 struct sk_buff *msg;
9751
Johannes Berg947add32013-02-22 22:05:20 +01009752 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
9753
Thomas Graf58050fc2012-06-28 03:57:45 +00009754 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01009755 if (!msg)
9756 return;
9757
John W. Linville66266b32012-03-15 13:25:41 -04009758 if (nl80211_send_station(msg, 0, 0, 0,
9759 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01009760 nlmsg_free(msg);
9761 return;
9762 }
9763
9764 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9765 nl80211_mlme_mcgrp.id, gfp);
9766}
Johannes Berg947add32013-02-22 22:05:20 +01009767EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +01009768
Johannes Berg947add32013-02-22 22:05:20 +01009769void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +02009770{
Johannes Berg947add32013-02-22 22:05:20 +01009771 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9772 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +02009773 struct sk_buff *msg;
9774 void *hdr;
9775
Johannes Berg947add32013-02-22 22:05:20 +01009776 trace_cfg80211_del_sta(dev, mac_addr);
9777
Thomas Graf58050fc2012-06-28 03:57:45 +00009778 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02009779 if (!msg)
9780 return;
9781
9782 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
9783 if (!hdr) {
9784 nlmsg_free(msg);
9785 return;
9786 }
9787
David S. Miller9360ffd2012-03-29 04:41:26 -04009788 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9789 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
9790 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02009791
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009792 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02009793
9794 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9795 nl80211_mlme_mcgrp.id, gfp);
9796 return;
9797
9798 nla_put_failure:
9799 genlmsg_cancel(msg, hdr);
9800 nlmsg_free(msg);
9801}
Johannes Berg947add32013-02-22 22:05:20 +01009802EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +02009803
Johannes Berg947add32013-02-22 22:05:20 +01009804void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
9805 enum nl80211_connect_failed_reason reason,
9806 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309807{
Johannes Berg947add32013-02-22 22:05:20 +01009808 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
9809 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309810 struct sk_buff *msg;
9811 void *hdr;
9812
9813 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
9814 if (!msg)
9815 return;
9816
9817 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
9818 if (!hdr) {
9819 nlmsg_free(msg);
9820 return;
9821 }
9822
9823 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9824 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
9825 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
9826 goto nla_put_failure;
9827
9828 genlmsg_end(msg, hdr);
9829
9830 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9831 nl80211_mlme_mcgrp.id, gfp);
9832 return;
9833
9834 nla_put_failure:
9835 genlmsg_cancel(msg, hdr);
9836 nlmsg_free(msg);
9837}
Johannes Berg947add32013-02-22 22:05:20 +01009838EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05309839
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009840static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
9841 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01009842{
9843 struct wireless_dev *wdev = dev->ieee80211_ptr;
9844 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
9845 struct sk_buff *msg;
9846 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +00009847 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009848
Eric W. Biederman15e47302012-09-07 20:12:54 +00009849 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01009850 return false;
9851
9852 msg = nlmsg_new(100, gfp);
9853 if (!msg)
9854 return true;
9855
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009856 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01009857 if (!hdr) {
9858 nlmsg_free(msg);
9859 return true;
9860 }
9861
David S. Miller9360ffd2012-03-29 04:41:26 -04009862 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9863 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
9864 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
9865 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01009866
Johannes Berg9c90a9f2013-06-04 12:46:03 +02009867 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +00009868 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01009869 return true;
9870
9871 nla_put_failure:
9872 genlmsg_cancel(msg, hdr);
9873 nlmsg_free(msg);
9874 return true;
9875}
9876
Johannes Berg947add32013-02-22 22:05:20 +01009877bool cfg80211_rx_spurious_frame(struct net_device *dev,
9878 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009879{
Johannes Berg947add32013-02-22 22:05:20 +01009880 struct wireless_dev *wdev = dev->ieee80211_ptr;
9881 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009882
Johannes Berg947add32013-02-22 22:05:20 +01009883 trace_cfg80211_rx_spurious_frame(dev, addr);
9884
9885 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9886 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
9887 trace_cfg80211_return_bool(false);
9888 return false;
9889 }
9890 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
9891 addr, gfp);
9892 trace_cfg80211_return_bool(ret);
9893 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009894}
Johannes Berg947add32013-02-22 22:05:20 +01009895EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
9896
9897bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
9898 const u8 *addr, gfp_t gfp)
9899{
9900 struct wireless_dev *wdev = dev->ieee80211_ptr;
9901 bool ret;
9902
9903 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
9904
9905 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
9906 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
9907 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
9908 trace_cfg80211_return_bool(false);
9909 return false;
9910 }
9911 ret = __nl80211_unexpected_frame(dev,
9912 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
9913 addr, gfp);
9914 trace_cfg80211_return_bool(ret);
9915 return ret;
9916}
9917EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +01009918
Johannes Berg2e161f72010-08-12 15:38:38 +02009919int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009920 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01009921 int freq, int sig_dbm,
9922 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009923{
Johannes Berg71bbc992012-06-15 15:30:18 +02009924 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009925 struct sk_buff *msg;
9926 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02009927
9928 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9929 if (!msg)
9930 return -ENOMEM;
9931
Johannes Berg2e161f72010-08-12 15:38:38 +02009932 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02009933 if (!hdr) {
9934 nlmsg_free(msg);
9935 return -ENOMEM;
9936 }
9937
David S. Miller9360ffd2012-03-29 04:41:26 -04009938 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009939 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9940 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +03009941 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009942 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
9943 (sig_dbm &&
9944 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
9945 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
9946 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009947
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009948 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009949
Eric W. Biederman15e47302012-09-07 20:12:54 +00009950 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02009951
9952 nla_put_failure:
9953 genlmsg_cancel(msg, hdr);
9954 nlmsg_free(msg);
9955 return -ENOBUFS;
9956}
9957
Johannes Berg947add32013-02-22 22:05:20 +01009958void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
9959 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02009960{
Johannes Berg947add32013-02-22 22:05:20 +01009961 struct wiphy *wiphy = wdev->wiphy;
9962 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +02009963 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02009964 struct sk_buff *msg;
9965 void *hdr;
9966
Johannes Berg947add32013-02-22 22:05:20 +01009967 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
9968
Jouni Malinen026331c2010-02-15 12:53:10 +02009969 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
9970 if (!msg)
9971 return;
9972
Johannes Berg2e161f72010-08-12 15:38:38 +02009973 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02009974 if (!hdr) {
9975 nlmsg_free(msg);
9976 return;
9977 }
9978
David S. Miller9360ffd2012-03-29 04:41:26 -04009979 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02009980 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9981 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +03009982 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04009983 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
9984 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
9985 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
9986 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02009987
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009988 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02009989
9990 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
9991 return;
9992
9993 nla_put_failure:
9994 genlmsg_cancel(msg, hdr);
9995 nlmsg_free(msg);
9996}
Johannes Berg947add32013-02-22 22:05:20 +01009997EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +02009998
Johannes Berg947add32013-02-22 22:05:20 +01009999void cfg80211_cqm_rssi_notify(struct net_device *dev,
10000 enum nl80211_cqm_rssi_threshold_event rssi_event,
10001 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010002{
Johannes Berg947add32013-02-22 22:05:20 +010010003 struct wireless_dev *wdev = dev->ieee80211_ptr;
10004 struct wiphy *wiphy = wdev->wiphy;
10005 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010006 struct sk_buff *msg;
10007 struct nlattr *pinfoattr;
10008 void *hdr;
10009
Johannes Berg947add32013-02-22 22:05:20 +010010010 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10011
Thomas Graf58050fc2012-06-28 03:57:45 +000010012 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010013 if (!msg)
10014 return;
10015
10016 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10017 if (!hdr) {
10018 nlmsg_free(msg);
10019 return;
10020 }
10021
David S. Miller9360ffd2012-03-29 04:41:26 -040010022 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010023 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040010024 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010025
10026 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10027 if (!pinfoattr)
10028 goto nla_put_failure;
10029
David S. Miller9360ffd2012-03-29 04:41:26 -040010030 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10031 rssi_event))
10032 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010033
10034 nla_nest_end(msg, pinfoattr);
10035
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010036 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010037
10038 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10039 nl80211_mlme_mcgrp.id, gfp);
10040 return;
10041
10042 nla_put_failure:
10043 genlmsg_cancel(msg, hdr);
10044 nlmsg_free(msg);
10045}
Johannes Berg947add32013-02-22 22:05:20 +010010046EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010047
Johannes Berg947add32013-02-22 22:05:20 +010010048static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10049 struct net_device *netdev, const u8 *bssid,
10050 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010051{
10052 struct sk_buff *msg;
10053 struct nlattr *rekey_attr;
10054 void *hdr;
10055
Thomas Graf58050fc2012-06-28 03:57:45 +000010056 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010057 if (!msg)
10058 return;
10059
10060 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10061 if (!hdr) {
10062 nlmsg_free(msg);
10063 return;
10064 }
10065
David S. Miller9360ffd2012-03-29 04:41:26 -040010066 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10067 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10068 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10069 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010070
10071 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10072 if (!rekey_attr)
10073 goto nla_put_failure;
10074
David S. Miller9360ffd2012-03-29 04:41:26 -040010075 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10076 NL80211_REPLAY_CTR_LEN, replay_ctr))
10077 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010078
10079 nla_nest_end(msg, rekey_attr);
10080
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010081 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010082
10083 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10084 nl80211_mlme_mcgrp.id, gfp);
10085 return;
10086
10087 nla_put_failure:
10088 genlmsg_cancel(msg, hdr);
10089 nlmsg_free(msg);
10090}
10091
Johannes Berg947add32013-02-22 22:05:20 +010010092void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10093 const u8 *replay_ctr, gfp_t gfp)
10094{
10095 struct wireless_dev *wdev = dev->ieee80211_ptr;
10096 struct wiphy *wiphy = wdev->wiphy;
10097 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10098
10099 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10100 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10101}
10102EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10103
10104static void
10105nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10106 struct net_device *netdev, int index,
10107 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010108{
10109 struct sk_buff *msg;
10110 struct nlattr *attr;
10111 void *hdr;
10112
Thomas Graf58050fc2012-06-28 03:57:45 +000010113 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010114 if (!msg)
10115 return;
10116
10117 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10118 if (!hdr) {
10119 nlmsg_free(msg);
10120 return;
10121 }
10122
David S. Miller9360ffd2012-03-29 04:41:26 -040010123 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10124 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10125 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010126
10127 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10128 if (!attr)
10129 goto nla_put_failure;
10130
David S. Miller9360ffd2012-03-29 04:41:26 -040010131 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10132 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10133 (preauth &&
10134 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10135 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010136
10137 nla_nest_end(msg, attr);
10138
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010139 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010140
10141 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10142 nl80211_mlme_mcgrp.id, gfp);
10143 return;
10144
10145 nla_put_failure:
10146 genlmsg_cancel(msg, hdr);
10147 nlmsg_free(msg);
10148}
10149
Johannes Berg947add32013-02-22 22:05:20 +010010150void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10151 const u8 *bssid, bool preauth, gfp_t gfp)
10152{
10153 struct wireless_dev *wdev = dev->ieee80211_ptr;
10154 struct wiphy *wiphy = wdev->wiphy;
10155 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10156
10157 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10158 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10159}
10160EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10161
10162static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10163 struct net_device *netdev,
10164 struct cfg80211_chan_def *chandef,
10165 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070010166{
10167 struct sk_buff *msg;
10168 void *hdr;
10169
Thomas Graf58050fc2012-06-28 03:57:45 +000010170 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070010171 if (!msg)
10172 return;
10173
10174 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10175 if (!hdr) {
10176 nlmsg_free(msg);
10177 return;
10178 }
10179
Johannes Berg683b6d32012-11-08 21:25:48 +010010180 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10181 goto nla_put_failure;
10182
10183 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040010184 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070010185
10186 genlmsg_end(msg, hdr);
10187
10188 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10189 nl80211_mlme_mcgrp.id, gfp);
10190 return;
10191
10192 nla_put_failure:
10193 genlmsg_cancel(msg, hdr);
10194 nlmsg_free(msg);
10195}
10196
Johannes Berg947add32013-02-22 22:05:20 +010010197void cfg80211_ch_switch_notify(struct net_device *dev,
10198 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070010199{
Johannes Berg947add32013-02-22 22:05:20 +010010200 struct wireless_dev *wdev = dev->ieee80211_ptr;
10201 struct wiphy *wiphy = wdev->wiphy;
10202 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10203
10204 trace_cfg80211_ch_switch_notify(dev, chandef);
10205
10206 wdev_lock(wdev);
10207
10208 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10209 wdev->iftype != NL80211_IFTYPE_P2P_GO))
10210 goto out;
10211
10212 wdev->channel = chandef->chan;
10213 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
10214out:
10215 wdev_unlock(wdev);
10216 return;
10217}
10218EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10219
10220void cfg80211_cqm_txe_notify(struct net_device *dev,
10221 const u8 *peer, u32 num_packets,
10222 u32 rate, u32 intvl, gfp_t gfp)
10223{
10224 struct wireless_dev *wdev = dev->ieee80211_ptr;
10225 struct wiphy *wiphy = wdev->wiphy;
10226 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010227 struct sk_buff *msg;
10228 struct nlattr *pinfoattr;
10229 void *hdr;
10230
10231 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10232 if (!msg)
10233 return;
10234
10235 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10236 if (!hdr) {
10237 nlmsg_free(msg);
10238 return;
10239 }
10240
10241 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010242 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070010243 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10244 goto nla_put_failure;
10245
10246 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10247 if (!pinfoattr)
10248 goto nla_put_failure;
10249
10250 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10251 goto nla_put_failure;
10252
10253 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10254 goto nla_put_failure;
10255
10256 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10257 goto nla_put_failure;
10258
10259 nla_nest_end(msg, pinfoattr);
10260
10261 genlmsg_end(msg, hdr);
10262
10263 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10264 nl80211_mlme_mcgrp.id, gfp);
10265 return;
10266
10267 nla_put_failure:
10268 genlmsg_cancel(msg, hdr);
10269 nlmsg_free(msg);
10270}
Johannes Berg947add32013-02-22 22:05:20 +010010271EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010272
10273void
Simon Wunderlich04f39042013-02-08 18:16:19 +010010274nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10275 struct cfg80211_chan_def *chandef,
10276 enum nl80211_radar_event event,
10277 struct net_device *netdev, gfp_t gfp)
10278{
10279 struct sk_buff *msg;
10280 void *hdr;
10281
10282 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10283 if (!msg)
10284 return;
10285
10286 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
10287 if (!hdr) {
10288 nlmsg_free(msg);
10289 return;
10290 }
10291
10292 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
10293 goto nla_put_failure;
10294
10295 /* NOP and radar events don't need a netdev parameter */
10296 if (netdev) {
10297 struct wireless_dev *wdev = netdev->ieee80211_ptr;
10298
10299 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10300 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10301 goto nla_put_failure;
10302 }
10303
10304 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
10305 goto nla_put_failure;
10306
10307 if (nl80211_send_chandef(msg, chandef))
10308 goto nla_put_failure;
10309
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010310 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010010311
10312 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10313 nl80211_mlme_mcgrp.id, gfp);
10314 return;
10315
10316 nla_put_failure:
10317 genlmsg_cancel(msg, hdr);
10318 nlmsg_free(msg);
10319}
10320
Johannes Berg947add32013-02-22 22:05:20 +010010321void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10322 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010010323{
Johannes Berg947add32013-02-22 22:05:20 +010010324 struct wireless_dev *wdev = dev->ieee80211_ptr;
10325 struct wiphy *wiphy = wdev->wiphy;
10326 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010327 struct sk_buff *msg;
10328 struct nlattr *pinfoattr;
10329 void *hdr;
10330
Johannes Berg947add32013-02-22 22:05:20 +010010331 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
10332
Thomas Graf58050fc2012-06-28 03:57:45 +000010333 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010334 if (!msg)
10335 return;
10336
10337 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10338 if (!hdr) {
10339 nlmsg_free(msg);
10340 return;
10341 }
10342
David S. Miller9360ffd2012-03-29 04:41:26 -040010343 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010344 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010345 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10346 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010347
10348 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10349 if (!pinfoattr)
10350 goto nla_put_failure;
10351
David S. Miller9360ffd2012-03-29 04:41:26 -040010352 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
10353 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010010354
10355 nla_nest_end(msg, pinfoattr);
10356
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010357 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010358
10359 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10360 nl80211_mlme_mcgrp.id, gfp);
10361 return;
10362
10363 nla_put_failure:
10364 genlmsg_cancel(msg, hdr);
10365 nlmsg_free(msg);
10366}
Johannes Berg947add32013-02-22 22:05:20 +010010367EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010010368
Johannes Berg7f6cf312011-11-04 11:18:15 +010010369void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10370 u64 cookie, bool acked, gfp_t gfp)
10371{
10372 struct wireless_dev *wdev = dev->ieee80211_ptr;
10373 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10374 struct sk_buff *msg;
10375 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010376
Beni Lev4ee3e062012-08-27 12:49:39 +030010377 trace_cfg80211_probe_status(dev, addr, cookie, acked);
10378
Thomas Graf58050fc2012-06-28 03:57:45 +000010379 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030010380
Johannes Berg7f6cf312011-11-04 11:18:15 +010010381 if (!msg)
10382 return;
10383
10384 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
10385 if (!hdr) {
10386 nlmsg_free(msg);
10387 return;
10388 }
10389
David S. Miller9360ffd2012-03-29 04:41:26 -040010390 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10391 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10392 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
10393 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10394 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
10395 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010010396
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010397 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010010398
10399 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10400 nl80211_mlme_mcgrp.id, gfp);
10401 return;
10402
10403 nla_put_failure:
10404 genlmsg_cancel(msg, hdr);
10405 nlmsg_free(msg);
10406}
10407EXPORT_SYMBOL(cfg80211_probe_status);
10408
Johannes Berg5e760232011-11-04 11:18:17 +010010409void cfg80211_report_obss_beacon(struct wiphy *wiphy,
10410 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070010411 int freq, int sig_dbm)
Johannes Berg5e760232011-11-04 11:18:17 +010010412{
10413 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10414 struct sk_buff *msg;
10415 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070010416 struct cfg80211_beacon_registration *reg;
Johannes Berg5e760232011-11-04 11:18:17 +010010417
Beni Lev4ee3e062012-08-27 12:49:39 +030010418 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
10419
Ben Greear37c73b52012-10-26 14:49:25 -070010420 spin_lock_bh(&rdev->beacon_registrations_lock);
10421 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
10422 msg = nlmsg_new(len + 100, GFP_ATOMIC);
10423 if (!msg) {
10424 spin_unlock_bh(&rdev->beacon_registrations_lock);
10425 return;
10426 }
Johannes Berg5e760232011-11-04 11:18:17 +010010427
Ben Greear37c73b52012-10-26 14:49:25 -070010428 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
10429 if (!hdr)
10430 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +010010431
Ben Greear37c73b52012-10-26 14:49:25 -070010432 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10433 (freq &&
10434 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
10435 (sig_dbm &&
10436 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
10437 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
10438 goto nla_put_failure;
10439
10440 genlmsg_end(msg, hdr);
10441
10442 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +010010443 }
Ben Greear37c73b52012-10-26 14:49:25 -070010444 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010445 return;
10446
10447 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070010448 spin_unlock_bh(&rdev->beacon_registrations_lock);
10449 if (hdr)
10450 genlmsg_cancel(msg, hdr);
Johannes Berg5e760232011-11-04 11:18:17 +010010451 nlmsg_free(msg);
10452}
10453EXPORT_SYMBOL(cfg80211_report_obss_beacon);
10454
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010455#ifdef CONFIG_PM
10456void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
10457 struct cfg80211_wowlan_wakeup *wakeup,
10458 gfp_t gfp)
10459{
10460 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10461 struct sk_buff *msg;
10462 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010463 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010464
10465 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
10466
10467 if (wakeup)
10468 size += wakeup->packet_present_len;
10469
10470 msg = nlmsg_new(size, gfp);
10471 if (!msg)
10472 return;
10473
10474 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
10475 if (!hdr)
10476 goto free_msg;
10477
10478 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10479 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10480 goto free_msg;
10481
10482 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10483 wdev->netdev->ifindex))
10484 goto free_msg;
10485
10486 if (wakeup) {
10487 struct nlattr *reasons;
10488
10489 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
10490
10491 if (wakeup->disconnect &&
10492 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
10493 goto free_msg;
10494 if (wakeup->magic_pkt &&
10495 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
10496 goto free_msg;
10497 if (wakeup->gtk_rekey_failure &&
10498 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
10499 goto free_msg;
10500 if (wakeup->eap_identity_req &&
10501 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
10502 goto free_msg;
10503 if (wakeup->four_way_handshake &&
10504 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
10505 goto free_msg;
10506 if (wakeup->rfkill_release &&
10507 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
10508 goto free_msg;
10509
10510 if (wakeup->pattern_idx >= 0 &&
10511 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
10512 wakeup->pattern_idx))
10513 goto free_msg;
10514
Johannes Berg2a0e0472013-01-23 22:57:40 +010010515 if (wakeup->tcp_match)
10516 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
10517
10518 if (wakeup->tcp_connlost)
10519 nla_put_flag(msg,
10520 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
10521
10522 if (wakeup->tcp_nomoretokens)
10523 nla_put_flag(msg,
10524 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
10525
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010526 if (wakeup->packet) {
10527 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
10528 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
10529
10530 if (!wakeup->packet_80211) {
10531 pkt_attr =
10532 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
10533 len_attr =
10534 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
10535 }
10536
10537 if (wakeup->packet_len &&
10538 nla_put_u32(msg, len_attr, wakeup->packet_len))
10539 goto free_msg;
10540
10541 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
10542 wakeup->packet))
10543 goto free_msg;
10544 }
10545
10546 nla_nest_end(msg, reasons);
10547 }
10548
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010549 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010010550
10551 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10552 nl80211_mlme_mcgrp.id, gfp);
10553 return;
10554
10555 free_msg:
10556 nlmsg_free(msg);
10557}
10558EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
10559#endif
10560
Jouni Malinen3475b092012-11-16 22:49:57 +020010561void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
10562 enum nl80211_tdls_operation oper,
10563 u16 reason_code, gfp_t gfp)
10564{
10565 struct wireless_dev *wdev = dev->ieee80211_ptr;
10566 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10567 struct sk_buff *msg;
10568 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020010569
10570 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
10571 reason_code);
10572
10573 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10574 if (!msg)
10575 return;
10576
10577 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
10578 if (!hdr) {
10579 nlmsg_free(msg);
10580 return;
10581 }
10582
10583 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10584 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10585 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
10586 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
10587 (reason_code > 0 &&
10588 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
10589 goto nla_put_failure;
10590
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010591 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020010592
10593 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10594 nl80211_mlme_mcgrp.id, gfp);
10595 return;
10596
10597 nla_put_failure:
10598 genlmsg_cancel(msg, hdr);
10599 nlmsg_free(msg);
10600}
10601EXPORT_SYMBOL(cfg80211_tdls_oper_request);
10602
Jouni Malinen026331c2010-02-15 12:53:10 +020010603static int nl80211_netlink_notify(struct notifier_block * nb,
10604 unsigned long state,
10605 void *_notify)
10606{
10607 struct netlink_notify *notify = _notify;
10608 struct cfg80211_registered_device *rdev;
10609 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070010610 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020010611
10612 if (state != NETLINK_URELEASE)
10613 return NOTIFY_DONE;
10614
10615 rcu_read_lock();
10616
Johannes Berg5e760232011-11-04 11:18:17 +010010617 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020010618 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000010619 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070010620
10621 spin_lock_bh(&rdev->beacon_registrations_lock);
10622 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
10623 list) {
10624 if (reg->nlportid == notify->portid) {
10625 list_del(&reg->list);
10626 kfree(reg);
10627 break;
10628 }
10629 }
10630 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e760232011-11-04 11:18:17 +010010631 }
Jouni Malinen026331c2010-02-15 12:53:10 +020010632
10633 rcu_read_unlock();
10634
10635 return NOTIFY_DONE;
10636}
10637
10638static struct notifier_block nl80211_netlink_notifier = {
10639 .notifier_call = nl80211_netlink_notify,
10640};
10641
Jouni Malinen355199e2013-02-27 17:14:27 +020010642void cfg80211_ft_event(struct net_device *netdev,
10643 struct cfg80211_ft_event_params *ft_event)
10644{
10645 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
10646 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10647 struct sk_buff *msg;
10648 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020010649
10650 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
10651
10652 if (!ft_event->target_ap)
10653 return;
10654
10655 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
10656 if (!msg)
10657 return;
10658
10659 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
10660 if (!hdr) {
10661 nlmsg_free(msg);
10662 return;
10663 }
10664
10665 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
10666 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
10667 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
10668 if (ft_event->ies)
10669 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
10670 if (ft_event->ric_ies)
10671 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
10672 ft_event->ric_ies);
10673
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010674 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020010675
10676 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10677 nl80211_mlme_mcgrp.id, GFP_KERNEL);
10678}
10679EXPORT_SYMBOL(cfg80211_ft_event);
10680
Arend van Spriel5de17982013-04-18 15:49:00 +020010681void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
10682{
10683 struct cfg80211_registered_device *rdev;
10684 struct sk_buff *msg;
10685 void *hdr;
10686 u32 nlportid;
10687
10688 rdev = wiphy_to_dev(wdev->wiphy);
10689 if (!rdev->crit_proto_nlportid)
10690 return;
10691
10692 nlportid = rdev->crit_proto_nlportid;
10693 rdev->crit_proto_nlportid = 0;
10694
10695 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10696 if (!msg)
10697 return;
10698
10699 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
10700 if (!hdr)
10701 goto nla_put_failure;
10702
10703 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10704 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
10705 goto nla_put_failure;
10706
10707 genlmsg_end(msg, hdr);
10708
10709 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
10710 return;
10711
10712 nla_put_failure:
10713 if (hdr)
10714 genlmsg_cancel(msg, hdr);
10715 nlmsg_free(msg);
10716
10717}
10718EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
10719
Johannes Berg55682962007-09-20 13:09:35 -040010720/* initialisation/exit functions */
10721
10722int nl80211_init(void)
10723{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010724 int err;
Johannes Berg55682962007-09-20 13:09:35 -040010725
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000010726 err = genl_register_family_with_ops(&nl80211_fam,
10727 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040010728 if (err)
10729 return err;
10730
Johannes Berg55682962007-09-20 13:09:35 -040010731 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
10732 if (err)
10733 goto err_out;
10734
Johannes Berg2a519312009-02-10 21:25:55 +010010735 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
10736 if (err)
10737 goto err_out;
10738
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040010739 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
10740 if (err)
10741 goto err_out;
10742
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010743 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
10744 if (err)
10745 goto err_out;
10746
Johannes Bergaff89a92009-07-01 21:26:51 +020010747#ifdef CONFIG_NL80211_TESTMODE
10748 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
10749 if (err)
10750 goto err_out;
10751#endif
10752
Jouni Malinen026331c2010-02-15 12:53:10 +020010753 err = netlink_register_notifier(&nl80211_netlink_notifier);
10754 if (err)
10755 goto err_out;
10756
Johannes Berg55682962007-09-20 13:09:35 -040010757 return 0;
10758 err_out:
10759 genl_unregister_family(&nl80211_fam);
10760 return err;
10761}
10762
10763void nl80211_exit(void)
10764{
Jouni Malinen026331c2010-02-15 12:53:10 +020010765 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040010766 genl_unregister_family(&nl80211_fam);
10767}