blob: 6989989de092a5c948182c4bd7387e09a1c035b4 [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 Malinen36aedc902008-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 },
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +0200352 [NL80211_ATTR_CH_SWITCH_COUNT] = { .type = NLA_U32 },
353 [NL80211_ATTR_CH_SWITCH_BLOCK_TX] = { .type = NLA_FLAG },
354 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
355 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
356 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
Sunil Duttc01fc9a2013-10-09 20:45:21 +0530357 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
358 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
Simon Wunderlich5336fa82013-10-07 18:41:05 +0200359 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +0100360 [NL80211_ATTR_OPMODE_NOTIF] = { .type = NLA_U8 },
Johannes Bergad7e7182013-11-13 13:37:47 +0100361 [NL80211_ATTR_VENDOR_ID] = { .type = NLA_U32 },
362 [NL80211_ATTR_VENDOR_SUBCMD] = { .type = NLA_U32 },
363 [NL80211_ATTR_VENDOR_DATA] = { .type = NLA_BINARY },
Johannes Berg55682962007-09-20 13:09:35 -0400364};
365
Johannes Berge31b8212010-10-05 19:39:30 +0200366/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000367static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200368 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200369 [NL80211_KEY_IDX] = { .type = NLA_U8 },
370 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200371 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200372 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
373 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200374 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100375 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
376};
377
378/* policy for the key default flags */
379static const struct nla_policy
380nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
381 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
382 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200383};
384
Johannes Bergff1b6e62011-05-04 15:37:28 +0200385/* policy for WoWLAN attributes */
386static const struct nla_policy
387nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
388 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
389 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
390 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
391 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200392 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
393 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
394 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
395 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Berg2a0e0472013-01-23 22:57:40 +0100396 [NL80211_WOWLAN_TRIG_TCP_CONNECTION] = { .type = NLA_NESTED },
397};
398
399static const struct nla_policy
400nl80211_wowlan_tcp_policy[NUM_NL80211_WOWLAN_TCP] = {
401 [NL80211_WOWLAN_TCP_SRC_IPV4] = { .type = NLA_U32 },
402 [NL80211_WOWLAN_TCP_DST_IPV4] = { .type = NLA_U32 },
403 [NL80211_WOWLAN_TCP_DST_MAC] = { .len = ETH_ALEN },
404 [NL80211_WOWLAN_TCP_SRC_PORT] = { .type = NLA_U16 },
405 [NL80211_WOWLAN_TCP_DST_PORT] = { .type = NLA_U16 },
406 [NL80211_WOWLAN_TCP_DATA_PAYLOAD] = { .len = 1 },
407 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ] = {
408 .len = sizeof(struct nl80211_wowlan_tcp_data_seq)
409 },
410 [NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN] = {
411 .len = sizeof(struct nl80211_wowlan_tcp_data_token)
412 },
413 [NL80211_WOWLAN_TCP_DATA_INTERVAL] = { .type = NLA_U32 },
414 [NL80211_WOWLAN_TCP_WAKE_PAYLOAD] = { .len = 1 },
415 [NL80211_WOWLAN_TCP_WAKE_MASK] = { .len = 1 },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200416};
417
Amitkumar Karwarbe29b992013-06-28 11:51:26 -0700418/* policy for coalesce rule attributes */
419static const struct nla_policy
420nl80211_coalesce_policy[NUM_NL80211_ATTR_COALESCE_RULE] = {
421 [NL80211_ATTR_COALESCE_RULE_DELAY] = { .type = NLA_U32 },
422 [NL80211_ATTR_COALESCE_RULE_CONDITION] = { .type = NLA_U32 },
423 [NL80211_ATTR_COALESCE_RULE_PKT_PATTERN] = { .type = NLA_NESTED },
424};
425
Johannes Berge5497d72011-07-05 16:35:40 +0200426/* policy for GTK rekey offload attributes */
427static const struct nla_policy
428nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
429 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
430 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
431 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
432};
433
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300434static const struct nla_policy
435nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200436 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300437 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700438 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300439};
440
Johannes Berg97990a02013-04-19 01:02:55 +0200441static int nl80211_prepare_wdev_dump(struct sk_buff *skb,
442 struct netlink_callback *cb,
443 struct cfg80211_registered_device **rdev,
444 struct wireless_dev **wdev)
Holger Schuriga0438972009-11-11 11:30:02 +0100445{
Johannes Berg67748892010-10-04 21:14:06 +0200446 int err;
447
Johannes Berg67748892010-10-04 21:14:06 +0200448 rtnl_lock();
449
Johannes Berg97990a02013-04-19 01:02:55 +0200450 if (!cb->args[0]) {
451 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
452 nl80211_fam.attrbuf, nl80211_fam.maxattr,
453 nl80211_policy);
454 if (err)
455 goto out_unlock;
456
457 *wdev = __cfg80211_wdev_from_attrs(sock_net(skb->sk),
458 nl80211_fam.attrbuf);
459 if (IS_ERR(*wdev)) {
460 err = PTR_ERR(*wdev);
461 goto out_unlock;
462 }
463 *rdev = wiphy_to_dev((*wdev)->wiphy);
Johannes Bergc319d502013-07-30 22:34:28 +0200464 /* 0 is the first index - add 1 to parse only once */
465 cb->args[0] = (*rdev)->wiphy_idx + 1;
Johannes Berg97990a02013-04-19 01:02:55 +0200466 cb->args[1] = (*wdev)->identifier;
467 } else {
Johannes Bergc319d502013-07-30 22:34:28 +0200468 /* subtract the 1 again here */
469 struct wiphy *wiphy = wiphy_idx_to_wiphy(cb->args[0] - 1);
Johannes Berg97990a02013-04-19 01:02:55 +0200470 struct wireless_dev *tmp;
471
472 if (!wiphy) {
473 err = -ENODEV;
474 goto out_unlock;
475 }
476 *rdev = wiphy_to_dev(wiphy);
477 *wdev = NULL;
478
Johannes Berg97990a02013-04-19 01:02:55 +0200479 list_for_each_entry(tmp, &(*rdev)->wdev_list, list) {
480 if (tmp->identifier == cb->args[1]) {
481 *wdev = tmp;
482 break;
483 }
484 }
Johannes Berg97990a02013-04-19 01:02:55 +0200485
486 if (!*wdev) {
487 err = -ENODEV;
488 goto out_unlock;
489 }
Johannes Berg67748892010-10-04 21:14:06 +0200490 }
491
Johannes Berg67748892010-10-04 21:14:06 +0200492 return 0;
Johannes Berg97990a02013-04-19 01:02:55 +0200493 out_unlock:
Johannes Berg67748892010-10-04 21:14:06 +0200494 rtnl_unlock();
495 return err;
496}
497
Johannes Berg97990a02013-04-19 01:02:55 +0200498static void nl80211_finish_wdev_dump(struct cfg80211_registered_device *rdev)
Johannes Berg67748892010-10-04 21:14:06 +0200499{
Johannes Berg67748892010-10-04 21:14:06 +0200500 rtnl_unlock();
501}
502
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100503/* IE validation */
504static bool is_valid_ie_attr(const struct nlattr *attr)
505{
506 const u8 *pos;
507 int len;
508
509 if (!attr)
510 return true;
511
512 pos = nla_data(attr);
513 len = nla_len(attr);
514
515 while (len) {
516 u8 elemlen;
517
518 if (len < 2)
519 return false;
520 len -= 2;
521
522 elemlen = pos[1];
523 if (elemlen > len)
524 return false;
525
526 len -= elemlen;
527 pos += 2 + elemlen;
528 }
529
530 return true;
531}
532
Johannes Berg55682962007-09-20 13:09:35 -0400533/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000534static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400535 int flags, u8 cmd)
536{
537 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000538 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400539}
540
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400541static int nl80211_msg_put_channel(struct sk_buff *msg,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100542 struct ieee80211_channel *chan,
543 bool large)
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400544{
David S. Miller9360ffd2012-03-29 04:41:26 -0400545 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
546 chan->center_freq))
547 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400548
David S. Miller9360ffd2012-03-29 04:41:26 -0400549 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
550 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
551 goto nla_put_failure;
Luis R. Rodriguez8fe02e12013-10-21 19:22:25 +0200552 if (chan->flags & IEEE80211_CHAN_NO_IR) {
553 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IR))
554 goto nla_put_failure;
555 if (nla_put_flag(msg, __NL80211_FREQUENCY_ATTR_NO_IBSS))
556 goto nla_put_failure;
557 }
Johannes Bergcdc89b92013-02-18 23:54:36 +0100558 if (chan->flags & IEEE80211_CHAN_RADAR) {
559 if (nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
560 goto nla_put_failure;
561 if (large) {
562 u32 time;
563
564 time = elapsed_jiffies_msecs(chan->dfs_state_entered);
565
566 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_STATE,
567 chan->dfs_state))
568 goto nla_put_failure;
569 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_DFS_TIME,
570 time))
571 goto nla_put_failure;
572 }
573 }
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400574
Johannes Bergfe1abaf2013-02-27 15:39:45 +0100575 if (large) {
576 if ((chan->flags & IEEE80211_CHAN_NO_HT40MINUS) &&
577 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_MINUS))
578 goto nla_put_failure;
579 if ((chan->flags & IEEE80211_CHAN_NO_HT40PLUS) &&
580 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_HT40_PLUS))
581 goto nla_put_failure;
582 if ((chan->flags & IEEE80211_CHAN_NO_80MHZ) &&
583 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_80MHZ))
584 goto nla_put_failure;
585 if ((chan->flags & IEEE80211_CHAN_NO_160MHZ) &&
586 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_160MHZ))
587 goto nla_put_failure;
588 }
589
David S. Miller9360ffd2012-03-29 04:41:26 -0400590 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
591 DBM_TO_MBM(chan->max_power)))
592 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400593
594 return 0;
595
596 nla_put_failure:
597 return -ENOBUFS;
598}
599
Johannes Berg55682962007-09-20 13:09:35 -0400600/* netlink command implementations */
601
Johannes Bergb9454e82009-07-08 13:29:08 +0200602struct key_parse {
603 struct key_params p;
604 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200605 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200606 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100607 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200608};
609
610static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
611{
612 struct nlattr *tb[NL80211_KEY_MAX + 1];
613 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
614 nl80211_key_policy);
615 if (err)
616 return err;
617
618 k->def = !!tb[NL80211_KEY_DEFAULT];
619 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
620
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100621 if (k->def) {
622 k->def_uni = true;
623 k->def_multi = true;
624 }
625 if (k->defmgmt)
626 k->def_multi = true;
627
Johannes Bergb9454e82009-07-08 13:29:08 +0200628 if (tb[NL80211_KEY_IDX])
629 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
630
631 if (tb[NL80211_KEY_DATA]) {
632 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
633 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
634 }
635
636 if (tb[NL80211_KEY_SEQ]) {
637 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
638 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
639 }
640
641 if (tb[NL80211_KEY_CIPHER])
642 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
643
Johannes Berge31b8212010-10-05 19:39:30 +0200644 if (tb[NL80211_KEY_TYPE]) {
645 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
646 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
647 return -EINVAL;
648 }
649
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100650 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
651 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100652 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
653 tb[NL80211_KEY_DEFAULT_TYPES],
654 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100655 if (err)
656 return err;
657
658 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
659 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
660 }
661
Johannes Bergb9454e82009-07-08 13:29:08 +0200662 return 0;
663}
664
665static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
666{
667 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
668 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
669 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
670 }
671
672 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
673 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
674 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
675 }
676
677 if (info->attrs[NL80211_ATTR_KEY_IDX])
678 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
679
680 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
681 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
682
683 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
684 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
685
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100686 if (k->def) {
687 k->def_uni = true;
688 k->def_multi = true;
689 }
690 if (k->defmgmt)
691 k->def_multi = true;
692
Johannes Berge31b8212010-10-05 19:39:30 +0200693 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
694 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
695 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
696 return -EINVAL;
697 }
698
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100699 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
700 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
701 int err = nla_parse_nested(
702 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
703 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
704 nl80211_key_default_policy);
705 if (err)
706 return err;
707
708 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
709 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
710 }
711
Johannes Bergb9454e82009-07-08 13:29:08 +0200712 return 0;
713}
714
715static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
716{
717 int err;
718
719 memset(k, 0, sizeof(*k));
720 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200721 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200722
723 if (info->attrs[NL80211_ATTR_KEY])
724 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
725 else
726 err = nl80211_parse_key_old(info, k);
727
728 if (err)
729 return err;
730
731 if (k->def && k->defmgmt)
732 return -EINVAL;
733
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100734 if (k->defmgmt) {
735 if (k->def_uni || !k->def_multi)
736 return -EINVAL;
737 }
738
Johannes Bergb9454e82009-07-08 13:29:08 +0200739 if (k->idx != -1) {
740 if (k->defmgmt) {
741 if (k->idx < 4 || k->idx > 5)
742 return -EINVAL;
743 } else if (k->def) {
744 if (k->idx < 0 || k->idx > 3)
745 return -EINVAL;
746 } else {
747 if (k->idx < 0 || k->idx > 5)
748 return -EINVAL;
749 }
750 }
751
752 return 0;
753}
754
Johannes Bergfffd0932009-07-08 14:22:54 +0200755static struct cfg80211_cached_keys *
756nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530757 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200758{
759 struct key_parse parse;
760 struct nlattr *key;
761 struct cfg80211_cached_keys *result;
762 int rem, err, def = 0;
763
764 result = kzalloc(sizeof(*result), GFP_KERNEL);
765 if (!result)
766 return ERR_PTR(-ENOMEM);
767
768 result->def = -1;
769 result->defmgmt = -1;
770
771 nla_for_each_nested(key, keys, rem) {
772 memset(&parse, 0, sizeof(parse));
773 parse.idx = -1;
774
775 err = nl80211_parse_key_new(key, &parse);
776 if (err)
777 goto error;
778 err = -EINVAL;
779 if (!parse.p.key)
780 goto error;
781 if (parse.idx < 0 || parse.idx > 4)
782 goto error;
783 if (parse.def) {
784 if (def)
785 goto error;
786 def = 1;
787 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100788 if (!parse.def_uni || !parse.def_multi)
789 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200790 } else if (parse.defmgmt)
791 goto error;
792 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200793 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200794 if (err)
795 goto error;
796 result->params[parse.idx].cipher = parse.p.cipher;
797 result->params[parse.idx].key_len = parse.p.key_len;
798 result->params[parse.idx].key = result->data[parse.idx];
799 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530800
801 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
802 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
803 if (no_ht)
804 *no_ht = true;
805 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200806 }
807
808 return result;
809 error:
810 kfree(result);
811 return ERR_PTR(err);
812}
813
814static int nl80211_key_allowed(struct wireless_dev *wdev)
815{
816 ASSERT_WDEV_LOCK(wdev);
817
Johannes Bergfffd0932009-07-08 14:22:54 +0200818 switch (wdev->iftype) {
819 case NL80211_IFTYPE_AP:
820 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200821 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700822 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200823 break;
824 case NL80211_IFTYPE_ADHOC:
Johannes Bergfffd0932009-07-08 14:22:54 +0200825 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200826 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergceca7b72013-05-16 00:55:45 +0200827 if (!wdev->current_bss)
Johannes Bergfffd0932009-07-08 14:22:54 +0200828 return -ENOLINK;
829 break;
830 default:
831 return -EINVAL;
832 }
833
834 return 0;
835}
836
Johannes Berg7527a782011-05-13 10:58:57 +0200837static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
838{
839 struct nlattr *nl_modes = nla_nest_start(msg, attr);
840 int i;
841
842 if (!nl_modes)
843 goto nla_put_failure;
844
845 i = 0;
846 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400847 if ((ifmodes & 1) && nla_put_flag(msg, i))
848 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200849 ifmodes >>= 1;
850 i++;
851 }
852
853 nla_nest_end(msg, nl_modes);
854 return 0;
855
856nla_put_failure:
857 return -ENOBUFS;
858}
859
860static int nl80211_put_iface_combinations(struct wiphy *wiphy,
Johannes Bergcdc89b92013-02-18 23:54:36 +0100861 struct sk_buff *msg,
862 bool large)
Johannes Berg7527a782011-05-13 10:58:57 +0200863{
864 struct nlattr *nl_combis;
865 int i, j;
866
867 nl_combis = nla_nest_start(msg,
868 NL80211_ATTR_INTERFACE_COMBINATIONS);
869 if (!nl_combis)
870 goto nla_put_failure;
871
872 for (i = 0; i < wiphy->n_iface_combinations; i++) {
873 const struct ieee80211_iface_combination *c;
874 struct nlattr *nl_combi, *nl_limits;
875
876 c = &wiphy->iface_combinations[i];
877
878 nl_combi = nla_nest_start(msg, i + 1);
879 if (!nl_combi)
880 goto nla_put_failure;
881
882 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
883 if (!nl_limits)
884 goto nla_put_failure;
885
886 for (j = 0; j < c->n_limits; j++) {
887 struct nlattr *nl_limit;
888
889 nl_limit = nla_nest_start(msg, j + 1);
890 if (!nl_limit)
891 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400892 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
893 c->limits[j].max))
894 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200895 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
896 c->limits[j].types))
897 goto nla_put_failure;
898 nla_nest_end(msg, nl_limit);
899 }
900
901 nla_nest_end(msg, nl_limits);
902
David S. Miller9360ffd2012-03-29 04:41:26 -0400903 if (c->beacon_int_infra_match &&
904 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
905 goto nla_put_failure;
906 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
907 c->num_different_channels) ||
908 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
909 c->max_interfaces))
910 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +0100911 if (large &&
912 nla_put_u32(msg, NL80211_IFACE_COMB_RADAR_DETECT_WIDTHS,
913 c->radar_detect_widths))
914 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200915
916 nla_nest_end(msg, nl_combi);
917 }
918
919 nla_nest_end(msg, nl_combis);
920
921 return 0;
922nla_put_failure:
923 return -ENOBUFS;
924}
925
Johannes Berg3713b4e2013-02-14 16:19:38 +0100926#ifdef CONFIG_PM
Johannes Bergb56cf722013-02-20 01:02:38 +0100927static int nl80211_send_wowlan_tcp_caps(struct cfg80211_registered_device *rdev,
928 struct sk_buff *msg)
929{
Johannes Berg964dc9e2013-06-03 17:25:34 +0200930 const struct wiphy_wowlan_tcp_support *tcp = rdev->wiphy.wowlan->tcp;
Johannes Bergb56cf722013-02-20 01:02:38 +0100931 struct nlattr *nl_tcp;
932
933 if (!tcp)
934 return 0;
935
936 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
937 if (!nl_tcp)
938 return -ENOBUFS;
939
940 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
941 tcp->data_payload_max))
942 return -ENOBUFS;
943
944 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
945 tcp->data_payload_max))
946 return -ENOBUFS;
947
948 if (tcp->seq && nla_put_flag(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ))
949 return -ENOBUFS;
950
951 if (tcp->tok && nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
952 sizeof(*tcp->tok), tcp->tok))
953 return -ENOBUFS;
954
955 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
956 tcp->data_interval_max))
957 return -ENOBUFS;
958
959 if (nla_put_u32(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
960 tcp->wake_payload_max))
961 return -ENOBUFS;
962
963 nla_nest_end(msg, nl_tcp);
964 return 0;
965}
966
Johannes Berg3713b4e2013-02-14 16:19:38 +0100967static int nl80211_send_wowlan(struct sk_buff *msg,
Johannes Bergb56cf722013-02-20 01:02:38 +0100968 struct cfg80211_registered_device *dev,
969 bool large)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100970{
971 struct nlattr *nl_wowlan;
972
Johannes Berg964dc9e2013-06-03 17:25:34 +0200973 if (!dev->wiphy.wowlan)
Johannes Berg3713b4e2013-02-14 16:19:38 +0100974 return 0;
975
976 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
977 if (!nl_wowlan)
978 return -ENOBUFS;
979
Johannes Berg964dc9e2013-06-03 17:25:34 +0200980 if (((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_ANY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100981 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200982 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_DISCONNECT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100983 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200984 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100985 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200986 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100987 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200988 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100989 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200990 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100991 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200992 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100993 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg964dc9e2013-06-03 17:25:34 +0200994 ((dev->wiphy.wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
Johannes Berg3713b4e2013-02-14 16:19:38 +0100995 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
996 return -ENOBUFS;
997
Johannes Berg964dc9e2013-06-03 17:25:34 +0200998 if (dev->wiphy.wowlan->n_patterns) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -0700999 struct nl80211_pattern_support pat = {
Johannes Berg964dc9e2013-06-03 17:25:34 +02001000 .max_patterns = dev->wiphy.wowlan->n_patterns,
1001 .min_pattern_len = dev->wiphy.wowlan->pattern_min_len,
1002 .max_pattern_len = dev->wiphy.wowlan->pattern_max_len,
1003 .max_pkt_offset = dev->wiphy.wowlan->max_pkt_offset,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001004 };
1005
1006 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1007 sizeof(pat), &pat))
1008 return -ENOBUFS;
1009 }
1010
Johannes Bergb56cf722013-02-20 01:02:38 +01001011 if (large && nl80211_send_wowlan_tcp_caps(dev, msg))
1012 return -ENOBUFS;
1013
Johannes Berg3713b4e2013-02-14 16:19:38 +01001014 nla_nest_end(msg, nl_wowlan);
1015
1016 return 0;
1017}
1018#endif
1019
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001020static int nl80211_send_coalesce(struct sk_buff *msg,
1021 struct cfg80211_registered_device *dev)
1022{
1023 struct nl80211_coalesce_rule_support rule;
1024
1025 if (!dev->wiphy.coalesce)
1026 return 0;
1027
1028 rule.max_rules = dev->wiphy.coalesce->n_rules;
1029 rule.max_delay = dev->wiphy.coalesce->max_delay;
1030 rule.pat.max_patterns = dev->wiphy.coalesce->n_patterns;
1031 rule.pat.min_pattern_len = dev->wiphy.coalesce->pattern_min_len;
1032 rule.pat.max_pattern_len = dev->wiphy.coalesce->pattern_max_len;
1033 rule.pat.max_pkt_offset = dev->wiphy.coalesce->max_pkt_offset;
1034
1035 if (nla_put(msg, NL80211_ATTR_COALESCE_RULE, sizeof(rule), &rule))
1036 return -ENOBUFS;
1037
1038 return 0;
1039}
1040
Johannes Berg3713b4e2013-02-14 16:19:38 +01001041static int nl80211_send_band_rateinfo(struct sk_buff *msg,
1042 struct ieee80211_supported_band *sband)
1043{
1044 struct nlattr *nl_rates, *nl_rate;
1045 struct ieee80211_rate *rate;
1046 int i;
1047
1048 /* add HT info */
1049 if (sband->ht_cap.ht_supported &&
1050 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
1051 sizeof(sband->ht_cap.mcs),
1052 &sband->ht_cap.mcs) ||
1053 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
1054 sband->ht_cap.cap) ||
1055 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
1056 sband->ht_cap.ampdu_factor) ||
1057 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
1058 sband->ht_cap.ampdu_density)))
1059 return -ENOBUFS;
1060
1061 /* add VHT info */
1062 if (sband->vht_cap.vht_supported &&
1063 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
1064 sizeof(sband->vht_cap.vht_mcs),
1065 &sband->vht_cap.vht_mcs) ||
1066 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
1067 sband->vht_cap.cap)))
1068 return -ENOBUFS;
1069
1070 /* add bitrates */
1071 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1072 if (!nl_rates)
1073 return -ENOBUFS;
1074
1075 for (i = 0; i < sband->n_bitrates; i++) {
1076 nl_rate = nla_nest_start(msg, i);
1077 if (!nl_rate)
1078 return -ENOBUFS;
1079
1080 rate = &sband->bitrates[i];
1081 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1082 rate->bitrate))
1083 return -ENOBUFS;
1084 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1085 nla_put_flag(msg,
1086 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1087 return -ENOBUFS;
1088
1089 nla_nest_end(msg, nl_rate);
1090 }
1091
1092 nla_nest_end(msg, nl_rates);
1093
1094 return 0;
1095}
1096
1097static int
1098nl80211_send_mgmt_stypes(struct sk_buff *msg,
1099 const struct ieee80211_txrx_stypes *mgmt_stypes)
1100{
1101 u16 stypes;
1102 struct nlattr *nl_ftypes, *nl_ifs;
1103 enum nl80211_iftype ift;
1104 int i;
1105
1106 if (!mgmt_stypes)
1107 return 0;
1108
1109 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1110 if (!nl_ifs)
1111 return -ENOBUFS;
1112
1113 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1114 nl_ftypes = nla_nest_start(msg, ift);
1115 if (!nl_ftypes)
1116 return -ENOBUFS;
1117 i = 0;
1118 stypes = mgmt_stypes[ift].tx;
1119 while (stypes) {
1120 if ((stypes & 1) &&
1121 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1122 (i << 4) | IEEE80211_FTYPE_MGMT))
1123 return -ENOBUFS;
1124 stypes >>= 1;
1125 i++;
1126 }
1127 nla_nest_end(msg, nl_ftypes);
1128 }
1129
1130 nla_nest_end(msg, nl_ifs);
1131
1132 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1133 if (!nl_ifs)
1134 return -ENOBUFS;
1135
1136 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1137 nl_ftypes = nla_nest_start(msg, ift);
1138 if (!nl_ftypes)
1139 return -ENOBUFS;
1140 i = 0;
1141 stypes = mgmt_stypes[ift].rx;
1142 while (stypes) {
1143 if ((stypes & 1) &&
1144 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1145 (i << 4) | IEEE80211_FTYPE_MGMT))
1146 return -ENOBUFS;
1147 stypes >>= 1;
1148 i++;
1149 }
1150 nla_nest_end(msg, nl_ftypes);
1151 }
1152 nla_nest_end(msg, nl_ifs);
1153
1154 return 0;
1155}
1156
Johannes Berg86e8cf92013-06-19 10:57:22 +02001157struct nl80211_dump_wiphy_state {
1158 s64 filter_wiphy;
1159 long start;
1160 long split_start, band_start, chan_start;
1161 bool split;
1162};
1163
Johannes Berg3713b4e2013-02-14 16:19:38 +01001164static int nl80211_send_wiphy(struct cfg80211_registered_device *dev,
1165 struct sk_buff *msg, u32 portid, u32 seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001166 int flags, struct nl80211_dump_wiphy_state *state)
Johannes Berg55682962007-09-20 13:09:35 -04001167{
1168 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +01001169 struct nlattr *nl_bands, *nl_band;
1170 struct nlattr *nl_freqs, *nl_freq;
Johannes Berg8fdc6212009-03-14 09:34:01 +01001171 struct nlattr *nl_cmds;
Johannes Bergad7e7182013-11-13 13:37:47 +01001172 struct nlattr *nl_vendor_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +01001173 enum ieee80211_band band;
1174 struct ieee80211_channel *chan;
Johannes Bergee688b002008-01-24 19:38:39 +01001175 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +02001176 const struct ieee80211_txrx_stypes *mgmt_stypes =
1177 dev->wiphy.mgmt_stypes;
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001178 u32 features;
Johannes Berg55682962007-09-20 13:09:35 -04001179
Eric W. Biederman15e47302012-09-07 20:12:54 +00001180 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -04001181 if (!hdr)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001182 return -ENOBUFS;
1183
Johannes Berg86e8cf92013-06-19 10:57:22 +02001184 if (WARN_ON(!state))
1185 return -EINVAL;
Johannes Berg55682962007-09-20 13:09:35 -04001186
David S. Miller9360ffd2012-03-29 04:41:26 -04001187 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
Johannes Berg3713b4e2013-02-14 16:19:38 +01001188 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME,
1189 wiphy_name(&dev->wiphy)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001190 nla_put_u32(msg, NL80211_ATTR_GENERATION,
Johannes Berg3713b4e2013-02-14 16:19:38 +01001191 cfg80211_rdev_list_generation))
David S. Miller9360ffd2012-03-29 04:41:26 -04001192 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001193
Johannes Berg86e8cf92013-06-19 10:57:22 +02001194 switch (state->split_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001195 case 0:
1196 if (nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
1197 dev->wiphy.retry_short) ||
1198 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
1199 dev->wiphy.retry_long) ||
1200 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
1201 dev->wiphy.frag_threshold) ||
1202 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
1203 dev->wiphy.rts_threshold) ||
1204 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
1205 dev->wiphy.coverage_class) ||
1206 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
1207 dev->wiphy.max_scan_ssids) ||
1208 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
1209 dev->wiphy.max_sched_scan_ssids) ||
1210 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
1211 dev->wiphy.max_scan_ie_len) ||
1212 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
1213 dev->wiphy.max_sched_scan_ie_len) ||
1214 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
1215 dev->wiphy.max_match_sets))
Johannes Bergee688b002008-01-24 19:38:39 +01001216 goto nla_put_failure;
1217
Johannes Berg3713b4e2013-02-14 16:19:38 +01001218 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
1219 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
1220 goto nla_put_failure;
1221 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
1222 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
1223 goto nla_put_failure;
1224 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
1225 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
1226 goto nla_put_failure;
1227 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
1228 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
1229 goto nla_put_failure;
1230 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
1231 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
1232 goto nla_put_failure;
1233 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
1234 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
David S. Miller9360ffd2012-03-29 04:41:26 -04001235 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001236 state->split_start++;
1237 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001238 break;
1239 case 1:
1240 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
1241 sizeof(u32) * dev->wiphy.n_cipher_suites,
1242 dev->wiphy.cipher_suites))
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +00001243 goto nla_put_failure;
1244
Johannes Berg3713b4e2013-02-14 16:19:38 +01001245 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
1246 dev->wiphy.max_num_pmkids))
Johannes Bergee688b002008-01-24 19:38:39 +01001247 goto nla_put_failure;
1248
Johannes Berg3713b4e2013-02-14 16:19:38 +01001249 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
1250 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
1251 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001252
Johannes Berg3713b4e2013-02-14 16:19:38 +01001253 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
1254 dev->wiphy.available_antennas_tx) ||
1255 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
1256 dev->wiphy.available_antennas_rx))
1257 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001258
Johannes Berg3713b4e2013-02-14 16:19:38 +01001259 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
1260 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
1261 dev->wiphy.probe_resp_offload))
1262 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001263
Johannes Berg3713b4e2013-02-14 16:19:38 +01001264 if ((dev->wiphy.available_antennas_tx ||
1265 dev->wiphy.available_antennas_rx) &&
1266 dev->ops->get_antenna) {
1267 u32 tx_ant = 0, rx_ant = 0;
1268 int res;
1269 res = rdev_get_antenna(dev, &tx_ant, &rx_ant);
1270 if (!res) {
1271 if (nla_put_u32(msg,
1272 NL80211_ATTR_WIPHY_ANTENNA_TX,
1273 tx_ant) ||
1274 nla_put_u32(msg,
1275 NL80211_ATTR_WIPHY_ANTENNA_RX,
1276 rx_ant))
1277 goto nla_put_failure;
1278 }
Johannes Bergee688b002008-01-24 19:38:39 +01001279 }
1280
Johannes Berg86e8cf92013-06-19 10:57:22 +02001281 state->split_start++;
1282 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001283 break;
1284 case 2:
1285 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
1286 dev->wiphy.interface_modes))
1287 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001288 state->split_start++;
1289 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001290 break;
1291 case 3:
1292 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
1293 if (!nl_bands)
Johannes Bergee688b002008-01-24 19:38:39 +01001294 goto nla_put_failure;
1295
Johannes Berg86e8cf92013-06-19 10:57:22 +02001296 for (band = state->band_start;
1297 band < IEEE80211_NUM_BANDS; band++) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001298 struct ieee80211_supported_band *sband;
1299
1300 sband = dev->wiphy.bands[band];
1301
1302 if (!sband)
1303 continue;
1304
1305 nl_band = nla_nest_start(msg, band);
1306 if (!nl_band)
Johannes Bergee688b002008-01-24 19:38:39 +01001307 goto nla_put_failure;
1308
Johannes Berg86e8cf92013-06-19 10:57:22 +02001309 switch (state->chan_start) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001310 case 0:
1311 if (nl80211_send_band_rateinfo(msg, sband))
1312 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001313 state->chan_start++;
1314 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001315 break;
1316 default:
1317 /* add frequencies */
1318 nl_freqs = nla_nest_start(
1319 msg, NL80211_BAND_ATTR_FREQS);
1320 if (!nl_freqs)
1321 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001322
Johannes Berg86e8cf92013-06-19 10:57:22 +02001323 for (i = state->chan_start - 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001324 i < sband->n_channels;
1325 i++) {
1326 nl_freq = nla_nest_start(msg, i);
1327 if (!nl_freq)
1328 goto nla_put_failure;
1329
1330 chan = &sband->channels[i];
1331
Johannes Berg86e8cf92013-06-19 10:57:22 +02001332 if (nl80211_msg_put_channel(
1333 msg, chan,
1334 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001335 goto nla_put_failure;
1336
1337 nla_nest_end(msg, nl_freq);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001338 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001339 break;
1340 }
1341 if (i < sband->n_channels)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001342 state->chan_start = i + 2;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001343 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001344 state->chan_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001345 nla_nest_end(msg, nl_freqs);
1346 }
1347
1348 nla_nest_end(msg, nl_band);
1349
Johannes Berg86e8cf92013-06-19 10:57:22 +02001350 if (state->split) {
Johannes Berg3713b4e2013-02-14 16:19:38 +01001351 /* start again here */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001352 if (state->chan_start)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001353 band--;
1354 break;
1355 }
Johannes Bergee688b002008-01-24 19:38:39 +01001356 }
Johannes Berg3713b4e2013-02-14 16:19:38 +01001357 nla_nest_end(msg, nl_bands);
Johannes Bergee688b002008-01-24 19:38:39 +01001358
Johannes Berg3713b4e2013-02-14 16:19:38 +01001359 if (band < IEEE80211_NUM_BANDS)
Johannes Berg86e8cf92013-06-19 10:57:22 +02001360 state->band_start = band + 1;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001361 else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001362 state->band_start = 0;
Johannes Bergee688b002008-01-24 19:38:39 +01001363
Johannes Berg3713b4e2013-02-14 16:19:38 +01001364 /* if bands & channels are done, continue outside */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001365 if (state->band_start == 0 && state->chan_start == 0)
1366 state->split_start++;
1367 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001368 break;
1369 case 4:
1370 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1371 if (!nl_cmds)
David S. Miller9360ffd2012-03-29 04:41:26 -04001372 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001373
1374 i = 0;
1375#define CMD(op, n) \
1376 do { \
1377 if (dev->ops->op) { \
1378 i++; \
1379 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1380 goto nla_put_failure; \
1381 } \
1382 } while (0)
1383
1384 CMD(add_virtual_intf, NEW_INTERFACE);
1385 CMD(change_virtual_intf, SET_INTERFACE);
1386 CMD(add_key, NEW_KEY);
1387 CMD(start_ap, START_AP);
1388 CMD(add_station, NEW_STATION);
1389 CMD(add_mpath, NEW_MPATH);
1390 CMD(update_mesh_config, SET_MESH_CONFIG);
1391 CMD(change_bss, SET_BSS);
1392 CMD(auth, AUTHENTICATE);
1393 CMD(assoc, ASSOCIATE);
1394 CMD(deauth, DEAUTHENTICATE);
1395 CMD(disassoc, DISASSOCIATE);
1396 CMD(join_ibss, JOIN_IBSS);
1397 CMD(join_mesh, JOIN_MESH);
1398 CMD(set_pmksa, SET_PMKSA);
1399 CMD(del_pmksa, DEL_PMKSA);
1400 CMD(flush_pmksa, FLUSH_PMKSA);
1401 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1402 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
1403 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
1404 CMD(mgmt_tx, FRAME);
1405 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
1406 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
1407 i++;
1408 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1409 goto nla_put_failure;
1410 }
1411 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
1412 dev->ops->join_mesh) {
1413 i++;
1414 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1415 goto nla_put_failure;
1416 }
1417 CMD(set_wds_peer, SET_WDS_PEER);
1418 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1419 CMD(tdls_mgmt, TDLS_MGMT);
1420 CMD(tdls_oper, TDLS_OPER);
1421 }
1422 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1423 CMD(sched_scan_start, START_SCHED_SCAN);
1424 CMD(probe_client, PROBE_CLIENT);
1425 CMD(set_noack_map, SET_NOACK_MAP);
1426 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1427 i++;
1428 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1429 goto nla_put_failure;
1430 }
1431 CMD(start_p2p_device, START_P2P_DEVICE);
1432 CMD(set_mcast_rate, SET_MCAST_RATE);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001433 if (state->split) {
Arend van Spriel5de17982013-04-18 15:49:00 +02001434 CMD(crit_proto_start, CRIT_PROTOCOL_START);
1435 CMD(crit_proto_stop, CRIT_PROTOCOL_STOP);
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02001436 if (dev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)
1437 CMD(channel_switch, CHANNEL_SWITCH);
Arend van Spriel5de17982013-04-18 15:49:00 +02001438 }
Johannes Berg8fdc6212009-03-14 09:34:01 +01001439
Kalle Valo4745fc02011-11-17 19:06:10 +02001440#ifdef CONFIG_NL80211_TESTMODE
Johannes Berg3713b4e2013-02-14 16:19:38 +01001441 CMD(testmode_cmd, TESTMODE);
Kalle Valo4745fc02011-11-17 19:06:10 +02001442#endif
1443
Johannes Berg8fdc6212009-03-14 09:34:01 +01001444#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001445
Johannes Berg3713b4e2013-02-14 16:19:38 +01001446 if (dev->ops->connect || dev->ops->auth) {
1447 i++;
1448 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
Johannes Berg2e161f72010-08-12 15:38:38 +02001449 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001450 }
1451
Johannes Berg3713b4e2013-02-14 16:19:38 +01001452 if (dev->ops->disconnect || dev->ops->deauth) {
1453 i++;
1454 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1455 goto nla_put_failure;
1456 }
Johannes Berg74b70a42010-08-24 12:15:53 +02001457
Johannes Berg3713b4e2013-02-14 16:19:38 +01001458 nla_nest_end(msg, nl_cmds);
Johannes Berg86e8cf92013-06-19 10:57:22 +02001459 state->split_start++;
1460 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001461 break;
1462 case 5:
1463 if (dev->ops->remain_on_channel &&
1464 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1465 nla_put_u32(msg,
1466 NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1467 dev->wiphy.max_remain_on_channel_duration))
Johannes Berg2e161f72010-08-12 15:38:38 +02001468 goto nla_put_failure;
1469
Johannes Berg3713b4e2013-02-14 16:19:38 +01001470 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1471 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1472 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001473
Johannes Berg3713b4e2013-02-14 16:19:38 +01001474 if (nl80211_send_mgmt_stypes(msg, mgmt_stypes))
1475 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001476 state->split_start++;
1477 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001478 break;
1479 case 6:
Johannes Bergdfb89c52012-06-27 09:23:48 +02001480#ifdef CONFIG_PM
Johannes Berg86e8cf92013-06-19 10:57:22 +02001481 if (nl80211_send_wowlan(msg, dev, state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001482 goto nla_put_failure;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001483 state->split_start++;
1484 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001485 break;
1486#else
Johannes Berg86e8cf92013-06-19 10:57:22 +02001487 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001488#endif
1489 case 7:
1490 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1491 dev->wiphy.software_iftypes))
Johannes Bergff1b6e62011-05-04 15:37:28 +02001492 goto nla_put_failure;
1493
Johannes Berg86e8cf92013-06-19 10:57:22 +02001494 if (nl80211_put_iface_combinations(&dev->wiphy, msg,
1495 state->split))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001496 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001497
Johannes Berg86e8cf92013-06-19 10:57:22 +02001498 state->split_start++;
1499 if (state->split)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001500 break;
1501 case 8:
1502 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1503 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1504 dev->wiphy.ap_sme_capa))
1505 goto nla_put_failure;
1506
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001507 features = dev->wiphy.features;
1508 /*
1509 * We can only add the per-channel limit information if the
1510 * dump is split, otherwise it makes it too big. Therefore
1511 * only advertise it in that case.
1512 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001513 if (state->split)
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001514 features |= NL80211_FEATURE_ADVERTISE_CHAN_LIMITS;
1515 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS, features))
Johannes Berg3713b4e2013-02-14 16:19:38 +01001516 goto nla_put_failure;
1517
1518 if (dev->wiphy.ht_capa_mod_mask &&
1519 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1520 sizeof(*dev->wiphy.ht_capa_mod_mask),
1521 dev->wiphy.ht_capa_mod_mask))
1522 goto nla_put_failure;
1523
1524 if (dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME &&
1525 dev->wiphy.max_acl_mac_addrs &&
1526 nla_put_u32(msg, NL80211_ATTR_MAC_ACL_MAX,
1527 dev->wiphy.max_acl_mac_addrs))
1528 goto nla_put_failure;
1529
1530 /*
1531 * Any information below this point is only available to
1532 * applications that can deal with it being split. This
1533 * helps ensure that newly added capabilities don't break
1534 * older tools by overrunning their buffers.
1535 *
1536 * We still increment split_start so that in the split
1537 * case we'll continue with more data in the next round,
1538 * but break unconditionally so unsplit data stops here.
1539 */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001540 state->split_start++;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001541 break;
1542 case 9:
Johannes Bergfe1abaf2013-02-27 15:39:45 +01001543 if (dev->wiphy.extended_capabilities &&
1544 (nla_put(msg, NL80211_ATTR_EXT_CAPA,
1545 dev->wiphy.extended_capabilities_len,
1546 dev->wiphy.extended_capabilities) ||
1547 nla_put(msg, NL80211_ATTR_EXT_CAPA_MASK,
1548 dev->wiphy.extended_capabilities_len,
1549 dev->wiphy.extended_capabilities_mask)))
1550 goto nla_put_failure;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001551
Johannes Bergee2aca32013-02-21 17:36:01 +01001552 if (dev->wiphy.vht_capa_mod_mask &&
1553 nla_put(msg, NL80211_ATTR_VHT_CAPABILITY_MASK,
1554 sizeof(*dev->wiphy.vht_capa_mod_mask),
1555 dev->wiphy.vht_capa_mod_mask))
1556 goto nla_put_failure;
1557
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07001558 state->split_start++;
1559 break;
1560 case 10:
1561 if (nl80211_send_coalesce(msg, dev))
1562 goto nla_put_failure;
1563
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001564 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ) &&
1565 (nla_put_flag(msg, NL80211_ATTR_SUPPORT_5_MHZ) ||
1566 nla_put_flag(msg, NL80211_ATTR_SUPPORT_10_MHZ)))
1567 goto nla_put_failure;
Johannes Bergad7e7182013-11-13 13:37:47 +01001568 state->split_start++;
1569 break;
1570 case 11:
1571 nl_vendor_cmds = nla_nest_start(msg, NL80211_ATTR_VENDOR_DATA);
1572 if (!nl_vendor_cmds)
1573 goto nla_put_failure;
1574
1575 for (i = 0; i < dev->wiphy.n_vendor_commands; i++)
1576 if (nla_put(msg, i + 1,
1577 sizeof(struct nl80211_vendor_cmd_info),
1578 &dev->wiphy.vendor_commands[i].info))
1579 goto nla_put_failure;
1580 nla_nest_end(msg, nl_vendor_cmds);
Felix Fietkau01e0daa2013-11-09 14:57:54 +01001581
Johannes Berg3713b4e2013-02-14 16:19:38 +01001582 /* done */
Johannes Berg86e8cf92013-06-19 10:57:22 +02001583 state->split_start = 0;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001584 break;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001585 }
Johannes Berg55682962007-09-20 13:09:35 -04001586 return genlmsg_end(msg, hdr);
1587
1588 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001589 genlmsg_cancel(msg, hdr);
1590 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001591}
1592
Johannes Berg86e8cf92013-06-19 10:57:22 +02001593static int nl80211_dump_wiphy_parse(struct sk_buff *skb,
1594 struct netlink_callback *cb,
1595 struct nl80211_dump_wiphy_state *state)
1596{
1597 struct nlattr **tb = nl80211_fam.attrbuf;
1598 int ret = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
1599 tb, nl80211_fam.maxattr, nl80211_policy);
1600 /* ignore parse errors for backward compatibility */
1601 if (ret)
1602 return 0;
1603
1604 state->split = tb[NL80211_ATTR_SPLIT_WIPHY_DUMP];
1605 if (tb[NL80211_ATTR_WIPHY])
1606 state->filter_wiphy = nla_get_u32(tb[NL80211_ATTR_WIPHY]);
1607 if (tb[NL80211_ATTR_WDEV])
1608 state->filter_wiphy = nla_get_u64(tb[NL80211_ATTR_WDEV]) >> 32;
1609 if (tb[NL80211_ATTR_IFINDEX]) {
1610 struct net_device *netdev;
1611 struct cfg80211_registered_device *rdev;
1612 int ifidx = nla_get_u32(tb[NL80211_ATTR_IFINDEX]);
1613
1614 netdev = dev_get_by_index(sock_net(skb->sk), ifidx);
1615 if (!netdev)
1616 return -ENODEV;
1617 if (netdev->ieee80211_ptr) {
1618 rdev = wiphy_to_dev(
1619 netdev->ieee80211_ptr->wiphy);
1620 state->filter_wiphy = rdev->wiphy_idx;
1621 }
1622 dev_put(netdev);
1623 }
1624
1625 return 0;
1626}
1627
Johannes Berg55682962007-09-20 13:09:35 -04001628static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1629{
Johannes Berg645e77d2013-03-01 14:03:49 +01001630 int idx = 0, ret;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001631 struct nl80211_dump_wiphy_state *state = (void *)cb->args[0];
Johannes Berg55682962007-09-20 13:09:35 -04001632 struct cfg80211_registered_device *dev;
Johannes Berg3a5a4232013-06-19 10:09:57 +02001633
Johannes Berg5fe231e2013-05-08 21:45:15 +02001634 rtnl_lock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001635 if (!state) {
1636 state = kzalloc(sizeof(*state), GFP_KERNEL);
John W. Linville57ed5cd2013-06-28 13:18:21 -04001637 if (!state) {
1638 rtnl_unlock();
Johannes Berg86e8cf92013-06-19 10:57:22 +02001639 return -ENOMEM;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001640 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001641 state->filter_wiphy = -1;
1642 ret = nl80211_dump_wiphy_parse(skb, cb, state);
1643 if (ret) {
1644 kfree(state);
1645 rtnl_unlock();
1646 return ret;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001647 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001648 cb->args[0] = (long)state;
Johannes Berg3713b4e2013-02-14 16:19:38 +01001649 }
1650
Johannes Berg79c97e92009-07-07 03:56:12 +02001651 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001652 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1653 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001654 if (++idx <= state->start)
Johannes Berg55682962007-09-20 13:09:35 -04001655 continue;
Johannes Berg86e8cf92013-06-19 10:57:22 +02001656 if (state->filter_wiphy != -1 &&
1657 state->filter_wiphy != dev->wiphy_idx)
Johannes Berg3713b4e2013-02-14 16:19:38 +01001658 continue;
1659 /* attempt to fit multiple wiphy data chunks into the skb */
1660 do {
1661 ret = nl80211_send_wiphy(dev, skb,
1662 NETLINK_CB(cb->skb).portid,
1663 cb->nlh->nlmsg_seq,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001664 NLM_F_MULTI, state);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001665 if (ret < 0) {
1666 /*
1667 * If sending the wiphy data didn't fit (ENOBUFS
1668 * or EMSGSIZE returned), this SKB is still
1669 * empty (so it's not too big because another
1670 * wiphy dataset is already in the skb) and
1671 * we've not tried to adjust the dump allocation
1672 * yet ... then adjust the alloc size to be
1673 * bigger, and return 1 but with the empty skb.
1674 * This results in an empty message being RX'ed
1675 * in userspace, but that is ignored.
1676 *
1677 * We can then retry with the larger buffer.
1678 */
1679 if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
1680 !skb->len &&
1681 cb->min_dump_alloc < 4096) {
1682 cb->min_dump_alloc = 4096;
David S. Millerd98cae64e2013-06-19 16:49:39 -07001683 rtnl_unlock();
Johannes Berg3713b4e2013-02-14 16:19:38 +01001684 return 1;
1685 }
1686 idx--;
1687 break;
Johannes Berg645e77d2013-03-01 14:03:49 +01001688 }
Johannes Berg86e8cf92013-06-19 10:57:22 +02001689 } while (state->split_start > 0);
Johannes Berg3713b4e2013-02-14 16:19:38 +01001690 break;
Johannes Berg55682962007-09-20 13:09:35 -04001691 }
Johannes Berg5fe231e2013-05-08 21:45:15 +02001692 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04001693
Johannes Berg86e8cf92013-06-19 10:57:22 +02001694 state->start = idx;
Johannes Berg55682962007-09-20 13:09:35 -04001695
1696 return skb->len;
1697}
1698
Johannes Berg86e8cf92013-06-19 10:57:22 +02001699static int nl80211_dump_wiphy_done(struct netlink_callback *cb)
1700{
1701 kfree((void *)cb->args[0]);
1702 return 0;
1703}
1704
Johannes Berg55682962007-09-20 13:09:35 -04001705static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1706{
1707 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001708 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg86e8cf92013-06-19 10:57:22 +02001709 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04001710
Johannes Berg645e77d2013-03-01 14:03:49 +01001711 msg = nlmsg_new(4096, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001712 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001713 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001714
Johannes Berg3713b4e2013-02-14 16:19:38 +01001715 if (nl80211_send_wiphy(dev, msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg86e8cf92013-06-19 10:57:22 +02001716 &state) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001717 nlmsg_free(msg);
1718 return -ENOBUFS;
1719 }
Johannes Berg55682962007-09-20 13:09:35 -04001720
Johannes Berg134e6372009-07-10 09:51:34 +00001721 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001722}
1723
Jouni Malinen31888482008-10-30 16:59:24 +02001724static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1725 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1726 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1727 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1728 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1729 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1730};
1731
1732static int parse_txq_params(struct nlattr *tb[],
1733 struct ieee80211_txq_params *txq_params)
1734{
Johannes Berga3304b02012-03-28 11:04:24 +02001735 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001736 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1737 !tb[NL80211_TXQ_ATTR_AIFS])
1738 return -EINVAL;
1739
Johannes Berga3304b02012-03-28 11:04:24 +02001740 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001741 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1742 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1743 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1744 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1745
Johannes Berga3304b02012-03-28 11:04:24 +02001746 if (txq_params->ac >= NL80211_NUM_ACS)
1747 return -EINVAL;
1748
Jouni Malinen31888482008-10-30 16:59:24 +02001749 return 0;
1750}
1751
Johannes Bergf444de02010-05-05 15:25:02 +02001752static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1753{
1754 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001755 * You can only set the channel explicitly for WDS interfaces,
1756 * all others have their channel managed via their respective
1757 * "establish a connection" command (connect, join, ...)
1758 *
1759 * For AP/GO and mesh mode, the channel can be set with the
1760 * channel userspace API, but is only stored and passed to the
1761 * low-level driver when the AP starts or the mesh is joined.
1762 * This is for backward compatibility, userspace can also give
1763 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001764 *
1765 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001766 * whatever else is going on, so they have their own special
1767 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001768 */
1769 return !wdev ||
1770 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001771 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001772 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1773 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001774}
1775
Johannes Berg683b6d32012-11-08 21:25:48 +01001776static int nl80211_parse_chandef(struct cfg80211_registered_device *rdev,
1777 struct genl_info *info,
1778 struct cfg80211_chan_def *chandef)
1779{
Mahesh Paliveladbeca2e2012-11-29 14:11:07 +05301780 u32 control_freq;
Johannes Berg683b6d32012-11-08 21:25:48 +01001781
1782 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1783 return -EINVAL;
1784
1785 control_freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1786
1787 chandef->chan = ieee80211_get_channel(&rdev->wiphy, control_freq);
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001788 chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
1789 chandef->center_freq1 = control_freq;
1790 chandef->center_freq2 = 0;
Johannes Berg683b6d32012-11-08 21:25:48 +01001791
1792 /* Primary channel not allowed */
1793 if (!chandef->chan || chandef->chan->flags & IEEE80211_CHAN_DISABLED)
1794 return -EINVAL;
1795
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001796 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1797 enum nl80211_channel_type chantype;
Johannes Berg683b6d32012-11-08 21:25:48 +01001798
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001799 chantype = nla_get_u32(
1800 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1801
1802 switch (chantype) {
1803 case NL80211_CHAN_NO_HT:
1804 case NL80211_CHAN_HT20:
1805 case NL80211_CHAN_HT40PLUS:
1806 case NL80211_CHAN_HT40MINUS:
1807 cfg80211_chandef_create(chandef, chandef->chan,
1808 chantype);
1809 break;
1810 default:
Johannes Berg683b6d32012-11-08 21:25:48 +01001811 return -EINVAL;
Johannes Berg683b6d32012-11-08 21:25:48 +01001812 }
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001813 } else if (info->attrs[NL80211_ATTR_CHANNEL_WIDTH]) {
1814 chandef->width =
1815 nla_get_u32(info->attrs[NL80211_ATTR_CHANNEL_WIDTH]);
1816 if (info->attrs[NL80211_ATTR_CENTER_FREQ1])
1817 chandef->center_freq1 =
1818 nla_get_u32(
1819 info->attrs[NL80211_ATTR_CENTER_FREQ1]);
1820 if (info->attrs[NL80211_ATTR_CENTER_FREQ2])
1821 chandef->center_freq2 =
1822 nla_get_u32(
1823 info->attrs[NL80211_ATTR_CENTER_FREQ2]);
1824 }
1825
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001826 if (!cfg80211_chandef_valid(chandef))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001827 return -EINVAL;
1828
Johannes Berg9f5e8f62012-11-22 16:59:45 +01001829 if (!cfg80211_chandef_usable(&rdev->wiphy, chandef,
1830 IEEE80211_CHAN_DISABLED))
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001831 return -EINVAL;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01001832
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02001833 if ((chandef->width == NL80211_CHAN_WIDTH_5 ||
1834 chandef->width == NL80211_CHAN_WIDTH_10) &&
1835 !(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_5_10_MHZ))
1836 return -EINVAL;
1837
Johannes Berg683b6d32012-11-08 21:25:48 +01001838 return 0;
1839}
1840
Johannes Bergf444de02010-05-05 15:25:02 +02001841static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1842 struct wireless_dev *wdev,
1843 struct genl_info *info)
1844{
Johannes Berg683b6d32012-11-08 21:25:48 +01001845 struct cfg80211_chan_def chandef;
Johannes Bergf444de02010-05-05 15:25:02 +02001846 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001847 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1848
1849 if (wdev)
1850 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001851
Johannes Bergf444de02010-05-05 15:25:02 +02001852 if (!nl80211_can_set_dev_channel(wdev))
1853 return -EOPNOTSUPP;
1854
Johannes Berg683b6d32012-11-08 21:25:48 +01001855 result = nl80211_parse_chandef(rdev, info, &chandef);
1856 if (result)
1857 return result;
Johannes Bergf444de02010-05-05 15:25:02 +02001858
Johannes Berge8c9bd52012-06-06 08:18:22 +02001859 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001860 case NL80211_IFTYPE_AP:
1861 case NL80211_IFTYPE_P2P_GO:
1862 if (wdev->beacon_interval) {
1863 result = -EBUSY;
1864 break;
1865 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001866 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &chandef)) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001867 result = -EINVAL;
1868 break;
1869 }
Johannes Berg683b6d32012-11-08 21:25:48 +01001870 wdev->preset_chandef = chandef;
Johannes Bergaa430da2012-05-16 23:50:18 +02001871 result = 0;
1872 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001873 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg683b6d32012-11-08 21:25:48 +01001874 result = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001875 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001876 case NL80211_IFTYPE_MONITOR:
Johannes Berg683b6d32012-11-08 21:25:48 +01001877 result = cfg80211_set_monitor_channel(rdev, &chandef);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001878 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001879 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001880 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001881 }
Johannes Bergf444de02010-05-05 15:25:02 +02001882
1883 return result;
1884}
1885
1886static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1887{
Johannes Berg4c476992010-10-04 21:36:35 +02001888 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1889 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001890
Johannes Berg4c476992010-10-04 21:36:35 +02001891 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001892}
1893
Bill Jordane8347eb2010-10-01 13:54:28 -04001894static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1895{
Johannes Berg43b19952010-10-07 13:10:30 +02001896 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1897 struct net_device *dev = info->user_ptr[1];
1898 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001899 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001900
1901 if (!info->attrs[NL80211_ATTR_MAC])
1902 return -EINVAL;
1903
Johannes Berg43b19952010-10-07 13:10:30 +02001904 if (netif_running(dev))
1905 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001906
Johannes Berg43b19952010-10-07 13:10:30 +02001907 if (!rdev->ops->set_wds_peer)
1908 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001909
Johannes Berg43b19952010-10-07 13:10:30 +02001910 if (wdev->iftype != NL80211_IFTYPE_WDS)
1911 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001912
1913 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Hila Gonene35e4d22012-06-27 17:19:42 +03001914 return rdev_set_wds_peer(rdev, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001915}
1916
1917
Johannes Berg55682962007-09-20 13:09:35 -04001918static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1919{
1920 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001921 struct net_device *netdev = NULL;
1922 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001923 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001924 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001925 u32 changed;
1926 u8 retry_short = 0, retry_long = 0;
1927 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001928 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001929
Johannes Berg5fe231e2013-05-08 21:45:15 +02001930 ASSERT_RTNL();
1931
Johannes Bergf444de02010-05-05 15:25:02 +02001932 /*
1933 * Try to find the wiphy and netdev. Normally this
1934 * function shouldn't need the netdev, but this is
1935 * done for backward compatibility -- previously
1936 * setting the channel was done per wiphy, but now
1937 * it is per netdev. Previous userland like hostapd
1938 * also passed a netdev to set_wiphy, so that it is
1939 * possible to let that go to the right netdev!
1940 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001941
Johannes Bergf444de02010-05-05 15:25:02 +02001942 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1943 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1944
1945 netdev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001946 if (netdev && netdev->ieee80211_ptr)
Johannes Bergf444de02010-05-05 15:25:02 +02001947 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001948 else
Johannes Bergf444de02010-05-05 15:25:02 +02001949 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001950 }
1951
Johannes Bergf444de02010-05-05 15:25:02 +02001952 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001953 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1954 info->attrs);
Johannes Berg5fe231e2013-05-08 21:45:15 +02001955 if (IS_ERR(rdev))
Johannes Berg4c476992010-10-04 21:36:35 +02001956 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001957 wdev = NULL;
1958 netdev = NULL;
1959 result = 0;
Johannes Berg71fe96b2012-10-24 10:04:58 +02001960 } else
Johannes Bergf444de02010-05-05 15:25:02 +02001961 wdev = netdev->ieee80211_ptr;
Johannes Bergf444de02010-05-05 15:25:02 +02001962
1963 /*
1964 * end workaround code, by now the rdev is available
1965 * and locked, and wdev may or may not be NULL.
1966 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001967
1968 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001969 result = cfg80211_dev_rename(
1970 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001971
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001972 if (result)
1973 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001974
Jouni Malinen31888482008-10-30 16:59:24 +02001975 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1976 struct ieee80211_txq_params txq_params;
1977 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1978
1979 if (!rdev->ops->set_txq_params) {
1980 result = -EOPNOTSUPP;
1981 goto bad_res;
1982 }
1983
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001984 if (!netdev) {
1985 result = -EINVAL;
1986 goto bad_res;
1987 }
1988
Johannes Berg133a3ff2011-11-03 14:50:13 +01001989 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1990 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1991 result = -EINVAL;
1992 goto bad_res;
1993 }
1994
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001995 if (!netif_running(netdev)) {
1996 result = -ENETDOWN;
1997 goto bad_res;
1998 }
1999
Jouni Malinen31888482008-10-30 16:59:24 +02002000 nla_for_each_nested(nl_txq_params,
2001 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
2002 rem_txq_params) {
2003 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
2004 nla_data(nl_txq_params),
2005 nla_len(nl_txq_params),
2006 txq_params_policy);
2007 result = parse_txq_params(tb, &txq_params);
2008 if (result)
2009 goto bad_res;
2010
Hila Gonene35e4d22012-06-27 17:19:42 +03002011 result = rdev_set_txq_params(rdev, netdev,
2012 &txq_params);
Jouni Malinen31888482008-10-30 16:59:24 +02002013 if (result)
2014 goto bad_res;
2015 }
2016 }
2017
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002018 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg71fe96b2012-10-24 10:04:58 +02002019 result = __nl80211_set_channel(rdev,
2020 nl80211_can_set_dev_channel(wdev) ? wdev : NULL,
2021 info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002022 if (result)
2023 goto bad_res;
2024 }
2025
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002026 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
Johannes Bergc8442112012-10-24 10:17:18 +02002027 struct wireless_dev *txp_wdev = wdev;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002028 enum nl80211_tx_power_setting type;
2029 int idx, mbm = 0;
2030
Johannes Bergc8442112012-10-24 10:17:18 +02002031 if (!(rdev->wiphy.features & NL80211_FEATURE_VIF_TXPOWER))
2032 txp_wdev = NULL;
2033
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002034 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02002035 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002036 goto bad_res;
2037 }
2038
2039 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
2040 type = nla_get_u32(info->attrs[idx]);
2041
2042 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
2043 (type != NL80211_TX_POWER_AUTOMATIC)) {
2044 result = -EINVAL;
2045 goto bad_res;
2046 }
2047
2048 if (type != NL80211_TX_POWER_AUTOMATIC) {
2049 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
2050 mbm = nla_get_u32(info->attrs[idx]);
2051 }
2052
Johannes Bergc8442112012-10-24 10:17:18 +02002053 result = rdev_set_tx_power(rdev, txp_wdev, type, mbm);
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03002054 if (result)
2055 goto bad_res;
2056 }
2057
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002058 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
2059 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
2060 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09002061 if ((!rdev->wiphy.available_antennas_tx &&
2062 !rdev->wiphy.available_antennas_rx) ||
2063 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002064 result = -EOPNOTSUPP;
2065 goto bad_res;
2066 }
2067
2068 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
2069 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
2070
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002071 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09002072 * available antenna masks, except for the "all" mask */
2073 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
2074 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002075 result = -EINVAL;
2076 goto bad_res;
2077 }
2078
Bruno Randolf7f531e02010-12-16 11:30:22 +09002079 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
2080 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09002081
Hila Gonene35e4d22012-06-27 17:19:42 +03002082 result = rdev_set_antenna(rdev, tx_ant, rx_ant);
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09002083 if (result)
2084 goto bad_res;
2085 }
2086
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002087 changed = 0;
2088
2089 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
2090 retry_short = nla_get_u8(
2091 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
2092 if (retry_short == 0) {
2093 result = -EINVAL;
2094 goto bad_res;
2095 }
2096 changed |= WIPHY_PARAM_RETRY_SHORT;
2097 }
2098
2099 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
2100 retry_long = nla_get_u8(
2101 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
2102 if (retry_long == 0) {
2103 result = -EINVAL;
2104 goto bad_res;
2105 }
2106 changed |= WIPHY_PARAM_RETRY_LONG;
2107 }
2108
2109 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
2110 frag_threshold = nla_get_u32(
2111 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
2112 if (frag_threshold < 256) {
2113 result = -EINVAL;
2114 goto bad_res;
2115 }
2116 if (frag_threshold != (u32) -1) {
2117 /*
2118 * Fragments (apart from the last one) are required to
2119 * have even length. Make the fragmentation code
2120 * simpler by stripping LSB should someone try to use
2121 * odd threshold value.
2122 */
2123 frag_threshold &= ~0x1;
2124 }
2125 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
2126 }
2127
2128 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
2129 rts_threshold = nla_get_u32(
2130 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
2131 changed |= WIPHY_PARAM_RTS_THRESHOLD;
2132 }
2133
Lukáš Turek81077e82009-12-21 22:50:47 +01002134 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
2135 coverage_class = nla_get_u8(
2136 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
2137 changed |= WIPHY_PARAM_COVERAGE_CLASS;
2138 }
2139
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002140 if (changed) {
2141 u8 old_retry_short, old_retry_long;
2142 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002143 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002144
2145 if (!rdev->ops->set_wiphy_params) {
2146 result = -EOPNOTSUPP;
2147 goto bad_res;
2148 }
2149
2150 old_retry_short = rdev->wiphy.retry_short;
2151 old_retry_long = rdev->wiphy.retry_long;
2152 old_frag_threshold = rdev->wiphy.frag_threshold;
2153 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002154 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002155
2156 if (changed & WIPHY_PARAM_RETRY_SHORT)
2157 rdev->wiphy.retry_short = retry_short;
2158 if (changed & WIPHY_PARAM_RETRY_LONG)
2159 rdev->wiphy.retry_long = retry_long;
2160 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
2161 rdev->wiphy.frag_threshold = frag_threshold;
2162 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
2163 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002164 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
2165 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002166
Hila Gonene35e4d22012-06-27 17:19:42 +03002167 result = rdev_set_wiphy_params(rdev, changed);
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002168 if (result) {
2169 rdev->wiphy.retry_short = old_retry_short;
2170 rdev->wiphy.retry_long = old_retry_long;
2171 rdev->wiphy.frag_threshold = old_frag_threshold;
2172 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01002173 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02002174 }
2175 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02002176
Johannes Berg306d6112008-12-08 12:39:04 +01002177 bad_res:
Johannes Bergf444de02010-05-05 15:25:02 +02002178 if (netdev)
2179 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04002180 return result;
2181}
2182
Johannes Berg71bbc992012-06-15 15:30:18 +02002183static inline u64 wdev_id(struct wireless_dev *wdev)
2184{
2185 return (u64)wdev->identifier |
2186 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
2187}
Johannes Berg55682962007-09-20 13:09:35 -04002188
Johannes Berg683b6d32012-11-08 21:25:48 +01002189static int nl80211_send_chandef(struct sk_buff *msg,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +01002190 const struct cfg80211_chan_def *chandef)
Johannes Berg683b6d32012-11-08 21:25:48 +01002191{
Johannes Berg9f5e8f62012-11-22 16:59:45 +01002192 WARN_ON(!cfg80211_chandef_valid(chandef));
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002193
Johannes Berg683b6d32012-11-08 21:25:48 +01002194 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
2195 chandef->chan->center_freq))
2196 return -ENOBUFS;
Johannes Berg3d9d1d62012-11-08 23:14:50 +01002197 switch (chandef->width) {
2198 case NL80211_CHAN_WIDTH_20_NOHT:
2199 case NL80211_CHAN_WIDTH_20:
2200 case NL80211_CHAN_WIDTH_40:
2201 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
2202 cfg80211_get_chandef_type(chandef)))
2203 return -ENOBUFS;
2204 break;
2205 default:
2206 break;
2207 }
2208 if (nla_put_u32(msg, NL80211_ATTR_CHANNEL_WIDTH, chandef->width))
2209 return -ENOBUFS;
2210 if (nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ1, chandef->center_freq1))
2211 return -ENOBUFS;
2212 if (chandef->center_freq2 &&
2213 nla_put_u32(msg, NL80211_ATTR_CENTER_FREQ2, chandef->center_freq2))
Johannes Berg683b6d32012-11-08 21:25:48 +01002214 return -ENOBUFS;
2215 return 0;
2216}
2217
Eric W. Biederman15e47302012-09-07 20:12:54 +00002218static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02002219 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002220 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04002221{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002222 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04002223 void *hdr;
2224
Eric W. Biederman15e47302012-09-07 20:12:54 +00002225 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04002226 if (!hdr)
2227 return -1;
2228
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002229 if (dev &&
2230 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002231 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002232 goto nla_put_failure;
2233
2234 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
2235 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02002236 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02002237 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002238 nla_put_u32(msg, NL80211_ATTR_GENERATION,
2239 rdev->devlist_generation ^
2240 (cfg80211_rdev_list_generation << 2)))
2241 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002242
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002243 if (rdev->ops->get_channel) {
Johannes Berg683b6d32012-11-08 21:25:48 +01002244 int ret;
2245 struct cfg80211_chan_def chandef;
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02002246
Johannes Berg683b6d32012-11-08 21:25:48 +01002247 ret = rdev_get_channel(rdev, wdev, &chandef);
2248 if (ret == 0) {
2249 if (nl80211_send_chandef(msg, &chandef))
2250 goto nla_put_failure;
2251 }
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02002252 }
2253
Antonio Quartullib84e7a02012-11-07 12:52:20 +01002254 if (wdev->ssid_len) {
2255 if (nla_put(msg, NL80211_ATTR_SSID, wdev->ssid_len, wdev->ssid))
2256 goto nla_put_failure;
2257 }
2258
Johannes Berg55682962007-09-20 13:09:35 -04002259 return genlmsg_end(msg, hdr);
2260
2261 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002262 genlmsg_cancel(msg, hdr);
2263 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04002264}
2265
2266static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
2267{
2268 int wp_idx = 0;
2269 int if_idx = 0;
2270 int wp_start = cb->args[0];
2271 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02002272 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04002273 struct wireless_dev *wdev;
2274
Johannes Berg5fe231e2013-05-08 21:45:15 +02002275 rtnl_lock();
Johannes Bergf5ea9122009-08-07 16:17:38 +02002276 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
2277 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02002278 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002279 if (wp_idx < wp_start) {
2280 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002281 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002282 }
Johannes Berg55682962007-09-20 13:09:35 -04002283 if_idx = 0;
2284
Johannes Berg89a54e42012-06-15 14:33:17 +02002285 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002286 if (if_idx < if_start) {
2287 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002288 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002289 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002290 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04002291 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002292 rdev, wdev) < 0) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02002293 goto out;
2294 }
2295 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002296 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002297
2298 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04002299 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02002300 out:
Johannes Berg5fe231e2013-05-08 21:45:15 +02002301 rtnl_unlock();
Johannes Berg55682962007-09-20 13:09:35 -04002302
2303 cb->args[0] = wp_idx;
2304 cb->args[1] = if_idx;
2305
2306 return skb->len;
2307}
2308
2309static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
2310{
2311 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02002312 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002313 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002314
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002315 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04002316 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002317 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04002318
Eric W. Biederman15e47302012-09-07 20:12:54 +00002319 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02002320 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02002321 nlmsg_free(msg);
2322 return -ENOBUFS;
2323 }
Johannes Berg55682962007-09-20 13:09:35 -04002324
Johannes Berg134e6372009-07-10 09:51:34 +00002325 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002326}
2327
Michael Wu66f7ac52008-01-31 19:48:22 +01002328static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
2329 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
2330 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
2331 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
2332 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
2333 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002334 [NL80211_MNTR_FLAG_ACTIVE] = { .type = NLA_FLAG },
Michael Wu66f7ac52008-01-31 19:48:22 +01002335};
2336
2337static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
2338{
2339 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
2340 int flag;
2341
2342 *mntrflags = 0;
2343
2344 if (!nla)
2345 return -EINVAL;
2346
2347 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
2348 nla, mntr_flags_policy))
2349 return -EINVAL;
2350
2351 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
2352 if (flags[flag])
2353 *mntrflags |= (1<<flag);
2354
2355 return 0;
2356}
2357
Johannes Berg9bc383d2009-11-19 11:55:19 +01002358static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002359 struct net_device *netdev, u8 use_4addr,
2360 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01002361{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002362 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00002363 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002364 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002365 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002366 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01002367
2368 switch (iftype) {
2369 case NL80211_IFTYPE_AP_VLAN:
2370 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
2371 return 0;
2372 break;
2373 case NL80211_IFTYPE_STATION:
2374 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
2375 return 0;
2376 break;
2377 default:
2378 break;
2379 }
2380
2381 return -EOPNOTSUPP;
2382}
2383
Johannes Berg55682962007-09-20 13:09:35 -04002384static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
2385{
Johannes Berg4c476992010-10-04 21:36:35 +02002386 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002387 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02002388 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02002389 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02002390 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02002391 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002392 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04002393
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002394 memset(&params, 0, sizeof(params));
2395
Johannes Berg04a773a2009-04-19 21:24:32 +02002396 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04002397
Johannes Berg723b0382008-09-16 20:22:09 +02002398 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002399 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02002400 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002401 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02002402 if (ntype > NL80211_IFTYPE_MAX)
2403 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02002404 }
2405
Johannes Berg92ffe052008-09-16 20:39:36 +02002406 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01002407 struct wireless_dev *wdev = dev->ieee80211_ptr;
2408
Johannes Berg4c476992010-10-04 21:36:35 +02002409 if (ntype != NL80211_IFTYPE_MESH_POINT)
2410 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01002411 if (netif_running(dev))
2412 return -EBUSY;
2413
2414 wdev_lock(wdev);
2415 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2416 IEEE80211_MAX_MESH_ID_LEN);
2417 wdev->mesh_id_up_len =
2418 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2419 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2420 wdev->mesh_id_up_len);
2421 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002422 }
2423
Felix Fietkau8b787642009-11-10 18:53:10 +01002424 if (info->attrs[NL80211_ATTR_4ADDR]) {
2425 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
2426 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002427 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002428 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002429 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01002430 } else {
2431 params.use_4addr = -1;
2432 }
2433
Johannes Berg92ffe052008-09-16 20:39:36 +02002434 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02002435 if (ntype != NL80211_IFTYPE_MONITOR)
2436 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02002437 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
2438 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002439 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002440 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002441
2442 flags = &_flags;
2443 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02002444 }
Johannes Berg3b858752009-03-12 09:55:09 +01002445
Luciano Coelho18003292013-08-29 13:26:57 +03002446 if (flags && (*flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002447 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2448 return -EOPNOTSUPP;
2449
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002450 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02002451 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01002452 else
2453 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02002454
Johannes Berg9bc383d2009-11-19 11:55:19 +01002455 if (!err && params.use_4addr != -1)
2456 dev->ieee80211_ptr->use_4addr = params.use_4addr;
2457
Johannes Berg55682962007-09-20 13:09:35 -04002458 return err;
2459}
2460
2461static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2462{
Johannes Berg4c476992010-10-04 21:36:35 +02002463 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002464 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02002465 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002466 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04002467 int err;
2468 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01002469 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04002470
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002471 memset(&params, 0, sizeof(params));
2472
Johannes Berg55682962007-09-20 13:09:35 -04002473 if (!info->attrs[NL80211_ATTR_IFNAME])
2474 return -EINVAL;
2475
2476 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2477 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2478 if (type > NL80211_IFTYPE_MAX)
2479 return -EINVAL;
2480 }
2481
Johannes Berg79c97e92009-07-07 03:56:12 +02002482 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002483 !(rdev->wiphy.interface_modes & (1 << type)))
2484 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002485
Arend van Spriel1c18f142013-01-08 10:17:27 +01002486 if (type == NL80211_IFTYPE_P2P_DEVICE && info->attrs[NL80211_ATTR_MAC]) {
2487 nla_memcpy(params.macaddr, info->attrs[NL80211_ATTR_MAC],
2488 ETH_ALEN);
2489 if (!is_valid_ether_addr(params.macaddr))
2490 return -EADDRNOTAVAIL;
2491 }
2492
Johannes Berg9bc383d2009-11-19 11:55:19 +01002493 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002494 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002495 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002496 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002497 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002498 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002499
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002500 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2501 if (!msg)
2502 return -ENOMEM;
2503
Michael Wu66f7ac52008-01-31 19:48:22 +01002504 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2505 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2506 &flags);
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002507
Luciano Coelho18003292013-08-29 13:26:57 +03002508 if (!err && (flags & MONITOR_FLAG_ACTIVE) &&
Felix Fietkaue057d3c2013-05-28 13:01:52 +02002509 !(rdev->wiphy.features & NL80211_FEATURE_ACTIVE_MONITOR))
2510 return -EOPNOTSUPP;
2511
Hila Gonene35e4d22012-06-27 17:19:42 +03002512 wdev = rdev_add_virtual_intf(rdev,
2513 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2514 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002515 if (IS_ERR(wdev)) {
2516 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002517 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002518 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002519
Johannes Berg98104fde2012-06-16 00:19:54 +02002520 switch (type) {
2521 case NL80211_IFTYPE_MESH_POINT:
2522 if (!info->attrs[NL80211_ATTR_MESH_ID])
2523 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002524 wdev_lock(wdev);
2525 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2526 IEEE80211_MAX_MESH_ID_LEN);
2527 wdev->mesh_id_up_len =
2528 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2529 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2530 wdev->mesh_id_up_len);
2531 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002532 break;
2533 case NL80211_IFTYPE_P2P_DEVICE:
2534 /*
2535 * P2P Device doesn't have a netdev, so doesn't go
2536 * through the netdev notifier and must be added here
2537 */
2538 mutex_init(&wdev->mtx);
2539 INIT_LIST_HEAD(&wdev->event_list);
2540 spin_lock_init(&wdev->event_lock);
2541 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2542 spin_lock_init(&wdev->mgmt_registrations_lock);
2543
Johannes Berg98104fde2012-06-16 00:19:54 +02002544 wdev->identifier = ++rdev->wdev_id;
2545 list_add_rcu(&wdev->list, &rdev->wdev_list);
2546 rdev->devlist_generation++;
Johannes Berg98104fde2012-06-16 00:19:54 +02002547 break;
2548 default:
2549 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002550 }
2551
Eric W. Biederman15e47302012-09-07 20:12:54 +00002552 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002553 rdev, wdev) < 0) {
2554 nlmsg_free(msg);
2555 return -ENOBUFS;
2556 }
2557
2558 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002559}
2560
2561static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2562{
Johannes Berg4c476992010-10-04 21:36:35 +02002563 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002564 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002565
Johannes Berg4c476992010-10-04 21:36:35 +02002566 if (!rdev->ops->del_virtual_intf)
2567 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002568
Johannes Berg84efbb82012-06-16 00:00:26 +02002569 /*
2570 * If we remove a wireless device without a netdev then clear
2571 * user_ptr[1] so that nl80211_post_doit won't dereference it
2572 * to check if it needs to do dev_put(). Otherwise it crashes
2573 * since the wdev has been freed, unlike with a netdev where
2574 * we need the dev_put() for the netdev to really be freed.
2575 */
2576 if (!wdev->netdev)
2577 info->user_ptr[1] = NULL;
2578
Hila Gonene35e4d22012-06-27 17:19:42 +03002579 return rdev_del_virtual_intf(rdev, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002580}
2581
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002582static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2583{
2584 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2585 struct net_device *dev = info->user_ptr[1];
2586 u16 noack_map;
2587
2588 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2589 return -EINVAL;
2590
2591 if (!rdev->ops->set_noack_map)
2592 return -EOPNOTSUPP;
2593
2594 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2595
Hila Gonene35e4d22012-06-27 17:19:42 +03002596 return rdev_set_noack_map(rdev, dev, noack_map);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002597}
2598
Johannes Berg41ade002007-12-19 02:03:29 +01002599struct get_key_cookie {
2600 struct sk_buff *msg;
2601 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002602 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002603};
2604
2605static void get_key_callback(void *c, struct key_params *params)
2606{
Johannes Bergb9454e82009-07-08 13:29:08 +02002607 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002608 struct get_key_cookie *cookie = c;
2609
David S. Miller9360ffd2012-03-29 04:41:26 -04002610 if ((params->key &&
2611 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2612 params->key_len, params->key)) ||
2613 (params->seq &&
2614 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2615 params->seq_len, params->seq)) ||
2616 (params->cipher &&
2617 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2618 params->cipher)))
2619 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002620
Johannes Bergb9454e82009-07-08 13:29:08 +02002621 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2622 if (!key)
2623 goto nla_put_failure;
2624
David S. Miller9360ffd2012-03-29 04:41:26 -04002625 if ((params->key &&
2626 nla_put(cookie->msg, NL80211_KEY_DATA,
2627 params->key_len, params->key)) ||
2628 (params->seq &&
2629 nla_put(cookie->msg, NL80211_KEY_SEQ,
2630 params->seq_len, params->seq)) ||
2631 (params->cipher &&
2632 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2633 params->cipher)))
2634 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002635
David S. Miller9360ffd2012-03-29 04:41:26 -04002636 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2637 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002638
2639 nla_nest_end(cookie->msg, key);
2640
Johannes Berg41ade002007-12-19 02:03:29 +01002641 return;
2642 nla_put_failure:
2643 cookie->error = 1;
2644}
2645
2646static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2647{
Johannes Berg4c476992010-10-04 21:36:35 +02002648 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002649 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002650 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002651 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002652 const u8 *mac_addr = NULL;
2653 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002654 struct get_key_cookie cookie = {
2655 .error = 0,
2656 };
2657 void *hdr;
2658 struct sk_buff *msg;
2659
2660 if (info->attrs[NL80211_ATTR_KEY_IDX])
2661 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2662
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002663 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002664 return -EINVAL;
2665
2666 if (info->attrs[NL80211_ATTR_MAC])
2667 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2668
Johannes Berge31b8212010-10-05 19:39:30 +02002669 pairwise = !!mac_addr;
2670 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2671 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2672 if (kt >= NUM_NL80211_KEYTYPES)
2673 return -EINVAL;
2674 if (kt != NL80211_KEYTYPE_GROUP &&
2675 kt != NL80211_KEYTYPE_PAIRWISE)
2676 return -EINVAL;
2677 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2678 }
2679
Johannes Berg4c476992010-10-04 21:36:35 +02002680 if (!rdev->ops->get_key)
2681 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002682
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002683 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002684 if (!msg)
2685 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002686
Eric W. Biederman15e47302012-09-07 20:12:54 +00002687 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002688 NL80211_CMD_NEW_KEY);
Dan Carpentercb35fba2013-08-14 14:50:01 +03002689 if (!hdr)
2690 return -ENOBUFS;
Johannes Berg41ade002007-12-19 02:03:29 +01002691
2692 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002693 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002694
David S. Miller9360ffd2012-03-29 04:41:26 -04002695 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2696 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2697 goto nla_put_failure;
2698 if (mac_addr &&
2699 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2700 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002701
Johannes Berge31b8212010-10-05 19:39:30 +02002702 if (pairwise && mac_addr &&
2703 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2704 return -ENOENT;
2705
Hila Gonene35e4d22012-06-27 17:19:42 +03002706 err = rdev_get_key(rdev, dev, key_idx, pairwise, mac_addr, &cookie,
2707 get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002708
2709 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002710 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002711
2712 if (cookie.error)
2713 goto nla_put_failure;
2714
2715 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002716 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002717
2718 nla_put_failure:
2719 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002720 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002721 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002722 return err;
2723}
2724
2725static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2726{
Johannes Berg4c476992010-10-04 21:36:35 +02002727 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002728 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002729 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002730 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002731
Johannes Bergb9454e82009-07-08 13:29:08 +02002732 err = nl80211_parse_key(info, &key);
2733 if (err)
2734 return err;
2735
2736 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002737 return -EINVAL;
2738
Johannes Bergb9454e82009-07-08 13:29:08 +02002739 /* only support setting default key */
2740 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002741 return -EINVAL;
2742
Johannes Bergfffd0932009-07-08 14:22:54 +02002743 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002744
2745 if (key.def) {
2746 if (!rdev->ops->set_default_key) {
2747 err = -EOPNOTSUPP;
2748 goto out;
2749 }
2750
2751 err = nl80211_key_allowed(dev->ieee80211_ptr);
2752 if (err)
2753 goto out;
2754
Hila Gonene35e4d22012-06-27 17:19:42 +03002755 err = rdev_set_default_key(rdev, dev, key.idx,
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002756 key.def_uni, key.def_multi);
2757
2758 if (err)
2759 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002760
Johannes Berg3d23e342009-09-29 23:27:28 +02002761#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002762 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002763#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002764 } else {
2765 if (key.def_uni || !key.def_multi) {
2766 err = -EINVAL;
2767 goto out;
2768 }
2769
2770 if (!rdev->ops->set_default_mgmt_key) {
2771 err = -EOPNOTSUPP;
2772 goto out;
2773 }
2774
2775 err = nl80211_key_allowed(dev->ieee80211_ptr);
2776 if (err)
2777 goto out;
2778
Hila Gonene35e4d22012-06-27 17:19:42 +03002779 err = rdev_set_default_mgmt_key(rdev, dev, key.idx);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002780 if (err)
2781 goto out;
2782
2783#ifdef CONFIG_CFG80211_WEXT
2784 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2785#endif
2786 }
2787
2788 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002789 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002790
Johannes Berg41ade002007-12-19 02:03:29 +01002791 return err;
2792}
2793
2794static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2795{
Johannes Berg4c476992010-10-04 21:36:35 +02002796 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002797 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002798 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002799 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002800 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002801
Johannes Bergb9454e82009-07-08 13:29:08 +02002802 err = nl80211_parse_key(info, &key);
2803 if (err)
2804 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002805
Johannes Bergb9454e82009-07-08 13:29:08 +02002806 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002807 return -EINVAL;
2808
Johannes Berg41ade002007-12-19 02:03:29 +01002809 if (info->attrs[NL80211_ATTR_MAC])
2810 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2811
Johannes Berge31b8212010-10-05 19:39:30 +02002812 if (key.type == -1) {
2813 if (mac_addr)
2814 key.type = NL80211_KEYTYPE_PAIRWISE;
2815 else
2816 key.type = NL80211_KEYTYPE_GROUP;
2817 }
2818
2819 /* for now */
2820 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2821 key.type != NL80211_KEYTYPE_GROUP)
2822 return -EINVAL;
2823
Johannes Berg4c476992010-10-04 21:36:35 +02002824 if (!rdev->ops->add_key)
2825 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002826
Johannes Berge31b8212010-10-05 19:39:30 +02002827 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2828 key.type == NL80211_KEYTYPE_PAIRWISE,
2829 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002830 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002831
2832 wdev_lock(dev->ieee80211_ptr);
2833 err = nl80211_key_allowed(dev->ieee80211_ptr);
2834 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002835 err = rdev_add_key(rdev, dev, key.idx,
2836 key.type == NL80211_KEYTYPE_PAIRWISE,
2837 mac_addr, &key.p);
Johannes Bergfffd0932009-07-08 14:22:54 +02002838 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002839
Johannes Berg41ade002007-12-19 02:03:29 +01002840 return err;
2841}
2842
2843static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2844{
Johannes Berg4c476992010-10-04 21:36:35 +02002845 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002846 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002847 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002848 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002849 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002850
Johannes Bergb9454e82009-07-08 13:29:08 +02002851 err = nl80211_parse_key(info, &key);
2852 if (err)
2853 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002854
2855 if (info->attrs[NL80211_ATTR_MAC])
2856 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2857
Johannes Berge31b8212010-10-05 19:39:30 +02002858 if (key.type == -1) {
2859 if (mac_addr)
2860 key.type = NL80211_KEYTYPE_PAIRWISE;
2861 else
2862 key.type = NL80211_KEYTYPE_GROUP;
2863 }
2864
2865 /* for now */
2866 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2867 key.type != NL80211_KEYTYPE_GROUP)
2868 return -EINVAL;
2869
Johannes Berg4c476992010-10-04 21:36:35 +02002870 if (!rdev->ops->del_key)
2871 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002872
Johannes Bergfffd0932009-07-08 14:22:54 +02002873 wdev_lock(dev->ieee80211_ptr);
2874 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002875
2876 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2877 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2878 err = -ENOENT;
2879
Johannes Bergfffd0932009-07-08 14:22:54 +02002880 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03002881 err = rdev_del_key(rdev, dev, key.idx,
2882 key.type == NL80211_KEYTYPE_PAIRWISE,
2883 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002884
Johannes Berg3d23e342009-09-29 23:27:28 +02002885#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002886 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002887 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002888 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002889 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002890 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2891 }
2892#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002893 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002894
Johannes Berg41ade002007-12-19 02:03:29 +01002895 return err;
2896}
2897
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05302898/* This function returns an error or the number of nested attributes */
2899static int validate_acl_mac_addrs(struct nlattr *nl_attr)
2900{
2901 struct nlattr *attr;
2902 int n_entries = 0, tmp;
2903
2904 nla_for_each_nested(attr, nl_attr, tmp) {
2905 if (nla_len(attr) != ETH_ALEN)
2906 return -EINVAL;
2907
2908 n_entries++;
2909 }
2910
2911 return n_entries;
2912}
2913
2914/*
2915 * This function parses ACL information and allocates memory for ACL data.
2916 * On successful return, the calling function is responsible to free the
2917 * ACL buffer returned by this function.
2918 */
2919static struct cfg80211_acl_data *parse_acl_data(struct wiphy *wiphy,
2920 struct genl_info *info)
2921{
2922 enum nl80211_acl_policy acl_policy;
2923 struct nlattr *attr;
2924 struct cfg80211_acl_data *acl;
2925 int i = 0, n_entries, tmp;
2926
2927 if (!wiphy->max_acl_mac_addrs)
2928 return ERR_PTR(-EOPNOTSUPP);
2929
2930 if (!info->attrs[NL80211_ATTR_ACL_POLICY])
2931 return ERR_PTR(-EINVAL);
2932
2933 acl_policy = nla_get_u32(info->attrs[NL80211_ATTR_ACL_POLICY]);
2934 if (acl_policy != NL80211_ACL_POLICY_ACCEPT_UNLESS_LISTED &&
2935 acl_policy != NL80211_ACL_POLICY_DENY_UNLESS_LISTED)
2936 return ERR_PTR(-EINVAL);
2937
2938 if (!info->attrs[NL80211_ATTR_MAC_ADDRS])
2939 return ERR_PTR(-EINVAL);
2940
2941 n_entries = validate_acl_mac_addrs(info->attrs[NL80211_ATTR_MAC_ADDRS]);
2942 if (n_entries < 0)
2943 return ERR_PTR(n_entries);
2944
2945 if (n_entries > wiphy->max_acl_mac_addrs)
2946 return ERR_PTR(-ENOTSUPP);
2947
2948 acl = kzalloc(sizeof(*acl) + (sizeof(struct mac_address) * n_entries),
2949 GFP_KERNEL);
2950 if (!acl)
2951 return ERR_PTR(-ENOMEM);
2952
2953 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_MAC_ADDRS], tmp) {
2954 memcpy(acl->mac_addrs[i].addr, nla_data(attr), ETH_ALEN);
2955 i++;
2956 }
2957
2958 acl->n_acl_entries = n_entries;
2959 acl->acl_policy = acl_policy;
2960
2961 return acl;
2962}
2963
2964static int nl80211_set_mac_acl(struct sk_buff *skb, struct genl_info *info)
2965{
2966 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2967 struct net_device *dev = info->user_ptr[1];
2968 struct cfg80211_acl_data *acl;
2969 int err;
2970
2971 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2972 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2973 return -EOPNOTSUPP;
2974
2975 if (!dev->ieee80211_ptr->beacon_interval)
2976 return -EINVAL;
2977
2978 acl = parse_acl_data(&rdev->wiphy, info);
2979 if (IS_ERR(acl))
2980 return PTR_ERR(acl);
2981
2982 err = rdev_set_mac_acl(rdev, dev, acl);
2983
2984 kfree(acl);
2985
2986 return err;
2987}
2988
Simon Wunderlicha1193be2013-06-14 14:15:19 +02002989static int nl80211_parse_beacon(struct nlattr *attrs[],
Johannes Berg88600202012-02-13 15:17:18 +01002990 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002991{
Johannes Berg88600202012-02-13 15:17:18 +01002992 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002993
Simon Wunderlicha1193be2013-06-14 14:15:19 +02002994 if (!is_valid_ie_attr(attrs[NL80211_ATTR_BEACON_TAIL]) ||
2995 !is_valid_ie_attr(attrs[NL80211_ATTR_IE]) ||
2996 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2997 !is_valid_ie_attr(attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002998 return -EINVAL;
2999
Johannes Berg88600202012-02-13 15:17:18 +01003000 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01003001
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003002 if (attrs[NL80211_ATTR_BEACON_HEAD]) {
3003 bcn->head = nla_data(attrs[NL80211_ATTR_BEACON_HEAD]);
3004 bcn->head_len = nla_len(attrs[NL80211_ATTR_BEACON_HEAD]);
Johannes Berg88600202012-02-13 15:17:18 +01003005 if (!bcn->head_len)
3006 return -EINVAL;
3007 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003008 }
3009
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003010 if (attrs[NL80211_ATTR_BEACON_TAIL]) {
3011 bcn->tail = nla_data(attrs[NL80211_ATTR_BEACON_TAIL]);
3012 bcn->tail_len = nla_len(attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01003013 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003014 }
3015
Johannes Berg4c476992010-10-04 21:36:35 +02003016 if (!haveinfo)
3017 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003018
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003019 if (attrs[NL80211_ATTR_IE]) {
3020 bcn->beacon_ies = nla_data(attrs[NL80211_ATTR_IE]);
3021 bcn->beacon_ies_len = nla_len(attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003022 }
3023
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003024 if (attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003025 bcn->proberesp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003026 nla_data(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003027 bcn->proberesp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003028 nla_len(attrs[NL80211_ATTR_IE_PROBE_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003029 }
3030
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003031 if (attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01003032 bcn->assocresp_ies =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003033 nla_data(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01003034 bcn->assocresp_ies_len =
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003035 nla_len(attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03003036 }
3037
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003038 if (attrs[NL80211_ATTR_PROBE_RESP]) {
3039 bcn->probe_resp = nla_data(attrs[NL80211_ATTR_PROBE_RESP]);
3040 bcn->probe_resp_len = nla_len(attrs[NL80211_ATTR_PROBE_RESP]);
Arik Nemtsov00f740e2011-11-10 11:28:56 +02003041 }
3042
Johannes Berg88600202012-02-13 15:17:18 +01003043 return 0;
3044}
3045
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003046static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
3047 struct cfg80211_ap_settings *params)
3048{
3049 struct wireless_dev *wdev;
3050 bool ret = false;
3051
Johannes Berg89a54e42012-06-15 14:33:17 +02003052 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003053 if (wdev->iftype != NL80211_IFTYPE_AP &&
3054 wdev->iftype != NL80211_IFTYPE_P2P_GO)
3055 continue;
3056
Johannes Berg683b6d32012-11-08 21:25:48 +01003057 if (!wdev->preset_chandef.chan)
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003058 continue;
3059
Johannes Berg683b6d32012-11-08 21:25:48 +01003060 params->chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003061 ret = true;
3062 break;
3063 }
3064
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003065 return ret;
3066}
3067
Jouni Malinene39e5b52012-09-30 19:29:39 +03003068static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
3069 enum nl80211_auth_type auth_type,
3070 enum nl80211_commands cmd)
3071{
3072 if (auth_type > NL80211_AUTHTYPE_MAX)
3073 return false;
3074
3075 switch (cmd) {
3076 case NL80211_CMD_AUTHENTICATE:
3077 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
3078 auth_type == NL80211_AUTHTYPE_SAE)
3079 return false;
3080 return true;
3081 case NL80211_CMD_CONNECT:
3082 case NL80211_CMD_START_AP:
3083 /* SAE not supported yet */
3084 if (auth_type == NL80211_AUTHTYPE_SAE)
3085 return false;
3086 return true;
3087 default:
3088 return false;
3089 }
3090}
3091
Johannes Berg88600202012-02-13 15:17:18 +01003092static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
3093{
3094 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3095 struct net_device *dev = info->user_ptr[1];
3096 struct wireless_dev *wdev = dev->ieee80211_ptr;
3097 struct cfg80211_ap_settings params;
3098 int err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01003099 u8 radar_detect_width = 0;
Johannes Berg88600202012-02-13 15:17:18 +01003100
3101 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3102 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3103 return -EOPNOTSUPP;
3104
3105 if (!rdev->ops->start_ap)
3106 return -EOPNOTSUPP;
3107
3108 if (wdev->beacon_interval)
3109 return -EALREADY;
3110
3111 memset(&params, 0, sizeof(params));
3112
3113 /* these are required for START_AP */
3114 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
3115 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
3116 !info->attrs[NL80211_ATTR_BEACON_HEAD])
3117 return -EINVAL;
3118
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003119 err = nl80211_parse_beacon(info->attrs, &params.beacon);
Johannes Berg88600202012-02-13 15:17:18 +01003120 if (err)
3121 return err;
3122
3123 params.beacon_interval =
3124 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3125 params.dtim_period =
3126 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
3127
3128 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
3129 if (err)
3130 return err;
3131
3132 /*
3133 * In theory, some of these attributes should be required here
3134 * but since they were not used when the command was originally
3135 * added, keep them optional for old user space programs to let
3136 * them continue to work with drivers that do not need the
3137 * additional information -- drivers must check!
3138 */
3139 if (info->attrs[NL80211_ATTR_SSID]) {
3140 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3141 params.ssid_len =
3142 nla_len(info->attrs[NL80211_ATTR_SSID]);
3143 if (params.ssid_len == 0 ||
3144 params.ssid_len > IEEE80211_MAX_SSID_LEN)
3145 return -EINVAL;
3146 }
3147
3148 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
3149 params.hidden_ssid = nla_get_u32(
3150 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
3151 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
3152 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
3153 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
3154 return -EINVAL;
3155 }
3156
3157 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
3158
3159 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3160 params.auth_type = nla_get_u32(
3161 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03003162 if (!nl80211_valid_auth_type(rdev, params.auth_type,
3163 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01003164 return -EINVAL;
3165 } else
3166 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3167
3168 err = nl80211_crypto_settings(rdev, info, &params.crypto,
3169 NL80211_MAX_NR_CIPHER_SUITES);
3170 if (err)
3171 return err;
3172
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05303173 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
3174 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
3175 return -EOPNOTSUPP;
3176 params.inactivity_timeout = nla_get_u16(
3177 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
3178 }
3179
Johannes Berg53cabad2012-11-14 15:17:28 +01003180 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
3181 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3182 return -EINVAL;
3183 params.p2p_ctwindow =
3184 nla_get_u8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
3185 if (params.p2p_ctwindow > 127)
3186 return -EINVAL;
3187 if (params.p2p_ctwindow != 0 &&
3188 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
3189 return -EINVAL;
3190 }
3191
3192 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
3193 u8 tmp;
3194
3195 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3196 return -EINVAL;
3197 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
3198 if (tmp > 1)
3199 return -EINVAL;
3200 params.p2p_opp_ps = tmp;
3201 if (params.p2p_opp_ps != 0 &&
3202 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
3203 return -EINVAL;
3204 }
3205
Johannes Bergaa430da2012-05-16 23:50:18 +02003206 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003207 err = nl80211_parse_chandef(rdev, info, &params.chandef);
3208 if (err)
3209 return err;
3210 } else if (wdev->preset_chandef.chan) {
3211 params.chandef = wdev->preset_chandef;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003212 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02003213 return -EINVAL;
3214
Johannes Berg683b6d32012-11-08 21:25:48 +01003215 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
Johannes Bergaa430da2012-05-16 23:50:18 +02003216 return -EINVAL;
3217
Simon Wunderlich04f39042013-02-08 18:16:19 +01003218 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef);
3219 if (err < 0)
3220 return err;
3221 if (err) {
3222 radar_detect_width = BIT(params.chandef.width);
3223 params.radar_required = true;
3224 }
3225
Simon Wunderlich04f39042013-02-08 18:16:19 +01003226 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
3227 params.chandef.chan,
3228 CHAN_MODE_SHARED,
3229 radar_detect_width);
Michal Kaziore4e32452012-06-29 12:47:08 +02003230 if (err)
3231 return err;
3232
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303233 if (info->attrs[NL80211_ATTR_ACL_POLICY]) {
3234 params.acl = parse_acl_data(&rdev->wiphy, info);
3235 if (IS_ERR(params.acl))
3236 return PTR_ERR(params.acl);
3237 }
3238
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003239 wdev_lock(wdev);
Hila Gonene35e4d22012-06-27 17:19:42 +03003240 err = rdev_start_ap(rdev, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003241 if (!err) {
Johannes Berg683b6d32012-11-08 21:25:48 +01003242 wdev->preset_chandef = params.chandef;
Johannes Berg88600202012-02-13 15:17:18 +01003243 wdev->beacon_interval = params.beacon_interval;
Johannes Berg683b6d32012-11-08 21:25:48 +01003244 wdev->channel = params.chandef.chan;
Antonio Quartulli06e191e2012-11-07 12:52:19 +01003245 wdev->ssid_len = params.ssid_len;
3246 memcpy(wdev->ssid, params.ssid, wdev->ssid_len);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02003247 }
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003248 wdev_unlock(wdev);
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05303249
3250 kfree(params.acl);
3251
Johannes Berg56d18932011-05-09 18:41:15 +02003252 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01003253}
3254
Johannes Berg88600202012-02-13 15:17:18 +01003255static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
3256{
3257 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3258 struct net_device *dev = info->user_ptr[1];
3259 struct wireless_dev *wdev = dev->ieee80211_ptr;
3260 struct cfg80211_beacon_data params;
3261 int err;
3262
3263 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3264 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3265 return -EOPNOTSUPP;
3266
3267 if (!rdev->ops->change_beacon)
3268 return -EOPNOTSUPP;
3269
3270 if (!wdev->beacon_interval)
3271 return -EINVAL;
3272
Simon Wunderlicha1193be2013-06-14 14:15:19 +02003273 err = nl80211_parse_beacon(info->attrs, &params);
Johannes Berg88600202012-02-13 15:17:18 +01003274 if (err)
3275 return err;
3276
Simon Wunderlichc56589e2013-11-21 18:19:49 +01003277 wdev_lock(wdev);
3278 err = rdev_change_beacon(rdev, dev, &params);
3279 wdev_unlock(wdev);
3280
3281 return err;
Johannes Berg88600202012-02-13 15:17:18 +01003282}
3283
3284static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01003285{
Johannes Berg4c476992010-10-04 21:36:35 +02003286 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3287 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01003288
Michal Kazior60771782012-06-29 12:46:56 +02003289 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01003290}
3291
Johannes Berg5727ef12007-12-19 02:03:34 +01003292static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
3293 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
3294 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
3295 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03003296 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07003297 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01003298 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01003299};
3300
Johannes Bergeccb8e82009-05-11 21:57:56 +03003301static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003302 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03003303 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01003304{
3305 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03003306 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01003307 int flag;
3308
Johannes Bergeccb8e82009-05-11 21:57:56 +03003309 /*
3310 * Try parsing the new attribute first so userspace
3311 * can specify both for older kernels.
3312 */
3313 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
3314 if (nla) {
3315 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01003316
Johannes Bergeccb8e82009-05-11 21:57:56 +03003317 sta_flags = nla_data(nla);
3318 params->sta_flags_mask = sta_flags->mask;
3319 params->sta_flags_set = sta_flags->set;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003320 params->sta_flags_set &= params->sta_flags_mask;
Johannes Bergeccb8e82009-05-11 21:57:56 +03003321 if ((params->sta_flags_mask |
3322 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
3323 return -EINVAL;
3324 return 0;
3325 }
3326
3327 /* if present, parse the old attribute */
3328
3329 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01003330 if (!nla)
3331 return 0;
3332
3333 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
3334 nla, sta_flags_policy))
3335 return -EINVAL;
3336
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003337 /*
3338 * Only allow certain flags for interface types so that
3339 * other attributes are silently ignored. Remember that
3340 * this is backward compatibility code with old userspace
3341 * and shouldn't be hit in other cases anyway.
3342 */
3343 switch (iftype) {
3344 case NL80211_IFTYPE_AP:
3345 case NL80211_IFTYPE_AP_VLAN:
3346 case NL80211_IFTYPE_P2P_GO:
3347 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3348 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3349 BIT(NL80211_STA_FLAG_WME) |
3350 BIT(NL80211_STA_FLAG_MFP);
3351 break;
3352 case NL80211_IFTYPE_P2P_CLIENT:
3353 case NL80211_IFTYPE_STATION:
3354 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
3355 BIT(NL80211_STA_FLAG_TDLS_PEER);
3356 break;
3357 case NL80211_IFTYPE_MESH_POINT:
3358 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3359 BIT(NL80211_STA_FLAG_MFP) |
3360 BIT(NL80211_STA_FLAG_AUTHORIZED);
3361 default:
3362 return -EINVAL;
3363 }
Johannes Berg5727ef12007-12-19 02:03:34 +01003364
Johannes Berg3383b5a2012-05-10 20:14:43 +02003365 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
3366 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03003367 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01003368
Johannes Berg3383b5a2012-05-10 20:14:43 +02003369 /* no longer support new API additions in old API */
3370 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
3371 return -EINVAL;
3372 }
3373 }
3374
Johannes Berg5727ef12007-12-19 02:03:34 +01003375 return 0;
3376}
3377
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003378static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
3379 int attr)
3380{
3381 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003382 u32 bitrate;
3383 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003384
3385 rate = nla_nest_start(msg, attr);
3386 if (!rate)
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003387 return false;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003388
3389 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
3390 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03003391 /* report 16-bit bitrate only if we can */
3392 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
Johannes Bergdb9c64c2012-11-09 14:56:41 +01003393 if (bitrate > 0 &&
3394 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate))
3395 return false;
3396 if (bitrate_compat > 0 &&
3397 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat))
3398 return false;
3399
3400 if (info->flags & RATE_INFO_FLAGS_MCS) {
3401 if (nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs))
3402 return false;
3403 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3404 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3405 return false;
3406 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3407 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3408 return false;
3409 } else if (info->flags & RATE_INFO_FLAGS_VHT_MCS) {
3410 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_MCS, info->mcs))
3411 return false;
3412 if (nla_put_u8(msg, NL80211_RATE_INFO_VHT_NSS, info->nss))
3413 return false;
3414 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH &&
3415 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH))
3416 return false;
3417 if (info->flags & RATE_INFO_FLAGS_80_MHZ_WIDTH &&
3418 nla_put_flag(msg, NL80211_RATE_INFO_80_MHZ_WIDTH))
3419 return false;
3420 if (info->flags & RATE_INFO_FLAGS_80P80_MHZ_WIDTH &&
3421 nla_put_flag(msg, NL80211_RATE_INFO_80P80_MHZ_WIDTH))
3422 return false;
3423 if (info->flags & RATE_INFO_FLAGS_160_MHZ_WIDTH &&
3424 nla_put_flag(msg, NL80211_RATE_INFO_160_MHZ_WIDTH))
3425 return false;
3426 if (info->flags & RATE_INFO_FLAGS_SHORT_GI &&
3427 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI))
3428 return false;
3429 }
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003430
3431 nla_nest_end(msg, rate);
3432 return true;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003433}
3434
Felix Fietkau119363c2013-04-22 16:29:30 +02003435static bool nl80211_put_signal(struct sk_buff *msg, u8 mask, s8 *signal,
3436 int id)
3437{
3438 void *attr;
3439 int i = 0;
3440
3441 if (!mask)
3442 return true;
3443
3444 attr = nla_nest_start(msg, id);
3445 if (!attr)
3446 return false;
3447
3448 for (i = 0; i < IEEE80211_MAX_CHAINS; i++) {
3449 if (!(mask & BIT(i)))
3450 continue;
3451
3452 if (nla_put_u8(msg, i, signal[i]))
3453 return false;
3454 }
3455
3456 nla_nest_end(msg, attr);
3457
3458 return true;
3459}
3460
Eric W. Biederman15e47302012-09-07 20:12:54 +00003461static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04003462 int flags,
3463 struct cfg80211_registered_device *rdev,
3464 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01003465 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003466{
3467 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07003468 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003469
Eric W. Biederman15e47302012-09-07 20:12:54 +00003470 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003471 if (!hdr)
3472 return -1;
3473
David S. Miller9360ffd2012-03-29 04:41:26 -04003474 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3475 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
3476 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
3477 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003478
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003479 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
3480 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003481 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003482 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
3483 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
3484 sinfo->connected_time))
3485 goto nla_put_failure;
3486 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
3487 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
3488 sinfo->inactive_time))
3489 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003490 if ((sinfo->filled & (STATION_INFO_RX_BYTES |
3491 STATION_INFO_RX_BYTES64)) &&
David S. Miller9360ffd2012-03-29 04:41:26 -04003492 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003493 (u32)sinfo->rx_bytes))
3494 goto nla_put_failure;
3495 if ((sinfo->filled & (STATION_INFO_TX_BYTES |
Felix Fietkau4325d722013-05-23 15:05:59 +02003496 STATION_INFO_TX_BYTES64)) &&
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003497 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
3498 (u32)sinfo->tx_bytes))
3499 goto nla_put_failure;
3500 if ((sinfo->filled & STATION_INFO_RX_BYTES64) &&
3501 nla_put_u64(msg, NL80211_STA_INFO_RX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003502 sinfo->rx_bytes))
3503 goto nla_put_failure;
Vladimir Kondratiev42745e02013-02-04 13:53:11 +02003504 if ((sinfo->filled & STATION_INFO_TX_BYTES64) &&
3505 nla_put_u64(msg, NL80211_STA_INFO_TX_BYTES64,
David S. Miller9360ffd2012-03-29 04:41:26 -04003506 sinfo->tx_bytes))
3507 goto nla_put_failure;
3508 if ((sinfo->filled & STATION_INFO_LLID) &&
3509 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
3510 goto nla_put_failure;
3511 if ((sinfo->filled & STATION_INFO_PLID) &&
3512 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
3513 goto nla_put_failure;
3514 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
3515 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
3516 sinfo->plink_state))
3517 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003518 switch (rdev->wiphy.signal_type) {
3519 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04003520 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
3521 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
3522 sinfo->signal))
3523 goto nla_put_failure;
3524 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
3525 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
3526 sinfo->signal_avg))
3527 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04003528 break;
3529 default:
3530 break;
3531 }
Felix Fietkau119363c2013-04-22 16:29:30 +02003532 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL) {
3533 if (!nl80211_put_signal(msg, sinfo->chains,
3534 sinfo->chain_signal,
3535 NL80211_STA_INFO_CHAIN_SIGNAL))
3536 goto nla_put_failure;
3537 }
3538 if (sinfo->filled & STATION_INFO_CHAIN_SIGNAL_AVG) {
3539 if (!nl80211_put_signal(msg, sinfo->chains,
3540 sinfo->chain_signal_avg,
3541 NL80211_STA_INFO_CHAIN_SIGNAL_AVG))
3542 goto nla_put_failure;
3543 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01003544 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003545 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
3546 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01003547 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01003548 }
3549 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
3550 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
3551 NL80211_STA_INFO_RX_BITRATE))
3552 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01003553 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003554 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
3555 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
3556 sinfo->rx_packets))
3557 goto nla_put_failure;
3558 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
3559 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
3560 sinfo->tx_packets))
3561 goto nla_put_failure;
3562 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
3563 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
3564 sinfo->tx_retries))
3565 goto nla_put_failure;
3566 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
3567 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
3568 sinfo->tx_failed))
3569 goto nla_put_failure;
3570 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
3571 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
3572 sinfo->beacon_loss_count))
3573 goto nla_put_failure;
Marco Porsch3b1c5a52013-01-07 16:04:52 +01003574 if ((sinfo->filled & STATION_INFO_LOCAL_PM) &&
3575 nla_put_u32(msg, NL80211_STA_INFO_LOCAL_PM,
3576 sinfo->local_pm))
3577 goto nla_put_failure;
3578 if ((sinfo->filled & STATION_INFO_PEER_PM) &&
3579 nla_put_u32(msg, NL80211_STA_INFO_PEER_PM,
3580 sinfo->peer_pm))
3581 goto nla_put_failure;
3582 if ((sinfo->filled & STATION_INFO_NONPEER_PM) &&
3583 nla_put_u32(msg, NL80211_STA_INFO_NONPEER_PM,
3584 sinfo->nonpeer_pm))
3585 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003586 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
3587 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
3588 if (!bss_param)
3589 goto nla_put_failure;
3590
David S. Miller9360ffd2012-03-29 04:41:26 -04003591 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
3592 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
3593 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
3594 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
3595 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
3596 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
3597 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
3598 sinfo->bss_param.dtim_period) ||
3599 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
3600 sinfo->bss_param.beacon_interval))
3601 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07003602
3603 nla_nest_end(msg, bss_param);
3604 }
David S. Miller9360ffd2012-03-29 04:41:26 -04003605 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
3606 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
3607 sizeof(struct nl80211_sta_flag_update),
3608 &sinfo->sta_flags))
3609 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04003610 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
3611 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
3612 sinfo->t_offset))
3613 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003614 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003615
David S. Miller9360ffd2012-03-29 04:41:26 -04003616 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
3617 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
3618 sinfo->assoc_req_ies))
3619 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03003620
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003621 return genlmsg_end(msg, hdr);
3622
3623 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003624 genlmsg_cancel(msg, hdr);
3625 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003626}
3627
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003628static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003629 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003630{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003631 struct station_info sinfo;
3632 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02003633 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003634 u8 mac_addr[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02003635 int sta_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003636 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003637
Johannes Berg97990a02013-04-19 01:02:55 +02003638 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02003639 if (err)
3640 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003641
Johannes Berg97990a02013-04-19 01:02:55 +02003642 if (!wdev->netdev) {
3643 err = -EINVAL;
3644 goto out_err;
3645 }
3646
Johannes Bergbba95fe2008-07-29 13:22:51 +02003647 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003648 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003649 goto out_err;
3650 }
3651
Johannes Bergbba95fe2008-07-29 13:22:51 +02003652 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03003653 memset(&sinfo, 0, sizeof(sinfo));
Johannes Berg97990a02013-04-19 01:02:55 +02003654 err = rdev_dump_station(dev, wdev->netdev, sta_idx,
Hila Gonene35e4d22012-06-27 17:19:42 +03003655 mac_addr, &sinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003656 if (err == -ENOENT)
3657 break;
3658 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003659 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003660
3661 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003662 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003663 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02003664 dev, wdev->netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003665 &sinfo) < 0)
3666 goto out;
3667
3668 sta_idx++;
3669 }
3670
3671
3672 out:
Johannes Berg97990a02013-04-19 01:02:55 +02003673 cb->args[2] = sta_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003674 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003675 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02003676 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003677
3678 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003679}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003680
Johannes Berg5727ef12007-12-19 02:03:34 +01003681static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
3682{
Johannes Berg4c476992010-10-04 21:36:35 +02003683 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3684 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003685 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003686 struct sk_buff *msg;
3687 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02003688 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003689
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003690 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003691
3692 if (!info->attrs[NL80211_ATTR_MAC])
3693 return -EINVAL;
3694
3695 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3696
Johannes Berg4c476992010-10-04 21:36:35 +02003697 if (!rdev->ops->get_station)
3698 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003699
Hila Gonene35e4d22012-06-27 17:19:42 +03003700 err = rdev_get_station(rdev, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003701 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003702 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003703
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003704 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003705 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003706 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003707
Eric W. Biederman15e47302012-09-07 20:12:54 +00003708 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003709 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003710 nlmsg_free(msg);
3711 return -ENOBUFS;
3712 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003713
Johannes Berg4c476992010-10-04 21:36:35 +02003714 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003715}
3716
Johannes Berg77ee7c82013-02-15 00:48:33 +01003717int cfg80211_check_station_change(struct wiphy *wiphy,
3718 struct station_parameters *params,
3719 enum cfg80211_station_type statype)
3720{
3721 if (params->listen_interval != -1)
3722 return -EINVAL;
3723 if (params->aid)
3724 return -EINVAL;
3725
3726 /* When you run into this, adjust the code below for the new flag */
3727 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
3728
3729 switch (statype) {
Thomas Pederseneef941e2013-03-04 13:06:11 -08003730 case CFG80211_STA_MESH_PEER_KERNEL:
3731 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003732 /*
3733 * No ignoring the TDLS flag here -- the userspace mesh
3734 * code doesn't have the bug of including TDLS in the
3735 * mask everywhere.
3736 */
3737 if (params->sta_flags_mask &
3738 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3739 BIT(NL80211_STA_FLAG_MFP) |
3740 BIT(NL80211_STA_FLAG_AUTHORIZED)))
3741 return -EINVAL;
3742 break;
3743 case CFG80211_STA_TDLS_PEER_SETUP:
3744 case CFG80211_STA_TDLS_PEER_ACTIVE:
3745 if (!(params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3746 return -EINVAL;
3747 /* ignore since it can't change */
3748 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3749 break;
3750 default:
3751 /* disallow mesh-specific things */
3752 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3753 return -EINVAL;
3754 if (params->local_pm)
3755 return -EINVAL;
3756 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3757 return -EINVAL;
3758 }
3759
3760 if (statype != CFG80211_STA_TDLS_PEER_SETUP &&
3761 statype != CFG80211_STA_TDLS_PEER_ACTIVE) {
3762 /* TDLS can't be set, ... */
3763 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3764 return -EINVAL;
3765 /*
3766 * ... but don't bother the driver with it. This works around
3767 * a hostapd/wpa_supplicant issue -- it always includes the
3768 * TLDS_PEER flag in the mask even for AP mode.
3769 */
3770 params->sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3771 }
3772
3773 if (statype != CFG80211_STA_TDLS_PEER_SETUP) {
3774 /* reject other things that can't change */
3775 if (params->sta_modify_mask & STATION_PARAM_APPLY_UAPSD)
3776 return -EINVAL;
3777 if (params->sta_modify_mask & STATION_PARAM_APPLY_CAPABILITY)
3778 return -EINVAL;
3779 if (params->supported_rates)
3780 return -EINVAL;
3781 if (params->ext_capab || params->ht_capa || params->vht_capa)
3782 return -EINVAL;
3783 }
3784
3785 if (statype != CFG80211_STA_AP_CLIENT) {
3786 if (params->vlan)
3787 return -EINVAL;
3788 }
3789
3790 switch (statype) {
3791 case CFG80211_STA_AP_MLME_CLIENT:
3792 /* Use this only for authorizing/unauthorizing a station */
3793 if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
3794 return -EOPNOTSUPP;
3795 break;
3796 case CFG80211_STA_AP_CLIENT:
3797 /* accept only the listed bits */
3798 if (params->sta_flags_mask &
3799 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3800 BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3801 BIT(NL80211_STA_FLAG_ASSOCIATED) |
3802 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3803 BIT(NL80211_STA_FLAG_WME) |
3804 BIT(NL80211_STA_FLAG_MFP)))
3805 return -EINVAL;
3806
3807 /* but authenticated/associated only if driver handles it */
3808 if (!(wiphy->features & NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
3809 params->sta_flags_mask &
3810 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
3811 BIT(NL80211_STA_FLAG_ASSOCIATED)))
3812 return -EINVAL;
3813 break;
3814 case CFG80211_STA_IBSS:
3815 case CFG80211_STA_AP_STA:
3816 /* reject any changes other than AUTHORIZED */
3817 if (params->sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3818 return -EINVAL;
3819 break;
3820 case CFG80211_STA_TDLS_PEER_SETUP:
3821 /* reject any changes other than AUTHORIZED or WME */
3822 if (params->sta_flags_mask & ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3823 BIT(NL80211_STA_FLAG_WME)))
3824 return -EINVAL;
3825 /* force (at least) rates when authorizing */
3826 if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED) &&
3827 !params->supported_rates)
3828 return -EINVAL;
3829 break;
3830 case CFG80211_STA_TDLS_PEER_ACTIVE:
3831 /* reject any changes */
3832 return -EINVAL;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003833 case CFG80211_STA_MESH_PEER_KERNEL:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003834 if (params->sta_modify_mask & STATION_PARAM_APPLY_PLINK_STATE)
3835 return -EINVAL;
3836 break;
Thomas Pederseneef941e2013-03-04 13:06:11 -08003837 case CFG80211_STA_MESH_PEER_USER:
Johannes Berg77ee7c82013-02-15 00:48:33 +01003838 if (params->plink_action != NL80211_PLINK_ACTION_NO_ACTION)
3839 return -EINVAL;
3840 break;
3841 }
3842
3843 return 0;
3844}
3845EXPORT_SYMBOL(cfg80211_check_station_change);
3846
Johannes Berg5727ef12007-12-19 02:03:34 +01003847/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003848 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003849 */
Johannes Berg80b99892011-11-18 16:23:01 +01003850static struct net_device *get_vlan(struct genl_info *info,
3851 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003852{
Johannes Berg463d0182009-07-14 00:33:35 +02003853 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003854 struct net_device *v;
3855 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003856
Johannes Berg80b99892011-11-18 16:23:01 +01003857 if (!vlanattr)
3858 return NULL;
3859
3860 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3861 if (!v)
3862 return ERR_PTR(-ENODEV);
3863
3864 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3865 ret = -EINVAL;
3866 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003867 }
Johannes Berg80b99892011-11-18 16:23:01 +01003868
Johannes Berg77ee7c82013-02-15 00:48:33 +01003869 if (v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
3870 v->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
3871 v->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
3872 ret = -EINVAL;
3873 goto error;
3874 }
3875
Johannes Berg80b99892011-11-18 16:23:01 +01003876 if (!netif_running(v)) {
3877 ret = -ENETDOWN;
3878 goto error;
3879 }
3880
3881 return v;
3882 error:
3883 dev_put(v);
3884 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003885}
3886
Jouni Malinendf881292013-02-14 21:10:54 +02003887static struct nla_policy
3888nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3889 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3890 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3891};
3892
Johannes Bergff276692013-02-15 00:09:01 +01003893static int nl80211_parse_sta_wme(struct genl_info *info,
3894 struct station_parameters *params)
Jouni Malinendf881292013-02-14 21:10:54 +02003895{
Jouni Malinendf881292013-02-14 21:10:54 +02003896 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3897 struct nlattr *nla;
3898 int err;
3899
Jouni Malinendf881292013-02-14 21:10:54 +02003900 /* parse WME attributes if present */
3901 if (!info->attrs[NL80211_ATTR_STA_WME])
3902 return 0;
3903
3904 nla = info->attrs[NL80211_ATTR_STA_WME];
3905 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3906 nl80211_sta_wme_policy);
3907 if (err)
3908 return err;
3909
3910 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3911 params->uapsd_queues = nla_get_u8(
3912 tb[NL80211_STA_WME_UAPSD_QUEUES]);
3913 if (params->uapsd_queues & ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3914 return -EINVAL;
3915
3916 if (tb[NL80211_STA_WME_MAX_SP])
3917 params->max_sp = nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3918
3919 if (params->max_sp & ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3920 return -EINVAL;
3921
3922 params->sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3923
3924 return 0;
3925}
3926
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303927static int nl80211_parse_sta_channel_info(struct genl_info *info,
3928 struct station_parameters *params)
3929{
3930 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3931 params->supported_channels =
3932 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3933 params->supported_channels_len =
3934 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3935 /*
3936 * Need to include at least one (first channel, number of
3937 * channels) tuple for each subband, and must have proper
3938 * tuples for the rest of the data as well.
3939 */
3940 if (params->supported_channels_len < 2)
3941 return -EINVAL;
3942 if (params->supported_channels_len % 2)
3943 return -EINVAL;
3944 }
3945
3946 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
3947 params->supported_oper_classes =
3948 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3949 params->supported_oper_classes_len =
3950 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3951 /*
3952 * The value of the Length field of the Supported Operating
3953 * Classes element is between 2 and 253.
3954 */
3955 if (params->supported_oper_classes_len < 2 ||
3956 params->supported_oper_classes_len > 253)
3957 return -EINVAL;
3958 }
3959 return 0;
3960}
3961
Johannes Bergff276692013-02-15 00:09:01 +01003962static int nl80211_set_station_tdls(struct genl_info *info,
3963 struct station_parameters *params)
3964{
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303965 int err;
Johannes Bergff276692013-02-15 00:09:01 +01003966 /* Dummy STA entry gets updated once the peer capabilities are known */
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03003967 if (info->attrs[NL80211_ATTR_PEER_AID])
3968 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Johannes Bergff276692013-02-15 00:09:01 +01003969 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3970 params->ht_capa =
3971 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3972 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3973 params->vht_capa =
3974 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3975
Sunil Duttc01fc9a2013-10-09 20:45:21 +05303976 err = nl80211_parse_sta_channel_info(info, params);
3977 if (err)
3978 return err;
3979
Johannes Bergff276692013-02-15 00:09:01 +01003980 return nl80211_parse_sta_wme(info, params);
3981}
3982
Johannes Berg5727ef12007-12-19 02:03:34 +01003983static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3984{
Johannes Berg4c476992010-10-04 21:36:35 +02003985 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003986 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003987 struct station_parameters params;
Johannes Berg77ee7c82013-02-15 00:48:33 +01003988 u8 *mac_addr;
3989 int err;
Johannes Berg5727ef12007-12-19 02:03:34 +01003990
3991 memset(&params, 0, sizeof(params));
3992
3993 params.listen_interval = -1;
3994
Johannes Berg77ee7c82013-02-15 00:48:33 +01003995 if (!rdev->ops->change_station)
3996 return -EOPNOTSUPP;
3997
Johannes Berg5727ef12007-12-19 02:03:34 +01003998 if (info->attrs[NL80211_ATTR_STA_AID])
3999 return -EINVAL;
4000
4001 if (!info->attrs[NL80211_ATTR_MAC])
4002 return -EINVAL;
4003
4004 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4005
4006 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
4007 params.supported_rates =
4008 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4009 params.supported_rates_len =
4010 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4011 }
4012
Jouni Malinen9d62a982013-02-14 21:10:13 +02004013 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4014 params.capability =
4015 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4016 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4017 }
4018
4019 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4020 params.ext_capab =
4021 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4022 params.ext_capab_len =
4023 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4024 }
4025
Jouni Malinendf881292013-02-14 21:10:54 +02004026 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
Johannes Bergba23d202012-12-27 17:32:09 +01004027 return -EINVAL;
Jouni Malinen36aedc902008-08-25 11:58:58 +03004028
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004029 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004030 return -EINVAL;
4031
Johannes Bergf8bacc22013-02-14 23:27:01 +01004032 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004033 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004034 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4035 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4036 return -EINVAL;
4037 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004038
Johannes Bergf8bacc22013-02-14 23:27:01 +01004039 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE]) {
Javier Cardona9c3990a2011-05-03 16:57:11 -07004040 params.plink_state =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004041 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
4042 if (params.plink_state >= NUM_NL80211_PLINK_STATES)
4043 return -EINVAL;
4044 params.sta_modify_mask |= STATION_PARAM_APPLY_PLINK_STATE;
4045 }
Javier Cardona9c3990a2011-05-03 16:57:11 -07004046
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004047 if (info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]) {
4048 enum nl80211_mesh_power_mode pm = nla_get_u32(
4049 info->attrs[NL80211_ATTR_LOCAL_MESH_POWER_MODE]);
4050
4051 if (pm <= NL80211_MESH_POWER_UNKNOWN ||
4052 pm > NL80211_MESH_POWER_MAX)
4053 return -EINVAL;
4054
4055 params.local_pm = pm;
4056 }
4057
Johannes Berg77ee7c82013-02-15 00:48:33 +01004058 /* Include parameters for TDLS peer (will check later) */
4059 err = nl80211_set_station_tdls(info, &params);
4060 if (err)
4061 return err;
4062
4063 params.vlan = get_vlan(info, rdev);
4064 if (IS_ERR(params.vlan))
4065 return PTR_ERR(params.vlan);
4066
Johannes Berga97f4422009-06-18 17:23:43 +02004067 switch (dev->ieee80211_ptr->iftype) {
4068 case NL80211_IFTYPE_AP:
4069 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004070 case NL80211_IFTYPE_P2P_GO:
Johannes Berg074ac8d2010-09-16 14:58:22 +02004071 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02004072 case NL80211_IFTYPE_STATION:
Antonio Quartulli267335d2012-01-31 20:25:47 +01004073 case NL80211_IFTYPE_ADHOC:
Johannes Berga97f4422009-06-18 17:23:43 +02004074 case NL80211_IFTYPE_MESH_POINT:
Johannes Berga97f4422009-06-18 17:23:43 +02004075 break;
4076 default:
Johannes Berg77ee7c82013-02-15 00:48:33 +01004077 err = -EOPNOTSUPP;
4078 goto out_put_vlan;
Johannes Berg034d6552009-05-27 10:35:29 +02004079 }
4080
Johannes Berg77ee7c82013-02-15 00:48:33 +01004081 /* driver will call cfg80211_check_station_change() */
Hila Gonene35e4d22012-06-27 17:19:42 +03004082 err = rdev_change_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004083
Johannes Berg77ee7c82013-02-15 00:48:33 +01004084 out_put_vlan:
Johannes Berg5727ef12007-12-19 02:03:34 +01004085 if (params.vlan)
4086 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01004087
Johannes Berg5727ef12007-12-19 02:03:34 +01004088 return err;
4089}
4090
4091static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4092{
Johannes Berg4c476992010-10-04 21:36:35 +02004093 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01004094 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004095 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004096 struct station_parameters params;
4097 u8 *mac_addr = NULL;
4098
4099 memset(&params, 0, sizeof(params));
4100
Johannes Berg984c3112013-02-14 23:43:25 +01004101 if (!rdev->ops->add_station)
4102 return -EOPNOTSUPP;
4103
Johannes Berg5727ef12007-12-19 02:03:34 +01004104 if (!info->attrs[NL80211_ATTR_MAC])
4105 return -EINVAL;
4106
Johannes Berg5727ef12007-12-19 02:03:34 +01004107 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
4108 return -EINVAL;
4109
4110 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
4111 return -EINVAL;
4112
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004113 if (!info->attrs[NL80211_ATTR_STA_AID] &&
4114 !info->attrs[NL80211_ATTR_PEER_AID])
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004115 return -EINVAL;
4116
Johannes Berg5727ef12007-12-19 02:03:34 +01004117 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4118 params.supported_rates =
4119 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4120 params.supported_rates_len =
4121 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
4122 params.listen_interval =
4123 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02004124
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004125 if (info->attrs[NL80211_ATTR_PEER_AID])
Jouni Malinen5e4b6f52013-05-16 20:11:08 +03004126 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004127 else
4128 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02004129 if (!params.aid || params.aid > IEEE80211_MAX_AID)
4130 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02004131
Jouni Malinen9d62a982013-02-14 21:10:13 +02004132 if (info->attrs[NL80211_ATTR_STA_CAPABILITY]) {
4133 params.capability =
4134 nla_get_u16(info->attrs[NL80211_ATTR_STA_CAPABILITY]);
4135 params.sta_modify_mask |= STATION_PARAM_APPLY_CAPABILITY;
4136 }
4137
4138 if (info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]) {
4139 params.ext_capab =
4140 nla_data(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4141 params.ext_capab_len =
4142 nla_len(info->attrs[NL80211_ATTR_STA_EXT_CAPABILITY]);
4143 }
4144
Jouni Malinen36aedc902008-08-25 11:58:58 +03004145 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
4146 params.ht_capa =
4147 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01004148
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00004149 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
4150 params.vht_capa =
4151 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
4152
Marek Kwaczynski60f4a7b2013-12-03 10:04:59 +01004153 if (info->attrs[NL80211_ATTR_OPMODE_NOTIF]) {
4154 params.opmode_notif_used = true;
4155 params.opmode_notif =
4156 nla_get_u8(info->attrs[NL80211_ATTR_OPMODE_NOTIF]);
4157 }
4158
Johannes Bergf8bacc22013-02-14 23:27:01 +01004159 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION]) {
Javier Cardona96b78df2011-04-07 15:08:33 -07004160 params.plink_action =
Johannes Bergf8bacc22013-02-14 23:27:01 +01004161 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
4162 if (params.plink_action >= NUM_NL80211_PLINK_ACTIONS)
4163 return -EINVAL;
4164 }
Javier Cardona96b78df2011-04-07 15:08:33 -07004165
Sunil Duttc01fc9a2013-10-09 20:45:21 +05304166 err = nl80211_parse_sta_channel_info(info, &params);
4167 if (err)
4168 return err;
4169
Johannes Bergff276692013-02-15 00:09:01 +01004170 err = nl80211_parse_sta_wme(info, &params);
4171 if (err)
4172 return err;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004173
Johannes Bergbdd3ae32012-01-02 13:30:03 +01004174 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01004175 return -EINVAL;
4176
Johannes Berg77ee7c82013-02-15 00:48:33 +01004177 /* When you run into this, adjust the code below for the new flag */
4178 BUILD_BUG_ON(NL80211_STA_FLAG_MAX != 7);
4179
Johannes Bergbdd90d52011-12-14 12:20:27 +01004180 switch (dev->ieee80211_ptr->iftype) {
4181 case NL80211_IFTYPE_AP:
4182 case NL80211_IFTYPE_AP_VLAN:
4183 case NL80211_IFTYPE_P2P_GO:
Johannes Berg984c3112013-02-14 23:43:25 +01004184 /* ignore WME attributes if iface/sta is not capable */
4185 if (!(rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) ||
4186 !(params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)))
4187 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004188
Johannes Bergbdd90d52011-12-14 12:20:27 +01004189 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004190 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4191 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004192 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004193 /* but don't bother the driver with it */
4194 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03004195
Johannes Bergd582cff2012-10-26 17:53:44 +02004196 /* allow authenticated/associated only if driver handles it */
4197 if (!(rdev->wiphy.features &
4198 NL80211_FEATURE_FULL_AP_CLIENT_STATE) &&
4199 params.sta_flags_mask &
4200 (BIT(NL80211_STA_FLAG_AUTHENTICATED) |
4201 BIT(NL80211_STA_FLAG_ASSOCIATED)))
4202 return -EINVAL;
4203
Johannes Bergbdd90d52011-12-14 12:20:27 +01004204 /* must be last in here for error handling */
4205 params.vlan = get_vlan(info, rdev);
4206 if (IS_ERR(params.vlan))
4207 return PTR_ERR(params.vlan);
4208 break;
4209 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg984c3112013-02-14 23:43:25 +01004210 /* ignore uAPSD data */
4211 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4212
Johannes Bergd582cff2012-10-26 17:53:44 +02004213 /* associated is disallowed */
4214 if (params.sta_flags_mask & BIT(NL80211_STA_FLAG_ASSOCIATED))
4215 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004216 /* TDLS peers cannot be added */
Jouni Malinen3d124ea2013-05-27 18:24:02 +03004217 if ((params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)) ||
4218 info->attrs[NL80211_ATTR_PEER_AID])
Johannes Berg4319e192011-09-07 11:50:48 +02004219 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004220 break;
4221 case NL80211_IFTYPE_STATION:
Johannes Berg93d08f02013-03-04 09:29:46 +01004222 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg984c3112013-02-14 23:43:25 +01004223 /* ignore uAPSD data */
4224 params.sta_modify_mask &= ~STATION_PARAM_APPLY_UAPSD;
4225
Johannes Berg77ee7c82013-02-15 00:48:33 +01004226 /* these are disallowed */
4227 if (params.sta_flags_mask &
4228 (BIT(NL80211_STA_FLAG_ASSOCIATED) |
4229 BIT(NL80211_STA_FLAG_AUTHENTICATED)))
Johannes Bergd582cff2012-10-26 17:53:44 +02004230 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01004231 /* Only TDLS peers can be added */
4232 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
4233 return -EINVAL;
4234 /* Can only add if TDLS ... */
4235 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
4236 return -EOPNOTSUPP;
4237 /* ... with external setup is supported */
4238 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
4239 return -EOPNOTSUPP;
Johannes Berg77ee7c82013-02-15 00:48:33 +01004240 /*
4241 * Older wpa_supplicant versions always mark the TDLS peer
4242 * as authorized, but it shouldn't yet be.
4243 */
4244 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_AUTHORIZED);
Johannes Bergbdd90d52011-12-14 12:20:27 +01004245 break;
4246 default:
4247 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03004248 }
4249
Johannes Bergbdd90d52011-12-14 12:20:27 +01004250 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01004251
Hila Gonene35e4d22012-06-27 17:19:42 +03004252 err = rdev_add_station(rdev, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01004253
Johannes Berg5727ef12007-12-19 02:03:34 +01004254 if (params.vlan)
4255 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01004256 return err;
4257}
4258
4259static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
4260{
Johannes Berg4c476992010-10-04 21:36:35 +02004261 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4262 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01004263 u8 *mac_addr = NULL;
4264
4265 if (info->attrs[NL80211_ATTR_MAC])
4266 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
4267
Johannes Berge80cf852009-05-11 14:43:13 +02004268 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02004269 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004270 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004271 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4272 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02004273
Johannes Berg4c476992010-10-04 21:36:35 +02004274 if (!rdev->ops->del_station)
4275 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01004276
Hila Gonene35e4d22012-06-27 17:19:42 +03004277 return rdev_del_station(rdev, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01004278}
4279
Eric W. Biederman15e47302012-09-07 20:12:54 +00004280static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004281 int flags, struct net_device *dev,
4282 u8 *dst, u8 *next_hop,
4283 struct mpath_info *pinfo)
4284{
4285 void *hdr;
4286 struct nlattr *pinfoattr;
4287
Eric W. Biederman15e47302012-09-07 20:12:54 +00004288 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004289 if (!hdr)
4290 return -1;
4291
David S. Miller9360ffd2012-03-29 04:41:26 -04004292 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4293 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
4294 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
4295 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
4296 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02004297
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004298 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
4299 if (!pinfoattr)
4300 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004301 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
4302 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
4303 pinfo->frame_qlen))
4304 goto nla_put_failure;
4305 if (((pinfo->filled & MPATH_INFO_SN) &&
4306 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
4307 ((pinfo->filled & MPATH_INFO_METRIC) &&
4308 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
4309 pinfo->metric)) ||
4310 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
4311 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
4312 pinfo->exptime)) ||
4313 ((pinfo->filled & MPATH_INFO_FLAGS) &&
4314 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
4315 pinfo->flags)) ||
4316 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
4317 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
4318 pinfo->discovery_timeout)) ||
4319 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
4320 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
4321 pinfo->discovery_retries)))
4322 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004323
4324 nla_nest_end(msg, pinfoattr);
4325
4326 return genlmsg_end(msg, hdr);
4327
4328 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07004329 genlmsg_cancel(msg, hdr);
4330 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004331}
4332
4333static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004334 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004335{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004336 struct mpath_info pinfo;
4337 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02004338 struct wireless_dev *wdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004339 u8 dst[ETH_ALEN];
4340 u8 next_hop[ETH_ALEN];
Johannes Berg97990a02013-04-19 01:02:55 +02004341 int path_idx = cb->args[2];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004342 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004343
Johannes Berg97990a02013-04-19 01:02:55 +02004344 err = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02004345 if (err)
4346 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004347
4348 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004349 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004350 goto out_err;
4351 }
4352
Johannes Berg97990a02013-04-19 01:02:55 +02004353 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT) {
Jouni Malineneec60b02009-03-20 21:21:19 +02004354 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02004355 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02004356 }
4357
Johannes Bergbba95fe2008-07-29 13:22:51 +02004358 while (1) {
Johannes Berg97990a02013-04-19 01:02:55 +02004359 err = rdev_dump_mpath(dev, wdev->netdev, path_idx, dst,
4360 next_hop, &pinfo);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004361 if (err == -ENOENT)
4362 break;
4363 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01004364 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004365
Eric W. Biederman15e47302012-09-07 20:12:54 +00004366 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004367 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02004368 wdev->netdev, dst, next_hop,
Johannes Bergbba95fe2008-07-29 13:22:51 +02004369 &pinfo) < 0)
4370 goto out;
4371
4372 path_idx++;
4373 }
4374
4375
4376 out:
Johannes Berg97990a02013-04-19 01:02:55 +02004377 cb->args[2] = path_idx;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004378 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02004379 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02004380 nl80211_finish_wdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02004381 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004382}
4383
4384static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
4385{
Johannes Berg4c476992010-10-04 21:36:35 +02004386 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004387 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02004388 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004389 struct mpath_info pinfo;
4390 struct sk_buff *msg;
4391 u8 *dst = NULL;
4392 u8 next_hop[ETH_ALEN];
4393
4394 memset(&pinfo, 0, sizeof(pinfo));
4395
4396 if (!info->attrs[NL80211_ATTR_MAC])
4397 return -EINVAL;
4398
4399 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4400
Johannes Berg4c476992010-10-04 21:36:35 +02004401 if (!rdev->ops->get_mpath)
4402 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004403
Johannes Berg4c476992010-10-04 21:36:35 +02004404 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4405 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004406
Hila Gonene35e4d22012-06-27 17:19:42 +03004407 err = rdev_get_mpath(rdev, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004408 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004409 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004410
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004411 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004412 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02004413 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004414
Eric W. Biederman15e47302012-09-07 20:12:54 +00004415 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02004416 dev, dst, next_hop, &pinfo) < 0) {
4417 nlmsg_free(msg);
4418 return -ENOBUFS;
4419 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004420
Johannes Berg4c476992010-10-04 21:36:35 +02004421 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004422}
4423
4424static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
4425{
Johannes Berg4c476992010-10-04 21:36:35 +02004426 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4427 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004428 u8 *dst = NULL;
4429 u8 *next_hop = NULL;
4430
4431 if (!info->attrs[NL80211_ATTR_MAC])
4432 return -EINVAL;
4433
4434 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4435 return -EINVAL;
4436
4437 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4438 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4439
Johannes Berg4c476992010-10-04 21:36:35 +02004440 if (!rdev->ops->change_mpath)
4441 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004442
Johannes Berg4c476992010-10-04 21:36:35 +02004443 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4444 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004445
Hila Gonene35e4d22012-06-27 17:19:42 +03004446 return rdev_change_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004447}
Johannes Berg4c476992010-10-04 21:36:35 +02004448
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004449static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
4450{
Johannes Berg4c476992010-10-04 21:36:35 +02004451 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4452 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004453 u8 *dst = NULL;
4454 u8 *next_hop = NULL;
4455
4456 if (!info->attrs[NL80211_ATTR_MAC])
4457 return -EINVAL;
4458
4459 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
4460 return -EINVAL;
4461
4462 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4463 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
4464
Johannes Berg4c476992010-10-04 21:36:35 +02004465 if (!rdev->ops->add_mpath)
4466 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004467
Johannes Berg4c476992010-10-04 21:36:35 +02004468 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
4469 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004470
Hila Gonene35e4d22012-06-27 17:19:42 +03004471 return rdev_add_mpath(rdev, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004472}
4473
4474static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
4475{
Johannes Berg4c476992010-10-04 21:36:35 +02004476 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4477 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004478 u8 *dst = NULL;
4479
4480 if (info->attrs[NL80211_ATTR_MAC])
4481 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
4482
Johannes Berg4c476992010-10-04 21:36:35 +02004483 if (!rdev->ops->del_mpath)
4484 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01004485
Hila Gonene35e4d22012-06-27 17:19:42 +03004486 return rdev_del_mpath(rdev, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004487}
4488
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004489static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
4490{
Johannes Berg4c476992010-10-04 21:36:35 +02004491 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4492 struct net_device *dev = info->user_ptr[1];
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004493 struct wireless_dev *wdev = dev->ieee80211_ptr;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004494 struct bss_parameters params;
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004495 int err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004496
4497 memset(&params, 0, sizeof(params));
4498 /* default to not changing parameters */
4499 params.use_cts_prot = -1;
4500 params.use_short_preamble = -1;
4501 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004502 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01004503 params.ht_opmode = -1;
Johannes Berg53cabad2012-11-14 15:17:28 +01004504 params.p2p_ctwindow = -1;
4505 params.p2p_opp_ps = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004506
4507 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
4508 params.use_cts_prot =
4509 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
4510 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
4511 params.use_short_preamble =
4512 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
4513 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
4514 params.use_short_slot_time =
4515 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02004516 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4517 params.basic_rates =
4518 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4519 params.basic_rates_len =
4520 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4521 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02004522 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
4523 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01004524 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
4525 params.ht_opmode =
4526 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004527
Johannes Berg53cabad2012-11-14 15:17:28 +01004528 if (info->attrs[NL80211_ATTR_P2P_CTWINDOW]) {
4529 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4530 return -EINVAL;
4531 params.p2p_ctwindow =
4532 nla_get_s8(info->attrs[NL80211_ATTR_P2P_CTWINDOW]);
4533 if (params.p2p_ctwindow < 0)
4534 return -EINVAL;
4535 if (params.p2p_ctwindow != 0 &&
4536 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_CTWIN))
4537 return -EINVAL;
4538 }
4539
4540 if (info->attrs[NL80211_ATTR_P2P_OPPPS]) {
4541 u8 tmp;
4542
4543 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4544 return -EINVAL;
4545 tmp = nla_get_u8(info->attrs[NL80211_ATTR_P2P_OPPPS]);
4546 if (tmp > 1)
4547 return -EINVAL;
4548 params.p2p_opp_ps = tmp;
4549 if (params.p2p_opp_ps &&
4550 !(rdev->wiphy.features & NL80211_FEATURE_P2P_GO_OPPPS))
4551 return -EINVAL;
4552 }
4553
Johannes Berg4c476992010-10-04 21:36:35 +02004554 if (!rdev->ops->change_bss)
4555 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004556
Johannes Berg074ac8d2010-09-16 14:58:22 +02004557 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02004558 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4559 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004560
Simon Wunderlichc56589e2013-11-21 18:19:49 +01004561 wdev_lock(wdev);
4562 err = rdev_change_bss(rdev, dev, &params);
4563 wdev_unlock(wdev);
4564
4565 return err;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004566}
4567
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004568static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004569 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
4570 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
4571 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
4572 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
4573 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
4574 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
4575};
4576
4577static int parse_reg_rule(struct nlattr *tb[],
4578 struct ieee80211_reg_rule *reg_rule)
4579{
4580 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
4581 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
4582
4583 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
4584 return -EINVAL;
4585 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
4586 return -EINVAL;
4587 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
4588 return -EINVAL;
4589 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
4590 return -EINVAL;
4591 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
4592 return -EINVAL;
4593
4594 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
4595
4596 freq_range->start_freq_khz =
4597 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
4598 freq_range->end_freq_khz =
4599 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
4600 freq_range->max_bandwidth_khz =
4601 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
4602
4603 power_rule->max_eirp =
4604 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
4605
4606 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
4607 power_rule->max_antenna_gain =
4608 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
4609
4610 return 0;
4611}
4612
4613static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4614{
4615 int r;
4616 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004617 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004618
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004619 /*
4620 * You should only get this when cfg80211 hasn't yet initialized
4621 * completely when built-in to the kernel right between the time
4622 * window between nl80211_init() and regulatory_init(), if that is
4623 * even possible.
4624 */
Johannes Berg458f4f92012-12-06 15:47:38 +01004625 if (unlikely(!rcu_access_pointer(cfg80211_regdomain)))
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004626 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05004627
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004628 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4629 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004630
4631 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4632
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004633 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
4634 user_reg_hint_type =
4635 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
4636 else
4637 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
4638
4639 switch (user_reg_hint_type) {
4640 case NL80211_USER_REG_HINT_USER:
4641 case NL80211_USER_REG_HINT_CELL_BASE:
4642 break;
4643 default:
4644 return -EINVAL;
4645 }
4646
4647 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05004648
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004649 return r;
4650}
4651
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004652static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004653 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004654{
Johannes Berg4c476992010-10-04 21:36:35 +02004655 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02004656 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01004657 struct wireless_dev *wdev = dev->ieee80211_ptr;
4658 struct mesh_config cur_params;
4659 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004660 void *hdr;
4661 struct nlattr *pinfoattr;
4662 struct sk_buff *msg;
4663
Johannes Berg29cbe682010-12-03 09:20:44 +01004664 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
4665 return -EOPNOTSUPP;
4666
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004667 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02004668 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02004669
Johannes Berg29cbe682010-12-03 09:20:44 +01004670 wdev_lock(wdev);
4671 /* If not connected, get default parameters */
4672 if (!wdev->mesh_id_len)
4673 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
4674 else
Hila Gonene35e4d22012-06-27 17:19:42 +03004675 err = rdev_get_mesh_config(rdev, dev, &cur_params);
Johannes Berg29cbe682010-12-03 09:20:44 +01004676 wdev_unlock(wdev);
4677
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004678 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02004679 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004680
4681 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004682 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004683 if (!msg)
4684 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00004685 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004686 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004687 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004688 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004689 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004690 if (!pinfoattr)
4691 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004692 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
4693 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
4694 cur_params.dot11MeshRetryTimeout) ||
4695 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4696 cur_params.dot11MeshConfirmTimeout) ||
4697 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
4698 cur_params.dot11MeshHoldingTimeout) ||
4699 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
4700 cur_params.dot11MeshMaxPeerLinks) ||
4701 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
4702 cur_params.dot11MeshMaxRetries) ||
4703 nla_put_u8(msg, NL80211_MESHCONF_TTL,
4704 cur_params.dot11MeshTTL) ||
4705 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
4706 cur_params.element_ttl) ||
4707 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4708 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04004709 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4710 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04004711 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4712 cur_params.dot11MeshHWMPmaxPREQretries) ||
4713 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
4714 cur_params.path_refresh_time) ||
4715 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4716 cur_params.min_discovery_timeout) ||
4717 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4718 cur_params.dot11MeshHWMPactivePathTimeout) ||
4719 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
4720 cur_params.dot11MeshHWMPpreqMinInterval) ||
4721 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
4722 cur_params.dot11MeshHWMPperrMinInterval) ||
4723 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4724 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
4725 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
4726 cur_params.dot11MeshHWMPRootMode) ||
4727 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
4728 cur_params.dot11MeshHWMPRannInterval) ||
4729 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
4730 cur_params.dot11MeshGateAnnouncementProtocol) ||
4731 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
4732 cur_params.dot11MeshForwarding) ||
4733 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07004734 cur_params.rssi_threshold) ||
4735 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004736 cur_params.ht_opmode) ||
4737 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4738 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
4739 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004740 cur_params.dot11MeshHWMProotInterval) ||
4741 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004742 cur_params.dot11MeshHWMPconfirmationInterval) ||
4743 nla_put_u32(msg, NL80211_MESHCONF_POWER_MODE,
4744 cur_params.power_mode) ||
4745 nla_put_u16(msg, NL80211_MESHCONF_AWAKE_WINDOW,
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004746 cur_params.dot11MeshAwakeWindowDuration) ||
4747 nla_put_u32(msg, NL80211_MESHCONF_PLINK_TIMEOUT,
4748 cur_params.plink_timeout))
David S. Miller9360ffd2012-03-29 04:41:26 -04004749 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004750 nla_nest_end(msg, pinfoattr);
4751 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004752 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004753
Johannes Berg3b858752009-03-12 09:55:09 +01004754 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004755 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004756 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04004757 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02004758 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004759}
4760
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004761static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004762 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
4763 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
4764 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
4765 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
4766 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
4767 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01004768 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004769 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07004770 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004771 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
4772 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
4773 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
4774 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
4775 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08004776 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004777 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07004778 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07004779 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07004780 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08004781 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004782 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
4783 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004784 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
4785 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004786 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004787 [NL80211_MESHCONF_POWER_MODE] = { .type = NLA_U32 },
4788 [NL80211_MESHCONF_AWAKE_WINDOW] = { .type = NLA_U16 },
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004789 [NL80211_MESHCONF_PLINK_TIMEOUT] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004790};
4791
Javier Cardonac80d5452010-12-16 17:37:49 -08004792static const struct nla_policy
4793 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07004794 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08004795 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
4796 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07004797 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Colleen Twitty6e16d902013-05-08 11:45:59 -07004798 [NL80211_MESH_SETUP_AUTH_PROTOCOL] = { .type = NLA_U8 },
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004799 [NL80211_MESH_SETUP_USERSPACE_MPM] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07004800 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004801 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07004802 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08004803};
4804
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004805static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004806 struct mesh_config *cfg,
4807 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004808{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004809 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004810 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004811
Marco Porschea54fba2013-01-07 16:04:48 +01004812#define FILL_IN_MESH_PARAM_IF_SET(tb, cfg, param, min, max, mask, attr, fn) \
4813do { \
4814 if (tb[attr]) { \
4815 if (fn(tb[attr]) < min || fn(tb[attr]) > max) \
4816 return -EINVAL; \
4817 cfg->param = fn(tb[attr]); \
4818 mask |= (1 << (attr - 1)); \
4819 } \
4820} while (0)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004821
4822
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004823 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004824 return -EINVAL;
4825 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004826 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004827 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004828 return -EINVAL;
4829
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004830 /* This makes sure that there aren't more than 32 mesh config
4831 * parameters (otherwise our bitfield scheme would not work.) */
4832 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
4833
4834 /* Fill in the params struct */
Marco Porschea54fba2013-01-07 16:04:48 +01004835 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004836 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
4837 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004838 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004839 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
4840 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004841 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004842 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
4843 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004844 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004845 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
4846 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004847 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004848 mask, NL80211_MESHCONF_MAX_RETRIES,
4849 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004850 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004851 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004852 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl, 1, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004853 mask, NL80211_MESHCONF_ELEMENT_TTL,
4854 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004855 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004856 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
4857 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004858 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor,
4859 1, 255, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004860 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
4861 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004862 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries, 0, 255,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004863 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
4864 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004865 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004866 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
4867 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004868 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout, 1, 65535,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004869 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
4870 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004871 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
4872 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004873 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
4874 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004875 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004876 1, 65535, mask,
4877 NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004878 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08004879 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Marco Porschea54fba2013-01-07 16:04:48 +01004880 1, 65535, mask,
4881 NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004882 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004883 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004884 dot11MeshHWMPnetDiameterTraversalTime,
4885 1, 65535, mask,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004886 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
4887 nla_get_u16);
Marco Porschea54fba2013-01-07 16:04:48 +01004888 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, 0, 4,
4889 mask, NL80211_MESHCONF_HWMP_ROOTMODE,
4890 nla_get_u8);
4891 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, 1, 65535,
4892 mask, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004893 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00004894 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004895 dot11MeshGateAnnouncementProtocol, 0, 1,
4896 mask, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004897 nla_get_u8);
Marco Porschea54fba2013-01-07 16:04:48 +01004898 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding, 0, 1,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004899 mask, NL80211_MESHCONF_FORWARDING,
4900 nla_get_u8);
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004901 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold, -255, 0,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004902 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
Chun-Yeow Yeoh83374fe2013-07-11 18:24:03 +08004903 nla_get_s32);
Marco Porschea54fba2013-01-07 16:04:48 +01004904 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode, 0, 16,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08004905 mask, NL80211_MESHCONF_HT_OPMODE,
4906 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004907 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
Marco Porschea54fba2013-01-07 16:04:48 +01004908 1, 65535, mask,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004909 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
4910 nla_get_u32);
Marco Porschea54fba2013-01-07 16:04:48 +01004911 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval, 1, 65535,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08004912 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
4913 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004914 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Marco Porschea54fba2013-01-07 16:04:48 +01004915 dot11MeshHWMPconfirmationInterval,
4916 1, 65535, mask,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08004917 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
4918 nla_get_u16);
Marco Porsch3b1c5a52013-01-07 16:04:52 +01004919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, power_mode,
4920 NL80211_MESH_POWER_ACTIVE,
4921 NL80211_MESH_POWER_MAX,
4922 mask, NL80211_MESHCONF_POWER_MODE,
4923 nla_get_u32);
4924 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
4925 0, 65535, mask,
4926 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
Colleen Twitty8e7c0532013-06-03 09:53:39 -07004927 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff,
4928 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
4929 nla_get_u32);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004930 if (mask_out)
4931 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08004932
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004933 return 0;
4934
4935#undef FILL_IN_MESH_PARAM_IF_SET
4936}
4937
Javier Cardonac80d5452010-12-16 17:37:49 -08004938static int nl80211_parse_mesh_setup(struct genl_info *info,
4939 struct mesh_setup *setup)
4940{
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004941 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Javier Cardonac80d5452010-12-16 17:37:49 -08004942 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
4943
4944 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
4945 return -EINVAL;
4946 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
4947 info->attrs[NL80211_ATTR_MESH_SETUP],
4948 nl80211_mesh_setup_params_policy))
4949 return -EINVAL;
4950
Javier Cardonad299a1f2012-03-31 11:31:33 -07004951 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
4952 setup->sync_method =
4953 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
4954 IEEE80211_SYNC_METHOD_VENDOR :
4955 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
4956
Javier Cardonac80d5452010-12-16 17:37:49 -08004957 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
4958 setup->path_sel_proto =
4959 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
4960 IEEE80211_PATH_PROTOCOL_VENDOR :
4961 IEEE80211_PATH_PROTOCOL_HWMP;
4962
4963 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
4964 setup->path_metric =
4965 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
4966 IEEE80211_PATH_METRIC_VENDOR :
4967 IEEE80211_PATH_METRIC_AIRTIME;
4968
Javier Cardona581a8b02011-04-07 15:08:27 -07004969
4970 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08004971 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07004972 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08004973 if (!is_valid_ie_attr(ieattr))
4974 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07004975 setup->ie = nla_data(ieattr);
4976 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08004977 }
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004978 if (tb[NL80211_MESH_SETUP_USERSPACE_MPM] &&
4979 !(rdev->wiphy.features & NL80211_FEATURE_USERSPACE_MPM))
4980 return -EINVAL;
4981 setup->user_mpm = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_MPM]);
Javier Cardonab130e5c2011-05-03 16:57:07 -07004982 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
4983 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Thomas Pedersenbb2798d2013-03-04 13:06:10 -08004984 if (setup->is_secure)
4985 setup->user_mpm = true;
Javier Cardonac80d5452010-12-16 17:37:49 -08004986
Colleen Twitty6e16d902013-05-08 11:45:59 -07004987 if (tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]) {
4988 if (!setup->user_mpm)
4989 return -EINVAL;
4990 setup->auth_id =
4991 nla_get_u8(tb[NL80211_MESH_SETUP_AUTH_PROTOCOL]);
4992 }
4993
Javier Cardonac80d5452010-12-16 17:37:49 -08004994 return 0;
4995}
4996
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004997static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01004998 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004999{
5000 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5001 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01005002 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005003 struct mesh_config cfg;
5004 u32 mask;
5005 int err;
5006
Johannes Berg29cbe682010-12-03 09:20:44 +01005007 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
5008 return -EOPNOTSUPP;
5009
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005010 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005011 return -EOPNOTSUPP;
5012
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005013 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01005014 if (err)
5015 return err;
5016
Johannes Berg29cbe682010-12-03 09:20:44 +01005017 wdev_lock(wdev);
5018 if (!wdev->mesh_id_len)
5019 err = -ENOLINK;
5020
5021 if (!err)
Hila Gonene35e4d22012-06-27 17:19:42 +03005022 err = rdev_update_mesh_config(rdev, dev, mask, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005023
5024 wdev_unlock(wdev);
5025
5026 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005027}
5028
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005029static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
5030{
Johannes Berg458f4f92012-12-06 15:47:38 +01005031 const struct ieee80211_regdomain *regdom;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005032 struct sk_buff *msg;
5033 void *hdr = NULL;
5034 struct nlattr *nl_reg_rules;
5035 unsigned int i;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005036
5037 if (!cfg80211_regdomain)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005038 return -EINVAL;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005039
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005040 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005041 if (!msg)
5042 return -ENOBUFS;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005043
Eric W. Biederman15e47302012-09-07 20:12:54 +00005044 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005045 NL80211_CMD_GET_REG);
5046 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01005047 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005048
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07005049 if (reg_last_request_cell_base() &&
5050 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
5051 NL80211_USER_REG_HINT_CELL_BASE))
5052 goto nla_put_failure;
5053
Johannes Berg458f4f92012-12-06 15:47:38 +01005054 rcu_read_lock();
5055 regdom = rcu_dereference(cfg80211_regdomain);
5056
5057 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2, regdom->alpha2) ||
5058 (regdom->dfs_region &&
5059 nla_put_u8(msg, NL80211_ATTR_DFS_REGION, regdom->dfs_region)))
5060 goto nla_put_failure_rcu;
5061
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005062 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
5063 if (!nl_reg_rules)
Johannes Berg458f4f92012-12-06 15:47:38 +01005064 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005065
Johannes Berg458f4f92012-12-06 15:47:38 +01005066 for (i = 0; i < regdom->n_reg_rules; i++) {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005067 struct nlattr *nl_reg_rule;
5068 const struct ieee80211_reg_rule *reg_rule;
5069 const struct ieee80211_freq_range *freq_range;
5070 const struct ieee80211_power_rule *power_rule;
5071
Johannes Berg458f4f92012-12-06 15:47:38 +01005072 reg_rule = &regdom->reg_rules[i];
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005073 freq_range = &reg_rule->freq_range;
5074 power_rule = &reg_rule->power_rule;
5075
5076 nl_reg_rule = nla_nest_start(msg, i);
5077 if (!nl_reg_rule)
Johannes Berg458f4f92012-12-06 15:47:38 +01005078 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005079
David S. Miller9360ffd2012-03-29 04:41:26 -04005080 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
5081 reg_rule->flags) ||
5082 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
5083 freq_range->start_freq_khz) ||
5084 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
5085 freq_range->end_freq_khz) ||
5086 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
5087 freq_range->max_bandwidth_khz) ||
5088 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
5089 power_rule->max_antenna_gain) ||
5090 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
5091 power_rule->max_eirp))
Johannes Berg458f4f92012-12-06 15:47:38 +01005092 goto nla_put_failure_rcu;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005093
5094 nla_nest_end(msg, nl_reg_rule);
5095 }
Johannes Berg458f4f92012-12-06 15:47:38 +01005096 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005097
5098 nla_nest_end(msg, nl_reg_rules);
5099
5100 genlmsg_end(msg, hdr);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005101 return genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005102
Johannes Berg458f4f92012-12-06 15:47:38 +01005103nla_put_failure_rcu:
5104 rcu_read_unlock();
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005105nla_put_failure:
5106 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01005107put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04005108 nlmsg_free(msg);
Johannes Berg5fe231e2013-05-08 21:45:15 +02005109 return -EMSGSIZE;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005110}
5111
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005112static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5113{
5114 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
5115 struct nlattr *nl_reg_rule;
5116 char *alpha2 = NULL;
5117 int rem_reg_rules = 0, r = 0;
5118 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez4c7d3982013-11-13 18:54:02 +01005119 enum nl80211_dfs_regions dfs_region = NL80211_DFS_UNSET;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005120 struct ieee80211_regdomain *rd = NULL;
5121
5122 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
5123 return -EINVAL;
5124
5125 if (!info->attrs[NL80211_ATTR_REG_RULES])
5126 return -EINVAL;
5127
5128 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
5129
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005130 if (info->attrs[NL80211_ATTR_DFS_REGION])
5131 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
5132
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005133 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005134 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005135 num_rules++;
5136 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04005137 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005138 }
5139
Luis R. Rodrigueze4387682013-11-05 09:18:01 -08005140 if (!reg_is_valid_request(alpha2))
5141 return -EINVAL;
5142
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005143 size_of_regd = sizeof(struct ieee80211_regdomain) +
Johannes Berg1a919312012-12-03 17:21:11 +01005144 num_rules * sizeof(struct ieee80211_reg_rule);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005145
5146 rd = kzalloc(size_of_regd, GFP_KERNEL);
Johannes Berg6913b492012-12-04 00:48:59 +01005147 if (!rd)
5148 return -ENOMEM;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005149
5150 rd->n_reg_rules = num_rules;
5151 rd->alpha2[0] = alpha2[0];
5152 rd->alpha2[1] = alpha2[1];
5153
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07005154 /*
5155 * Disable DFS master mode if the DFS region was
5156 * not supported or known on this kernel.
5157 */
5158 if (reg_supported_dfs_region(dfs_region))
5159 rd->dfs_region = dfs_region;
5160
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005161 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
Johannes Berg1a919312012-12-03 17:21:11 +01005162 rem_reg_rules) {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005163 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
Johannes Berg1a919312012-12-03 17:21:11 +01005164 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
5165 reg_rule_policy);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005166 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
5167 if (r)
5168 goto bad_reg;
5169
5170 rule_idx++;
5171
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005172 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
5173 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005174 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005175 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005176 }
5177
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005178 r = set_regdom(rd);
Johannes Berg6913b492012-12-04 00:48:59 +01005179 /* set_regdom took ownership */
Johannes Berg1a919312012-12-03 17:21:11 +01005180 rd = NULL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005181
Johannes Bergd2372b32008-10-24 20:32:20 +02005182 bad_reg:
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005183 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04005184 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005185}
5186
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005187static int validate_scan_freqs(struct nlattr *freqs)
5188{
5189 struct nlattr *attr1, *attr2;
5190 int n_channels = 0, tmp1, tmp2;
5191
5192 nla_for_each_nested(attr1, freqs, tmp1) {
5193 n_channels++;
5194 /*
5195 * Some hardware has a limited channel list for
5196 * scanning, and it is pretty much nonsensical
5197 * to scan for a channel twice, so disallow that
5198 * and don't require drivers to check that the
5199 * channel list they get isn't longer than what
5200 * they can scan, as long as they can scan all
5201 * the channels they registered at once.
5202 */
5203 nla_for_each_nested(attr2, freqs, tmp2)
5204 if (attr1 != attr2 &&
5205 nla_get_u32(attr1) == nla_get_u32(attr2))
5206 return 0;
5207 }
5208
5209 return n_channels;
5210}
5211
Johannes Berg2a519312009-02-10 21:25:55 +01005212static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5213{
Johannes Berg4c476992010-10-04 21:36:35 +02005214 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02005215 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01005216 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01005217 struct nlattr *attr;
5218 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005219 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005220 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01005221
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005222 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5223 return -EINVAL;
5224
Johannes Berg79c97e92009-07-07 03:56:12 +02005225 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01005226
Johannes Berg4c476992010-10-04 21:36:35 +02005227 if (!rdev->ops->scan)
5228 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01005229
Johannes Bergf9f47522013-03-19 15:04:07 +01005230 if (rdev->scan_req) {
5231 err = -EBUSY;
5232 goto unlock;
5233 }
Johannes Berg2a519312009-02-10 21:25:55 +01005234
5235 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005236 n_channels = validate_scan_freqs(
5237 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Bergf9f47522013-03-19 15:04:07 +01005238 if (!n_channels) {
5239 err = -EINVAL;
5240 goto unlock;
5241 }
Johannes Berg2a519312009-02-10 21:25:55 +01005242 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005243 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02005244 n_channels = 0;
5245
Johannes Berg2a519312009-02-10 21:25:55 +01005246 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5247 if (wiphy->bands[band])
5248 n_channels += wiphy->bands[band]->n_channels;
5249 }
5250
5251 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5252 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
5253 n_ssids++;
5254
Johannes Bergf9f47522013-03-19 15:04:07 +01005255 if (n_ssids > wiphy->max_scan_ssids) {
5256 err = -EINVAL;
5257 goto unlock;
5258 }
Johannes Berg2a519312009-02-10 21:25:55 +01005259
Jouni Malinen70692ad2009-02-16 19:39:13 +02005260 if (info->attrs[NL80211_ATTR_IE])
5261 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5262 else
5263 ie_len = 0;
5264
Johannes Bergf9f47522013-03-19 15:04:07 +01005265 if (ie_len > wiphy->max_scan_ie_len) {
5266 err = -EINVAL;
5267 goto unlock;
5268 }
Johannes Berg18a83652009-03-31 12:12:05 +02005269
Johannes Berg2a519312009-02-10 21:25:55 +01005270 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005271 + sizeof(*request->ssids) * n_ssids
5272 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02005273 + ie_len, GFP_KERNEL);
Johannes Bergf9f47522013-03-19 15:04:07 +01005274 if (!request) {
5275 err = -ENOMEM;
5276 goto unlock;
5277 }
Johannes Berg2a519312009-02-10 21:25:55 +01005278
Johannes Berg2a519312009-02-10 21:25:55 +01005279 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02005280 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01005281 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02005282 if (ie_len) {
5283 if (request->ssids)
5284 request->ie = (void *)(request->ssids + n_ssids);
5285 else
5286 request->ie = (void *)(request->channels + n_channels);
5287 }
Johannes Berg2a519312009-02-10 21:25:55 +01005288
Johannes Berg584991d2009-11-02 13:32:03 +01005289 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005290 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5291 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01005292 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01005293 struct ieee80211_channel *chan;
5294
5295 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5296
5297 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01005298 err = -EINVAL;
5299 goto out_free;
5300 }
Johannes Berg584991d2009-11-02 13:32:03 +01005301
5302 /* ignore disabled channels */
5303 if (chan->flags & IEEE80211_CHAN_DISABLED)
5304 continue;
5305
5306 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005307 i++;
5308 }
5309 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02005310 enum ieee80211_band band;
5311
Johannes Berg2a519312009-02-10 21:25:55 +01005312 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01005313 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5314 int j;
5315 if (!wiphy->bands[band])
5316 continue;
5317 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01005318 struct ieee80211_channel *chan;
5319
5320 chan = &wiphy->bands[band]->channels[j];
5321
5322 if (chan->flags & IEEE80211_CHAN_DISABLED)
5323 continue;
5324
5325 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01005326 i++;
5327 }
5328 }
5329 }
5330
Johannes Berg584991d2009-11-02 13:32:03 +01005331 if (!i) {
5332 err = -EINVAL;
5333 goto out_free;
5334 }
5335
5336 request->n_channels = i;
5337
Johannes Berg2a519312009-02-10 21:25:55 +01005338 i = 0;
5339 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5340 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005341 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01005342 err = -EINVAL;
5343 goto out_free;
5344 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005345 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01005346 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01005347 i++;
5348 }
5349 }
5350
Jouni Malinen70692ad2009-02-16 19:39:13 +02005351 if (info->attrs[NL80211_ATTR_IE]) {
5352 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02005353 memcpy((void *)request->ie,
5354 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02005355 request->ie_len);
5356 }
5357
Johannes Berg34850ab2011-07-18 18:08:35 +02005358 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02005359 if (wiphy->bands[i])
5360 request->rates[i] =
5361 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02005362
5363 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
5364 nla_for_each_nested(attr,
5365 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
5366 tmp) {
5367 enum ieee80211_band band = nla_type(attr);
5368
Dan Carpenter84404622011-07-29 11:52:18 +03005369 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02005370 err = -EINVAL;
5371 goto out_free;
5372 }
5373 err = ieee80211_get_ratemask(wiphy->bands[band],
5374 nla_data(attr),
5375 nla_len(attr),
5376 &request->rates[band]);
5377 if (err)
5378 goto out_free;
5379 }
5380 }
5381
Sam Leffler46856bb2012-10-11 21:03:32 -07005382 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005383 request->flags = nla_get_u32(
5384 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005385 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5386 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005387 err = -EOPNOTSUPP;
5388 goto out_free;
5389 }
5390 }
Sam Lefflered4737712012-10-11 21:03:31 -07005391
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05305392 request->no_cck =
5393 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
5394
Johannes Bergfd014282012-06-18 19:17:03 +02005395 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02005396 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07005397 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01005398
Johannes Berg79c97e92009-07-07 03:56:12 +02005399 rdev->scan_req = request;
Hila Gonene35e4d22012-06-27 17:19:42 +03005400 err = rdev_scan(rdev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01005401
Johannes Berg463d0182009-07-14 00:33:35 +02005402 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02005403 nl80211_send_scan_start(rdev, wdev);
5404 if (wdev->netdev)
5405 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02005406 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01005407 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02005408 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01005409 kfree(request);
5410 }
Johannes Berg3b858752009-03-12 09:55:09 +01005411
Johannes Bergf9f47522013-03-19 15:04:07 +01005412 unlock:
Johannes Berg2a519312009-02-10 21:25:55 +01005413 return err;
5414}
5415
Luciano Coelho807f8a82011-05-11 17:09:35 +03005416static int nl80211_start_sched_scan(struct sk_buff *skb,
5417 struct genl_info *info)
5418{
5419 struct cfg80211_sched_scan_request *request;
5420 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5421 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005422 struct nlattr *attr;
5423 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005424 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005425 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005426 enum ieee80211_band band;
5427 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005428 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03005429
5430 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5431 !rdev->ops->sched_scan_start)
5432 return -EOPNOTSUPP;
5433
5434 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5435 return -EINVAL;
5436
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005437 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
5438 return -EINVAL;
5439
5440 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
5441 if (interval == 0)
5442 return -EINVAL;
5443
Luciano Coelho807f8a82011-05-11 17:09:35 +03005444 wiphy = &rdev->wiphy;
5445
5446 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5447 n_channels = validate_scan_freqs(
5448 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
5449 if (!n_channels)
5450 return -EINVAL;
5451 } else {
5452 n_channels = 0;
5453
5454 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
5455 if (wiphy->bands[band])
5456 n_channels += wiphy->bands[band]->n_channels;
5457 }
5458
5459 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
5460 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5461 tmp)
5462 n_ssids++;
5463
Luciano Coelho93b6aa62011-07-13 14:57:28 +03005464 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005465 return -EINVAL;
5466
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005467 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
5468 nla_for_each_nested(attr,
5469 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5470 tmp)
5471 n_match_sets++;
5472
5473 if (n_match_sets > wiphy->max_match_sets)
5474 return -EINVAL;
5475
Luciano Coelho807f8a82011-05-11 17:09:35 +03005476 if (info->attrs[NL80211_ATTR_IE])
5477 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5478 else
5479 ie_len = 0;
5480
Luciano Coelho5a865ba2011-07-13 14:57:29 +03005481 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03005482 return -EINVAL;
5483
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005484 if (rdev->sched_scan_req) {
5485 err = -EINPROGRESS;
5486 goto out;
5487 }
5488
Luciano Coelho807f8a82011-05-11 17:09:35 +03005489 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005490 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005491 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03005492 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03005493 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03005494 if (!request) {
5495 err = -ENOMEM;
5496 goto out;
5497 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03005498
5499 if (n_ssids)
5500 request->ssids = (void *)&request->channels[n_channels];
5501 request->n_ssids = n_ssids;
5502 if (ie_len) {
5503 if (request->ssids)
5504 request->ie = (void *)(request->ssids + n_ssids);
5505 else
5506 request->ie = (void *)(request->channels + n_channels);
5507 }
5508
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005509 if (n_match_sets) {
5510 if (request->ie)
5511 request->match_sets = (void *)(request->ie + ie_len);
5512 else if (request->ssids)
5513 request->match_sets =
5514 (void *)(request->ssids + n_ssids);
5515 else
5516 request->match_sets =
5517 (void *)(request->channels + n_channels);
5518 }
5519 request->n_match_sets = n_match_sets;
5520
Luciano Coelho807f8a82011-05-11 17:09:35 +03005521 i = 0;
5522 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
5523 /* user specified, bail out if channel not found */
5524 nla_for_each_nested(attr,
5525 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
5526 tmp) {
5527 struct ieee80211_channel *chan;
5528
5529 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
5530
5531 if (!chan) {
5532 err = -EINVAL;
5533 goto out_free;
5534 }
5535
5536 /* ignore disabled channels */
5537 if (chan->flags & IEEE80211_CHAN_DISABLED)
5538 continue;
5539
5540 request->channels[i] = chan;
5541 i++;
5542 }
5543 } else {
5544 /* all channels */
5545 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5546 int j;
5547 if (!wiphy->bands[band])
5548 continue;
5549 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
5550 struct ieee80211_channel *chan;
5551
5552 chan = &wiphy->bands[band]->channels[j];
5553
5554 if (chan->flags & IEEE80211_CHAN_DISABLED)
5555 continue;
5556
5557 request->channels[i] = chan;
5558 i++;
5559 }
5560 }
5561 }
5562
5563 if (!i) {
5564 err = -EINVAL;
5565 goto out_free;
5566 }
5567
5568 request->n_channels = i;
5569
5570 i = 0;
5571 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
5572 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
5573 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03005574 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005575 err = -EINVAL;
5576 goto out_free;
5577 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03005578 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005579 memcpy(request->ssids[i].ssid, nla_data(attr),
5580 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03005581 i++;
5582 }
5583 }
5584
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005585 i = 0;
5586 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
5587 nla_for_each_nested(attr,
5588 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
5589 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005590 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005591
5592 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
5593 nla_data(attr), nla_len(attr),
5594 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02005595 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005596 if (ssid) {
5597 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
5598 err = -EINVAL;
5599 goto out_free;
5600 }
5601 memcpy(request->match_sets[i].ssid.ssid,
5602 nla_data(ssid), nla_len(ssid));
5603 request->match_sets[i].ssid.ssid_len =
5604 nla_len(ssid);
5605 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07005606 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
5607 if (rssi)
5608 request->rssi_thold = nla_get_u32(rssi);
5609 else
5610 request->rssi_thold =
5611 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03005612 i++;
5613 }
5614 }
5615
Luciano Coelho807f8a82011-05-11 17:09:35 +03005616 if (info->attrs[NL80211_ATTR_IE]) {
5617 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5618 memcpy((void *)request->ie,
5619 nla_data(info->attrs[NL80211_ATTR_IE]),
5620 request->ie_len);
5621 }
5622
Sam Leffler46856bb2012-10-11 21:03:32 -07005623 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07005624 request->flags = nla_get_u32(
5625 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Johannes Berg00c3a6e2013-10-26 17:14:38 +02005626 if ((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
5627 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) {
Sam Leffler46856bb2012-10-11 21:03:32 -07005628 err = -EOPNOTSUPP;
5629 goto out_free;
5630 }
5631 }
Sam Lefflered4737712012-10-11 21:03:31 -07005632
Luciano Coelho807f8a82011-05-11 17:09:35 +03005633 request->dev = dev;
5634 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03005635 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07005636 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03005637
Hila Gonene35e4d22012-06-27 17:19:42 +03005638 err = rdev_sched_scan_start(rdev, dev, request);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005639 if (!err) {
5640 rdev->sched_scan_req = request;
5641 nl80211_send_sched_scan(rdev, dev,
5642 NL80211_CMD_START_SCHED_SCAN);
5643 goto out;
5644 }
5645
5646out_free:
5647 kfree(request);
5648out:
5649 return err;
5650}
5651
5652static int nl80211_stop_sched_scan(struct sk_buff *skb,
5653 struct genl_info *info)
5654{
5655 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5656
5657 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
5658 !rdev->ops->sched_scan_stop)
5659 return -EOPNOTSUPP;
5660
Johannes Berg5fe231e2013-05-08 21:45:15 +02005661 return __cfg80211_stop_sched_scan(rdev, false);
Luciano Coelho807f8a82011-05-11 17:09:35 +03005662}
5663
Simon Wunderlich04f39042013-02-08 18:16:19 +01005664static int nl80211_start_radar_detection(struct sk_buff *skb,
5665 struct genl_info *info)
5666{
5667 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5668 struct net_device *dev = info->user_ptr[1];
5669 struct wireless_dev *wdev = dev->ieee80211_ptr;
5670 struct cfg80211_chan_def chandef;
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005671 enum nl80211_dfs_regions dfs_region;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005672 int err;
5673
Luis R. Rodriguez55f74352013-11-25 20:56:10 +01005674 dfs_region = reg_get_dfs_region(wdev->wiphy);
5675 if (dfs_region == NL80211_DFS_UNSET)
5676 return -EINVAL;
5677
Simon Wunderlich04f39042013-02-08 18:16:19 +01005678 err = nl80211_parse_chandef(rdev, info, &chandef);
5679 if (err)
5680 return err;
5681
Simon Wunderlichff311bc2013-09-03 19:43:18 +02005682 if (netif_carrier_ok(dev))
5683 return -EBUSY;
5684
Simon Wunderlich04f39042013-02-08 18:16:19 +01005685 if (wdev->cac_started)
5686 return -EBUSY;
5687
5688 err = cfg80211_chandef_dfs_required(wdev->wiphy, &chandef);
5689 if (err < 0)
5690 return err;
5691
5692 if (err == 0)
5693 return -EINVAL;
5694
Janusz Dziedzicfe7c3a12013-11-05 14:48:48 +01005695 if (!cfg80211_chandef_dfs_usable(wdev->wiphy, &chandef))
Simon Wunderlich04f39042013-02-08 18:16:19 +01005696 return -EINVAL;
5697
5698 if (!rdev->ops->start_radar_detection)
5699 return -EOPNOTSUPP;
5700
Simon Wunderlich04f39042013-02-08 18:16:19 +01005701 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5702 chandef.chan, CHAN_MODE_SHARED,
5703 BIT(chandef.width));
5704 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02005705 return err;
Simon Wunderlich04f39042013-02-08 18:16:19 +01005706
5707 err = rdev->ops->start_radar_detection(&rdev->wiphy, dev, &chandef);
5708 if (!err) {
5709 wdev->channel = chandef.chan;
5710 wdev->cac_started = true;
5711 wdev->cac_start_time = jiffies;
5712 }
Simon Wunderlich04f39042013-02-08 18:16:19 +01005713 return err;
5714}
5715
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005716static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5717{
5718 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5719 struct net_device *dev = info->user_ptr[1];
5720 struct wireless_dev *wdev = dev->ieee80211_ptr;
5721 struct cfg80211_csa_settings params;
5722 /* csa_attrs is defined static to avoid waste of stack size - this
5723 * function is called under RTNL lock, so this should not be a problem.
5724 */
5725 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5726 u8 radar_detect_width = 0;
5727 int err;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005728 bool need_new_beacon = false;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005729
5730 if (!rdev->ops->channel_switch ||
5731 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5732 return -EOPNOTSUPP;
5733
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005734 switch (dev->ieee80211_ptr->iftype) {
5735 case NL80211_IFTYPE_AP:
5736 case NL80211_IFTYPE_P2P_GO:
5737 need_new_beacon = true;
5738
5739 /* useless if AP is not running */
5740 if (!wdev->beacon_interval)
5741 return -EINVAL;
5742 break;
5743 case NL80211_IFTYPE_ADHOC:
Chun-Yeow Yeohc6da6742013-10-14 19:08:28 -07005744 case NL80211_IFTYPE_MESH_POINT:
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005745 break;
5746 default:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005747 return -EOPNOTSUPP;
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005748 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005749
5750 memset(&params, 0, sizeof(params));
5751
5752 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5753 !info->attrs[NL80211_ATTR_CH_SWITCH_COUNT])
5754 return -EINVAL;
5755
5756 /* only important for AP, IBSS and mesh create IEs internally */
Andrei Otcheretianskid0a361a2013-10-17 10:52:17 +02005757 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005758 return -EINVAL;
5759
5760 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5761
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005762 if (!need_new_beacon)
5763 goto skip_beacons;
5764
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005765 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5766 if (err)
5767 return err;
5768
5769 err = nla_parse_nested(csa_attrs, NL80211_ATTR_MAX,
5770 info->attrs[NL80211_ATTR_CSA_IES],
5771 nl80211_policy);
5772 if (err)
5773 return err;
5774
5775 err = nl80211_parse_beacon(csa_attrs, &params.beacon_csa);
5776 if (err)
5777 return err;
5778
5779 if (!csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON])
5780 return -EINVAL;
5781
5782 params.counter_offset_beacon =
5783 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_BEACON]);
5784 if (params.counter_offset_beacon >= params.beacon_csa.tail_len)
5785 return -EINVAL;
5786
5787 /* sanity check - counters should be the same */
5788 if (params.beacon_csa.tail[params.counter_offset_beacon] !=
5789 params.count)
5790 return -EINVAL;
5791
5792 if (csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]) {
5793 params.counter_offset_presp =
5794 nla_get_u16(csa_attrs[NL80211_ATTR_CSA_C_OFF_PRESP]);
5795 if (params.counter_offset_presp >=
5796 params.beacon_csa.probe_resp_len)
5797 return -EINVAL;
5798
5799 if (params.beacon_csa.probe_resp[params.counter_offset_presp] !=
5800 params.count)
5801 return -EINVAL;
5802 }
5803
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005804skip_beacons:
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005805 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5806 if (err)
5807 return err;
5808
5809 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5810 return -EINVAL;
5811
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005812 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP ||
Simon Wunderlich5336fa82013-10-07 18:41:05 +02005813 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO ||
5814 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_ADHOC) {
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +02005815 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5816 &params.chandef);
5817 if (err < 0) {
5818 return err;
5819 } else if (err) {
5820 radar_detect_width = BIT(params.chandef.width);
5821 params.radar_required = true;
5822 }
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005823 }
5824
5825 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
5826 params.chandef.chan,
5827 CHAN_MODE_SHARED,
5828 radar_detect_width);
5829 if (err)
5830 return err;
5831
5832 if (info->attrs[NL80211_ATTR_CH_SWITCH_BLOCK_TX])
5833 params.block_tx = true;
5834
Simon Wunderlichc56589e2013-11-21 18:19:49 +01005835 wdev_lock(wdev);
5836 err = rdev_channel_switch(rdev, dev, &params);
5837 wdev_unlock(wdev);
5838
5839 return err;
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02005840}
5841
Johannes Berg9720bb32011-06-21 09:45:33 +02005842static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
5843 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005844 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02005845 struct wireless_dev *wdev,
5846 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01005847{
Johannes Berg48ab9052009-07-10 18:42:31 +02005848 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg9caf0362012-11-29 01:25:20 +01005849 const struct cfg80211_bss_ies *ies;
Johannes Berg2a519312009-02-10 21:25:55 +01005850 void *hdr;
5851 struct nlattr *bss;
Johannes Berg8cef2c92013-02-05 16:54:31 +01005852 bool tsf = false;
Johannes Berg48ab9052009-07-10 18:42:31 +02005853
5854 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005855
Eric W. Biederman15e47302012-09-07 20:12:54 +00005856 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01005857 NL80211_CMD_NEW_SCAN_RESULTS);
5858 if (!hdr)
5859 return -1;
5860
Johannes Berg9720bb32011-06-21 09:45:33 +02005861 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
5862
Johannes Berg97990a02013-04-19 01:02:55 +02005863 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation))
5864 goto nla_put_failure;
5865 if (wdev->netdev &&
David S. Miller9360ffd2012-03-29 04:41:26 -04005866 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
5867 goto nla_put_failure;
Johannes Berg97990a02013-04-19 01:02:55 +02005868 if (nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
5869 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005870
5871 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
5872 if (!bss)
5873 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04005874 if ((!is_zero_ether_addr(res->bssid) &&
Johannes Berg9caf0362012-11-29 01:25:20 +01005875 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)))
David S. Miller9360ffd2012-03-29 04:41:26 -04005876 goto nla_put_failure;
Johannes Berg9caf0362012-11-29 01:25:20 +01005877
5878 rcu_read_lock();
5879 ies = rcu_dereference(res->ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005880 if (ies) {
5881 if (nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5882 goto fail_unlock_rcu;
5883 tsf = true;
5884 if (ies->len && nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
5885 ies->len, ies->data))
5886 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005887 }
5888 ies = rcu_dereference(res->beacon_ies);
Johannes Berg8cef2c92013-02-05 16:54:31 +01005889 if (ies) {
5890 if (!tsf && nla_put_u64(msg, NL80211_BSS_TSF, ies->tsf))
5891 goto fail_unlock_rcu;
5892 if (ies->len && nla_put(msg, NL80211_BSS_BEACON_IES,
5893 ies->len, ies->data))
5894 goto fail_unlock_rcu;
Johannes Berg9caf0362012-11-29 01:25:20 +01005895 }
5896 rcu_read_unlock();
5897
David S. Miller9360ffd2012-03-29 04:41:26 -04005898 if (res->beacon_interval &&
5899 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
5900 goto nla_put_failure;
5901 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
5902 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
Simon Wunderlichdcd6eac2013-07-08 16:55:49 +02005903 nla_put_u32(msg, NL80211_BSS_CHAN_WIDTH, res->scan_width) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04005904 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
5905 jiffies_to_msecs(jiffies - intbss->ts)))
5906 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005907
Johannes Berg77965c92009-02-18 18:45:06 +01005908 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01005909 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04005910 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
5911 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005912 break;
5913 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005914 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
5915 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01005916 break;
5917 default:
5918 break;
5919 }
5920
Johannes Berg48ab9052009-07-10 18:42:31 +02005921 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02005922 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02005923 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04005924 if (intbss == wdev->current_bss &&
5925 nla_put_u32(msg, NL80211_BSS_STATUS,
5926 NL80211_BSS_STATUS_ASSOCIATED))
5927 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005928 break;
5929 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04005930 if (intbss == wdev->current_bss &&
5931 nla_put_u32(msg, NL80211_BSS_STATUS,
5932 NL80211_BSS_STATUS_IBSS_JOINED))
5933 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02005934 break;
5935 default:
5936 break;
5937 }
5938
Johannes Berg2a519312009-02-10 21:25:55 +01005939 nla_nest_end(msg, bss);
5940
5941 return genlmsg_end(msg, hdr);
5942
Johannes Berg8cef2c92013-02-05 16:54:31 +01005943 fail_unlock_rcu:
5944 rcu_read_unlock();
Johannes Berg2a519312009-02-10 21:25:55 +01005945 nla_put_failure:
5946 genlmsg_cancel(msg, hdr);
5947 return -EMSGSIZE;
5948}
5949
Johannes Berg97990a02013-04-19 01:02:55 +02005950static int nl80211_dump_scan(struct sk_buff *skb, struct netlink_callback *cb)
Johannes Berg2a519312009-02-10 21:25:55 +01005951{
Johannes Berg48ab9052009-07-10 18:42:31 +02005952 struct cfg80211_registered_device *rdev;
Johannes Berg2a519312009-02-10 21:25:55 +01005953 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02005954 struct wireless_dev *wdev;
Johannes Berg97990a02013-04-19 01:02:55 +02005955 int start = cb->args[2], idx = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01005956 int err;
5957
Johannes Berg97990a02013-04-19 01:02:55 +02005958 err = nl80211_prepare_wdev_dump(skb, cb, &rdev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02005959 if (err)
5960 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01005961
Johannes Berg48ab9052009-07-10 18:42:31 +02005962 wdev_lock(wdev);
5963 spin_lock_bh(&rdev->bss_lock);
5964 cfg80211_bss_expire(rdev);
5965
Johannes Berg9720bb32011-06-21 09:45:33 +02005966 cb->seq = rdev->bss_generation;
5967
Johannes Berg48ab9052009-07-10 18:42:31 +02005968 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01005969 if (++idx <= start)
5970 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02005971 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01005972 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02005973 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005974 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02005975 break;
Johannes Berg2a519312009-02-10 21:25:55 +01005976 }
5977 }
5978
Johannes Berg48ab9052009-07-10 18:42:31 +02005979 spin_unlock_bh(&rdev->bss_lock);
5980 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005981
Johannes Berg97990a02013-04-19 01:02:55 +02005982 cb->args[2] = idx;
5983 nl80211_finish_wdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005984
Johannes Berg67748892010-10-04 21:14:06 +02005985 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01005986}
5987
Eric W. Biederman15e47302012-09-07 20:12:54 +00005988static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01005989 int flags, struct net_device *dev,
5990 struct survey_info *survey)
5991{
5992 void *hdr;
5993 struct nlattr *infoattr;
5994
Eric W. Biederman15e47302012-09-07 20:12:54 +00005995 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01005996 NL80211_CMD_NEW_SURVEY_RESULTS);
5997 if (!hdr)
5998 return -ENOMEM;
5999
David S. Miller9360ffd2012-03-29 04:41:26 -04006000 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
6001 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006002
6003 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
6004 if (!infoattr)
6005 goto nla_put_failure;
6006
David S. Miller9360ffd2012-03-29 04:41:26 -04006007 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
6008 survey->channel->center_freq))
6009 goto nla_put_failure;
6010
6011 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
6012 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
6013 goto nla_put_failure;
6014 if ((survey->filled & SURVEY_INFO_IN_USE) &&
6015 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
6016 goto nla_put_failure;
6017 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
6018 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
6019 survey->channel_time))
6020 goto nla_put_failure;
6021 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
6022 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
6023 survey->channel_time_busy))
6024 goto nla_put_failure;
6025 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
6026 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
6027 survey->channel_time_ext_busy))
6028 goto nla_put_failure;
6029 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
6030 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
6031 survey->channel_time_rx))
6032 goto nla_put_failure;
6033 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
6034 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
6035 survey->channel_time_tx))
6036 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01006037
6038 nla_nest_end(msg, infoattr);
6039
6040 return genlmsg_end(msg, hdr);
6041
6042 nla_put_failure:
6043 genlmsg_cancel(msg, hdr);
6044 return -EMSGSIZE;
6045}
6046
6047static int nl80211_dump_survey(struct sk_buff *skb,
6048 struct netlink_callback *cb)
6049{
6050 struct survey_info survey;
6051 struct cfg80211_registered_device *dev;
Johannes Berg97990a02013-04-19 01:02:55 +02006052 struct wireless_dev *wdev;
6053 int survey_idx = cb->args[2];
Holger Schurig61fa7132009-11-11 12:25:40 +01006054 int res;
6055
Johannes Berg97990a02013-04-19 01:02:55 +02006056 res = nl80211_prepare_wdev_dump(skb, cb, &dev, &wdev);
Johannes Berg67748892010-10-04 21:14:06 +02006057 if (res)
6058 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01006059
Johannes Berg97990a02013-04-19 01:02:55 +02006060 if (!wdev->netdev) {
6061 res = -EINVAL;
6062 goto out_err;
6063 }
6064
Holger Schurig61fa7132009-11-11 12:25:40 +01006065 if (!dev->ops->dump_survey) {
6066 res = -EOPNOTSUPP;
6067 goto out_err;
6068 }
6069
6070 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006071 struct ieee80211_channel *chan;
6072
Johannes Berg97990a02013-04-19 01:02:55 +02006073 res = rdev_dump_survey(dev, wdev->netdev, survey_idx, &survey);
Holger Schurig61fa7132009-11-11 12:25:40 +01006074 if (res == -ENOENT)
6075 break;
6076 if (res)
6077 goto out_err;
6078
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07006079 /* Survey without a channel doesn't make sense */
6080 if (!survey.channel) {
6081 res = -EINVAL;
6082 goto out;
6083 }
6084
6085 chan = ieee80211_get_channel(&dev->wiphy,
6086 survey.channel->center_freq);
6087 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
6088 survey_idx++;
6089 continue;
6090 }
6091
Holger Schurig61fa7132009-11-11 12:25:40 +01006092 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00006093 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01006094 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg97990a02013-04-19 01:02:55 +02006095 wdev->netdev, &survey) < 0)
Holger Schurig61fa7132009-11-11 12:25:40 +01006096 goto out;
6097 survey_idx++;
6098 }
6099
6100 out:
Johannes Berg97990a02013-04-19 01:02:55 +02006101 cb->args[2] = survey_idx;
Holger Schurig61fa7132009-11-11 12:25:40 +01006102 res = skb->len;
6103 out_err:
Johannes Berg97990a02013-04-19 01:02:55 +02006104 nl80211_finish_wdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01006105 return res;
6106}
6107
Samuel Ortizb23aa672009-07-01 21:26:54 +02006108static bool nl80211_valid_wpa_versions(u32 wpa_versions)
6109{
6110 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
6111 NL80211_WPA_VERSION_2));
6112}
6113
Jouni Malinen636a5d32009-03-19 13:39:22 +02006114static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
6115{
Johannes Berg4c476992010-10-04 21:36:35 +02006116 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6117 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006118 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03006119 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
6120 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02006121 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02006122 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006123 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006124
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006125 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6126 return -EINVAL;
6127
6128 if (!info->attrs[NL80211_ATTR_MAC])
6129 return -EINVAL;
6130
Jouni Malinen17780922009-03-27 20:52:47 +02006131 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
6132 return -EINVAL;
6133
Johannes Berg19957bb2009-07-02 17:20:43 +02006134 if (!info->attrs[NL80211_ATTR_SSID])
6135 return -EINVAL;
6136
6137 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
6138 return -EINVAL;
6139
Johannes Bergfffd0932009-07-08 14:22:54 +02006140 err = nl80211_parse_key(info, &key);
6141 if (err)
6142 return err;
6143
6144 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02006145 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
6146 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02006147 if (!key.p.key || !key.p.key_len)
6148 return -EINVAL;
6149 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
6150 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
6151 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
6152 key.p.key_len != WLAN_KEY_LEN_WEP104))
6153 return -EINVAL;
6154 if (key.idx > 4)
6155 return -EINVAL;
6156 } else {
6157 key.p.key_len = 0;
6158 key.p.key = NULL;
6159 }
6160
Johannes Bergafea0b72010-08-10 09:46:42 +02006161 if (key.idx >= 0) {
6162 int i;
6163 bool ok = false;
6164 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
6165 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
6166 ok = true;
6167 break;
6168 }
6169 }
Johannes Berg4c476992010-10-04 21:36:35 +02006170 if (!ok)
6171 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02006172 }
6173
Johannes Berg4c476992010-10-04 21:36:35 +02006174 if (!rdev->ops->auth)
6175 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006176
Johannes Berg074ac8d2010-09-16 14:58:22 +02006177 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006178 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6179 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006180
Johannes Berg19957bb2009-07-02 17:20:43 +02006181 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02006182 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02006183 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006184 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6185 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006186
Johannes Berg19957bb2009-07-02 17:20:43 +02006187 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6188 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6189
6190 if (info->attrs[NL80211_ATTR_IE]) {
6191 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6192 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6193 }
6194
6195 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006196 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02006197 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02006198
Jouni Malinene39e5b52012-09-30 19:29:39 +03006199 if (auth_type == NL80211_AUTHTYPE_SAE &&
6200 !info->attrs[NL80211_ATTR_SAE_DATA])
6201 return -EINVAL;
6202
6203 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
6204 if (auth_type != NL80211_AUTHTYPE_SAE)
6205 return -EINVAL;
6206 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
6207 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
6208 /* need to include at least Auth Transaction and Status Code */
6209 if (sae_data_len < 4)
6210 return -EINVAL;
6211 }
6212
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006213 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6214
Johannes Berg95de8172012-01-20 13:55:25 +01006215 /*
6216 * Since we no longer track auth state, ignore
6217 * requests to only change local state.
6218 */
6219 if (local_state_change)
6220 return 0;
6221
Johannes Berg91bf9b22013-05-15 17:44:01 +02006222 wdev_lock(dev->ieee80211_ptr);
6223 err = cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
6224 ssid, ssid_len, ie, ie_len,
6225 key.p.key, key.p.key_len, key.idx,
6226 sae_data, sae_data_len);
6227 wdev_unlock(dev->ieee80211_ptr);
6228 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006229}
6230
Johannes Bergc0692b82010-08-27 14:26:53 +03006231static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
6232 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006233 struct cfg80211_crypto_settings *settings,
6234 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006235{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02006236 memset(settings, 0, sizeof(*settings));
6237
Samuel Ortizb23aa672009-07-01 21:26:54 +02006238 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
6239
Johannes Bergc0692b82010-08-27 14:26:53 +03006240 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
6241 u16 proto;
6242 proto = nla_get_u16(
6243 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
6244 settings->control_port_ethertype = cpu_to_be16(proto);
6245 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
6246 proto != ETH_P_PAE)
6247 return -EINVAL;
6248 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
6249 settings->control_port_no_encrypt = true;
6250 } else
6251 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
6252
Samuel Ortizb23aa672009-07-01 21:26:54 +02006253 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
6254 void *data;
6255 int len, i;
6256
6257 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6258 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
6259 settings->n_ciphers_pairwise = len / sizeof(u32);
6260
6261 if (len % sizeof(u32))
6262 return -EINVAL;
6263
Johannes Berg3dc27d22009-07-02 21:36:37 +02006264 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006265 return -EINVAL;
6266
6267 memcpy(settings->ciphers_pairwise, data, len);
6268
6269 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006270 if (!cfg80211_supported_cipher_suite(
6271 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006272 settings->ciphers_pairwise[i]))
6273 return -EINVAL;
6274 }
6275
6276 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
6277 settings->cipher_group =
6278 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03006279 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
6280 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006281 return -EINVAL;
6282 }
6283
6284 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
6285 settings->wpa_versions =
6286 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
6287 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
6288 return -EINVAL;
6289 }
6290
6291 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
6292 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03006293 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006294
6295 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
6296 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
6297 settings->n_akm_suites = len / sizeof(u32);
6298
6299 if (len % sizeof(u32))
6300 return -EINVAL;
6301
Jouni Malinen1b9ca022011-09-21 16:13:07 +03006302 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
6303 return -EINVAL;
6304
Samuel Ortizb23aa672009-07-01 21:26:54 +02006305 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006306 }
6307
6308 return 0;
6309}
6310
Jouni Malinen636a5d32009-03-19 13:39:22 +02006311static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
6312{
Johannes Berg4c476992010-10-04 21:36:35 +02006313 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6314 struct net_device *dev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02006315 struct ieee80211_channel *chan;
Johannes Bergf62fab72013-02-21 20:09:09 +01006316 struct cfg80211_assoc_request req = {};
6317 const u8 *bssid, *ssid;
6318 int err, ssid_len = 0;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006319
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006320 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6321 return -EINVAL;
6322
6323 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02006324 !info->attrs[NL80211_ATTR_SSID] ||
6325 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006326 return -EINVAL;
6327
Johannes Berg4c476992010-10-04 21:36:35 +02006328 if (!rdev->ops->assoc)
6329 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006330
Johannes Berg074ac8d2010-09-16 14:58:22 +02006331 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006332 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6333 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006334
Johannes Berg19957bb2009-07-02 17:20:43 +02006335 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006336
Johannes Berg19957bb2009-07-02 17:20:43 +02006337 chan = ieee80211_get_channel(&rdev->wiphy,
6338 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02006339 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
6340 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006341
Johannes Berg19957bb2009-07-02 17:20:43 +02006342 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6343 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006344
6345 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006346 req.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6347 req.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006348 }
6349
Jouni Malinendc6382c2009-05-06 22:09:37 +03006350 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006351 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03006352 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02006353 if (mfp == NL80211_MFP_REQUIRED)
Johannes Bergf62fab72013-02-21 20:09:09 +01006354 req.use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02006355 else if (mfp != NL80211_MFP_NO)
6356 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03006357 }
6358
Johannes Berg3e5d7642009-07-07 14:37:26 +02006359 if (info->attrs[NL80211_ATTR_PREV_BSSID])
Johannes Bergf62fab72013-02-21 20:09:09 +01006360 req.prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
Johannes Berg3e5d7642009-07-07 14:37:26 +02006361
Ben Greear7e7c8922011-11-18 11:31:59 -08006362 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006363 req.flags |= ASSOC_REQ_DISABLE_HT;
Ben Greear7e7c8922011-11-18 11:31:59 -08006364
6365 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006366 memcpy(&req.ht_capa_mask,
6367 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6368 sizeof(req.ht_capa_mask));
Ben Greear7e7c8922011-11-18 11:31:59 -08006369
6370 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006371 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
Ben Greear7e7c8922011-11-18 11:31:59 -08006372 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006373 memcpy(&req.ht_capa,
6374 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6375 sizeof(req.ht_capa));
Ben Greear7e7c8922011-11-18 11:31:59 -08006376 }
6377
Johannes Bergee2aca32013-02-21 17:36:01 +01006378 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
Johannes Bergf62fab72013-02-21 20:09:09 +01006379 req.flags |= ASSOC_REQ_DISABLE_VHT;
Johannes Bergee2aca32013-02-21 17:36:01 +01006380
6381 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergf62fab72013-02-21 20:09:09 +01006382 memcpy(&req.vht_capa_mask,
6383 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6384 sizeof(req.vht_capa_mask));
Johannes Bergee2aca32013-02-21 17:36:01 +01006385
6386 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
Johannes Bergf62fab72013-02-21 20:09:09 +01006387 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
Johannes Bergee2aca32013-02-21 17:36:01 +01006388 return -EINVAL;
Johannes Bergf62fab72013-02-21 20:09:09 +01006389 memcpy(&req.vht_capa,
6390 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
6391 sizeof(req.vht_capa));
Johannes Bergee2aca32013-02-21 17:36:01 +01006392 }
6393
Johannes Bergf62fab72013-02-21 20:09:09 +01006394 err = nl80211_crypto_settings(rdev, info, &req.crypto, 1);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006395 if (!err) {
6396 wdev_lock(dev->ieee80211_ptr);
Johannes Bergf62fab72013-02-21 20:09:09 +01006397 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid,
6398 ssid, ssid_len, &req);
Johannes Berg91bf9b22013-05-15 17:44:01 +02006399 wdev_unlock(dev->ieee80211_ptr);
6400 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006401
Jouni Malinen636a5d32009-03-19 13:39:22 +02006402 return err;
6403}
6404
6405static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
6406{
Johannes Berg4c476992010-10-04 21:36:35 +02006407 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6408 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006409 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006410 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006411 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006412 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006413
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006414 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6415 return -EINVAL;
6416
6417 if (!info->attrs[NL80211_ATTR_MAC])
6418 return -EINVAL;
6419
6420 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6421 return -EINVAL;
6422
Johannes Berg4c476992010-10-04 21:36:35 +02006423 if (!rdev->ops->deauth)
6424 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006425
Johannes Berg074ac8d2010-09-16 14:58:22 +02006426 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006427 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6428 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006429
Johannes Berg19957bb2009-07-02 17:20:43 +02006430 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006431
Johannes Berg19957bb2009-07-02 17:20:43 +02006432 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6433 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006434 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006435 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006436 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006437
6438 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006439 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6440 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006441 }
6442
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006443 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6444
Johannes Berg91bf9b22013-05-15 17:44:01 +02006445 wdev_lock(dev->ieee80211_ptr);
6446 err = cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
6447 local_state_change);
6448 wdev_unlock(dev->ieee80211_ptr);
6449 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006450}
6451
6452static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
6453{
Johannes Berg4c476992010-10-04 21:36:35 +02006454 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6455 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02006456 const u8 *ie = NULL, *bssid;
Johannes Berg91bf9b22013-05-15 17:44:01 +02006457 int ie_len = 0, err;
Johannes Berg19957bb2009-07-02 17:20:43 +02006458 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006459 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006460
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006461 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6462 return -EINVAL;
6463
6464 if (!info->attrs[NL80211_ATTR_MAC])
6465 return -EINVAL;
6466
6467 if (!info->attrs[NL80211_ATTR_REASON_CODE])
6468 return -EINVAL;
6469
Johannes Berg4c476992010-10-04 21:36:35 +02006470 if (!rdev->ops->disassoc)
6471 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006472
Johannes Berg074ac8d2010-09-16 14:58:22 +02006473 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006474 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6475 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02006476
Johannes Berg19957bb2009-07-02 17:20:43 +02006477 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006478
Johannes Berg19957bb2009-07-02 17:20:43 +02006479 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
6480 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01006481 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02006482 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02006483 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02006484
6485 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02006486 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6487 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02006488 }
6489
Jouni Malinend5cdfac2010-04-04 09:37:19 +03006490 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
6491
Johannes Berg91bf9b22013-05-15 17:44:01 +02006492 wdev_lock(dev->ieee80211_ptr);
6493 err = cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
6494 local_state_change);
6495 wdev_unlock(dev->ieee80211_ptr);
6496 return err;
Jouni Malinen636a5d32009-03-19 13:39:22 +02006497}
6498
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006499static bool
6500nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
6501 int mcast_rate[IEEE80211_NUM_BANDS],
6502 int rateval)
6503{
6504 struct wiphy *wiphy = &rdev->wiphy;
6505 bool found = false;
6506 int band, i;
6507
6508 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
6509 struct ieee80211_supported_band *sband;
6510
6511 sband = wiphy->bands[band];
6512 if (!sband)
6513 continue;
6514
6515 for (i = 0; i < sband->n_bitrates; i++) {
6516 if (sband->bitrates[i].bitrate == rateval) {
6517 mcast_rate[band] = i + 1;
6518 found = true;
6519 break;
6520 }
6521 }
6522 }
6523
6524 return found;
6525}
6526
Johannes Berg04a773a2009-04-19 21:24:32 +02006527static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6528{
Johannes Berg4c476992010-10-04 21:36:35 +02006529 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6530 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006531 struct cfg80211_ibss_params ibss;
6532 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006533 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02006534 int err;
6535
Johannes Berg8e30bc52009-04-22 17:45:38 +02006536 memset(&ibss, 0, sizeof(ibss));
6537
Johannes Berg04a773a2009-04-19 21:24:32 +02006538 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6539 return -EINVAL;
6540
Johannes Berg683b6d32012-11-08 21:25:48 +01006541 if (!info->attrs[NL80211_ATTR_SSID] ||
Johannes Berg04a773a2009-04-19 21:24:32 +02006542 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6543 return -EINVAL;
6544
Johannes Berg8e30bc52009-04-22 17:45:38 +02006545 ibss.beacon_interval = 100;
6546
6547 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
6548 ibss.beacon_interval =
6549 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
6550 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
6551 return -EINVAL;
6552 }
6553
Johannes Berg4c476992010-10-04 21:36:35 +02006554 if (!rdev->ops->join_ibss)
6555 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006556
Johannes Berg4c476992010-10-04 21:36:35 +02006557 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6558 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006559
Johannes Berg79c97e92009-07-07 03:56:12 +02006560 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02006561
Johannes Berg39193492011-09-16 13:45:25 +02006562 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02006563 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02006564
6565 if (!is_valid_ether_addr(ibss.bssid))
6566 return -EINVAL;
6567 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006568 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6569 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6570
6571 if (info->attrs[NL80211_ATTR_IE]) {
6572 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6573 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6574 }
6575
Johannes Berg683b6d32012-11-08 21:25:48 +01006576 err = nl80211_parse_chandef(rdev, info, &ibss.chandef);
6577 if (err)
6578 return err;
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006579
Johannes Berg683b6d32012-11-08 21:25:48 +01006580 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &ibss.chandef))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01006581 return -EINVAL;
6582
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006583 switch (ibss.chandef.width) {
Simon Wunderlichbf372642013-07-08 16:55:58 +02006584 case NL80211_CHAN_WIDTH_5:
6585 case NL80211_CHAN_WIDTH_10:
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006586 case NL80211_CHAN_WIDTH_20_NOHT:
6587 break;
6588 case NL80211_CHAN_WIDTH_20:
6589 case NL80211_CHAN_WIDTH_40:
6590 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS)
6591 break;
6592 default:
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006593 return -EINVAL;
Simon Wunderlich2f301ab2013-05-16 13:00:28 +02006594 }
Johannes Bergdb9c64c2012-11-09 14:56:41 +01006595
Johannes Berg04a773a2009-04-19 21:24:32 +02006596 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02006597 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02006598
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006599 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
6600 u8 *rates =
6601 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6602 int n_rates =
6603 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
6604 struct ieee80211_supported_band *sband =
Johannes Berg683b6d32012-11-08 21:25:48 +01006605 wiphy->bands[ibss.chandef.chan->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006606
Johannes Berg34850ab2011-07-18 18:08:35 +02006607 err = ieee80211_get_ratemask(sband, rates, n_rates,
6608 &ibss.basic_rates);
6609 if (err)
6610 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006611 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006612
Simon Wunderlich803768f2013-06-28 10:39:58 +02006613 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6614 memcpy(&ibss.ht_capa_mask,
6615 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6616 sizeof(ibss.ht_capa_mask));
6617
6618 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
6619 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6620 return -EINVAL;
6621 memcpy(&ibss.ht_capa,
6622 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6623 sizeof(ibss.ht_capa));
6624 }
6625
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01006626 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6627 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
6628 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6629 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03006630
Johannes Berg4c476992010-10-04 21:36:35 +02006631 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306632 bool no_ht = false;
6633
Johannes Berg4c476992010-10-04 21:36:35 +02006634 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306635 info->attrs[NL80211_ATTR_KEYS],
6636 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02006637 if (IS_ERR(connkeys))
6638 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05306639
Johannes Berg3d9d1d62012-11-08 23:14:50 +01006640 if ((ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) &&
6641 no_ht) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05306642 kfree(connkeys);
6643 return -EINVAL;
6644 }
Johannes Berg4c476992010-10-04 21:36:35 +02006645 }
Johannes Berg04a773a2009-04-19 21:24:32 +02006646
Antonio Quartulli267335d2012-01-31 20:25:47 +01006647 ibss.control_port =
6648 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6649
Simon Wunderlich5336fa82013-10-07 18:41:05 +02006650 ibss.userspace_handles_dfs =
6651 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6652
Johannes Berg4c476992010-10-04 21:36:35 +02006653 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006654 if (err)
6655 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02006656 return err;
6657}
6658
6659static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
6660{
Johannes Berg4c476992010-10-04 21:36:35 +02006661 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6662 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02006663
Johannes Berg4c476992010-10-04 21:36:35 +02006664 if (!rdev->ops->leave_ibss)
6665 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006666
Johannes Berg4c476992010-10-04 21:36:35 +02006667 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
6668 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02006669
Johannes Berg4c476992010-10-04 21:36:35 +02006670 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02006671}
6672
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006673static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6674{
6675 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6676 struct net_device *dev = info->user_ptr[1];
6677 int mcast_rate[IEEE80211_NUM_BANDS];
6678 u32 nla_rate;
6679 int err;
6680
6681 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
6682 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
6683 return -EOPNOTSUPP;
6684
6685 if (!rdev->ops->set_mcast_rate)
6686 return -EOPNOTSUPP;
6687
6688 memset(mcast_rate, 0, sizeof(mcast_rate));
6689
6690 if (!info->attrs[NL80211_ATTR_MCAST_RATE])
6691 return -EINVAL;
6692
6693 nla_rate = nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE]);
6694 if (!nl80211_parse_mcast_rate(rdev, mcast_rate, nla_rate))
6695 return -EINVAL;
6696
6697 err = rdev->ops->set_mcast_rate(&rdev->wiphy, dev, mcast_rate);
6698
6699 return err;
6700}
6701
Johannes Bergad7e7182013-11-13 13:37:47 +01006702static struct sk_buff *
6703__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
6704 int approxlen, u32 portid, u32 seq,
6705 enum nl80211_commands cmd,
6706 enum nl80211_attrs attr, gfp_t gfp)
6707{
6708 struct sk_buff *skb;
6709 void *hdr;
6710 struct nlattr *data;
6711
6712 skb = nlmsg_new(approxlen + 100, gfp);
6713 if (!skb)
6714 return NULL;
6715
6716 hdr = nl80211hdr_put(skb, portid, seq, 0, cmd);
6717 if (!hdr) {
6718 kfree_skb(skb);
6719 return NULL;
6720 }
6721
6722 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
6723 goto nla_put_failure;
6724 data = nla_nest_start(skb, attr);
6725
6726 ((void **)skb->cb)[0] = rdev;
6727 ((void **)skb->cb)[1] = hdr;
6728 ((void **)skb->cb)[2] = data;
6729
6730 return skb;
6731
6732 nla_put_failure:
6733 kfree_skb(skb);
6734 return NULL;
6735}
Antonio Quartullif4e583c2012-11-02 13:27:48 +01006736
Johannes Bergaff89a92009-07-01 21:26:51 +02006737#ifdef CONFIG_NL80211_TESTMODE
6738static struct genl_multicast_group nl80211_testmode_mcgrp = {
6739 .name = "testmode",
6740};
6741
6742static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6743{
Johannes Berg4c476992010-10-04 21:36:35 +02006744 struct cfg80211_registered_device *rdev = info->user_ptr[0];
David Spinadelfc73f112013-07-31 18:04:15 +03006745 struct wireless_dev *wdev =
6746 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
Johannes Bergaff89a92009-07-01 21:26:51 +02006747 int err;
6748
David Spinadelfc73f112013-07-31 18:04:15 +03006749 if (!rdev->ops->testmode_cmd)
6750 return -EOPNOTSUPP;
6751
6752 if (IS_ERR(wdev)) {
6753 err = PTR_ERR(wdev);
6754 if (err != -EINVAL)
6755 return err;
6756 wdev = NULL;
6757 } else if (wdev->wiphy != &rdev->wiphy) {
6758 return -EINVAL;
6759 }
6760
Johannes Bergaff89a92009-07-01 21:26:51 +02006761 if (!info->attrs[NL80211_ATTR_TESTDATA])
6762 return -EINVAL;
6763
Johannes Bergad7e7182013-11-13 13:37:47 +01006764 rdev->cur_cmd_info = info;
David Spinadelfc73f112013-07-31 18:04:15 +03006765 err = rdev_testmode_cmd(rdev, wdev,
Johannes Bergaff89a92009-07-01 21:26:51 +02006766 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
6767 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
Johannes Bergad7e7182013-11-13 13:37:47 +01006768 rdev->cur_cmd_info = NULL;
Johannes Bergaff89a92009-07-01 21:26:51 +02006769
Johannes Bergaff89a92009-07-01 21:26:51 +02006770 return err;
6771}
6772
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006773static int nl80211_testmode_dump(struct sk_buff *skb,
6774 struct netlink_callback *cb)
6775{
Johannes Berg00918d32011-12-13 17:22:05 +01006776 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006777 int err;
6778 long phy_idx;
6779 void *data = NULL;
6780 int data_len = 0;
6781
Johannes Berg5fe231e2013-05-08 21:45:15 +02006782 rtnl_lock();
6783
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006784 if (cb->args[0]) {
6785 /*
6786 * 0 is a valid index, but not valid for args[0],
6787 * so we need to offset by 1.
6788 */
6789 phy_idx = cb->args[0] - 1;
6790 } else {
6791 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
6792 nl80211_fam.attrbuf, nl80211_fam.maxattr,
6793 nl80211_policy);
6794 if (err)
Johannes Berg5fe231e2013-05-08 21:45:15 +02006795 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006796
Johannes Berg2bd7e352012-06-15 14:23:16 +02006797 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
6798 nl80211_fam.attrbuf);
6799 if (IS_ERR(rdev)) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006800 err = PTR_ERR(rdev);
6801 goto out_err;
Johannes Berg00918d32011-12-13 17:22:05 +01006802 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02006803 phy_idx = rdev->wiphy_idx;
6804 rdev = NULL;
Johannes Berg2bd7e352012-06-15 14:23:16 +02006805
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006806 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
6807 cb->args[1] =
6808 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
6809 }
6810
6811 if (cb->args[1]) {
6812 data = nla_data((void *)cb->args[1]);
6813 data_len = nla_len((void *)cb->args[1]);
6814 }
6815
Johannes Berg00918d32011-12-13 17:22:05 +01006816 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
6817 if (!rdev) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02006818 err = -ENOENT;
6819 goto out_err;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006820 }
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006821
Johannes Berg00918d32011-12-13 17:22:05 +01006822 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006823 err = -EOPNOTSUPP;
6824 goto out_err;
6825 }
6826
6827 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00006828 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006829 cb->nlh->nlmsg_seq, NLM_F_MULTI,
6830 NL80211_CMD_TESTMODE);
6831 struct nlattr *tmdata;
6832
Dan Carpentercb35fba2013-08-14 14:50:01 +03006833 if (!hdr)
6834 break;
6835
David S. Miller9360ffd2012-03-29 04:41:26 -04006836 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006837 genlmsg_cancel(skb, hdr);
6838 break;
6839 }
6840
6841 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
6842 if (!tmdata) {
6843 genlmsg_cancel(skb, hdr);
6844 break;
6845 }
Hila Gonene35e4d22012-06-27 17:19:42 +03006846 err = rdev_testmode_dump(rdev, skb, cb, data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006847 nla_nest_end(skb, tmdata);
6848
6849 if (err == -ENOBUFS || err == -ENOENT) {
6850 genlmsg_cancel(skb, hdr);
6851 break;
6852 } else if (err) {
6853 genlmsg_cancel(skb, hdr);
6854 goto out_err;
6855 }
6856
6857 genlmsg_end(skb, hdr);
6858 }
6859
6860 err = skb->len;
6861 /* see above */
6862 cb->args[0] = phy_idx + 1;
6863 out_err:
Johannes Berg5fe231e2013-05-08 21:45:15 +02006864 rtnl_unlock();
Wey-Yi Guy71063f02011-05-20 09:05:54 -07006865 return err;
6866}
6867
Johannes Bergaff89a92009-07-01 21:26:51 +02006868struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
6869 int approxlen, gfp_t gfp)
6870{
6871 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
6872
Johannes Bergad7e7182013-11-13 13:37:47 +01006873 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0,
6874 NL80211_CMD_TESTMODE,
6875 NL80211_ATTR_TESTDATA, gfp);
Johannes Bergaff89a92009-07-01 21:26:51 +02006876}
6877EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
6878
6879void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6880{
Michal Kaziora0ec5702013-06-25 09:17:17 +02006881 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02006882 void *hdr = ((void **)skb->cb)[1];
6883 struct nlattr *data = ((void **)skb->cb)[2];
6884
6885 nla_nest_end(skb, data);
6886 genlmsg_end(skb, hdr);
Michal Kaziora0ec5702013-06-25 09:17:17 +02006887 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), skb, 0,
6888 nl80211_testmode_mcgrp.id, gfp);
Johannes Bergaff89a92009-07-01 21:26:51 +02006889}
6890EXPORT_SYMBOL(cfg80211_testmode_event);
6891#endif
6892
Samuel Ortizb23aa672009-07-01 21:26:54 +02006893static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
6894{
Johannes Berg4c476992010-10-04 21:36:35 +02006895 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6896 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02006897 struct cfg80211_connect_params connect;
6898 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02006899 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006900 int err;
6901
6902 memset(&connect, 0, sizeof(connect));
6903
6904 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
6905 return -EINVAL;
6906
6907 if (!info->attrs[NL80211_ATTR_SSID] ||
6908 !nla_len(info->attrs[NL80211_ATTR_SSID]))
6909 return -EINVAL;
6910
6911 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
6912 connect.auth_type =
6913 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03006914 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
6915 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02006916 return -EINVAL;
6917 } else
6918 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
6919
6920 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
6921
Johannes Bergc0692b82010-08-27 14:26:53 +03006922 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02006923 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006924 if (err)
6925 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006926
Johannes Berg074ac8d2010-09-16 14:58:22 +02006927 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006928 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
6929 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006930
Johannes Berg79c97e92009-07-07 03:56:12 +02006931 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006932
Bala Shanmugam4486ea92012-03-07 17:27:12 +05306933 connect.bg_scan_period = -1;
6934 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
6935 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
6936 connect.bg_scan_period =
6937 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
6938 }
6939
Samuel Ortizb23aa672009-07-01 21:26:54 +02006940 if (info->attrs[NL80211_ATTR_MAC])
6941 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
6942 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
6943 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
6944
6945 if (info->attrs[NL80211_ATTR_IE]) {
6946 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
6947 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
6948 }
6949
Jouni Malinencee00a92013-01-15 17:15:57 +02006950 if (info->attrs[NL80211_ATTR_USE_MFP]) {
6951 connect.mfp = nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
6952 if (connect.mfp != NL80211_MFP_REQUIRED &&
6953 connect.mfp != NL80211_MFP_NO)
6954 return -EINVAL;
6955 } else {
6956 connect.mfp = NL80211_MFP_NO;
6957 }
6958
Samuel Ortizb23aa672009-07-01 21:26:54 +02006959 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6960 connect.channel =
6961 ieee80211_get_channel(wiphy,
6962 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
6963 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02006964 connect.channel->flags & IEEE80211_CHAN_DISABLED)
6965 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02006966 }
6967
Johannes Bergfffd0932009-07-08 14:22:54 +02006968 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
6969 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05306970 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02006971 if (IS_ERR(connkeys))
6972 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02006973 }
6974
Ben Greear7e7c8922011-11-18 11:31:59 -08006975 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
6976 connect.flags |= ASSOC_REQ_DISABLE_HT;
6977
6978 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
6979 memcpy(&connect.ht_capa_mask,
6980 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
6981 sizeof(connect.ht_capa_mask));
6982
6983 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006984 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
6985 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08006986 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08006987 }
Ben Greear7e7c8922011-11-18 11:31:59 -08006988 memcpy(&connect.ht_capa,
6989 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
6990 sizeof(connect.ht_capa));
6991 }
6992
Johannes Bergee2aca32013-02-21 17:36:01 +01006993 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_VHT]))
6994 connect.flags |= ASSOC_REQ_DISABLE_VHT;
6995
6996 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK])
6997 memcpy(&connect.vht_capa_mask,
6998 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]),
6999 sizeof(connect.vht_capa_mask));
7000
7001 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY]) {
7002 if (!info->attrs[NL80211_ATTR_VHT_CAPABILITY_MASK]) {
7003 kfree(connkeys);
7004 return -EINVAL;
7005 }
7006 memcpy(&connect.vht_capa,
7007 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]),
7008 sizeof(connect.vht_capa));
7009 }
7010
Johannes Berg83739b02013-05-15 17:44:01 +02007011 wdev_lock(dev->ieee80211_ptr);
7012 err = cfg80211_connect(rdev, dev, &connect, connkeys, NULL);
7013 wdev_unlock(dev->ieee80211_ptr);
Johannes Bergfffd0932009-07-08 14:22:54 +02007014 if (err)
7015 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02007016 return err;
7017}
7018
7019static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
7020{
Johannes Berg4c476992010-10-04 21:36:35 +02007021 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7022 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02007023 u16 reason;
Johannes Berg83739b02013-05-15 17:44:01 +02007024 int ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007025
7026 if (!info->attrs[NL80211_ATTR_REASON_CODE])
7027 reason = WLAN_REASON_DEAUTH_LEAVING;
7028 else
7029 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
7030
7031 if (reason == 0)
7032 return -EINVAL;
7033
Johannes Berg074ac8d2010-09-16 14:58:22 +02007034 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007035 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7036 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007037
Johannes Berg83739b02013-05-15 17:44:01 +02007038 wdev_lock(dev->ieee80211_ptr);
7039 ret = cfg80211_disconnect(rdev, dev, reason, true);
7040 wdev_unlock(dev->ieee80211_ptr);
7041 return ret;
Samuel Ortizb23aa672009-07-01 21:26:54 +02007042}
7043
Johannes Berg463d0182009-07-14 00:33:35 +02007044static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
7045{
Johannes Berg4c476992010-10-04 21:36:35 +02007046 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02007047 struct net *net;
7048 int err;
7049 u32 pid;
7050
7051 if (!info->attrs[NL80211_ATTR_PID])
7052 return -EINVAL;
7053
7054 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
7055
Johannes Berg463d0182009-07-14 00:33:35 +02007056 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02007057 if (IS_ERR(net))
7058 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007059
7060 err = 0;
7061
7062 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02007063 if (!net_eq(wiphy_net(&rdev->wiphy), net))
7064 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02007065
Johannes Berg463d0182009-07-14 00:33:35 +02007066 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02007067 return err;
7068}
7069
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007070static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
7071{
Johannes Berg4c476992010-10-04 21:36:35 +02007072 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007073 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
7074 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02007075 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007076 struct cfg80211_pmksa pmksa;
7077
7078 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
7079
7080 if (!info->attrs[NL80211_ATTR_MAC])
7081 return -EINVAL;
7082
7083 if (!info->attrs[NL80211_ATTR_PMKID])
7084 return -EINVAL;
7085
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007086 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
7087 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
7088
Johannes Berg074ac8d2010-09-16 14:58:22 +02007089 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007090 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7091 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007092
7093 switch (info->genlhdr->cmd) {
7094 case NL80211_CMD_SET_PMKSA:
7095 rdev_ops = rdev->ops->set_pmksa;
7096 break;
7097 case NL80211_CMD_DEL_PMKSA:
7098 rdev_ops = rdev->ops->del_pmksa;
7099 break;
7100 default:
7101 WARN_ON(1);
7102 break;
7103 }
7104
Johannes Berg4c476992010-10-04 21:36:35 +02007105 if (!rdev_ops)
7106 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007107
Johannes Berg4c476992010-10-04 21:36:35 +02007108 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007109}
7110
7111static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
7112{
Johannes Berg4c476992010-10-04 21:36:35 +02007113 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7114 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007115
Johannes Berg074ac8d2010-09-16 14:58:22 +02007116 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007117 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
7118 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007119
Johannes Berg4c476992010-10-04 21:36:35 +02007120 if (!rdev->ops->flush_pmksa)
7121 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007122
Hila Gonene35e4d22012-06-27 17:19:42 +03007123 return rdev_flush_pmksa(rdev, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007124}
7125
Arik Nemtsov109086c2011-09-28 14:12:50 +03007126static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
7127{
7128 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7129 struct net_device *dev = info->user_ptr[1];
7130 u8 action_code, dialog_token;
7131 u16 status_code;
7132 u8 *peer;
7133
7134 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7135 !rdev->ops->tdls_mgmt)
7136 return -EOPNOTSUPP;
7137
7138 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
7139 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
7140 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
7141 !info->attrs[NL80211_ATTR_IE] ||
7142 !info->attrs[NL80211_ATTR_MAC])
7143 return -EINVAL;
7144
7145 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7146 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
7147 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
7148 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
7149
Hila Gonene35e4d22012-06-27 17:19:42 +03007150 return rdev_tdls_mgmt(rdev, dev, peer, action_code,
7151 dialog_token, status_code,
7152 nla_data(info->attrs[NL80211_ATTR_IE]),
7153 nla_len(info->attrs[NL80211_ATTR_IE]));
Arik Nemtsov109086c2011-09-28 14:12:50 +03007154}
7155
7156static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
7157{
7158 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7159 struct net_device *dev = info->user_ptr[1];
7160 enum nl80211_tdls_operation operation;
7161 u8 *peer;
7162
7163 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
7164 !rdev->ops->tdls_oper)
7165 return -EOPNOTSUPP;
7166
7167 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
7168 !info->attrs[NL80211_ATTR_MAC])
7169 return -EINVAL;
7170
7171 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
7172 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
7173
Hila Gonene35e4d22012-06-27 17:19:42 +03007174 return rdev_tdls_oper(rdev, dev, peer, operation);
Arik Nemtsov109086c2011-09-28 14:12:50 +03007175}
7176
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007177static int nl80211_remain_on_channel(struct sk_buff *skb,
7178 struct genl_info *info)
7179{
Johannes Berg4c476992010-10-04 21:36:35 +02007180 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007181 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007182 struct cfg80211_chan_def chandef;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007183 struct sk_buff *msg;
7184 void *hdr;
7185 u64 cookie;
Johannes Berg683b6d32012-11-08 21:25:48 +01007186 u32 duration;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007187 int err;
7188
7189 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
7190 !info->attrs[NL80211_ATTR_DURATION])
7191 return -EINVAL;
7192
7193 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
7194
Johannes Berg7c4ef712011-11-18 15:33:48 +01007195 if (!rdev->ops->remain_on_channel ||
7196 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02007197 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007198
Johannes Bergebf348f2012-06-01 12:50:54 +02007199 /*
7200 * We should be on that channel for at least a minimum amount of
7201 * time (10ms) but no longer than the driver supports.
7202 */
7203 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7204 duration > rdev->wiphy.max_remain_on_channel_duration)
7205 return -EINVAL;
7206
Johannes Berg683b6d32012-11-08 21:25:48 +01007207 err = nl80211_parse_chandef(rdev, info, &chandef);
7208 if (err)
7209 return err;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007210
7211 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007212 if (!msg)
7213 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007214
Eric W. Biederman15e47302012-09-07 20:12:54 +00007215 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007216 NL80211_CMD_REMAIN_ON_CHANNEL);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007217 if (!hdr) {
7218 err = -ENOBUFS;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007219 goto free_msg;
7220 }
7221
Johannes Berg683b6d32012-11-08 21:25:48 +01007222 err = rdev_remain_on_channel(rdev, wdev, chandef.chan,
7223 duration, &cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007224
7225 if (err)
7226 goto free_msg;
7227
David S. Miller9360ffd2012-03-29 04:41:26 -04007228 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7229 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007230
7231 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007232
7233 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007234
7235 nla_put_failure:
7236 err = -ENOBUFS;
7237 free_msg:
7238 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007239 return err;
7240}
7241
7242static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
7243 struct genl_info *info)
7244{
Johannes Berg4c476992010-10-04 21:36:35 +02007245 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007246 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007247 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007248
7249 if (!info->attrs[NL80211_ATTR_COOKIE])
7250 return -EINVAL;
7251
Johannes Berg4c476992010-10-04 21:36:35 +02007252 if (!rdev->ops->cancel_remain_on_channel)
7253 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007254
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007255 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7256
Hila Gonene35e4d22012-06-27 17:19:42 +03007257 return rdev_cancel_remain_on_channel(rdev, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007258}
7259
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007260static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
7261 u8 *rates, u8 rates_len)
7262{
7263 u8 i;
7264 u32 mask = 0;
7265
7266 for (i = 0; i < rates_len; i++) {
7267 int rate = (rates[i] & 0x7f) * 5;
7268 int ridx;
7269 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
7270 struct ieee80211_rate *srate =
7271 &sband->bitrates[ridx];
7272 if (rate == srate->bitrate) {
7273 mask |= 1 << ridx;
7274 break;
7275 }
7276 }
7277 if (ridx == sband->n_bitrates)
7278 return 0; /* rate not found */
7279 }
7280
7281 return mask;
7282}
7283
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007284static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
7285 u8 *rates, u8 rates_len,
7286 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
7287{
7288 u8 i;
7289
7290 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
7291
7292 for (i = 0; i < rates_len; i++) {
7293 int ridx, rbit;
7294
7295 ridx = rates[i] / 8;
7296 rbit = BIT(rates[i] % 8);
7297
7298 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03007299 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007300 return false;
7301
7302 /* check availability */
7303 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
7304 mcs[ridx] |= rbit;
7305 else
7306 return false;
7307 }
7308
7309 return true;
7310}
7311
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00007312static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007313 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
7314 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007315 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
7316 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007317};
7318
7319static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
7320 struct genl_info *info)
7321{
7322 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02007323 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007324 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02007325 int rem, i;
7326 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007327 struct nlattr *tx_rates;
7328 struct ieee80211_supported_band *sband;
7329
7330 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
7331 return -EINVAL;
7332
Johannes Berg4c476992010-10-04 21:36:35 +02007333 if (!rdev->ops->set_bitrate_mask)
7334 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007335
7336 memset(&mask, 0, sizeof(mask));
7337 /* Default to all rates enabled */
7338 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
7339 sband = rdev->wiphy.bands[i];
Janusz Dziedzic78693032013-12-03 09:50:44 +01007340
7341 if (!sband)
7342 continue;
7343
7344 mask.control[i].legacy = (1 << sband->n_bitrates) - 1;
7345 memcpy(mask.control[i].mcs,
7346 sband->ht_cap.mcs.rx_mask,
7347 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007348 }
7349
7350 /*
7351 * The nested attribute uses enum nl80211_band as the index. This maps
7352 * directly to the enum ieee80211_band values used in cfg80211.
7353 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007354 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007355 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
7356 {
7357 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02007358 if (band < 0 || band >= IEEE80211_NUM_BANDS)
7359 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007360 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02007361 if (sband == NULL)
7362 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007363 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
7364 nla_len(tx_rates), nl80211_txattr_policy);
7365 if (tb[NL80211_TXRATE_LEGACY]) {
7366 mask.control[band].legacy = rateset_to_mask(
7367 sband,
7368 nla_data(tb[NL80211_TXRATE_LEGACY]),
7369 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05307370 if ((mask.control[band].legacy == 0) &&
7371 nla_len(tb[NL80211_TXRATE_LEGACY]))
7372 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01007373 }
7374 if (tb[NL80211_TXRATE_MCS]) {
7375 if (!ht_rateset_to_mask(
7376 sband,
7377 nla_data(tb[NL80211_TXRATE_MCS]),
7378 nla_len(tb[NL80211_TXRATE_MCS]),
7379 mask.control[band].mcs))
7380 return -EINVAL;
7381 }
7382
7383 if (mask.control[band].legacy == 0) {
7384 /* don't allow empty legacy rates if HT
7385 * is not even supported. */
7386 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
7387 return -EINVAL;
7388
7389 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
7390 if (mask.control[band].mcs[i])
7391 break;
7392
7393 /* legacy and mcs rates may not be both empty */
7394 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02007395 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007396 }
7397 }
7398
Hila Gonene35e4d22012-06-27 17:19:42 +03007399 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007400}
7401
Johannes Berg2e161f72010-08-12 15:38:38 +02007402static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007403{
Johannes Berg4c476992010-10-04 21:36:35 +02007404 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007405 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02007406 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02007407
7408 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
7409 return -EINVAL;
7410
Johannes Berg2e161f72010-08-12 15:38:38 +02007411 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
7412 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02007413
Johannes Berg71bbc992012-06-15 15:30:18 +02007414 switch (wdev->iftype) {
7415 case NL80211_IFTYPE_STATION:
7416 case NL80211_IFTYPE_ADHOC:
7417 case NL80211_IFTYPE_P2P_CLIENT:
7418 case NL80211_IFTYPE_AP:
7419 case NL80211_IFTYPE_AP_VLAN:
7420 case NL80211_IFTYPE_MESH_POINT:
7421 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007422 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007423 break;
7424 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007425 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007426 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007427
7428 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02007429 if (!rdev->ops->mgmt_tx)
7430 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007431
Eric W. Biederman15e47302012-09-07 20:12:54 +00007432 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02007433 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
7434 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02007435}
7436
Johannes Berg2e161f72010-08-12 15:38:38 +02007437static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02007438{
Johannes Berg4c476992010-10-04 21:36:35 +02007439 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007440 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg683b6d32012-11-08 21:25:48 +01007441 struct cfg80211_chan_def chandef;
Jouni Malinen026331c2010-02-15 12:53:10 +02007442 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01007443 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007444 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01007445 struct sk_buff *msg = NULL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007446 struct cfg80211_mgmt_tx_params params = {
7447 .dont_wait_for_ack =
7448 info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK],
7449 };
Jouni Malinen026331c2010-02-15 12:53:10 +02007450
Johannes Berg683b6d32012-11-08 21:25:48 +01007451 if (!info->attrs[NL80211_ATTR_FRAME])
Jouni Malinen026331c2010-02-15 12:53:10 +02007452 return -EINVAL;
7453
Johannes Berg4c476992010-10-04 21:36:35 +02007454 if (!rdev->ops->mgmt_tx)
7455 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02007456
Johannes Berg71bbc992012-06-15 15:30:18 +02007457 switch (wdev->iftype) {
Antonio Quartulliea141b752013-06-11 14:20:03 +02007458 case NL80211_IFTYPE_P2P_DEVICE:
7459 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
7460 return -EINVAL;
Johannes Berg71bbc992012-06-15 15:30:18 +02007461 case NL80211_IFTYPE_STATION:
7462 case NL80211_IFTYPE_ADHOC:
7463 case NL80211_IFTYPE_P2P_CLIENT:
7464 case NL80211_IFTYPE_AP:
7465 case NL80211_IFTYPE_AP_VLAN:
7466 case NL80211_IFTYPE_MESH_POINT:
7467 case NL80211_IFTYPE_P2P_GO:
7468 break;
7469 default:
Johannes Berg4c476992010-10-04 21:36:35 +02007470 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007471 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007472
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007473 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01007474 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007475 return -EINVAL;
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007476 params.wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02007477
7478 /*
7479 * We should wait on the channel for at least a minimum amount
7480 * of time (10ms) but no longer than the driver supports.
7481 */
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007482 if (params.wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
7483 params.wait > rdev->wiphy.max_remain_on_channel_duration)
Johannes Bergebf348f2012-06-01 12:50:54 +02007484 return -EINVAL;
7485
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007486 }
7487
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007488 params.offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007489
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007490 if (params.offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Berg7c4ef712011-11-18 15:33:48 +01007491 return -EINVAL;
7492
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007493 params.no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05307494
Antonio Quartulliea141b752013-06-11 14:20:03 +02007495 /* get the channel if any has been specified, otherwise pass NULL to
7496 * the driver. The latter will use the current one
7497 */
7498 chandef.chan = NULL;
7499 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
7500 err = nl80211_parse_chandef(rdev, info, &chandef);
7501 if (err)
7502 return err;
7503 }
7504
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007505 if (!chandef.chan && params.offchan)
Antonio Quartulliea141b752013-06-11 14:20:03 +02007506 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02007507
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007508 if (!params.dont_wait_for_ack) {
Johannes Berge247bd902011-11-04 11:18:21 +01007509 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7510 if (!msg)
7511 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02007512
Eric W. Biederman15e47302012-09-07 20:12:54 +00007513 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01007514 NL80211_CMD_FRAME);
Dan Carpentercb35fba2013-08-14 14:50:01 +03007515 if (!hdr) {
7516 err = -ENOBUFS;
Johannes Berge247bd902011-11-04 11:18:21 +01007517 goto free_msg;
7518 }
Jouni Malinen026331c2010-02-15 12:53:10 +02007519 }
Johannes Berge247bd902011-11-04 11:18:21 +01007520
Andrei Otcheretianskib176e622013-11-18 19:06:49 +02007521 params.buf = nla_data(info->attrs[NL80211_ATTR_FRAME]);
7522 params.len = nla_len(info->attrs[NL80211_ATTR_FRAME]);
7523 params.chan = chandef.chan;
7524 err = cfg80211_mlme_mgmt_tx(rdev, wdev, &params, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02007525 if (err)
7526 goto free_msg;
7527
Johannes Berge247bd902011-11-04 11:18:21 +01007528 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04007529 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
7530 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02007531
Johannes Berge247bd902011-11-04 11:18:21 +01007532 genlmsg_end(msg, hdr);
7533 return genlmsg_reply(msg, info);
7534 }
7535
7536 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02007537
7538 nla_put_failure:
7539 err = -ENOBUFS;
7540 free_msg:
7541 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02007542 return err;
7543}
7544
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007545static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
7546{
7547 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02007548 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007549 u64 cookie;
7550
7551 if (!info->attrs[NL80211_ATTR_COOKIE])
7552 return -EINVAL;
7553
7554 if (!rdev->ops->mgmt_tx_cancel_wait)
7555 return -EOPNOTSUPP;
7556
Johannes Berg71bbc992012-06-15 15:30:18 +02007557 switch (wdev->iftype) {
7558 case NL80211_IFTYPE_STATION:
7559 case NL80211_IFTYPE_ADHOC:
7560 case NL80211_IFTYPE_P2P_CLIENT:
7561 case NL80211_IFTYPE_AP:
7562 case NL80211_IFTYPE_AP_VLAN:
7563 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02007564 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02007565 break;
7566 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007567 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02007568 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007569
7570 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
7571
Hila Gonene35e4d22012-06-27 17:19:42 +03007572 return rdev_mgmt_tx_cancel_wait(rdev, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007573}
7574
Kalle Valoffb9eb32010-02-17 17:58:10 +02007575static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
7576{
Johannes Berg4c476992010-10-04 21:36:35 +02007577 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007578 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007579 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007580 u8 ps_state;
7581 bool state;
7582 int err;
7583
Johannes Berg4c476992010-10-04 21:36:35 +02007584 if (!info->attrs[NL80211_ATTR_PS_STATE])
7585 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007586
7587 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
7588
Johannes Berg4c476992010-10-04 21:36:35 +02007589 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
7590 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007591
7592 wdev = dev->ieee80211_ptr;
7593
Johannes Berg4c476992010-10-04 21:36:35 +02007594 if (!rdev->ops->set_power_mgmt)
7595 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007596
7597 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
7598
7599 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02007600 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007601
Hila Gonene35e4d22012-06-27 17:19:42 +03007602 err = rdev_set_power_mgmt(rdev, dev, state, wdev->ps_timeout);
Johannes Berg4c476992010-10-04 21:36:35 +02007603 if (!err)
7604 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007605 return err;
7606}
7607
7608static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
7609{
Johannes Berg4c476992010-10-04 21:36:35 +02007610 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007611 enum nl80211_ps_state ps_state;
7612 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007613 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02007614 struct sk_buff *msg;
7615 void *hdr;
7616 int err;
7617
Kalle Valoffb9eb32010-02-17 17:58:10 +02007618 wdev = dev->ieee80211_ptr;
7619
Johannes Berg4c476992010-10-04 21:36:35 +02007620 if (!rdev->ops->set_power_mgmt)
7621 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007622
7623 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02007624 if (!msg)
7625 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007626
Eric W. Biederman15e47302012-09-07 20:12:54 +00007627 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007628 NL80211_CMD_GET_POWER_SAVE);
7629 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02007630 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007631 goto free_msg;
7632 }
7633
7634 if (wdev->ps)
7635 ps_state = NL80211_PS_ENABLED;
7636 else
7637 ps_state = NL80211_PS_DISABLED;
7638
David S. Miller9360ffd2012-03-29 04:41:26 -04007639 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
7640 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02007641
7642 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02007643 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007644
Johannes Berg4c476992010-10-04 21:36:35 +02007645 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007646 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02007647 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02007648 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02007649 return err;
7650}
7651
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007652static struct nla_policy
7653nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
7654 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
7655 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
7656 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07007657 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
7658 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
7659 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007660};
7661
Thomas Pedersen84f10702012-07-12 16:17:33 -07007662static int nl80211_set_cqm_txe(struct genl_info *info,
Johannes Bergd9d8b012012-11-26 12:51:52 +01007663 u32 rate, u32 pkts, u32 intvl)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007664{
7665 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Thomas Pedersen84f10702012-07-12 16:17:33 -07007666 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007667 struct wireless_dev *wdev = dev->ieee80211_ptr;
Thomas Pedersen84f10702012-07-12 16:17:33 -07007668
Johannes Bergd9d8b012012-11-26 12:51:52 +01007669 if (rate > 100 || intvl > NL80211_CQM_TXE_MAX_INTVL)
Thomas Pedersen84f10702012-07-12 16:17:33 -07007670 return -EINVAL;
7671
Thomas Pedersen84f10702012-07-12 16:17:33 -07007672 if (!rdev->ops->set_cqm_txe_config)
7673 return -EOPNOTSUPP;
7674
7675 if (wdev->iftype != NL80211_IFTYPE_STATION &&
7676 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7677 return -EOPNOTSUPP;
7678
Hila Gonene35e4d22012-06-27 17:19:42 +03007679 return rdev_set_cqm_txe_config(rdev, dev, rate, pkts, intvl);
Thomas Pedersen84f10702012-07-12 16:17:33 -07007680}
7681
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007682static int nl80211_set_cqm_rssi(struct genl_info *info,
7683 s32 threshold, u32 hysteresis)
7684{
Johannes Berg4c476992010-10-04 21:36:35 +02007685 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02007686 struct net_device *dev = info->user_ptr[1];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007687 struct wireless_dev *wdev = dev->ieee80211_ptr;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007688
7689 if (threshold > 0)
7690 return -EINVAL;
7691
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007692 /* disabling - hysteresis should also be zero then */
7693 if (threshold == 0)
7694 hysteresis = 0;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007695
Johannes Berg4c476992010-10-04 21:36:35 +02007696 if (!rdev->ops->set_cqm_rssi_config)
7697 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007698
Johannes Berg074ac8d2010-09-16 14:58:22 +02007699 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02007700 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
7701 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007702
Hila Gonene35e4d22012-06-27 17:19:42 +03007703 return rdev_set_cqm_rssi_config(rdev, dev, threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007704}
7705
7706static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
7707{
7708 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
7709 struct nlattr *cqm;
7710 int err;
7711
7712 cqm = info->attrs[NL80211_ATTR_CQM];
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007713 if (!cqm)
7714 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007715
7716 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
7717 nl80211_attr_cqm_policy);
7718 if (err)
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007719 return err;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007720
7721 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
7722 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007723 s32 threshold = nla_get_s32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
7724 u32 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007725
Johannes Berg1da5fcc2013-08-06 14:10:48 +02007726 return nl80211_set_cqm_rssi(info, threshold, hysteresis);
7727 }
7728
7729 if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
7730 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
7731 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
7732 u32 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
7733 u32 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
7734 u32 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
7735
7736 return nl80211_set_cqm_txe(info, rate, pkts, intvl);
7737 }
7738
7739 return -EINVAL;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007740}
7741
Johannes Berg29cbe682010-12-03 09:20:44 +01007742static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
7743{
7744 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7745 struct net_device *dev = info->user_ptr[1];
7746 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08007747 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01007748 int err;
7749
7750 /* start with default */
7751 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08007752 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01007753
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007754 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01007755 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007756 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01007757 if (err)
7758 return err;
7759 }
7760
7761 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
7762 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
7763 return -EINVAL;
7764
Javier Cardonac80d5452010-12-16 17:37:49 -08007765 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
7766 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
7767
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08007768 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
7769 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
7770 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
7771 return -EINVAL;
7772
Marco Porsch9bdbf042013-01-07 16:04:51 +01007773 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
7774 setup.beacon_interval =
7775 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
7776 if (setup.beacon_interval < 10 ||
7777 setup.beacon_interval > 10000)
7778 return -EINVAL;
7779 }
7780
7781 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
7782 setup.dtim_period =
7783 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
7784 if (setup.dtim_period < 1 || setup.dtim_period > 100)
7785 return -EINVAL;
7786 }
7787
Javier Cardonac80d5452010-12-16 17:37:49 -08007788 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
7789 /* parse additional setup parameters if given */
7790 err = nl80211_parse_mesh_setup(info, &setup);
7791 if (err)
7792 return err;
7793 }
7794
Thomas Pedersend37bb182013-03-04 13:06:13 -08007795 if (setup.user_mpm)
7796 cfg.auto_open_plinks = false;
7797
Johannes Bergcc1d2802012-05-16 23:50:20 +02007798 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Berg683b6d32012-11-08 21:25:48 +01007799 err = nl80211_parse_chandef(rdev, info, &setup.chandef);
7800 if (err)
7801 return err;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007802 } else {
7803 /* cfg80211_join_mesh() will sort it out */
Johannes Berg683b6d32012-11-08 21:25:48 +01007804 setup.chandef.chan = NULL;
Johannes Bergcc1d2802012-05-16 23:50:20 +02007805 }
7806
Ashok Nagarajanffb3cf32013-06-03 10:33:36 -07007807 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
7808 u8 *rates = nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7809 int n_rates =
7810 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
7811 struct ieee80211_supported_band *sband;
7812
7813 if (!setup.chandef.chan)
7814 return -EINVAL;
7815
7816 sband = rdev->wiphy.bands[setup.chandef.chan->band];
7817
7818 err = ieee80211_get_ratemask(sband, rates, n_rates,
7819 &setup.basic_rates);
7820 if (err)
7821 return err;
7822 }
7823
Javier Cardonac80d5452010-12-16 17:37:49 -08007824 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01007825}
7826
7827static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
7828{
7829 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7830 struct net_device *dev = info->user_ptr[1];
7831
7832 return cfg80211_leave_mesh(rdev, dev);
7833}
7834
Johannes Bergdfb89c52012-06-27 09:23:48 +02007835#ifdef CONFIG_PM
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007836static int nl80211_send_wowlan_patterns(struct sk_buff *msg,
7837 struct cfg80211_registered_device *rdev)
7838{
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007839 struct cfg80211_wowlan *wowlan = rdev->wiphy.wowlan_config;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007840 struct nlattr *nl_pats, *nl_pat;
7841 int i, pat_len;
7842
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007843 if (!wowlan->n_patterns)
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007844 return 0;
7845
7846 nl_pats = nla_nest_start(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN);
7847 if (!nl_pats)
7848 return -ENOBUFS;
7849
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007850 for (i = 0; i < wowlan->n_patterns; i++) {
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007851 nl_pat = nla_nest_start(msg, i + 1);
7852 if (!nl_pat)
7853 return -ENOBUFS;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007854 pat_len = wowlan->patterns[i].pattern_len;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07007855 if (nla_put(msg, NL80211_PKTPAT_MASK, DIV_ROUND_UP(pat_len, 8),
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007856 wowlan->patterns[i].mask) ||
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07007857 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
7858 wowlan->patterns[i].pattern) ||
7859 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007860 wowlan->patterns[i].pkt_offset))
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007861 return -ENOBUFS;
7862 nla_nest_end(msg, nl_pat);
7863 }
7864 nla_nest_end(msg, nl_pats);
7865
7866 return 0;
7867}
7868
Johannes Berg2a0e0472013-01-23 22:57:40 +01007869static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
7870 struct cfg80211_wowlan_tcp *tcp)
7871{
7872 struct nlattr *nl_tcp;
7873
7874 if (!tcp)
7875 return 0;
7876
7877 nl_tcp = nla_nest_start(msg, NL80211_WOWLAN_TRIG_TCP_CONNECTION);
7878 if (!nl_tcp)
7879 return -ENOBUFS;
7880
7881 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
7882 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
7883 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
7884 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
7885 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
7886 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD,
7887 tcp->payload_len, tcp->payload) ||
7888 nla_put_u32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
7889 tcp->data_interval) ||
7890 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
7891 tcp->wake_len, tcp->wake_data) ||
7892 nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
7893 DIV_ROUND_UP(tcp->wake_len, 8), tcp->wake_mask))
7894 return -ENOBUFS;
7895
7896 if (tcp->payload_seq.len &&
7897 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ,
7898 sizeof(tcp->payload_seq), &tcp->payload_seq))
7899 return -ENOBUFS;
7900
7901 if (tcp->payload_tok.len &&
7902 nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN,
7903 sizeof(tcp->payload_tok) + tcp->tokens_size,
7904 &tcp->payload_tok))
7905 return -ENOBUFS;
7906
Johannes Berge248ad32013-05-16 10:24:28 +02007907 nla_nest_end(msg, nl_tcp);
7908
Johannes Berg2a0e0472013-01-23 22:57:40 +01007909 return 0;
7910}
7911
Johannes Bergff1b6e62011-05-04 15:37:28 +02007912static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
7913{
7914 struct cfg80211_registered_device *rdev = info->user_ptr[0];
7915 struct sk_buff *msg;
7916 void *hdr;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007917 u32 size = NLMSG_DEFAULT_SIZE;
Johannes Bergff1b6e62011-05-04 15:37:28 +02007918
Johannes Berg964dc9e2013-06-03 17:25:34 +02007919 if (!rdev->wiphy.wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02007920 return -EOPNOTSUPP;
7921
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007922 if (rdev->wiphy.wowlan_config && rdev->wiphy.wowlan_config->tcp) {
Johannes Berg2a0e0472013-01-23 22:57:40 +01007923 /* adjust size to have room for all the data */
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007924 size += rdev->wiphy.wowlan_config->tcp->tokens_size +
7925 rdev->wiphy.wowlan_config->tcp->payload_len +
7926 rdev->wiphy.wowlan_config->tcp->wake_len +
7927 rdev->wiphy.wowlan_config->tcp->wake_len / 8;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007928 }
7929
7930 msg = nlmsg_new(size, GFP_KERNEL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02007931 if (!msg)
7932 return -ENOMEM;
7933
Eric W. Biederman15e47302012-09-07 20:12:54 +00007934 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02007935 NL80211_CMD_GET_WOWLAN);
7936 if (!hdr)
7937 goto nla_put_failure;
7938
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007939 if (rdev->wiphy.wowlan_config) {
Johannes Bergff1b6e62011-05-04 15:37:28 +02007940 struct nlattr *nl_wowlan;
7941
7942 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
7943 if (!nl_wowlan)
7944 goto nla_put_failure;
7945
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007946 if ((rdev->wiphy.wowlan_config->any &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007947 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007948 (rdev->wiphy.wowlan_config->disconnect &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007949 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007950 (rdev->wiphy.wowlan_config->magic_pkt &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007951 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007952 (rdev->wiphy.wowlan_config->gtk_rekey_failure &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007953 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007954 (rdev->wiphy.wowlan_config->eap_identity_req &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007955 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007956 (rdev->wiphy.wowlan_config->four_way_handshake &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007957 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007958 (rdev->wiphy.wowlan_config->rfkill_release &&
David S. Miller9360ffd2012-03-29 04:41:26 -04007959 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
7960 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007961
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08007962 if (nl80211_send_wowlan_patterns(msg, rdev))
7963 goto nla_put_failure;
Johannes Berg2a0e0472013-01-23 22:57:40 +01007964
Johannes Berg6abb9cb2013-05-15 09:30:07 +02007965 if (nl80211_send_wowlan_tcp(msg,
7966 rdev->wiphy.wowlan_config->tcp))
Johannes Berg2a0e0472013-01-23 22:57:40 +01007967 goto nla_put_failure;
7968
Johannes Bergff1b6e62011-05-04 15:37:28 +02007969 nla_nest_end(msg, nl_wowlan);
7970 }
7971
7972 genlmsg_end(msg, hdr);
7973 return genlmsg_reply(msg, info);
7974
7975nla_put_failure:
7976 nlmsg_free(msg);
7977 return -ENOBUFS;
7978}
7979
Johannes Berg2a0e0472013-01-23 22:57:40 +01007980static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
7981 struct nlattr *attr,
7982 struct cfg80211_wowlan *trig)
7983{
7984 struct nlattr *tb[NUM_NL80211_WOWLAN_TCP];
7985 struct cfg80211_wowlan_tcp *cfg;
7986 struct nl80211_wowlan_tcp_data_token *tok = NULL;
7987 struct nl80211_wowlan_tcp_data_seq *seq = NULL;
7988 u32 size;
7989 u32 data_size, wake_size, tokens_size = 0, wake_mask_size;
7990 int err, port;
7991
Johannes Berg964dc9e2013-06-03 17:25:34 +02007992 if (!rdev->wiphy.wowlan->tcp)
Johannes Berg2a0e0472013-01-23 22:57:40 +01007993 return -EINVAL;
7994
7995 err = nla_parse(tb, MAX_NL80211_WOWLAN_TCP,
7996 nla_data(attr), nla_len(attr),
7997 nl80211_wowlan_tcp_policy);
7998 if (err)
7999 return err;
8000
8001 if (!tb[NL80211_WOWLAN_TCP_SRC_IPV4] ||
8002 !tb[NL80211_WOWLAN_TCP_DST_IPV4] ||
8003 !tb[NL80211_WOWLAN_TCP_DST_MAC] ||
8004 !tb[NL80211_WOWLAN_TCP_DST_PORT] ||
8005 !tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD] ||
8006 !tb[NL80211_WOWLAN_TCP_DATA_INTERVAL] ||
8007 !tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD] ||
8008 !tb[NL80211_WOWLAN_TCP_WAKE_MASK])
8009 return -EINVAL;
8010
8011 data_size = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008012 if (data_size > rdev->wiphy.wowlan->tcp->data_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008013 return -EINVAL;
8014
8015 if (nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) >
Johannes Berg964dc9e2013-06-03 17:25:34 +02008016 rdev->wiphy.wowlan->tcp->data_interval_max ||
Johannes Berg723d5682013-02-26 13:56:40 +01008017 nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]) == 0)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008018 return -EINVAL;
8019
8020 wake_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008021 if (wake_size > rdev->wiphy.wowlan->tcp->wake_payload_max)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008022 return -EINVAL;
8023
8024 wake_mask_size = nla_len(tb[NL80211_WOWLAN_TCP_WAKE_MASK]);
8025 if (wake_mask_size != DIV_ROUND_UP(wake_size, 8))
8026 return -EINVAL;
8027
8028 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]) {
8029 u32 tokln = nla_len(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8030
8031 tok = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_TOKEN]);
8032 tokens_size = tokln - sizeof(*tok);
8033
8034 if (!tok->len || tokens_size % tok->len)
8035 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008036 if (!rdev->wiphy.wowlan->tcp->tok)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008037 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008038 if (tok->len > rdev->wiphy.wowlan->tcp->tok->max_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008039 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008040 if (tok->len < rdev->wiphy.wowlan->tcp->tok->min_len)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008041 return -EINVAL;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008042 if (tokens_size > rdev->wiphy.wowlan->tcp->tok->bufsize)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008043 return -EINVAL;
8044 if (tok->offset + tok->len > data_size)
8045 return -EINVAL;
8046 }
8047
8048 if (tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]) {
8049 seq = nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD_SEQ]);
Johannes Berg964dc9e2013-06-03 17:25:34 +02008050 if (!rdev->wiphy.wowlan->tcp->seq)
Johannes Berg2a0e0472013-01-23 22:57:40 +01008051 return -EINVAL;
8052 if (seq->len == 0 || seq->len > 4)
8053 return -EINVAL;
8054 if (seq->len + seq->offset > data_size)
8055 return -EINVAL;
8056 }
8057
8058 size = sizeof(*cfg);
8059 size += data_size;
8060 size += wake_size + wake_mask_size;
8061 size += tokens_size;
8062
8063 cfg = kzalloc(size, GFP_KERNEL);
8064 if (!cfg)
8065 return -ENOMEM;
8066 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8067 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8068 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8069 ETH_ALEN);
8070 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
8071 port = nla_get_u16(tb[NL80211_WOWLAN_TCP_SRC_PORT]);
8072 else
8073 port = 0;
8074#ifdef CONFIG_INET
8075 /* allocate a socket and port for it and use it */
8076 err = __sock_create(wiphy_net(&rdev->wiphy), PF_INET, SOCK_STREAM,
8077 IPPROTO_TCP, &cfg->sock, 1);
8078 if (err) {
8079 kfree(cfg);
8080 return err;
8081 }
8082 if (inet_csk_get_port(cfg->sock->sk, port)) {
8083 sock_release(cfg->sock);
8084 kfree(cfg);
8085 return -EADDRINUSE;
8086 }
8087 cfg->src_port = inet_sk(cfg->sock->sk)->inet_num;
8088#else
8089 if (!port) {
8090 kfree(cfg);
8091 return -EINVAL;
8092 }
8093 cfg->src_port = port;
8094#endif
8095
8096 cfg->dst_port = nla_get_u16(tb[NL80211_WOWLAN_TCP_DST_PORT]);
8097 cfg->payload_len = data_size;
8098 cfg->payload = (u8 *)cfg + sizeof(*cfg) + tokens_size;
8099 memcpy((void *)cfg->payload,
8100 nla_data(tb[NL80211_WOWLAN_TCP_DATA_PAYLOAD]),
8101 data_size);
8102 if (seq)
8103 cfg->payload_seq = *seq;
8104 cfg->data_interval = nla_get_u32(tb[NL80211_WOWLAN_TCP_DATA_INTERVAL]);
8105 cfg->wake_len = wake_size;
8106 cfg->wake_data = (u8 *)cfg + sizeof(*cfg) + tokens_size + data_size;
8107 memcpy((void *)cfg->wake_data,
8108 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_PAYLOAD]),
8109 wake_size);
8110 cfg->wake_mask = (u8 *)cfg + sizeof(*cfg) + tokens_size +
8111 data_size + wake_size;
8112 memcpy((void *)cfg->wake_mask,
8113 nla_data(tb[NL80211_WOWLAN_TCP_WAKE_MASK]),
8114 wake_mask_size);
8115 if (tok) {
8116 cfg->tokens_size = tokens_size;
8117 memcpy(&cfg->payload_tok, tok, sizeof(*tok) + tokens_size);
8118 }
8119
8120 trig->tcp = cfg;
8121
8122 return 0;
8123}
8124
Johannes Bergff1b6e62011-05-04 15:37:28 +02008125static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
8126{
8127 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8128 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008129 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02008130 struct cfg80211_wowlan *ntrig;
Johannes Berg964dc9e2013-06-03 17:25:34 +02008131 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008132 int err, i;
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008133 bool prev_enabled = rdev->wiphy.wowlan_config;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008134
Johannes Berg964dc9e2013-06-03 17:25:34 +02008135 if (!wowlan)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008136 return -EOPNOTSUPP;
8137
Johannes Bergae33bd82012-07-12 16:25:02 +02008138 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
8139 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008140 rdev->wiphy.wowlan_config = NULL;
Johannes Bergae33bd82012-07-12 16:25:02 +02008141 goto set_wakeup;
8142 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02008143
8144 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
8145 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8146 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
8147 nl80211_wowlan_policy);
8148 if (err)
8149 return err;
8150
8151 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
8152 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
8153 return -EINVAL;
8154 new_triggers.any = true;
8155 }
8156
8157 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
8158 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
8159 return -EINVAL;
8160 new_triggers.disconnect = true;
8161 }
8162
8163 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
8164 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
8165 return -EINVAL;
8166 new_triggers.magic_pkt = true;
8167 }
8168
Johannes Berg77dbbb12011-07-13 10:48:55 +02008169 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
8170 return -EINVAL;
8171
8172 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
8173 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
8174 return -EINVAL;
8175 new_triggers.gtk_rekey_failure = true;
8176 }
8177
8178 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
8179 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
8180 return -EINVAL;
8181 new_triggers.eap_identity_req = true;
8182 }
8183
8184 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
8185 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
8186 return -EINVAL;
8187 new_triggers.four_way_handshake = true;
8188 }
8189
8190 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
8191 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
8192 return -EINVAL;
8193 new_triggers.rfkill_release = true;
8194 }
8195
Johannes Bergff1b6e62011-05-04 15:37:28 +02008196 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
8197 struct nlattr *pat;
8198 int n_patterns = 0;
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008199 int rem, pat_len, mask_len, pkt_offset;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008200 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
Johannes Bergff1b6e62011-05-04 15:37:28 +02008201
8202 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8203 rem)
8204 n_patterns++;
8205 if (n_patterns > wowlan->n_patterns)
8206 return -EINVAL;
8207
8208 new_triggers.patterns = kcalloc(n_patterns,
8209 sizeof(new_triggers.patterns[0]),
8210 GFP_KERNEL);
8211 if (!new_triggers.patterns)
8212 return -ENOMEM;
8213
8214 new_triggers.n_patterns = n_patterns;
8215 i = 0;
8216
8217 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
8218 rem) {
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008219 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8220 nla_len(pat), NULL);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008221 err = -EINVAL;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008222 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8223 !pat_tb[NL80211_PKTPAT_PATTERN])
Johannes Bergff1b6e62011-05-04 15:37:28 +02008224 goto error;
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008225 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008226 mask_len = DIV_ROUND_UP(pat_len, 8);
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008227 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
Johannes Bergff1b6e62011-05-04 15:37:28 +02008228 goto error;
8229 if (pat_len > wowlan->pattern_max_len ||
8230 pat_len < wowlan->pattern_min_len)
8231 goto error;
8232
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008233 if (!pat_tb[NL80211_PKTPAT_OFFSET])
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008234 pkt_offset = 0;
8235 else
8236 pkt_offset = nla_get_u32(
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008237 pat_tb[NL80211_PKTPAT_OFFSET]);
Amitkumar Karwarbb92d192013-02-12 12:16:26 -08008238 if (pkt_offset > wowlan->max_pkt_offset)
8239 goto error;
8240 new_triggers.patterns[i].pkt_offset = pkt_offset;
8241
Johannes Bergff1b6e62011-05-04 15:37:28 +02008242 new_triggers.patterns[i].mask =
8243 kmalloc(mask_len + pat_len, GFP_KERNEL);
8244 if (!new_triggers.patterns[i].mask) {
8245 err = -ENOMEM;
8246 goto error;
8247 }
8248 new_triggers.patterns[i].pattern =
8249 new_triggers.patterns[i].mask + mask_len;
8250 memcpy(new_triggers.patterns[i].mask,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008251 nla_data(pat_tb[NL80211_PKTPAT_MASK]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008252 mask_len);
8253 new_triggers.patterns[i].pattern_len = pat_len;
8254 memcpy(new_triggers.patterns[i].pattern,
Amitkumar Karwar50ac6602013-06-25 19:03:56 -07008255 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]),
Johannes Bergff1b6e62011-05-04 15:37:28 +02008256 pat_len);
8257 i++;
8258 }
8259 }
8260
Johannes Berg2a0e0472013-01-23 22:57:40 +01008261 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
8262 err = nl80211_parse_wowlan_tcp(
8263 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
8264 &new_triggers);
8265 if (err)
8266 goto error;
8267 }
8268
Johannes Bergae33bd82012-07-12 16:25:02 +02008269 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
8270 if (!ntrig) {
8271 err = -ENOMEM;
8272 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008273 }
Johannes Bergae33bd82012-07-12 16:25:02 +02008274 cfg80211_rdev_free_wowlan(rdev);
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008275 rdev->wiphy.wowlan_config = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02008276
Johannes Bergae33bd82012-07-12 16:25:02 +02008277 set_wakeup:
Johannes Berg6abb9cb2013-05-15 09:30:07 +02008278 if (rdev->ops->set_wakeup &&
8279 prev_enabled != !!rdev->wiphy.wowlan_config)
8280 rdev_set_wakeup(rdev, rdev->wiphy.wowlan_config);
Johannes Berg6d525632012-04-04 15:05:25 +02008281
Johannes Bergff1b6e62011-05-04 15:37:28 +02008282 return 0;
8283 error:
8284 for (i = 0; i < new_triggers.n_patterns; i++)
8285 kfree(new_triggers.patterns[i].mask);
8286 kfree(new_triggers.patterns);
Johannes Berg2a0e0472013-01-23 22:57:40 +01008287 if (new_triggers.tcp && new_triggers.tcp->sock)
8288 sock_release(new_triggers.tcp->sock);
8289 kfree(new_triggers.tcp);
Johannes Bergff1b6e62011-05-04 15:37:28 +02008290 return err;
8291}
Johannes Bergdfb89c52012-06-27 09:23:48 +02008292#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02008293
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07008294static int nl80211_send_coalesce_rules(struct sk_buff *msg,
8295 struct cfg80211_registered_device *rdev)
8296{
8297 struct nlattr *nl_pats, *nl_pat, *nl_rule, *nl_rules;
8298 int i, j, pat_len;
8299 struct cfg80211_coalesce_rules *rule;
8300
8301 if (!rdev->coalesce->n_rules)
8302 return 0;
8303
8304 nl_rules = nla_nest_start(msg, NL80211_ATTR_COALESCE_RULE);
8305 if (!nl_rules)
8306 return -ENOBUFS;
8307
8308 for (i = 0; i < rdev->coalesce->n_rules; i++) {
8309 nl_rule = nla_nest_start(msg, i + 1);
8310 if (!nl_rule)
8311 return -ENOBUFS;
8312
8313 rule = &rdev->coalesce->rules[i];
8314 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_DELAY,
8315 rule->delay))
8316 return -ENOBUFS;
8317
8318 if (nla_put_u32(msg, NL80211_ATTR_COALESCE_RULE_CONDITION,
8319 rule->condition))
8320 return -ENOBUFS;
8321
8322 nl_pats = nla_nest_start(msg,
8323 NL80211_ATTR_COALESCE_RULE_PKT_PATTERN);
8324 if (!nl_pats)
8325 return -ENOBUFS;
8326
8327 for (j = 0; j < rule->n_patterns; j++) {
8328 nl_pat = nla_nest_start(msg, j + 1);
8329 if (!nl_pat)
8330 return -ENOBUFS;
8331 pat_len = rule->patterns[j].pattern_len;
8332 if (nla_put(msg, NL80211_PKTPAT_MASK,
8333 DIV_ROUND_UP(pat_len, 8),
8334 rule->patterns[j].mask) ||
8335 nla_put(msg, NL80211_PKTPAT_PATTERN, pat_len,
8336 rule->patterns[j].pattern) ||
8337 nla_put_u32(msg, NL80211_PKTPAT_OFFSET,
8338 rule->patterns[j].pkt_offset))
8339 return -ENOBUFS;
8340 nla_nest_end(msg, nl_pat);
8341 }
8342 nla_nest_end(msg, nl_pats);
8343 nla_nest_end(msg, nl_rule);
8344 }
8345 nla_nest_end(msg, nl_rules);
8346
8347 return 0;
8348}
8349
8350static int nl80211_get_coalesce(struct sk_buff *skb, struct genl_info *info)
8351{
8352 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8353 struct sk_buff *msg;
8354 void *hdr;
8355
8356 if (!rdev->wiphy.coalesce)
8357 return -EOPNOTSUPP;
8358
8359 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8360 if (!msg)
8361 return -ENOMEM;
8362
8363 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8364 NL80211_CMD_GET_COALESCE);
8365 if (!hdr)
8366 goto nla_put_failure;
8367
8368 if (rdev->coalesce && nl80211_send_coalesce_rules(msg, rdev))
8369 goto nla_put_failure;
8370
8371 genlmsg_end(msg, hdr);
8372 return genlmsg_reply(msg, info);
8373
8374nla_put_failure:
8375 nlmsg_free(msg);
8376 return -ENOBUFS;
8377}
8378
8379void cfg80211_rdev_free_coalesce(struct cfg80211_registered_device *rdev)
8380{
8381 struct cfg80211_coalesce *coalesce = rdev->coalesce;
8382 int i, j;
8383 struct cfg80211_coalesce_rules *rule;
8384
8385 if (!coalesce)
8386 return;
8387
8388 for (i = 0; i < coalesce->n_rules; i++) {
8389 rule = &coalesce->rules[i];
8390 for (j = 0; j < rule->n_patterns; j++)
8391 kfree(rule->patterns[j].mask);
8392 kfree(rule->patterns);
8393 }
8394 kfree(coalesce->rules);
8395 kfree(coalesce);
8396 rdev->coalesce = NULL;
8397}
8398
8399static int nl80211_parse_coalesce_rule(struct cfg80211_registered_device *rdev,
8400 struct nlattr *rule,
8401 struct cfg80211_coalesce_rules *new_rule)
8402{
8403 int err, i;
8404 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8405 struct nlattr *tb[NUM_NL80211_ATTR_COALESCE_RULE], *pat;
8406 int rem, pat_len, mask_len, pkt_offset, n_patterns = 0;
8407 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
8408
8409 err = nla_parse(tb, NL80211_ATTR_COALESCE_RULE_MAX, nla_data(rule),
8410 nla_len(rule), nl80211_coalesce_policy);
8411 if (err)
8412 return err;
8413
8414 if (tb[NL80211_ATTR_COALESCE_RULE_DELAY])
8415 new_rule->delay =
8416 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_DELAY]);
8417 if (new_rule->delay > coalesce->max_delay)
8418 return -EINVAL;
8419
8420 if (tb[NL80211_ATTR_COALESCE_RULE_CONDITION])
8421 new_rule->condition =
8422 nla_get_u32(tb[NL80211_ATTR_COALESCE_RULE_CONDITION]);
8423 if (new_rule->condition != NL80211_COALESCE_CONDITION_MATCH &&
8424 new_rule->condition != NL80211_COALESCE_CONDITION_NO_MATCH)
8425 return -EINVAL;
8426
8427 if (!tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN])
8428 return -EINVAL;
8429
8430 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8431 rem)
8432 n_patterns++;
8433 if (n_patterns > coalesce->n_patterns)
8434 return -EINVAL;
8435
8436 new_rule->patterns = kcalloc(n_patterns, sizeof(new_rule->patterns[0]),
8437 GFP_KERNEL);
8438 if (!new_rule->patterns)
8439 return -ENOMEM;
8440
8441 new_rule->n_patterns = n_patterns;
8442 i = 0;
8443
8444 nla_for_each_nested(pat, tb[NL80211_ATTR_COALESCE_RULE_PKT_PATTERN],
8445 rem) {
8446 nla_parse(pat_tb, MAX_NL80211_PKTPAT, nla_data(pat),
8447 nla_len(pat), NULL);
8448 if (!pat_tb[NL80211_PKTPAT_MASK] ||
8449 !pat_tb[NL80211_PKTPAT_PATTERN])
8450 return -EINVAL;
8451 pat_len = nla_len(pat_tb[NL80211_PKTPAT_PATTERN]);
8452 mask_len = DIV_ROUND_UP(pat_len, 8);
8453 if (nla_len(pat_tb[NL80211_PKTPAT_MASK]) != mask_len)
8454 return -EINVAL;
8455 if (pat_len > coalesce->pattern_max_len ||
8456 pat_len < coalesce->pattern_min_len)
8457 return -EINVAL;
8458
8459 if (!pat_tb[NL80211_PKTPAT_OFFSET])
8460 pkt_offset = 0;
8461 else
8462 pkt_offset = nla_get_u32(pat_tb[NL80211_PKTPAT_OFFSET]);
8463 if (pkt_offset > coalesce->max_pkt_offset)
8464 return -EINVAL;
8465 new_rule->patterns[i].pkt_offset = pkt_offset;
8466
8467 new_rule->patterns[i].mask =
8468 kmalloc(mask_len + pat_len, GFP_KERNEL);
8469 if (!new_rule->patterns[i].mask)
8470 return -ENOMEM;
8471 new_rule->patterns[i].pattern =
8472 new_rule->patterns[i].mask + mask_len;
8473 memcpy(new_rule->patterns[i].mask,
8474 nla_data(pat_tb[NL80211_PKTPAT_MASK]), mask_len);
8475 new_rule->patterns[i].pattern_len = pat_len;
8476 memcpy(new_rule->patterns[i].pattern,
8477 nla_data(pat_tb[NL80211_PKTPAT_PATTERN]), pat_len);
8478 i++;
8479 }
8480
8481 return 0;
8482}
8483
8484static int nl80211_set_coalesce(struct sk_buff *skb, struct genl_info *info)
8485{
8486 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8487 const struct wiphy_coalesce_support *coalesce = rdev->wiphy.coalesce;
8488 struct cfg80211_coalesce new_coalesce = {};
8489 struct cfg80211_coalesce *n_coalesce;
8490 int err, rem_rule, n_rules = 0, i, j;
8491 struct nlattr *rule;
8492 struct cfg80211_coalesce_rules *tmp_rule;
8493
8494 if (!rdev->wiphy.coalesce || !rdev->ops->set_coalesce)
8495 return -EOPNOTSUPP;
8496
8497 if (!info->attrs[NL80211_ATTR_COALESCE_RULE]) {
8498 cfg80211_rdev_free_coalesce(rdev);
8499 rdev->ops->set_coalesce(&rdev->wiphy, NULL);
8500 return 0;
8501 }
8502
8503 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8504 rem_rule)
8505 n_rules++;
8506 if (n_rules > coalesce->n_rules)
8507 return -EINVAL;
8508
8509 new_coalesce.rules = kcalloc(n_rules, sizeof(new_coalesce.rules[0]),
8510 GFP_KERNEL);
8511 if (!new_coalesce.rules)
8512 return -ENOMEM;
8513
8514 new_coalesce.n_rules = n_rules;
8515 i = 0;
8516
8517 nla_for_each_nested(rule, info->attrs[NL80211_ATTR_COALESCE_RULE],
8518 rem_rule) {
8519 err = nl80211_parse_coalesce_rule(rdev, rule,
8520 &new_coalesce.rules[i]);
8521 if (err)
8522 goto error;
8523
8524 i++;
8525 }
8526
8527 err = rdev->ops->set_coalesce(&rdev->wiphy, &new_coalesce);
8528 if (err)
8529 goto error;
8530
8531 n_coalesce = kmemdup(&new_coalesce, sizeof(new_coalesce), GFP_KERNEL);
8532 if (!n_coalesce) {
8533 err = -ENOMEM;
8534 goto error;
8535 }
8536 cfg80211_rdev_free_coalesce(rdev);
8537 rdev->coalesce = n_coalesce;
8538
8539 return 0;
8540error:
8541 for (i = 0; i < new_coalesce.n_rules; i++) {
8542 tmp_rule = &new_coalesce.rules[i];
8543 for (j = 0; j < tmp_rule->n_patterns; j++)
8544 kfree(tmp_rule->patterns[j].mask);
8545 kfree(tmp_rule->patterns);
8546 }
8547 kfree(new_coalesce.rules);
8548
8549 return err;
8550}
8551
Johannes Berge5497d72011-07-05 16:35:40 +02008552static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
8553{
8554 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8555 struct net_device *dev = info->user_ptr[1];
8556 struct wireless_dev *wdev = dev->ieee80211_ptr;
8557 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
8558 struct cfg80211_gtk_rekey_data rekey_data;
8559 int err;
8560
8561 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
8562 return -EINVAL;
8563
8564 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
8565 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
8566 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
8567 nl80211_rekey_policy);
8568 if (err)
8569 return err;
8570
8571 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
8572 return -ERANGE;
8573 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
8574 return -ERANGE;
8575 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
8576 return -ERANGE;
8577
8578 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
8579 NL80211_KEK_LEN);
8580 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
8581 NL80211_KCK_LEN);
8582 memcpy(rekey_data.replay_ctr,
8583 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
8584 NL80211_REPLAY_CTR_LEN);
8585
8586 wdev_lock(wdev);
8587 if (!wdev->current_bss) {
8588 err = -ENOTCONN;
8589 goto out;
8590 }
8591
8592 if (!rdev->ops->set_rekey_data) {
8593 err = -EOPNOTSUPP;
8594 goto out;
8595 }
8596
Hila Gonene35e4d22012-06-27 17:19:42 +03008597 err = rdev_set_rekey_data(rdev, dev, &rekey_data);
Johannes Berge5497d72011-07-05 16:35:40 +02008598 out:
8599 wdev_unlock(wdev);
8600 return err;
8601}
8602
Johannes Berg28946da2011-11-04 11:18:12 +01008603static int nl80211_register_unexpected_frame(struct sk_buff *skb,
8604 struct genl_info *info)
8605{
8606 struct net_device *dev = info->user_ptr[1];
8607 struct wireless_dev *wdev = dev->ieee80211_ptr;
8608
8609 if (wdev->iftype != NL80211_IFTYPE_AP &&
8610 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8611 return -EINVAL;
8612
Eric W. Biederman15e47302012-09-07 20:12:54 +00008613 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008614 return -EBUSY;
8615
Eric W. Biederman15e47302012-09-07 20:12:54 +00008616 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01008617 return 0;
8618}
8619
Johannes Berg7f6cf312011-11-04 11:18:15 +01008620static int nl80211_probe_client(struct sk_buff *skb,
8621 struct genl_info *info)
8622{
8623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8624 struct net_device *dev = info->user_ptr[1];
8625 struct wireless_dev *wdev = dev->ieee80211_ptr;
8626 struct sk_buff *msg;
8627 void *hdr;
8628 const u8 *addr;
8629 u64 cookie;
8630 int err;
8631
8632 if (wdev->iftype != NL80211_IFTYPE_AP &&
8633 wdev->iftype != NL80211_IFTYPE_P2P_GO)
8634 return -EOPNOTSUPP;
8635
8636 if (!info->attrs[NL80211_ATTR_MAC])
8637 return -EINVAL;
8638
8639 if (!rdev->ops->probe_client)
8640 return -EOPNOTSUPP;
8641
8642 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8643 if (!msg)
8644 return -ENOMEM;
8645
Eric W. Biederman15e47302012-09-07 20:12:54 +00008646 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01008647 NL80211_CMD_PROBE_CLIENT);
Dan Carpentercb35fba2013-08-14 14:50:01 +03008648 if (!hdr) {
8649 err = -ENOBUFS;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008650 goto free_msg;
8651 }
8652
8653 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
8654
Hila Gonene35e4d22012-06-27 17:19:42 +03008655 err = rdev_probe_client(rdev, dev, addr, &cookie);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008656 if (err)
8657 goto free_msg;
8658
David S. Miller9360ffd2012-03-29 04:41:26 -04008659 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8660 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008661
8662 genlmsg_end(msg, hdr);
8663
8664 return genlmsg_reply(msg, info);
8665
8666 nla_put_failure:
8667 err = -ENOBUFS;
8668 free_msg:
8669 nlmsg_free(msg);
8670 return err;
8671}
8672
Johannes Berg5e7602302011-11-04 11:18:17 +01008673static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
8674{
8675 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Ben Greear37c73b52012-10-26 14:49:25 -07008676 struct cfg80211_beacon_registration *reg, *nreg;
8677 int rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008678
8679 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
8680 return -EOPNOTSUPP;
8681
Ben Greear37c73b52012-10-26 14:49:25 -07008682 nreg = kzalloc(sizeof(*nreg), GFP_KERNEL);
8683 if (!nreg)
8684 return -ENOMEM;
Johannes Berg5e7602302011-11-04 11:18:17 +01008685
Ben Greear37c73b52012-10-26 14:49:25 -07008686 /* First, check if already registered. */
8687 spin_lock_bh(&rdev->beacon_registrations_lock);
8688 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
8689 if (reg->nlportid == info->snd_portid) {
8690 rv = -EALREADY;
8691 goto out_err;
8692 }
8693 }
8694 /* Add it to the list */
8695 nreg->nlportid = info->snd_portid;
8696 list_add(&nreg->list, &rdev->beacon_registrations);
8697
8698 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +01008699
8700 return 0;
Ben Greear37c73b52012-10-26 14:49:25 -07008701out_err:
8702 spin_unlock_bh(&rdev->beacon_registrations_lock);
8703 kfree(nreg);
8704 return rv;
Johannes Berg5e7602302011-11-04 11:18:17 +01008705}
8706
Johannes Berg98104fde2012-06-16 00:19:54 +02008707static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
8708{
8709 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8710 struct wireless_dev *wdev = info->user_ptr[1];
8711 int err;
8712
8713 if (!rdev->ops->start_p2p_device)
8714 return -EOPNOTSUPP;
8715
8716 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8717 return -EOPNOTSUPP;
8718
8719 if (wdev->p2p_started)
8720 return 0;
8721
Johannes Berg98104fde2012-06-16 00:19:54 +02008722 err = cfg80211_can_add_interface(rdev, wdev->iftype);
Johannes Berg98104fde2012-06-16 00:19:54 +02008723 if (err)
8724 return err;
8725
Johannes Bergeeb126e2012-10-23 15:16:50 +02008726 err = rdev_start_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008727 if (err)
8728 return err;
8729
8730 wdev->p2p_started = true;
Johannes Berg98104fde2012-06-16 00:19:54 +02008731 rdev->opencount++;
Johannes Berg98104fde2012-06-16 00:19:54 +02008732
8733 return 0;
8734}
8735
8736static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
8737{
8738 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8739 struct wireless_dev *wdev = info->user_ptr[1];
8740
8741 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
8742 return -EOPNOTSUPP;
8743
8744 if (!rdev->ops->stop_p2p_device)
8745 return -EOPNOTSUPP;
8746
Johannes Bergf9f47522013-03-19 15:04:07 +01008747 cfg80211_stop_p2p_device(rdev, wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02008748
8749 return 0;
8750}
8751
Johannes Berg3713b4e2013-02-14 16:19:38 +01008752static int nl80211_get_protocol_features(struct sk_buff *skb,
8753 struct genl_info *info)
8754{
8755 void *hdr;
8756 struct sk_buff *msg;
8757
8758 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
8759 if (!msg)
8760 return -ENOMEM;
8761
8762 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
8763 NL80211_CMD_GET_PROTOCOL_FEATURES);
8764 if (!hdr)
8765 goto nla_put_failure;
8766
8767 if (nla_put_u32(msg, NL80211_ATTR_PROTOCOL_FEATURES,
8768 NL80211_PROTOCOL_FEATURE_SPLIT_WIPHY_DUMP))
8769 goto nla_put_failure;
8770
8771 genlmsg_end(msg, hdr);
8772 return genlmsg_reply(msg, info);
8773
8774 nla_put_failure:
8775 kfree_skb(msg);
8776 return -ENOBUFS;
8777}
8778
Jouni Malinen355199e2013-02-27 17:14:27 +02008779static int nl80211_update_ft_ies(struct sk_buff *skb, struct genl_info *info)
8780{
8781 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8782 struct cfg80211_update_ft_ies_params ft_params;
8783 struct net_device *dev = info->user_ptr[1];
8784
8785 if (!rdev->ops->update_ft_ies)
8786 return -EOPNOTSUPP;
8787
8788 if (!info->attrs[NL80211_ATTR_MDID] ||
8789 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
8790 return -EINVAL;
8791
8792 memset(&ft_params, 0, sizeof(ft_params));
8793 ft_params.md = nla_get_u16(info->attrs[NL80211_ATTR_MDID]);
8794 ft_params.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
8795 ft_params.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
8796
8797 return rdev_update_ft_ies(rdev, dev, &ft_params);
8798}
8799
Arend van Spriel5de17982013-04-18 15:49:00 +02008800static int nl80211_crit_protocol_start(struct sk_buff *skb,
8801 struct genl_info *info)
8802{
8803 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8804 struct wireless_dev *wdev = info->user_ptr[1];
8805 enum nl80211_crit_proto_id proto = NL80211_CRIT_PROTO_UNSPEC;
8806 u16 duration;
8807 int ret;
8808
8809 if (!rdev->ops->crit_proto_start)
8810 return -EOPNOTSUPP;
8811
8812 if (WARN_ON(!rdev->ops->crit_proto_stop))
8813 return -EINVAL;
8814
8815 if (rdev->crit_proto_nlportid)
8816 return -EBUSY;
8817
8818 /* determine protocol if provided */
8819 if (info->attrs[NL80211_ATTR_CRIT_PROT_ID])
8820 proto = nla_get_u16(info->attrs[NL80211_ATTR_CRIT_PROT_ID]);
8821
8822 if (proto >= NUM_NL80211_CRIT_PROTO)
8823 return -EINVAL;
8824
8825 /* timeout must be provided */
8826 if (!info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION])
8827 return -EINVAL;
8828
8829 duration =
8830 nla_get_u16(info->attrs[NL80211_ATTR_MAX_CRIT_PROT_DURATION]);
8831
8832 if (duration > NL80211_CRIT_PROTO_MAX_DURATION)
8833 return -ERANGE;
8834
8835 ret = rdev_crit_proto_start(rdev, wdev, proto, duration);
8836 if (!ret)
8837 rdev->crit_proto_nlportid = info->snd_portid;
8838
8839 return ret;
8840}
8841
8842static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8843 struct genl_info *info)
8844{
8845 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8846 struct wireless_dev *wdev = info->user_ptr[1];
8847
8848 if (!rdev->ops->crit_proto_stop)
8849 return -EOPNOTSUPP;
8850
8851 if (rdev->crit_proto_nlportid) {
8852 rdev->crit_proto_nlportid = 0;
8853 rdev_crit_proto_stop(rdev, wdev);
8854 }
8855 return 0;
8856}
8857
Johannes Bergad7e7182013-11-13 13:37:47 +01008858static int nl80211_vendor_cmd(struct sk_buff *skb, struct genl_info *info)
8859{
8860 struct cfg80211_registered_device *rdev = info->user_ptr[0];
8861 struct wireless_dev *wdev =
8862 __cfg80211_wdev_from_attrs(genl_info_net(info), info->attrs);
8863 int i, err;
8864 u32 vid, subcmd;
8865
8866 if (!rdev->wiphy.vendor_commands)
8867 return -EOPNOTSUPP;
8868
8869 if (IS_ERR(wdev)) {
8870 err = PTR_ERR(wdev);
8871 if (err != -EINVAL)
8872 return err;
8873 wdev = NULL;
8874 } else if (wdev->wiphy != &rdev->wiphy) {
8875 return -EINVAL;
8876 }
8877
8878 if (!info->attrs[NL80211_ATTR_VENDOR_ID] ||
8879 !info->attrs[NL80211_ATTR_VENDOR_SUBCMD])
8880 return -EINVAL;
8881
8882 vid = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_ID]);
8883 subcmd = nla_get_u32(info->attrs[NL80211_ATTR_VENDOR_SUBCMD]);
8884 for (i = 0; i < rdev->wiphy.n_vendor_commands; i++) {
8885 const struct wiphy_vendor_command *vcmd;
8886 void *data = NULL;
8887 int len = 0;
8888
8889 vcmd = &rdev->wiphy.vendor_commands[i];
8890
8891 if (vcmd->info.vendor_id != vid || vcmd->info.subcmd != subcmd)
8892 continue;
8893
8894 if (vcmd->flags & (WIPHY_VENDOR_CMD_NEED_WDEV |
8895 WIPHY_VENDOR_CMD_NEED_NETDEV)) {
8896 if (!wdev)
8897 return -EINVAL;
8898 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_NETDEV &&
8899 !wdev->netdev)
8900 return -EINVAL;
8901
8902 if (vcmd->flags & WIPHY_VENDOR_CMD_NEED_RUNNING) {
8903 if (wdev->netdev &&
8904 !netif_running(wdev->netdev))
8905 return -ENETDOWN;
8906 if (!wdev->netdev && !wdev->p2p_started)
8907 return -ENETDOWN;
8908 }
8909 } else {
8910 wdev = NULL;
8911 }
8912
8913 if (info->attrs[NL80211_ATTR_VENDOR_DATA]) {
8914 data = nla_data(info->attrs[NL80211_ATTR_VENDOR_DATA]);
8915 len = nla_len(info->attrs[NL80211_ATTR_VENDOR_DATA]);
8916 }
8917
8918 rdev->cur_cmd_info = info;
8919 err = rdev->wiphy.vendor_commands[i].doit(&rdev->wiphy, wdev,
8920 data, len);
8921 rdev->cur_cmd_info = NULL;
8922 return err;
8923 }
8924
8925 return -EOPNOTSUPP;
8926}
8927
8928struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
8929 enum nl80211_commands cmd,
8930 enum nl80211_attrs attr,
8931 int approxlen)
8932{
8933 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8934
8935 if (WARN_ON(!rdev->cur_cmd_info))
8936 return NULL;
8937
8938 return __cfg80211_alloc_vendor_skb(rdev, approxlen,
8939 rdev->cur_cmd_info->snd_portid,
8940 rdev->cur_cmd_info->snd_seq,
8941 cmd, attr, GFP_KERNEL);
8942}
8943EXPORT_SYMBOL(__cfg80211_alloc_reply_skb);
8944
8945int cfg80211_vendor_cmd_reply(struct sk_buff *skb)
8946{
8947 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
8948 void *hdr = ((void **)skb->cb)[1];
8949 struct nlattr *data = ((void **)skb->cb)[2];
8950
8951 if (WARN_ON(!rdev->cur_cmd_info)) {
8952 kfree_skb(skb);
8953 return -EINVAL;
8954 }
8955
8956 nla_nest_end(skb, data);
8957 genlmsg_end(skb, hdr);
8958 return genlmsg_reply(skb, rdev->cur_cmd_info);
8959}
8960EXPORT_SYMBOL_GPL(cfg80211_vendor_cmd_reply);
8961
8962
Johannes Berg4c476992010-10-04 21:36:35 +02008963#define NL80211_FLAG_NEED_WIPHY 0x01
8964#define NL80211_FLAG_NEED_NETDEV 0x02
8965#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02008966#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
8967#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
8968 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02008969#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02008970/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02008971#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8972 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02008973
8974static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8975 struct genl_info *info)
8976{
8977 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02008978 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02008979 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02008980 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
8981
8982 if (rtnl)
8983 rtnl_lock();
8984
8985 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02008986 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02008987 if (IS_ERR(rdev)) {
8988 if (rtnl)
8989 rtnl_unlock();
8990 return PTR_ERR(rdev);
8991 }
8992 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02008993 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
8994 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg5fe231e2013-05-08 21:45:15 +02008995 ASSERT_RTNL();
8996
Johannes Berg89a54e42012-06-15 14:33:17 +02008997 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
8998 info->attrs);
8999 if (IS_ERR(wdev)) {
Johannes Berg4c476992010-10-04 21:36:35 +02009000 if (rtnl)
9001 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02009002 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02009003 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009004
Johannes Berg89a54e42012-06-15 14:33:17 +02009005 dev = wdev->netdev;
9006 rdev = wiphy_to_dev(wdev->wiphy);
9007
Johannes Berg1bf614e2012-06-15 15:23:36 +02009008 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
9009 if (!dev) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009010 if (rtnl)
9011 rtnl_unlock();
9012 return -EINVAL;
9013 }
9014
9015 info->user_ptr[1] = dev;
9016 } else {
9017 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02009018 }
Johannes Berg89a54e42012-06-15 14:33:17 +02009019
Johannes Berg1bf614e2012-06-15 15:23:36 +02009020 if (dev) {
9021 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
9022 !netif_running(dev)) {
Johannes Berg1bf614e2012-06-15 15:23:36 +02009023 if (rtnl)
9024 rtnl_unlock();
9025 return -ENETDOWN;
9026 }
9027
9028 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02009029 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
9030 if (!wdev->p2p_started) {
Johannes Berg98104fde2012-06-16 00:19:54 +02009031 if (rtnl)
9032 rtnl_unlock();
9033 return -ENETDOWN;
9034 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02009035 }
9036
Johannes Berg4c476992010-10-04 21:36:35 +02009037 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02009038 }
9039
9040 return 0;
9041}
9042
9043static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
9044 struct genl_info *info)
9045{
Johannes Berg1bf614e2012-06-15 15:23:36 +02009046 if (info->user_ptr[1]) {
9047 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
9048 struct wireless_dev *wdev = info->user_ptr[1];
9049
9050 if (wdev->netdev)
9051 dev_put(wdev->netdev);
9052 } else {
9053 dev_put(info->user_ptr[1]);
9054 }
9055 }
Johannes Berg4c476992010-10-04 21:36:35 +02009056 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
9057 rtnl_unlock();
9058}
9059
Johannes Berg55682962007-09-20 13:09:35 -04009060static struct genl_ops nl80211_ops[] = {
9061 {
9062 .cmd = NL80211_CMD_GET_WIPHY,
9063 .doit = nl80211_get_wiphy,
9064 .dumpit = nl80211_dump_wiphy,
Johannes Berg86e8cf92013-06-19 10:57:22 +02009065 .done = nl80211_dump_wiphy_done,
Johannes Berg55682962007-09-20 13:09:35 -04009066 .policy = nl80211_policy,
9067 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009068 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9069 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009070 },
9071 {
9072 .cmd = NL80211_CMD_SET_WIPHY,
9073 .doit = nl80211_set_wiphy,
9074 .policy = nl80211_policy,
9075 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009076 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009077 },
9078 {
9079 .cmd = NL80211_CMD_GET_INTERFACE,
9080 .doit = nl80211_get_interface,
9081 .dumpit = nl80211_dump_interface,
9082 .policy = nl80211_policy,
9083 /* can be retrieved by unprivileged users */
Johannes Berg5fe231e2013-05-08 21:45:15 +02009084 .internal_flags = NL80211_FLAG_NEED_WDEV |
9085 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009086 },
9087 {
9088 .cmd = NL80211_CMD_SET_INTERFACE,
9089 .doit = nl80211_set_interface,
9090 .policy = nl80211_policy,
9091 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009092 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9093 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009094 },
9095 {
9096 .cmd = NL80211_CMD_NEW_INTERFACE,
9097 .doit = nl80211_new_interface,
9098 .policy = nl80211_policy,
9099 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009100 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9101 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009102 },
9103 {
9104 .cmd = NL80211_CMD_DEL_INTERFACE,
9105 .doit = nl80211_del_interface,
9106 .policy = nl80211_policy,
9107 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02009108 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009109 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04009110 },
Johannes Berg41ade002007-12-19 02:03:29 +01009111 {
9112 .cmd = NL80211_CMD_GET_KEY,
9113 .doit = nl80211_get_key,
9114 .policy = nl80211_policy,
9115 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009116 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009117 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009118 },
9119 {
9120 .cmd = NL80211_CMD_SET_KEY,
9121 .doit = nl80211_set_key,
9122 .policy = nl80211_policy,
9123 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009124 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009125 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009126 },
9127 {
9128 .cmd = NL80211_CMD_NEW_KEY,
9129 .doit = nl80211_new_key,
9130 .policy = nl80211_policy,
9131 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009132 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009133 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009134 },
9135 {
9136 .cmd = NL80211_CMD_DEL_KEY,
9137 .doit = nl80211_del_key,
9138 .policy = nl80211_policy,
9139 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009140 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009141 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01009142 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01009143 {
9144 .cmd = NL80211_CMD_SET_BEACON,
9145 .policy = nl80211_policy,
9146 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009147 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009148 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009149 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009150 },
9151 {
Johannes Berg88600202012-02-13 15:17:18 +01009152 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009153 .policy = nl80211_policy,
9154 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009155 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009156 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009157 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009158 },
9159 {
Johannes Berg88600202012-02-13 15:17:18 +01009160 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009161 .policy = nl80211_policy,
9162 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01009163 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009164 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009165 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01009166 },
Johannes Berg5727ef12007-12-19 02:03:34 +01009167 {
9168 .cmd = NL80211_CMD_GET_STATION,
9169 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009170 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01009171 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02009172 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9173 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009174 },
9175 {
9176 .cmd = NL80211_CMD_SET_STATION,
9177 .doit = nl80211_set_station,
9178 .policy = nl80211_policy,
9179 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009180 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009181 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009182 },
9183 {
9184 .cmd = NL80211_CMD_NEW_STATION,
9185 .doit = nl80211_new_station,
9186 .policy = nl80211_policy,
9187 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009188 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009189 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009190 },
9191 {
9192 .cmd = NL80211_CMD_DEL_STATION,
9193 .doit = nl80211_del_station,
9194 .policy = nl80211_policy,
9195 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009196 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009197 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01009198 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009199 {
9200 .cmd = NL80211_CMD_GET_MPATH,
9201 .doit = nl80211_get_mpath,
9202 .dumpit = nl80211_dump_mpath,
9203 .policy = nl80211_policy,
9204 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009205 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009206 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009207 },
9208 {
9209 .cmd = NL80211_CMD_SET_MPATH,
9210 .doit = nl80211_set_mpath,
9211 .policy = nl80211_policy,
9212 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009213 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009214 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009215 },
9216 {
9217 .cmd = NL80211_CMD_NEW_MPATH,
9218 .doit = nl80211_new_mpath,
9219 .policy = nl80211_policy,
9220 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009221 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009222 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009223 },
9224 {
9225 .cmd = NL80211_CMD_DEL_MPATH,
9226 .doit = nl80211_del_mpath,
9227 .policy = nl80211_policy,
9228 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009229 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009230 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01009231 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009232 {
9233 .cmd = NL80211_CMD_SET_BSS,
9234 .doit = nl80211_set_bss,
9235 .policy = nl80211_policy,
9236 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009237 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009238 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03009239 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009240 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009241 .cmd = NL80211_CMD_GET_REG,
9242 .doit = nl80211_get_reg,
9243 .policy = nl80211_policy,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009244 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08009245 /* can be retrieved by unprivileged users */
9246 },
9247 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009248 .cmd = NL80211_CMD_SET_REG,
9249 .doit = nl80211_set_reg,
9250 .policy = nl80211_policy,
9251 .flags = GENL_ADMIN_PERM,
Johannes Berg5fe231e2013-05-08 21:45:15 +02009252 .internal_flags = NL80211_FLAG_NEED_RTNL,
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07009253 },
9254 {
9255 .cmd = NL80211_CMD_REQ_SET_REG,
9256 .doit = nl80211_req_set_reg,
9257 .policy = nl80211_policy,
9258 .flags = GENL_ADMIN_PERM,
9259 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009260 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009261 .cmd = NL80211_CMD_GET_MESH_CONFIG,
9262 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009263 .policy = nl80211_policy,
9264 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009265 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009266 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009267 },
9268 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08009269 .cmd = NL80211_CMD_SET_MESH_CONFIG,
9270 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009271 .policy = nl80211_policy,
9272 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01009273 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009274 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07009275 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02009276 {
Johannes Berg2a519312009-02-10 21:25:55 +01009277 .cmd = NL80211_CMD_TRIGGER_SCAN,
9278 .doit = nl80211_trigger_scan,
9279 .policy = nl80211_policy,
9280 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02009281 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009282 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01009283 },
9284 {
9285 .cmd = NL80211_CMD_GET_SCAN,
9286 .policy = nl80211_policy,
9287 .dumpit = nl80211_dump_scan,
9288 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02009289 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03009290 .cmd = NL80211_CMD_START_SCHED_SCAN,
9291 .doit = nl80211_start_sched_scan,
9292 .policy = nl80211_policy,
9293 .flags = GENL_ADMIN_PERM,
9294 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9295 NL80211_FLAG_NEED_RTNL,
9296 },
9297 {
9298 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
9299 .doit = nl80211_stop_sched_scan,
9300 .policy = nl80211_policy,
9301 .flags = GENL_ADMIN_PERM,
9302 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9303 NL80211_FLAG_NEED_RTNL,
9304 },
9305 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02009306 .cmd = NL80211_CMD_AUTHENTICATE,
9307 .doit = nl80211_authenticate,
9308 .policy = nl80211_policy,
9309 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009310 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009311 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009312 },
9313 {
9314 .cmd = NL80211_CMD_ASSOCIATE,
9315 .doit = nl80211_associate,
9316 .policy = nl80211_policy,
9317 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009318 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009319 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009320 },
9321 {
9322 .cmd = NL80211_CMD_DEAUTHENTICATE,
9323 .doit = nl80211_deauthenticate,
9324 .policy = nl80211_policy,
9325 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009326 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009327 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009328 },
9329 {
9330 .cmd = NL80211_CMD_DISASSOCIATE,
9331 .doit = nl80211_disassociate,
9332 .policy = nl80211_policy,
9333 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009334 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009335 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02009336 },
Johannes Berg04a773a2009-04-19 21:24:32 +02009337 {
9338 .cmd = NL80211_CMD_JOIN_IBSS,
9339 .doit = nl80211_join_ibss,
9340 .policy = nl80211_policy,
9341 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009342 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009343 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009344 },
9345 {
9346 .cmd = NL80211_CMD_LEAVE_IBSS,
9347 .doit = nl80211_leave_ibss,
9348 .policy = nl80211_policy,
9349 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009350 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009351 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02009352 },
Johannes Bergaff89a92009-07-01 21:26:51 +02009353#ifdef CONFIG_NL80211_TESTMODE
9354 {
9355 .cmd = NL80211_CMD_TESTMODE,
9356 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07009357 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02009358 .policy = nl80211_policy,
9359 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009360 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9361 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02009362 },
9363#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02009364 {
9365 .cmd = NL80211_CMD_CONNECT,
9366 .doit = nl80211_connect,
9367 .policy = nl80211_policy,
9368 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009369 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009370 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009371 },
9372 {
9373 .cmd = NL80211_CMD_DISCONNECT,
9374 .doit = nl80211_disconnect,
9375 .policy = nl80211_policy,
9376 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02009377 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009378 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02009379 },
Johannes Berg463d0182009-07-14 00:33:35 +02009380 {
9381 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
9382 .doit = nl80211_wiphy_netns,
9383 .policy = nl80211_policy,
9384 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009385 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9386 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02009387 },
Holger Schurig61fa7132009-11-11 12:25:40 +01009388 {
9389 .cmd = NL80211_CMD_GET_SURVEY,
9390 .policy = nl80211_policy,
9391 .dumpit = nl80211_dump_survey,
9392 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009393 {
9394 .cmd = NL80211_CMD_SET_PMKSA,
9395 .doit = nl80211_setdel_pmksa,
9396 .policy = nl80211_policy,
9397 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009398 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009399 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009400 },
9401 {
9402 .cmd = NL80211_CMD_DEL_PMKSA,
9403 .doit = nl80211_setdel_pmksa,
9404 .policy = nl80211_policy,
9405 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009406 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009407 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009408 },
9409 {
9410 .cmd = NL80211_CMD_FLUSH_PMKSA,
9411 .doit = nl80211_flush_pmksa,
9412 .policy = nl80211_policy,
9413 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009414 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009415 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01009416 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009417 {
9418 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
9419 .doit = nl80211_remain_on_channel,
9420 .policy = nl80211_policy,
9421 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009422 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009423 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009424 },
9425 {
9426 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
9427 .doit = nl80211_cancel_remain_on_channel,
9428 .policy = nl80211_policy,
9429 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009430 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009431 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009432 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009433 {
9434 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
9435 .doit = nl80211_set_tx_bitrate_mask,
9436 .policy = nl80211_policy,
9437 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009438 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9439 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02009440 },
Jouni Malinen026331c2010-02-15 12:53:10 +02009441 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009442 .cmd = NL80211_CMD_REGISTER_FRAME,
9443 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009444 .policy = nl80211_policy,
9445 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009446 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02009447 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009448 },
9449 {
Johannes Berg2e161f72010-08-12 15:38:38 +02009450 .cmd = NL80211_CMD_FRAME,
9451 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02009452 .policy = nl80211_policy,
9453 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009454 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02009455 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02009456 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02009457 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009458 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
9459 .doit = nl80211_tx_mgmt_cancel_wait,
9460 .policy = nl80211_policy,
9461 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02009462 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01009463 NL80211_FLAG_NEED_RTNL,
9464 },
9465 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02009466 .cmd = NL80211_CMD_SET_POWER_SAVE,
9467 .doit = nl80211_set_power_save,
9468 .policy = nl80211_policy,
9469 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009470 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9471 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009472 },
9473 {
9474 .cmd = NL80211_CMD_GET_POWER_SAVE,
9475 .doit = nl80211_get_power_save,
9476 .policy = nl80211_policy,
9477 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02009478 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9479 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02009480 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009481 {
9482 .cmd = NL80211_CMD_SET_CQM,
9483 .doit = nl80211_set_cqm,
9484 .policy = nl80211_policy,
9485 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009486 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9487 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02009488 },
Johannes Bergf444de02010-05-05 15:25:02 +02009489 {
9490 .cmd = NL80211_CMD_SET_CHANNEL,
9491 .doit = nl80211_set_channel,
9492 .policy = nl80211_policy,
9493 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02009494 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9495 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02009496 },
Bill Jordane8347eb2010-10-01 13:54:28 -04009497 {
9498 .cmd = NL80211_CMD_SET_WDS_PEER,
9499 .doit = nl80211_set_wds_peer,
9500 .policy = nl80211_policy,
9501 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02009502 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9503 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04009504 },
Johannes Berg29cbe682010-12-03 09:20:44 +01009505 {
9506 .cmd = NL80211_CMD_JOIN_MESH,
9507 .doit = nl80211_join_mesh,
9508 .policy = nl80211_policy,
9509 .flags = GENL_ADMIN_PERM,
9510 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9511 NL80211_FLAG_NEED_RTNL,
9512 },
9513 {
9514 .cmd = NL80211_CMD_LEAVE_MESH,
9515 .doit = nl80211_leave_mesh,
9516 .policy = nl80211_policy,
9517 .flags = GENL_ADMIN_PERM,
9518 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9519 NL80211_FLAG_NEED_RTNL,
9520 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009521#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02009522 {
9523 .cmd = NL80211_CMD_GET_WOWLAN,
9524 .doit = nl80211_get_wowlan,
9525 .policy = nl80211_policy,
9526 /* can be retrieved by unprivileged users */
9527 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9528 NL80211_FLAG_NEED_RTNL,
9529 },
9530 {
9531 .cmd = NL80211_CMD_SET_WOWLAN,
9532 .doit = nl80211_set_wowlan,
9533 .policy = nl80211_policy,
9534 .flags = GENL_ADMIN_PERM,
9535 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9536 NL80211_FLAG_NEED_RTNL,
9537 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02009538#endif
Johannes Berge5497d72011-07-05 16:35:40 +02009539 {
9540 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
9541 .doit = nl80211_set_rekey_data,
9542 .policy = nl80211_policy,
9543 .flags = GENL_ADMIN_PERM,
9544 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9545 NL80211_FLAG_NEED_RTNL,
9546 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03009547 {
9548 .cmd = NL80211_CMD_TDLS_MGMT,
9549 .doit = nl80211_tdls_mgmt,
9550 .policy = nl80211_policy,
9551 .flags = GENL_ADMIN_PERM,
9552 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9553 NL80211_FLAG_NEED_RTNL,
9554 },
9555 {
9556 .cmd = NL80211_CMD_TDLS_OPER,
9557 .doit = nl80211_tdls_oper,
9558 .policy = nl80211_policy,
9559 .flags = GENL_ADMIN_PERM,
9560 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9561 NL80211_FLAG_NEED_RTNL,
9562 },
Johannes Berg28946da2011-11-04 11:18:12 +01009563 {
9564 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
9565 .doit = nl80211_register_unexpected_frame,
9566 .policy = nl80211_policy,
9567 .flags = GENL_ADMIN_PERM,
9568 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9569 NL80211_FLAG_NEED_RTNL,
9570 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01009571 {
9572 .cmd = NL80211_CMD_PROBE_CLIENT,
9573 .doit = nl80211_probe_client,
9574 .policy = nl80211_policy,
9575 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02009576 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01009577 NL80211_FLAG_NEED_RTNL,
9578 },
Johannes Berg5e7602302011-11-04 11:18:17 +01009579 {
9580 .cmd = NL80211_CMD_REGISTER_BEACONS,
9581 .doit = nl80211_register_beacons,
9582 .policy = nl80211_policy,
9583 .flags = GENL_ADMIN_PERM,
9584 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9585 NL80211_FLAG_NEED_RTNL,
9586 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01009587 {
9588 .cmd = NL80211_CMD_SET_NOACK_MAP,
9589 .doit = nl80211_set_noack_map,
9590 .policy = nl80211_policy,
9591 .flags = GENL_ADMIN_PERM,
9592 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9593 NL80211_FLAG_NEED_RTNL,
9594 },
Johannes Berg98104fde2012-06-16 00:19:54 +02009595 {
9596 .cmd = NL80211_CMD_START_P2P_DEVICE,
9597 .doit = nl80211_start_p2p_device,
9598 .policy = nl80211_policy,
9599 .flags = GENL_ADMIN_PERM,
9600 .internal_flags = NL80211_FLAG_NEED_WDEV |
9601 NL80211_FLAG_NEED_RTNL,
9602 },
9603 {
9604 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
9605 .doit = nl80211_stop_p2p_device,
9606 .policy = nl80211_policy,
9607 .flags = GENL_ADMIN_PERM,
9608 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9609 NL80211_FLAG_NEED_RTNL,
9610 },
Antonio Quartullif4e583c2012-11-02 13:27:48 +01009611 {
9612 .cmd = NL80211_CMD_SET_MCAST_RATE,
9613 .doit = nl80211_set_mcast_rate,
9614 .policy = nl80211_policy,
9615 .flags = GENL_ADMIN_PERM,
9616 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9617 NL80211_FLAG_NEED_RTNL,
9618 },
Vasanthakumar Thiagarajan77765ea2013-01-18 11:18:45 +05309619 {
9620 .cmd = NL80211_CMD_SET_MAC_ACL,
9621 .doit = nl80211_set_mac_acl,
9622 .policy = nl80211_policy,
9623 .flags = GENL_ADMIN_PERM,
9624 .internal_flags = NL80211_FLAG_NEED_NETDEV |
9625 NL80211_FLAG_NEED_RTNL,
9626 },
Simon Wunderlich04f39042013-02-08 18:16:19 +01009627 {
9628 .cmd = NL80211_CMD_RADAR_DETECT,
9629 .doit = nl80211_start_radar_detection,
9630 .policy = nl80211_policy,
9631 .flags = GENL_ADMIN_PERM,
9632 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9633 NL80211_FLAG_NEED_RTNL,
9634 },
Johannes Berg3713b4e2013-02-14 16:19:38 +01009635 {
9636 .cmd = NL80211_CMD_GET_PROTOCOL_FEATURES,
9637 .doit = nl80211_get_protocol_features,
9638 .policy = nl80211_policy,
9639 },
Jouni Malinen355199e2013-02-27 17:14:27 +02009640 {
9641 .cmd = NL80211_CMD_UPDATE_FT_IES,
9642 .doit = nl80211_update_ft_ies,
9643 .policy = nl80211_policy,
9644 .flags = GENL_ADMIN_PERM,
9645 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9646 NL80211_FLAG_NEED_RTNL,
9647 },
Arend van Spriel5de17982013-04-18 15:49:00 +02009648 {
9649 .cmd = NL80211_CMD_CRIT_PROTOCOL_START,
9650 .doit = nl80211_crit_protocol_start,
9651 .policy = nl80211_policy,
9652 .flags = GENL_ADMIN_PERM,
9653 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9654 NL80211_FLAG_NEED_RTNL,
9655 },
9656 {
9657 .cmd = NL80211_CMD_CRIT_PROTOCOL_STOP,
9658 .doit = nl80211_crit_protocol_stop,
9659 .policy = nl80211_policy,
9660 .flags = GENL_ADMIN_PERM,
9661 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
9662 NL80211_FLAG_NEED_RTNL,
Amitkumar Karwarbe29b992013-06-28 11:51:26 -07009663 },
9664 {
9665 .cmd = NL80211_CMD_GET_COALESCE,
9666 .doit = nl80211_get_coalesce,
9667 .policy = nl80211_policy,
9668 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9669 NL80211_FLAG_NEED_RTNL,
9670 },
9671 {
9672 .cmd = NL80211_CMD_SET_COALESCE,
9673 .doit = nl80211_set_coalesce,
9674 .policy = nl80211_policy,
9675 .flags = GENL_ADMIN_PERM,
9676 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9677 NL80211_FLAG_NEED_RTNL,
Simon Wunderlich16ef1fe2013-07-11 16:09:05 +02009678 },
9679 {
9680 .cmd = NL80211_CMD_CHANNEL_SWITCH,
9681 .doit = nl80211_channel_switch,
9682 .policy = nl80211_policy,
9683 .flags = GENL_ADMIN_PERM,
9684 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
9685 NL80211_FLAG_NEED_RTNL,
9686 },
Johannes Bergad7e7182013-11-13 13:37:47 +01009687 {
9688 .cmd = NL80211_CMD_VENDOR,
9689 .doit = nl80211_vendor_cmd,
9690 .policy = nl80211_policy,
9691 .flags = GENL_ADMIN_PERM,
9692 .internal_flags = NL80211_FLAG_NEED_WIPHY |
9693 NL80211_FLAG_NEED_RTNL,
9694 },
Johannes Berg55682962007-09-20 13:09:35 -04009695};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01009696
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009697static struct genl_multicast_group nl80211_mlme_mcgrp = {
9698 .name = "mlme",
9699};
Johannes Berg55682962007-09-20 13:09:35 -04009700
9701/* multicast groups */
9702static struct genl_multicast_group nl80211_config_mcgrp = {
9703 .name = "config",
9704};
Johannes Berg2a519312009-02-10 21:25:55 +01009705static struct genl_multicast_group nl80211_scan_mcgrp = {
9706 .name = "scan",
9707};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009708static struct genl_multicast_group nl80211_regulatory_mcgrp = {
9709 .name = "regulatory",
9710};
Johannes Berg55682962007-09-20 13:09:35 -04009711
9712/* notification functions */
9713
9714void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9715{
9716 struct sk_buff *msg;
Johannes Berg86e8cf92013-06-19 10:57:22 +02009717 struct nl80211_dump_wiphy_state state = {};
Johannes Berg55682962007-09-20 13:09:35 -04009718
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009719 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009720 if (!msg)
9721 return;
9722
Johannes Berg86e8cf92013-06-19 10:57:22 +02009723 if (nl80211_send_wiphy(rdev, msg, 0, 0, 0, &state) < 0) {
Johannes Berg55682962007-09-20 13:09:35 -04009724 nlmsg_free(msg);
9725 return;
9726 }
9727
Johannes Berg463d0182009-07-14 00:33:35 +02009728 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9729 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04009730}
9731
Johannes Berg362a4152009-05-24 16:43:15 +02009732static int nl80211_add_scan_req(struct sk_buff *msg,
9733 struct cfg80211_registered_device *rdev)
9734{
9735 struct cfg80211_scan_request *req = rdev->scan_req;
9736 struct nlattr *nest;
9737 int i;
9738
9739 if (WARN_ON(!req))
9740 return 0;
9741
9742 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
9743 if (!nest)
9744 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009745 for (i = 0; i < req->n_ssids; i++) {
9746 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
9747 goto nla_put_failure;
9748 }
Johannes Berg362a4152009-05-24 16:43:15 +02009749 nla_nest_end(msg, nest);
9750
9751 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
9752 if (!nest)
9753 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04009754 for (i = 0; i < req->n_channels; i++) {
9755 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
9756 goto nla_put_failure;
9757 }
Johannes Berg362a4152009-05-24 16:43:15 +02009758 nla_nest_end(msg, nest);
9759
David S. Miller9360ffd2012-03-29 04:41:26 -04009760 if (req->ie &&
9761 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9762 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02009763
Sam Lefflered4737712012-10-11 21:03:31 -07009764 if (req->flags)
9765 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
9766
Johannes Berg362a4152009-05-24 16:43:15 +02009767 return 0;
9768 nla_put_failure:
9769 return -ENOBUFS;
9770}
9771
Johannes Berga538e2d2009-06-16 19:56:42 +02009772static int nl80211_send_scan_msg(struct sk_buff *msg,
9773 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009774 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009775 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02009776 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01009777{
9778 void *hdr;
9779
Eric W. Biederman15e47302012-09-07 20:12:54 +00009780 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01009781 if (!hdr)
9782 return -1;
9783
David S. Miller9360ffd2012-03-29 04:41:26 -04009784 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02009785 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
9786 wdev->netdev->ifindex)) ||
9787 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04009788 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01009789
Johannes Berg362a4152009-05-24 16:43:15 +02009790 /* ignore errors and send incomplete event anyway */
9791 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01009792
9793 return genlmsg_end(msg, hdr);
9794
9795 nla_put_failure:
9796 genlmsg_cancel(msg, hdr);
9797 return -EMSGSIZE;
9798}
9799
Luciano Coelho807f8a82011-05-11 17:09:35 +03009800static int
9801nl80211_send_sched_scan_msg(struct sk_buff *msg,
9802 struct cfg80211_registered_device *rdev,
9803 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00009804 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03009805{
9806 void *hdr;
9807
Eric W. Biederman15e47302012-09-07 20:12:54 +00009808 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009809 if (!hdr)
9810 return -1;
9811
David S. Miller9360ffd2012-03-29 04:41:26 -04009812 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9813 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
9814 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03009815
9816 return genlmsg_end(msg, hdr);
9817
9818 nla_put_failure:
9819 genlmsg_cancel(msg, hdr);
9820 return -EMSGSIZE;
9821}
9822
Johannes Berga538e2d2009-06-16 19:56:42 +02009823void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009824 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02009825{
9826 struct sk_buff *msg;
9827
Thomas Graf58050fc2012-06-28 03:57:45 +00009828 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009829 if (!msg)
9830 return;
9831
Johannes Bergfd014282012-06-18 19:17:03 +02009832 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009833 NL80211_CMD_TRIGGER_SCAN) < 0) {
9834 nlmsg_free(msg);
9835 return;
9836 }
9837
Johannes Berg463d0182009-07-14 00:33:35 +02009838 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9839 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02009840}
9841
Johannes Berg2a519312009-02-10 21:25:55 +01009842void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009843 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009844{
9845 struct sk_buff *msg;
9846
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009847 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009848 if (!msg)
9849 return;
9850
Johannes Bergfd014282012-06-18 19:17:03 +02009851 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009852 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009853 nlmsg_free(msg);
9854 return;
9855 }
9856
Johannes Berg463d0182009-07-14 00:33:35 +02009857 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9858 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009859}
9860
9861void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02009862 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01009863{
9864 struct sk_buff *msg;
9865
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009866 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009867 if (!msg)
9868 return;
9869
Johannes Bergfd014282012-06-18 19:17:03 +02009870 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02009871 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01009872 nlmsg_free(msg);
9873 return;
9874 }
9875
Johannes Berg463d0182009-07-14 00:33:35 +02009876 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9877 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01009878}
9879
Luciano Coelho807f8a82011-05-11 17:09:35 +03009880void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9881 struct net_device *netdev)
9882{
9883 struct sk_buff *msg;
9884
9885 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
9886 if (!msg)
9887 return;
9888
9889 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
9890 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
9891 nlmsg_free(msg);
9892 return;
9893 }
9894
9895 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9896 nl80211_scan_mcgrp.id, GFP_KERNEL);
9897}
9898
9899void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9900 struct net_device *netdev, u32 cmd)
9901{
9902 struct sk_buff *msg;
9903
Thomas Graf58050fc2012-06-28 03:57:45 +00009904 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03009905 if (!msg)
9906 return;
9907
9908 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
9909 nlmsg_free(msg);
9910 return;
9911 }
9912
9913 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
9914 nl80211_scan_mcgrp.id, GFP_KERNEL);
9915}
9916
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009917/*
9918 * This can happen on global regulatory changes or device specific settings
9919 * based on custom world regulatory domains.
9920 */
9921void nl80211_send_reg_change_event(struct regulatory_request *request)
9922{
9923 struct sk_buff *msg;
9924 void *hdr;
9925
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07009926 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009927 if (!msg)
9928 return;
9929
9930 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
9931 if (!hdr) {
9932 nlmsg_free(msg);
9933 return;
9934 }
9935
9936 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04009937 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
9938 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009939
David S. Miller9360ffd2012-03-29 04:41:26 -04009940 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
9941 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9942 NL80211_REGDOM_TYPE_WORLD))
9943 goto nla_put_failure;
9944 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
9945 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9946 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
9947 goto nla_put_failure;
9948 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
9949 request->intersect) {
9950 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9951 NL80211_REGDOM_TYPE_INTERSECTION))
9952 goto nla_put_failure;
9953 } else {
9954 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
9955 NL80211_REGDOM_TYPE_COUNTRY) ||
9956 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
9957 request->alpha2))
9958 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009959 }
9960
Johannes Bergf4173762012-12-03 18:23:37 +01009961 if (request->wiphy_idx != WIPHY_IDX_INVALID &&
David S. Miller9360ffd2012-03-29 04:41:26 -04009962 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
9963 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009964
Johannes Berg3b7b72e2011-10-22 19:05:51 +02009965 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009966
Johannes Bergbc43b282009-07-25 10:54:13 +02009967 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02009968 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02009969 GFP_ATOMIC);
9970 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009971
9972 return;
9973
9974nla_put_failure:
9975 genlmsg_cancel(msg, hdr);
9976 nlmsg_free(msg);
9977}
9978
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009979static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9980 struct net_device *netdev,
9981 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02009982 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009983{
9984 struct sk_buff *msg;
9985 void *hdr;
9986
Johannes Berge6d6e342009-07-01 21:26:47 +02009987 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009988 if (!msg)
9989 return;
9990
9991 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
9992 if (!hdr) {
9993 nlmsg_free(msg);
9994 return;
9995 }
9996
David S. Miller9360ffd2012-03-29 04:41:26 -04009997 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
9998 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
9999 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
10000 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010001
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010002 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010003
Johannes Berg463d0182009-07-14 00:33:35 +020010004 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10005 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010006 return;
10007
10008 nla_put_failure:
10009 genlmsg_cancel(msg, hdr);
10010 nlmsg_free(msg);
10011}
10012
10013void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010014 struct net_device *netdev, const u8 *buf,
10015 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010016{
10017 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010018 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010019}
10020
10021void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
10022 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010023 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010024{
Johannes Berge6d6e342009-07-01 21:26:47 +020010025 nl80211_send_mlme_event(rdev, netdev, buf, len,
10026 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010027}
10028
Jouni Malinen53b46b82009-03-27 20:53:56 +020010029void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010030 struct net_device *netdev, const u8 *buf,
10031 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010032{
10033 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010034 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010035}
10036
Jouni Malinen53b46b82009-03-27 20:53:56 +020010037void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
10038 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +020010039 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010040{
10041 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +020010042 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +020010043}
10044
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010045void cfg80211_rx_unprot_mlme_mgmt(struct net_device *dev, const u8 *buf,
10046 size_t len)
Jouni Malinencf4e5942010-12-16 00:52:40 +020010047{
Johannes Berg947add32013-02-22 22:05:20 +010010048 struct wireless_dev *wdev = dev->ieee80211_ptr;
10049 struct wiphy *wiphy = wdev->wiphy;
10050 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010051 const struct ieee80211_mgmt *mgmt = (void *)buf;
10052 u32 cmd;
Jouni Malinencf4e5942010-12-16 00:52:40 +020010053
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010054 if (WARN_ON(len < 2))
10055 return;
10056
10057 if (ieee80211_is_deauth(mgmt->frame_control))
10058 cmd = NL80211_CMD_UNPROT_DEAUTHENTICATE;
10059 else
10060 cmd = NL80211_CMD_UNPROT_DISASSOCIATE;
10061
10062 trace_cfg80211_rx_unprot_mlme_mgmt(dev, buf, len);
10063 nl80211_send_mlme_event(rdev, dev, buf, len, cmd, GFP_ATOMIC);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010064}
Johannes Berg6ff57cf2013-05-16 00:55:00 +020010065EXPORT_SYMBOL(cfg80211_rx_unprot_mlme_mgmt);
Jouni Malinencf4e5942010-12-16 00:52:40 +020010066
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -040010067static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
10068 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +020010069 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010070{
10071 struct sk_buff *msg;
10072 void *hdr;
10073
Johannes Berge6d6e342009-07-01 21:26:47 +020010074 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010075 if (!msg)
10076 return;
10077
10078 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10079 if (!hdr) {
10080 nlmsg_free(msg);
10081 return;
10082 }
10083
David S. Miller9360ffd2012-03-29 04:41:26 -040010084 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10085 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10086 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
10087 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10088 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +030010089
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010090 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +030010091
Johannes Berg463d0182009-07-14 00:33:35 +020010092 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10093 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010094 return;
10095
10096 nla_put_failure:
10097 genlmsg_cancel(msg, hdr);
10098 nlmsg_free(msg);
10099}
10100
10101void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010102 struct net_device *netdev, const u8 *addr,
10103 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010104{
10105 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +020010106 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010107}
10108
10109void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +020010110 struct net_device *netdev, const u8 *addr,
10111 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +030010112{
Johannes Berge6d6e342009-07-01 21:26:47 +020010113 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
10114 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +030010115}
10116
Samuel Ortizb23aa672009-07-01 21:26:54 +020010117void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
10118 struct net_device *netdev, const u8 *bssid,
10119 const u8 *req_ie, size_t req_ie_len,
10120 const u8 *resp_ie, size_t resp_ie_len,
10121 u16 status, gfp_t gfp)
10122{
10123 struct sk_buff *msg;
10124 void *hdr;
10125
Thomas Graf58050fc2012-06-28 03:57:45 +000010126 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010127 if (!msg)
10128 return;
10129
10130 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
10131 if (!hdr) {
10132 nlmsg_free(msg);
10133 return;
10134 }
10135
David S. Miller9360ffd2012-03-29 04:41:26 -040010136 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10137 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10138 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
10139 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
10140 (req_ie &&
10141 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10142 (resp_ie &&
10143 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10144 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010145
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010146 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010147
Johannes Berg463d0182009-07-14 00:33:35 +020010148 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10149 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010150 return;
10151
10152 nla_put_failure:
10153 genlmsg_cancel(msg, hdr);
10154 nlmsg_free(msg);
10155
10156}
10157
10158void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
10159 struct net_device *netdev, const u8 *bssid,
10160 const u8 *req_ie, size_t req_ie_len,
10161 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
10162{
10163 struct sk_buff *msg;
10164 void *hdr;
10165
Thomas Graf58050fc2012-06-28 03:57:45 +000010166 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010167 if (!msg)
10168 return;
10169
10170 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
10171 if (!hdr) {
10172 nlmsg_free(msg);
10173 return;
10174 }
10175
David S. Miller9360ffd2012-03-29 04:41:26 -040010176 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10177 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10178 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
10179 (req_ie &&
10180 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
10181 (resp_ie &&
10182 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
10183 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010184
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010185 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010186
Johannes Berg463d0182009-07-14 00:33:35 +020010187 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10188 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010189 return;
10190
10191 nla_put_failure:
10192 genlmsg_cancel(msg, hdr);
10193 nlmsg_free(msg);
10194
10195}
10196
10197void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10198 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +020010199 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +020010200{
10201 struct sk_buff *msg;
10202 void *hdr;
10203
Thomas Graf58050fc2012-06-28 03:57:45 +000010204 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010205 if (!msg)
10206 return;
10207
10208 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
10209 if (!hdr) {
10210 nlmsg_free(msg);
10211 return;
10212 }
10213
David S. Miller9360ffd2012-03-29 04:41:26 -040010214 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10215 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10216 (from_ap && reason &&
10217 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
10218 (from_ap &&
10219 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
10220 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
10221 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +020010222
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010223 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010224
Johannes Berg463d0182009-07-14 00:33:35 +020010225 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10226 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +020010227 return;
10228
10229 nla_put_failure:
10230 genlmsg_cancel(msg, hdr);
10231 nlmsg_free(msg);
10232
10233}
10234
Johannes Berg04a773a2009-04-19 21:24:32 +020010235void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10236 struct net_device *netdev, const u8 *bssid,
10237 gfp_t gfp)
10238{
10239 struct sk_buff *msg;
10240 void *hdr;
10241
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010242 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010243 if (!msg)
10244 return;
10245
10246 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
10247 if (!hdr) {
10248 nlmsg_free(msg);
10249 return;
10250 }
10251
David S. Miller9360ffd2012-03-29 04:41:26 -040010252 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10253 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10254 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10255 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +020010256
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010257 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +020010258
Johannes Berg463d0182009-07-14 00:33:35 +020010259 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10260 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +020010261 return;
10262
10263 nla_put_failure:
10264 genlmsg_cancel(msg, hdr);
10265 nlmsg_free(msg);
10266}
10267
Johannes Berg947add32013-02-22 22:05:20 +010010268void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10269 const u8* ie, u8 ie_len, gfp_t gfp)
Javier Cardonac93b5e72011-04-07 15:08:34 -070010270{
Johannes Berg947add32013-02-22 22:05:20 +010010271 struct wireless_dev *wdev = dev->ieee80211_ptr;
10272 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010273 struct sk_buff *msg;
10274 void *hdr;
10275
Johannes Berg947add32013-02-22 22:05:20 +010010276 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10277 return;
10278
10279 trace_cfg80211_notify_new_peer_candidate(dev, addr);
10280
Javier Cardonac93b5e72011-04-07 15:08:34 -070010281 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10282 if (!msg)
10283 return;
10284
10285 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
10286 if (!hdr) {
10287 nlmsg_free(msg);
10288 return;
10289 }
10290
David S. Miller9360ffd2012-03-29 04:41:26 -040010291 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010292 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10293 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010294 (ie_len && ie &&
10295 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
10296 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -070010297
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010298 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010299
10300 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10301 nl80211_mlme_mcgrp.id, gfp);
10302 return;
10303
10304 nla_put_failure:
10305 genlmsg_cancel(msg, hdr);
10306 nlmsg_free(msg);
10307}
Johannes Berg947add32013-02-22 22:05:20 +010010308EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
Javier Cardonac93b5e72011-04-07 15:08:34 -070010309
Jouni Malinena3b8b052009-03-27 21:59:49 +020010310void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10311 struct net_device *netdev, const u8 *addr,
10312 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +020010313 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +020010314{
10315 struct sk_buff *msg;
10316 void *hdr;
10317
Johannes Berge6d6e342009-07-01 21:26:47 +020010318 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010319 if (!msg)
10320 return;
10321
10322 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
10323 if (!hdr) {
10324 nlmsg_free(msg);
10325 return;
10326 }
10327
David S. Miller9360ffd2012-03-29 04:41:26 -040010328 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10329 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10330 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
10331 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
10332 (key_id != -1 &&
10333 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
10334 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
10335 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +020010336
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010337 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010338
Johannes Berg463d0182009-07-14 00:33:35 +020010339 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10340 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +020010341 return;
10342
10343 nla_put_failure:
10344 genlmsg_cancel(msg, hdr);
10345 nlmsg_free(msg);
10346}
10347
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010348void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10349 struct ieee80211_channel *channel_before,
10350 struct ieee80211_channel *channel_after)
10351{
10352 struct sk_buff *msg;
10353 void *hdr;
10354 struct nlattr *nl_freq;
10355
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070010356 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010357 if (!msg)
10358 return;
10359
10360 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
10361 if (!hdr) {
10362 nlmsg_free(msg);
10363 return;
10364 }
10365
10366 /*
10367 * Since we are applying the beacon hint to a wiphy we know its
10368 * wiphy_idx is valid
10369 */
David S. Miller9360ffd2012-03-29 04:41:26 -040010370 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
10371 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010372
10373 /* Before */
10374 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
10375 if (!nl_freq)
10376 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010377 if (nl80211_msg_put_channel(msg, channel_before, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010378 goto nla_put_failure;
10379 nla_nest_end(msg, nl_freq);
10380
10381 /* After */
10382 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
10383 if (!nl_freq)
10384 goto nla_put_failure;
Johannes Bergcdc89b92013-02-18 23:54:36 +010010385 if (nl80211_msg_put_channel(msg, channel_after, false))
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010386 goto nla_put_failure;
10387 nla_nest_end(msg, nl_freq);
10388
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010389 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010390
Johannes Berg463d0182009-07-14 00:33:35 +020010391 rcu_read_lock();
10392 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
10393 GFP_ATOMIC);
10394 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -040010395
10396 return;
10397
10398nla_put_failure:
10399 genlmsg_cancel(msg, hdr);
10400 nlmsg_free(msg);
10401}
10402
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010403static void nl80211_send_remain_on_chan_event(
10404 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +020010405 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010406 struct ieee80211_channel *chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010407 unsigned int duration, gfp_t gfp)
10408{
10409 struct sk_buff *msg;
10410 void *hdr;
10411
10412 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10413 if (!msg)
10414 return;
10415
10416 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
10417 if (!hdr) {
10418 nlmsg_free(msg);
10419 return;
10420 }
10421
David S. Miller9360ffd2012-03-29 04:41:26 -040010422 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010423 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10424 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +020010425 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010426 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
Johannes Berg42d97a52012-11-08 18:31:02 +010010427 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
10428 NL80211_CHAN_NO_HT) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010429 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
10430 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010431
David S. Miller9360ffd2012-03-29 04:41:26 -040010432 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
10433 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
10434 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010435
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010436 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010437
10438 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10439 nl80211_mlme_mcgrp.id, gfp);
10440 return;
10441
10442 nla_put_failure:
10443 genlmsg_cancel(msg, hdr);
10444 nlmsg_free(msg);
10445}
10446
Johannes Berg947add32013-02-22 22:05:20 +010010447void cfg80211_ready_on_channel(struct wireless_dev *wdev, u64 cookie,
10448 struct ieee80211_channel *chan,
10449 unsigned int duration, gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010450{
Johannes Berg947add32013-02-22 22:05:20 +010010451 struct wiphy *wiphy = wdev->wiphy;
10452 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10453
10454 trace_cfg80211_ready_on_channel(wdev, cookie, chan, duration);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010455 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +020010456 rdev, wdev, cookie, chan,
Johannes Berg42d97a52012-11-08 18:31:02 +010010457 duration, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010458}
Johannes Berg947add32013-02-22 22:05:20 +010010459EXPORT_SYMBOL(cfg80211_ready_on_channel);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010460
Johannes Berg947add32013-02-22 22:05:20 +010010461void cfg80211_remain_on_channel_expired(struct wireless_dev *wdev, u64 cookie,
10462 struct ieee80211_channel *chan,
10463 gfp_t gfp)
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010464{
Johannes Berg947add32013-02-22 22:05:20 +010010465 struct wiphy *wiphy = wdev->wiphy;
10466 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10467
10468 trace_cfg80211_ready_on_channel_expired(wdev, cookie, chan);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010469 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg42d97a52012-11-08 18:31:02 +010010470 rdev, wdev, cookie, chan, 0, gfp);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010471}
Johannes Berg947add32013-02-22 22:05:20 +010010472EXPORT_SYMBOL(cfg80211_remain_on_channel_expired);
Jouni Malinen9588bbd2009-12-23 13:15:41 +010010473
Johannes Berg947add32013-02-22 22:05:20 +010010474void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10475 struct station_info *sinfo, gfp_t gfp)
Johannes Berg98b62182009-12-23 13:15:44 +010010476{
Johannes Berg947add32013-02-22 22:05:20 +010010477 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10478 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg98b62182009-12-23 13:15:44 +010010479 struct sk_buff *msg;
10480
Johannes Berg947add32013-02-22 22:05:20 +010010481 trace_cfg80211_new_sta(dev, mac_addr, sinfo);
10482
Thomas Graf58050fc2012-06-28 03:57:45 +000010483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +010010484 if (!msg)
10485 return;
10486
John W. Linville66266b32012-03-15 13:25:41 -040010487 if (nl80211_send_station(msg, 0, 0, 0,
10488 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +010010489 nlmsg_free(msg);
10490 return;
10491 }
10492
10493 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10494 nl80211_mlme_mcgrp.id, gfp);
10495}
Johannes Berg947add32013-02-22 22:05:20 +010010496EXPORT_SYMBOL(cfg80211_new_sta);
Johannes Berg98b62182009-12-23 13:15:44 +010010497
Johannes Berg947add32013-02-22 22:05:20 +010010498void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
Jouni Malinenec15e682011-03-23 15:29:52 +020010499{
Johannes Berg947add32013-02-22 22:05:20 +010010500 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10501 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Jouni Malinenec15e682011-03-23 15:29:52 +020010502 struct sk_buff *msg;
10503 void *hdr;
10504
Johannes Berg947add32013-02-22 22:05:20 +010010505 trace_cfg80211_del_sta(dev, mac_addr);
10506
Thomas Graf58050fc2012-06-28 03:57:45 +000010507 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +020010508 if (!msg)
10509 return;
10510
10511 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
10512 if (!hdr) {
10513 nlmsg_free(msg);
10514 return;
10515 }
10516
David S. Miller9360ffd2012-03-29 04:41:26 -040010517 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10518 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
10519 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +020010520
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010521 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +020010522
10523 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10524 nl80211_mlme_mcgrp.id, gfp);
10525 return;
10526
10527 nla_put_failure:
10528 genlmsg_cancel(msg, hdr);
10529 nlmsg_free(msg);
10530}
Johannes Berg947add32013-02-22 22:05:20 +010010531EXPORT_SYMBOL(cfg80211_del_sta);
Jouni Malinenec15e682011-03-23 15:29:52 +020010532
Johannes Berg947add32013-02-22 22:05:20 +010010533void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10534 enum nl80211_connect_failed_reason reason,
10535 gfp_t gfp)
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010536{
Johannes Berg947add32013-02-22 22:05:20 +010010537 struct wiphy *wiphy = dev->ieee80211_ptr->wiphy;
10538 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010539 struct sk_buff *msg;
10540 void *hdr;
10541
10542 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10543 if (!msg)
10544 return;
10545
10546 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
10547 if (!hdr) {
10548 nlmsg_free(msg);
10549 return;
10550 }
10551
10552 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10553 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
10554 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
10555 goto nla_put_failure;
10556
10557 genlmsg_end(msg, hdr);
10558
10559 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10560 nl80211_mlme_mcgrp.id, gfp);
10561 return;
10562
10563 nla_put_failure:
10564 genlmsg_cancel(msg, hdr);
10565 nlmsg_free(msg);
10566}
Johannes Berg947add32013-02-22 22:05:20 +010010567EXPORT_SYMBOL(cfg80211_conn_failed);
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +053010568
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010569static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
10570 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +010010571{
10572 struct wireless_dev *wdev = dev->ieee80211_ptr;
10573 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
10574 struct sk_buff *msg;
10575 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +000010576 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010577
Eric W. Biederman15e47302012-09-07 20:12:54 +000010578 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +010010579 return false;
10580
10581 msg = nlmsg_new(100, gfp);
10582 if (!msg)
10583 return true;
10584
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010585 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +010010586 if (!hdr) {
10587 nlmsg_free(msg);
10588 return true;
10589 }
10590
David S. Miller9360ffd2012-03-29 04:41:26 -040010591 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10592 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
10593 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
10594 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +010010595
Johannes Berg9c90a9f2013-06-04 12:46:03 +020010596 genlmsg_end(msg, hdr);
Eric W. Biederman15e47302012-09-07 20:12:54 +000010597 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +010010598 return true;
10599
10600 nla_put_failure:
10601 genlmsg_cancel(msg, hdr);
10602 nlmsg_free(msg);
10603 return true;
10604}
10605
Johannes Berg947add32013-02-22 22:05:20 +010010606bool cfg80211_rx_spurious_frame(struct net_device *dev,
10607 const u8 *addr, gfp_t gfp)
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010608{
Johannes Berg947add32013-02-22 22:05:20 +010010609 struct wireless_dev *wdev = dev->ieee80211_ptr;
10610 bool ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010611
Johannes Berg947add32013-02-22 22:05:20 +010010612 trace_cfg80211_rx_spurious_frame(dev, addr);
10613
10614 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10615 wdev->iftype != NL80211_IFTYPE_P2P_GO)) {
10616 trace_cfg80211_return_bool(false);
10617 return false;
10618 }
10619 ret = __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
10620 addr, gfp);
10621 trace_cfg80211_return_bool(ret);
10622 return ret;
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010623}
Johannes Berg947add32013-02-22 22:05:20 +010010624EXPORT_SYMBOL(cfg80211_rx_spurious_frame);
10625
10626bool cfg80211_rx_unexpected_4addr_frame(struct net_device *dev,
10627 const u8 *addr, gfp_t gfp)
10628{
10629 struct wireless_dev *wdev = dev->ieee80211_ptr;
10630 bool ret;
10631
10632 trace_cfg80211_rx_unexpected_4addr_frame(dev, addr);
10633
10634 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10635 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10636 wdev->iftype != NL80211_IFTYPE_AP_VLAN)) {
10637 trace_cfg80211_return_bool(false);
10638 return false;
10639 }
10640 ret = __nl80211_unexpected_frame(dev,
10641 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
10642 addr, gfp);
10643 trace_cfg80211_return_bool(ret);
10644 return ret;
10645}
10646EXPORT_SYMBOL(cfg80211_rx_unexpected_4addr_frame);
Johannes Bergb92ab5d2011-11-04 11:18:19 +010010647
Johannes Berg2e161f72010-08-12 15:38:38 +020010648int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +000010649 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +010010650 int freq, int sig_dbm,
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010651 const u8 *buf, size_t len, u32 flags, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010652{
Johannes Berg71bbc992012-06-15 15:30:18 +020010653 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010654 struct sk_buff *msg;
10655 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +020010656
10657 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10658 if (!msg)
10659 return -ENOMEM;
10660
Johannes Berg2e161f72010-08-12 15:38:38 +020010661 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +020010662 if (!hdr) {
10663 nlmsg_free(msg);
10664 return -ENOMEM;
10665 }
10666
David S. Miller9360ffd2012-03-29 04:41:26 -040010667 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010668 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10669 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010670 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010671 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
10672 (sig_dbm &&
10673 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
Vladimir Kondratiev19504cf2013-08-15 14:51:28 +030010674 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10675 (flags &&
10676 nla_put_u32(msg, NL80211_ATTR_RXMGMT_FLAGS, flags)))
David S. Miller9360ffd2012-03-29 04:41:26 -040010677 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010678
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010679 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010680
Eric W. Biederman15e47302012-09-07 20:12:54 +000010681 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +020010682
10683 nla_put_failure:
10684 genlmsg_cancel(msg, hdr);
10685 nlmsg_free(msg);
10686 return -ENOBUFS;
10687}
10688
Johannes Berg947add32013-02-22 22:05:20 +010010689void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10690 const u8 *buf, size_t len, bool ack, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +020010691{
Johannes Berg947add32013-02-22 22:05:20 +010010692 struct wiphy *wiphy = wdev->wiphy;
10693 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Berg71bbc992012-06-15 15:30:18 +020010694 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +020010695 struct sk_buff *msg;
10696 void *hdr;
10697
Johannes Berg947add32013-02-22 22:05:20 +010010698 trace_cfg80211_mgmt_tx_status(wdev, cookie, ack);
10699
Jouni Malinen026331c2010-02-15 12:53:10 +020010700 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
10701 if (!msg)
10702 return;
10703
Johannes Berg2e161f72010-08-12 15:38:38 +020010704 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +020010705 if (!hdr) {
10706 nlmsg_free(msg);
10707 return;
10708 }
10709
David S. Miller9360ffd2012-03-29 04:41:26 -040010710 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +020010711 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
10712 netdev->ifindex)) ||
Ilan Peera8384902013-05-08 16:35:55 +030010713 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040010714 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
10715 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
10716 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
10717 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +020010718
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010719 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +020010720
Michal Kaziora0ec5702013-06-25 09:17:17 +020010721 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10722 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen026331c2010-02-15 12:53:10 +020010723 return;
10724
10725 nla_put_failure:
10726 genlmsg_cancel(msg, hdr);
10727 nlmsg_free(msg);
10728}
Johannes Berg947add32013-02-22 22:05:20 +010010729EXPORT_SYMBOL(cfg80211_mgmt_tx_status);
Jouni Malinen026331c2010-02-15 12:53:10 +020010730
Johannes Berg947add32013-02-22 22:05:20 +010010731void cfg80211_cqm_rssi_notify(struct net_device *dev,
10732 enum nl80211_cqm_rssi_threshold_event rssi_event,
10733 gfp_t gfp)
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010734{
Johannes Berg947add32013-02-22 22:05:20 +010010735 struct wireless_dev *wdev = dev->ieee80211_ptr;
10736 struct wiphy *wiphy = wdev->wiphy;
10737 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010738 struct sk_buff *msg;
10739 struct nlattr *pinfoattr;
10740 void *hdr;
10741
Johannes Berg947add32013-02-22 22:05:20 +010010742 trace_cfg80211_cqm_rssi_notify(dev, rssi_event);
10743
Thomas Graf58050fc2012-06-28 03:57:45 +000010744 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010745 if (!msg)
10746 return;
10747
10748 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10749 if (!hdr) {
10750 nlmsg_free(msg);
10751 return;
10752 }
10753
David S. Miller9360ffd2012-03-29 04:41:26 -040010754 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010755 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
David S. Miller9360ffd2012-03-29 04:41:26 -040010756 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010757
10758 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10759 if (!pinfoattr)
10760 goto nla_put_failure;
10761
David S. Miller9360ffd2012-03-29 04:41:26 -040010762 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
10763 rssi_event))
10764 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010765
10766 nla_nest_end(msg, pinfoattr);
10767
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010768 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010769
10770 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10771 nl80211_mlme_mcgrp.id, gfp);
10772 return;
10773
10774 nla_put_failure:
10775 genlmsg_cancel(msg, hdr);
10776 nlmsg_free(msg);
10777}
Johannes Berg947add32013-02-22 22:05:20 +010010778EXPORT_SYMBOL(cfg80211_cqm_rssi_notify);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +020010779
Johannes Berg947add32013-02-22 22:05:20 +010010780static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10781 struct net_device *netdev, const u8 *bssid,
10782 const u8 *replay_ctr, gfp_t gfp)
Johannes Berge5497d72011-07-05 16:35:40 +020010783{
10784 struct sk_buff *msg;
10785 struct nlattr *rekey_attr;
10786 void *hdr;
10787
Thomas Graf58050fc2012-06-28 03:57:45 +000010788 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +020010789 if (!msg)
10790 return;
10791
10792 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
10793 if (!hdr) {
10794 nlmsg_free(msg);
10795 return;
10796 }
10797
David S. Miller9360ffd2012-03-29 04:41:26 -040010798 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10799 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
10800 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
10801 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010802
10803 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
10804 if (!rekey_attr)
10805 goto nla_put_failure;
10806
David S. Miller9360ffd2012-03-29 04:41:26 -040010807 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
10808 NL80211_REPLAY_CTR_LEN, replay_ctr))
10809 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +020010810
10811 nla_nest_end(msg, rekey_attr);
10812
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010813 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +020010814
10815 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10816 nl80211_mlme_mcgrp.id, gfp);
10817 return;
10818
10819 nla_put_failure:
10820 genlmsg_cancel(msg, hdr);
10821 nlmsg_free(msg);
10822}
10823
Johannes Berg947add32013-02-22 22:05:20 +010010824void cfg80211_gtk_rekey_notify(struct net_device *dev, const u8 *bssid,
10825 const u8 *replay_ctr, gfp_t gfp)
10826{
10827 struct wireless_dev *wdev = dev->ieee80211_ptr;
10828 struct wiphy *wiphy = wdev->wiphy;
10829 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10830
10831 trace_cfg80211_gtk_rekey_notify(dev, bssid);
10832 nl80211_gtk_rekey_notify(rdev, dev, bssid, replay_ctr, gfp);
10833}
10834EXPORT_SYMBOL(cfg80211_gtk_rekey_notify);
10835
10836static void
10837nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10838 struct net_device *netdev, int index,
10839 const u8 *bssid, bool preauth, gfp_t gfp)
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010840{
10841 struct sk_buff *msg;
10842 struct nlattr *attr;
10843 void *hdr;
10844
Thomas Graf58050fc2012-06-28 03:57:45 +000010845 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010846 if (!msg)
10847 return;
10848
10849 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
10850 if (!hdr) {
10851 nlmsg_free(msg);
10852 return;
10853 }
10854
David S. Miller9360ffd2012-03-29 04:41:26 -040010855 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
10856 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10857 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010858
10859 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
10860 if (!attr)
10861 goto nla_put_failure;
10862
David S. Miller9360ffd2012-03-29 04:41:26 -040010863 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
10864 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
10865 (preauth &&
10866 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
10867 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010868
10869 nla_nest_end(msg, attr);
10870
Johannes Berg3b7b72e2011-10-22 19:05:51 +020010871 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +030010872
10873 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10874 nl80211_mlme_mcgrp.id, gfp);
10875 return;
10876
10877 nla_put_failure:
10878 genlmsg_cancel(msg, hdr);
10879 nlmsg_free(msg);
10880}
10881
Johannes Berg947add32013-02-22 22:05:20 +010010882void cfg80211_pmksa_candidate_notify(struct net_device *dev, int index,
10883 const u8 *bssid, bool preauth, gfp_t gfp)
10884{
10885 struct wireless_dev *wdev = dev->ieee80211_ptr;
10886 struct wiphy *wiphy = wdev->wiphy;
10887 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10888
10889 trace_cfg80211_pmksa_candidate_notify(dev, index, bssid, preauth);
10890 nl80211_pmksa_candidate_notify(rdev, dev, index, bssid, preauth, gfp);
10891}
10892EXPORT_SYMBOL(cfg80211_pmksa_candidate_notify);
10893
10894static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10895 struct net_device *netdev,
10896 struct cfg80211_chan_def *chandef,
10897 gfp_t gfp)
Thomas Pedersen53145262012-04-06 13:35:47 -070010898{
10899 struct sk_buff *msg;
10900 void *hdr;
10901
Thomas Graf58050fc2012-06-28 03:57:45 +000010902 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -070010903 if (!msg)
10904 return;
10905
10906 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
10907 if (!hdr) {
10908 nlmsg_free(msg);
10909 return;
10910 }
10911
Johannes Berg683b6d32012-11-08 21:25:48 +010010912 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
10913 goto nla_put_failure;
10914
10915 if (nl80211_send_chandef(msg, chandef))
John W. Linville7eab0f62012-04-12 14:25:14 -040010916 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -070010917
10918 genlmsg_end(msg, hdr);
10919
10920 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10921 nl80211_mlme_mcgrp.id, gfp);
10922 return;
10923
10924 nla_put_failure:
10925 genlmsg_cancel(msg, hdr);
10926 nlmsg_free(msg);
10927}
10928
Johannes Berg947add32013-02-22 22:05:20 +010010929void cfg80211_ch_switch_notify(struct net_device *dev,
10930 struct cfg80211_chan_def *chandef)
Thomas Pedersen84f10702012-07-12 16:17:33 -070010931{
Johannes Berg947add32013-02-22 22:05:20 +010010932 struct wireless_dev *wdev = dev->ieee80211_ptr;
10933 struct wiphy *wiphy = wdev->wiphy;
10934 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
10935
Simon Wunderliche487eae2013-11-21 18:19:51 +010010936 ASSERT_WDEV_LOCK(wdev);
Johannes Berg947add32013-02-22 22:05:20 +010010937
Simon Wunderliche487eae2013-11-21 18:19:51 +010010938 trace_cfg80211_ch_switch_notify(dev, chandef);
Johannes Berg947add32013-02-22 22:05:20 +010010939
10940 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
Simon Wunderlichee4bc9e2013-08-28 13:41:33 +020010941 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
Chun-Yeow Yeohb8456a12013-10-17 15:55:02 -070010942 wdev->iftype != NL80211_IFTYPE_ADHOC &&
10943 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
Simon Wunderliche487eae2013-11-21 18:19:51 +010010944 return;
Johannes Berg947add32013-02-22 22:05:20 +010010945
10946 wdev->channel = chandef->chan;
10947 nl80211_ch_switch_notify(rdev, dev, chandef, GFP_KERNEL);
Johannes Berg947add32013-02-22 22:05:20 +010010948}
10949EXPORT_SYMBOL(cfg80211_ch_switch_notify);
10950
10951void cfg80211_cqm_txe_notify(struct net_device *dev,
10952 const u8 *peer, u32 num_packets,
10953 u32 rate, u32 intvl, gfp_t gfp)
10954{
10955 struct wireless_dev *wdev = dev->ieee80211_ptr;
10956 struct wiphy *wiphy = wdev->wiphy;
10957 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Thomas Pedersen84f10702012-07-12 16:17:33 -070010958 struct sk_buff *msg;
10959 struct nlattr *pinfoattr;
10960 void *hdr;
10961
10962 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
10963 if (!msg)
10964 return;
10965
10966 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
10967 if (!hdr) {
10968 nlmsg_free(msg);
10969 return;
10970 }
10971
10972 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010010973 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Thomas Pedersen84f10702012-07-12 16:17:33 -070010974 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
10975 goto nla_put_failure;
10976
10977 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
10978 if (!pinfoattr)
10979 goto nla_put_failure;
10980
10981 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
10982 goto nla_put_failure;
10983
10984 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
10985 goto nla_put_failure;
10986
10987 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
10988 goto nla_put_failure;
10989
10990 nla_nest_end(msg, pinfoattr);
10991
10992 genlmsg_end(msg, hdr);
10993
10994 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
10995 nl80211_mlme_mcgrp.id, gfp);
10996 return;
10997
10998 nla_put_failure:
10999 genlmsg_cancel(msg, hdr);
11000 nlmsg_free(msg);
11001}
Johannes Berg947add32013-02-22 22:05:20 +010011002EXPORT_SYMBOL(cfg80211_cqm_txe_notify);
Thomas Pedersen84f10702012-07-12 16:17:33 -070011003
11004void
Simon Wunderlich04f39042013-02-08 18:16:19 +010011005nl80211_radar_notify(struct cfg80211_registered_device *rdev,
Janusz Dziedzicd2859df2013-11-06 13:55:51 +010011006 const struct cfg80211_chan_def *chandef,
Simon Wunderlich04f39042013-02-08 18:16:19 +010011007 enum nl80211_radar_event event,
11008 struct net_device *netdev, gfp_t gfp)
11009{
11010 struct sk_buff *msg;
11011 void *hdr;
11012
11013 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11014 if (!msg)
11015 return;
11016
11017 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_RADAR_DETECT);
11018 if (!hdr) {
11019 nlmsg_free(msg);
11020 return;
11021 }
11022
11023 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
11024 goto nla_put_failure;
11025
11026 /* NOP and radar events don't need a netdev parameter */
11027 if (netdev) {
11028 struct wireless_dev *wdev = netdev->ieee80211_ptr;
11029
11030 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11031 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11032 goto nla_put_failure;
11033 }
11034
11035 if (nla_put_u32(msg, NL80211_ATTR_RADAR_EVENT, event))
11036 goto nla_put_failure;
11037
11038 if (nl80211_send_chandef(msg, chandef))
11039 goto nla_put_failure;
11040
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011041 genlmsg_end(msg, hdr);
Simon Wunderlich04f39042013-02-08 18:16:19 +010011042
11043 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11044 nl80211_mlme_mcgrp.id, gfp);
11045 return;
11046
11047 nla_put_failure:
11048 genlmsg_cancel(msg, hdr);
11049 nlmsg_free(msg);
11050}
11051
Johannes Berg947add32013-02-22 22:05:20 +010011052void cfg80211_cqm_pktloss_notify(struct net_device *dev,
11053 const u8 *peer, u32 num_packets, gfp_t gfp)
Johannes Bergc063dbf2010-11-24 08:10:05 +010011054{
Johannes Berg947add32013-02-22 22:05:20 +010011055 struct wireless_dev *wdev = dev->ieee80211_ptr;
11056 struct wiphy *wiphy = wdev->wiphy;
11057 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011058 struct sk_buff *msg;
11059 struct nlattr *pinfoattr;
11060 void *hdr;
11061
Johannes Berg947add32013-02-22 22:05:20 +010011062 trace_cfg80211_cqm_pktloss_notify(dev, peer, num_packets);
11063
Thomas Graf58050fc2012-06-28 03:57:45 +000011064 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011065 if (!msg)
11066 return;
11067
11068 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
11069 if (!hdr) {
11070 nlmsg_free(msg);
11071 return;
11072 }
11073
David S. Miller9360ffd2012-03-29 04:41:26 -040011074 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg947add32013-02-22 22:05:20 +010011075 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
David S. Miller9360ffd2012-03-29 04:41:26 -040011076 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
11077 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011078
11079 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
11080 if (!pinfoattr)
11081 goto nla_put_failure;
11082
David S. Miller9360ffd2012-03-29 04:41:26 -040011083 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
11084 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +010011085
11086 nla_nest_end(msg, pinfoattr);
11087
Johannes Berg3b7b72e2011-10-22 19:05:51 +020011088 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011089
11090 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11091 nl80211_mlme_mcgrp.id, gfp);
11092 return;
11093
11094 nla_put_failure:
11095 genlmsg_cancel(msg, hdr);
11096 nlmsg_free(msg);
11097}
Johannes Berg947add32013-02-22 22:05:20 +010011098EXPORT_SYMBOL(cfg80211_cqm_pktloss_notify);
Johannes Bergc063dbf2010-11-24 08:10:05 +010011099
Johannes Berg7f6cf312011-11-04 11:18:15 +010011100void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
11101 u64 cookie, bool acked, gfp_t gfp)
11102{
11103 struct wireless_dev *wdev = dev->ieee80211_ptr;
11104 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11105 struct sk_buff *msg;
11106 void *hdr;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011107
Beni Lev4ee3e062012-08-27 12:49:39 +030011108 trace_cfg80211_probe_status(dev, addr, cookie, acked);
11109
Thomas Graf58050fc2012-06-28 03:57:45 +000011110 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Beni Lev4ee3e062012-08-27 12:49:39 +030011111
Johannes Berg7f6cf312011-11-04 11:18:15 +010011112 if (!msg)
11113 return;
11114
11115 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
11116 if (!hdr) {
11117 nlmsg_free(msg);
11118 return;
11119 }
11120
David S. Miller9360ffd2012-03-29 04:41:26 -040011121 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11122 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11123 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
11124 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
11125 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
11126 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +010011127
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011128 genlmsg_end(msg, hdr);
Johannes Berg7f6cf312011-11-04 11:18:15 +010011129
11130 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11131 nl80211_mlme_mcgrp.id, gfp);
11132 return;
11133
11134 nla_put_failure:
11135 genlmsg_cancel(msg, hdr);
11136 nlmsg_free(msg);
11137}
11138EXPORT_SYMBOL(cfg80211_probe_status);
11139
Johannes Berg5e7602302011-11-04 11:18:17 +010011140void cfg80211_report_obss_beacon(struct wiphy *wiphy,
11141 const u8 *frame, size_t len,
Ben Greear37c73b52012-10-26 14:49:25 -070011142 int freq, int sig_dbm)
Johannes Berg5e7602302011-11-04 11:18:17 +010011143{
11144 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11145 struct sk_buff *msg;
11146 void *hdr;
Ben Greear37c73b52012-10-26 14:49:25 -070011147 struct cfg80211_beacon_registration *reg;
Johannes Berg5e7602302011-11-04 11:18:17 +010011148
Beni Lev4ee3e062012-08-27 12:49:39 +030011149 trace_cfg80211_report_obss_beacon(wiphy, frame, len, freq, sig_dbm);
11150
Ben Greear37c73b52012-10-26 14:49:25 -070011151 spin_lock_bh(&rdev->beacon_registrations_lock);
11152 list_for_each_entry(reg, &rdev->beacon_registrations, list) {
11153 msg = nlmsg_new(len + 100, GFP_ATOMIC);
11154 if (!msg) {
11155 spin_unlock_bh(&rdev->beacon_registrations_lock);
11156 return;
11157 }
Johannes Berg5e7602302011-11-04 11:18:17 +010011158
Ben Greear37c73b52012-10-26 14:49:25 -070011159 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
11160 if (!hdr)
11161 goto nla_put_failure;
Johannes Berg5e7602302011-11-04 11:18:17 +010011162
Ben Greear37c73b52012-10-26 14:49:25 -070011163 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11164 (freq &&
11165 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
11166 (sig_dbm &&
11167 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
11168 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
11169 goto nla_put_failure;
11170
11171 genlmsg_end(msg, hdr);
11172
11173 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, reg->nlportid);
Johannes Berg5e7602302011-11-04 11:18:17 +010011174 }
Ben Greear37c73b52012-10-26 14:49:25 -070011175 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011176 return;
11177
11178 nla_put_failure:
Ben Greear37c73b52012-10-26 14:49:25 -070011179 spin_unlock_bh(&rdev->beacon_registrations_lock);
11180 if (hdr)
11181 genlmsg_cancel(msg, hdr);
Johannes Berg5e7602302011-11-04 11:18:17 +010011182 nlmsg_free(msg);
11183}
11184EXPORT_SYMBOL(cfg80211_report_obss_beacon);
11185
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011186#ifdef CONFIG_PM
11187void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11188 struct cfg80211_wowlan_wakeup *wakeup,
11189 gfp_t gfp)
11190{
11191 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11192 struct sk_buff *msg;
11193 void *hdr;
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011194 int size = 200;
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011195
11196 trace_cfg80211_report_wowlan_wakeup(wdev->wiphy, wdev, wakeup);
11197
11198 if (wakeup)
11199 size += wakeup->packet_present_len;
11200
11201 msg = nlmsg_new(size, gfp);
11202 if (!msg)
11203 return;
11204
11205 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_WOWLAN);
11206 if (!hdr)
11207 goto free_msg;
11208
11209 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11210 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11211 goto free_msg;
11212
11213 if (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
11214 wdev->netdev->ifindex))
11215 goto free_msg;
11216
11217 if (wakeup) {
11218 struct nlattr *reasons;
11219
11220 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
11221
11222 if (wakeup->disconnect &&
11223 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
11224 goto free_msg;
11225 if (wakeup->magic_pkt &&
11226 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT))
11227 goto free_msg;
11228 if (wakeup->gtk_rekey_failure &&
11229 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE))
11230 goto free_msg;
11231 if (wakeup->eap_identity_req &&
11232 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST))
11233 goto free_msg;
11234 if (wakeup->four_way_handshake &&
11235 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE))
11236 goto free_msg;
11237 if (wakeup->rfkill_release &&
11238 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE))
11239 goto free_msg;
11240
11241 if (wakeup->pattern_idx >= 0 &&
11242 nla_put_u32(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
11243 wakeup->pattern_idx))
11244 goto free_msg;
11245
Johannes Berg2a0e0472013-01-23 22:57:40 +010011246 if (wakeup->tcp_match)
11247 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH);
11248
11249 if (wakeup->tcp_connlost)
11250 nla_put_flag(msg,
11251 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST);
11252
11253 if (wakeup->tcp_nomoretokens)
11254 nla_put_flag(msg,
11255 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS);
11256
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011257 if (wakeup->packet) {
11258 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
11259 u32 len_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211_LEN;
11260
11261 if (!wakeup->packet_80211) {
11262 pkt_attr =
11263 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023;
11264 len_attr =
11265 NL80211_WOWLAN_TRIG_WAKEUP_PKT_8023_LEN;
11266 }
11267
11268 if (wakeup->packet_len &&
11269 nla_put_u32(msg, len_attr, wakeup->packet_len))
11270 goto free_msg;
11271
11272 if (nla_put(msg, pkt_attr, wakeup->packet_present_len,
11273 wakeup->packet))
11274 goto free_msg;
11275 }
11276
11277 nla_nest_end(msg, reasons);
11278 }
11279
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011280 genlmsg_end(msg, hdr);
Johannes Bergcd8f7cb2013-01-22 12:34:29 +010011281
11282 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11283 nl80211_mlme_mcgrp.id, gfp);
11284 return;
11285
11286 free_msg:
11287 nlmsg_free(msg);
11288}
11289EXPORT_SYMBOL(cfg80211_report_wowlan_wakeup);
11290#endif
11291
Jouni Malinen3475b092012-11-16 22:49:57 +020011292void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11293 enum nl80211_tdls_operation oper,
11294 u16 reason_code, gfp_t gfp)
11295{
11296 struct wireless_dev *wdev = dev->ieee80211_ptr;
11297 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
11298 struct sk_buff *msg;
11299 void *hdr;
Jouni Malinen3475b092012-11-16 22:49:57 +020011300
11301 trace_cfg80211_tdls_oper_request(wdev->wiphy, dev, peer, oper,
11302 reason_code);
11303
11304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11305 if (!msg)
11306 return;
11307
11308 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_TDLS_OPER);
11309 if (!hdr) {
11310 nlmsg_free(msg);
11311 return;
11312 }
11313
11314 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11315 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
11316 nla_put_u8(msg, NL80211_ATTR_TDLS_OPERATION, oper) ||
11317 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer) ||
11318 (reason_code > 0 &&
11319 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason_code)))
11320 goto nla_put_failure;
11321
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011322 genlmsg_end(msg, hdr);
Jouni Malinen3475b092012-11-16 22:49:57 +020011323
11324 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11325 nl80211_mlme_mcgrp.id, gfp);
11326 return;
11327
11328 nla_put_failure:
11329 genlmsg_cancel(msg, hdr);
11330 nlmsg_free(msg);
11331}
11332EXPORT_SYMBOL(cfg80211_tdls_oper_request);
11333
Jouni Malinen026331c2010-02-15 12:53:10 +020011334static int nl80211_netlink_notify(struct notifier_block * nb,
11335 unsigned long state,
11336 void *_notify)
11337{
11338 struct netlink_notify *notify = _notify;
11339 struct cfg80211_registered_device *rdev;
11340 struct wireless_dev *wdev;
Ben Greear37c73b52012-10-26 14:49:25 -070011341 struct cfg80211_beacon_registration *reg, *tmp;
Jouni Malinen026331c2010-02-15 12:53:10 +020011342
11343 if (state != NETLINK_URELEASE)
11344 return NOTIFY_DONE;
11345
11346 rcu_read_lock();
11347
Johannes Berg5e7602302011-11-04 11:18:17 +010011348 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +020011349 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +000011350 cfg80211_mlme_unregister_socket(wdev, notify->portid);
Ben Greear37c73b52012-10-26 14:49:25 -070011351
11352 spin_lock_bh(&rdev->beacon_registrations_lock);
11353 list_for_each_entry_safe(reg, tmp, &rdev->beacon_registrations,
11354 list) {
11355 if (reg->nlportid == notify->portid) {
11356 list_del(&reg->list);
11357 kfree(reg);
11358 break;
11359 }
11360 }
11361 spin_unlock_bh(&rdev->beacon_registrations_lock);
Johannes Berg5e7602302011-11-04 11:18:17 +010011362 }
Jouni Malinen026331c2010-02-15 12:53:10 +020011363
11364 rcu_read_unlock();
11365
11366 return NOTIFY_DONE;
11367}
11368
11369static struct notifier_block nl80211_netlink_notifier = {
11370 .notifier_call = nl80211_netlink_notify,
11371};
11372
Jouni Malinen355199e2013-02-27 17:14:27 +020011373void cfg80211_ft_event(struct net_device *netdev,
11374 struct cfg80211_ft_event_params *ft_event)
11375{
11376 struct wiphy *wiphy = netdev->ieee80211_ptr->wiphy;
11377 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
11378 struct sk_buff *msg;
11379 void *hdr;
Jouni Malinen355199e2013-02-27 17:14:27 +020011380
11381 trace_cfg80211_ft_event(wiphy, netdev, ft_event);
11382
11383 if (!ft_event->target_ap)
11384 return;
11385
11386 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
11387 if (!msg)
11388 return;
11389
11390 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
11391 if (!hdr) {
11392 nlmsg_free(msg);
11393 return;
11394 }
11395
11396 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
11397 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
11398 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap);
11399 if (ft_event->ies)
11400 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies);
11401 if (ft_event->ric_ies)
11402 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11403 ft_event->ric_ies);
11404
Johannes Berg9c90a9f2013-06-04 12:46:03 +020011405 genlmsg_end(msg, hdr);
Jouni Malinen355199e2013-02-27 17:14:27 +020011406
11407 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
11408 nl80211_mlme_mcgrp.id, GFP_KERNEL);
11409}
11410EXPORT_SYMBOL(cfg80211_ft_event);
11411
Arend van Spriel5de17982013-04-18 15:49:00 +020011412void cfg80211_crit_proto_stopped(struct wireless_dev *wdev, gfp_t gfp)
11413{
11414 struct cfg80211_registered_device *rdev;
11415 struct sk_buff *msg;
11416 void *hdr;
11417 u32 nlportid;
11418
11419 rdev = wiphy_to_dev(wdev->wiphy);
11420 if (!rdev->crit_proto_nlportid)
11421 return;
11422
11423 nlportid = rdev->crit_proto_nlportid;
11424 rdev->crit_proto_nlportid = 0;
11425
11426 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
11427 if (!msg)
11428 return;
11429
11430 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CRIT_PROTOCOL_STOP);
11431 if (!hdr)
11432 goto nla_put_failure;
11433
11434 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11435 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
11436 goto nla_put_failure;
11437
11438 genlmsg_end(msg, hdr);
11439
11440 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
11441 return;
11442
11443 nla_put_failure:
11444 if (hdr)
11445 genlmsg_cancel(msg, hdr);
11446 nlmsg_free(msg);
11447
11448}
11449EXPORT_SYMBOL(cfg80211_crit_proto_stopped);
11450
Johannes Berg55682962007-09-20 13:09:35 -040011451/* initialisation/exit functions */
11452
11453int nl80211_init(void)
11454{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011455 int err;
Johannes Berg55682962007-09-20 13:09:35 -040011456
Michał Mirosław0d63cbb2009-05-21 10:34:06 +000011457 err = genl_register_family_with_ops(&nl80211_fam,
11458 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -040011459 if (err)
11460 return err;
11461
Johannes Berg55682962007-09-20 13:09:35 -040011462 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
11463 if (err)
11464 goto err_out;
11465
Johannes Berg2a519312009-02-10 21:25:55 +010011466 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
11467 if (err)
11468 goto err_out;
11469
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -040011470 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
11471 if (err)
11472 goto err_out;
11473
Jouni Malinen6039f6d2009-03-19 13:39:21 +020011474 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
11475 if (err)
11476 goto err_out;
11477
Johannes Bergaff89a92009-07-01 21:26:51 +020011478#ifdef CONFIG_NL80211_TESTMODE
11479 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
11480 if (err)
11481 goto err_out;
11482#endif
11483
Jouni Malinen026331c2010-02-15 12:53:10 +020011484 err = netlink_register_notifier(&nl80211_netlink_notifier);
11485 if (err)
11486 goto err_out;
11487
Johannes Berg55682962007-09-20 13:09:35 -040011488 return 0;
11489 err_out:
11490 genl_unregister_family(&nl80211_fam);
11491 return err;
11492}
11493
11494void nl80211_exit(void)
11495{
Jouni Malinen026331c2010-02-15 12:53:10 +020011496 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -040011497 genl_unregister_family(&nl80211_fam);
11498}