blob: 9e5a7206b0b467b872920cacd36083ab2cfbd6a0 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
27 struct genl_info *info,
28 struct cfg80211_crypto_settings *settings,
29 int cipher_limit);
30
Johannes Berg4c476992010-10-04 21:36:35 +020031static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
32 struct genl_info *info);
33static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info);
35
Johannes Berg55682962007-09-20 13:09:35 -040036/* the netlink family */
37static struct genl_family nl80211_fam = {
38 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
39 .name = "nl80211", /* have users key off the name instead */
40 .hdrsize = 0, /* no private header */
41 .version = 1, /* no particular meaning now */
42 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020043 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020044 .pre_doit = nl80211_pre_doit,
45 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040046};
47
Johannes Berg89a54e42012-06-15 14:33:17 +020048/* returns ERR_PTR values */
49static struct wireless_dev *
50__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
Johannes Berg55682962007-09-20 13:09:35 -040051{
Johannes Berg89a54e42012-06-15 14:33:17 +020052 struct cfg80211_registered_device *rdev;
53 struct wireless_dev *result = NULL;
54 bool have_ifidx = attrs[NL80211_ATTR_IFINDEX];
55 bool have_wdev_id = attrs[NL80211_ATTR_WDEV];
56 u64 wdev_id;
57 int wiphy_idx = -1;
58 int ifidx = -1;
Johannes Berg55682962007-09-20 13:09:35 -040059
Johannes Berg89a54e42012-06-15 14:33:17 +020060 assert_cfg80211_lock();
Johannes Berg55682962007-09-20 13:09:35 -040061
Johannes Berg89a54e42012-06-15 14:33:17 +020062 if (!have_ifidx && !have_wdev_id)
63 return ERR_PTR(-EINVAL);
Johannes Berg55682962007-09-20 13:09:35 -040064
Johannes Berg89a54e42012-06-15 14:33:17 +020065 if (have_ifidx)
66 ifidx = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
67 if (have_wdev_id) {
68 wdev_id = nla_get_u64(attrs[NL80211_ATTR_WDEV]);
69 wiphy_idx = wdev_id >> 32;
Johannes Berg55682962007-09-20 13:09:35 -040070 }
71
Johannes Berg89a54e42012-06-15 14:33:17 +020072 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
73 struct wireless_dev *wdev;
74
75 if (wiphy_net(&rdev->wiphy) != netns)
76 continue;
77
78 if (have_wdev_id && rdev->wiphy_idx != wiphy_idx)
79 continue;
80
81 mutex_lock(&rdev->devlist_mtx);
82 list_for_each_entry(wdev, &rdev->wdev_list, list) {
83 if (have_ifidx && wdev->netdev &&
84 wdev->netdev->ifindex == ifidx) {
85 result = wdev;
86 break;
87 }
88 if (have_wdev_id && wdev->identifier == (u32)wdev_id) {
89 result = wdev;
90 break;
91 }
92 }
93 mutex_unlock(&rdev->devlist_mtx);
94
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
110 assert_cfg80211_lock();
111
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 */
129 mutex_lock(&tmp->devlist_mtx);
130 list_for_each_entry(wdev, &tmp->wdev_list, list) {
131 if (wdev->identifier != (u32)wdev_id)
132 continue;
133 found = true;
134 break;
135 }
136 mutex_unlock(&tmp->devlist_mtx);
137
138 if (!found)
139 tmp = NULL;
140
141 if (rdev && tmp != rdev)
142 return ERR_PTR(-EINVAL);
143 rdev = tmp;
144 }
145 }
146
Johannes Berg878d9ec2012-06-15 14:18:32 +0200147 if (attrs[NL80211_ATTR_IFINDEX]) {
148 int ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg4f7eff12012-06-15 14:14:22 +0200149 netdev = dev_get_by_index(netns, ifindex);
Johannes Berg7fee4772012-06-15 14:09:58 +0200150 if (netdev) {
151 if (netdev->ieee80211_ptr)
152 tmp = wiphy_to_dev(
153 netdev->ieee80211_ptr->wiphy);
154 else
155 tmp = NULL;
156
157 dev_put(netdev);
158
159 /* not wireless device -- return error */
160 if (!tmp)
161 return ERR_PTR(-EINVAL);
162
163 /* mismatch -- return error */
164 if (rdev && tmp != rdev)
165 return ERR_PTR(-EINVAL);
166
167 rdev = tmp;
Johannes Berga9455402012-06-15 13:32:49 +0200168 }
Johannes Berga9455402012-06-15 13:32:49 +0200169 }
170
Johannes Berg4f7eff12012-06-15 14:14:22 +0200171 if (!rdev)
172 return ERR_PTR(-ENODEV);
Johannes Berga9455402012-06-15 13:32:49 +0200173
Johannes Berg4f7eff12012-06-15 14:14:22 +0200174 if (netns != wiphy_net(&rdev->wiphy))
175 return ERR_PTR(-ENODEV);
176
177 return rdev;
Johannes Berga9455402012-06-15 13:32:49 +0200178}
179
180/*
181 * This function returns a pointer to the driver
182 * that the genl_info item that is passed refers to.
183 * If successful, it returns non-NULL and also locks
184 * the driver's mutex!
185 *
186 * This means that you need to call cfg80211_unlock_rdev()
187 * before being allowed to acquire &cfg80211_mutex!
188 *
189 * This is necessary because we need to lock the global
190 * mutex to get an item off the list safely, and then
191 * we lock the rdev mutex so it doesn't go away under us.
192 *
193 * We don't want to keep cfg80211_mutex locked
194 * for all the time in order to allow requests on
195 * other interfaces to go through at the same time.
196 *
197 * The result of this can be a PTR_ERR and hence must
198 * be checked with IS_ERR() for errors.
199 */
200static struct cfg80211_registered_device *
Johannes Berg4f7eff12012-06-15 14:14:22 +0200201cfg80211_get_dev_from_info(struct net *netns, struct genl_info *info)
Johannes Berga9455402012-06-15 13:32:49 +0200202{
203 struct cfg80211_registered_device *rdev;
204
205 mutex_lock(&cfg80211_mutex);
Johannes Berg878d9ec2012-06-15 14:18:32 +0200206 rdev = __cfg80211_rdev_from_attrs(netns, info->attrs);
Johannes Berga9455402012-06-15 13:32:49 +0200207
208 /* if it is not an error we grab the lock on
209 * it to assure it won't be going away while
210 * we operate on it */
211 if (!IS_ERR(rdev))
212 mutex_lock(&rdev->mtx);
213
214 mutex_unlock(&cfg80211_mutex);
215
216 return rdev;
217}
218
Johannes Berg55682962007-09-20 13:09:35 -0400219/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000220static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -0400221 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
222 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -0700223 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +0200224 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +0200225 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +0530226 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200227 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
228 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
229 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
230 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +0100231 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400232
233 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
234 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
235 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +0100236
Eliad Pellere007b852011-11-24 18:13:56 +0200237 [NL80211_ATTR_MAC] = { .len = ETH_ALEN },
238 [NL80211_ATTR_PREV_BSSID] = { .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +0100239
Johannes Bergb9454e82009-07-08 13:29:08 +0200240 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +0100241 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
242 .len = WLAN_MAX_KEY_LEN },
243 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
244 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
245 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen81962262011-11-02 23:36:31 +0200246 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Berge31b8212010-10-05 19:39:30 +0200247 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100248
249 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
250 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
251 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
252 .len = IEEE80211_MAX_DATA_LEN },
253 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
254 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100255 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
256 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
257 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
258 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
259 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100260 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100261 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200262 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100263 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +0800264 .len = IEEE80211_MAX_MESH_ID_LEN },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100265 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300266
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700267 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
268 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
269
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300270 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
271 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
272 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200273 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
274 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100275 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300276
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800277 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700278 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700279
Johannes Berg6c739412011-11-03 09:27:01 +0100280 [NL80211_ATTR_HT_CAPABILITY] = { .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200281
282 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
283 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
284 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100285 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
286 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200287
288 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
289 .len = IEEE80211_MAX_SSID_LEN },
290 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
291 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200292 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300293 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300294 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300295 [NL80211_ATTR_STA_FLAGS2] = {
296 .len = sizeof(struct nl80211_sta_flag_update),
297 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300298 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300299 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
300 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200301 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
302 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
303 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200304 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100305 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100306 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
307 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100308 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
309 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200310 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200311 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
312 .len = IEEE80211_MAX_DATA_LEN },
313 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200314 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200315 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300316 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200317 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300318 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
319 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200320 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900321 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
322 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100323 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100324 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100325 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200326 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700327 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300328 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200329 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200330 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300331 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300332 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
333 .len = IEEE80211_MAX_DATA_LEN },
334 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
335 .len = IEEE80211_MAX_DATA_LEN },
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530336 [NL80211_ATTR_ROAM_SUPPORT] = { .type = NLA_FLAG },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300337 [NL80211_ATTR_SCHED_SCAN_MATCH] = { .type = NLA_NESTED },
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +0530338 [NL80211_ATTR_TX_NO_CCK_RATE] = { .type = NLA_FLAG },
Arik Nemtsov109086c2011-09-28 14:12:50 +0300339 [NL80211_ATTR_TDLS_ACTION] = { .type = NLA_U8 },
340 [NL80211_ATTR_TDLS_DIALOG_TOKEN] = { .type = NLA_U8 },
341 [NL80211_ATTR_TDLS_OPERATION] = { .type = NLA_U8 },
342 [NL80211_ATTR_TDLS_SUPPORT] = { .type = NLA_FLAG },
343 [NL80211_ATTR_TDLS_EXTERNAL_SETUP] = { .type = NLA_FLAG },
Johannes Berge247bd902011-11-04 11:18:21 +0100344 [NL80211_ATTR_DONT_WAIT_FOR_ACK] = { .type = NLA_FLAG },
Arik Nemtsov00f740e2011-11-10 11:28:56 +0200345 [NL80211_ATTR_PROBE_RESP] = { .type = NLA_BINARY,
346 .len = IEEE80211_MAX_DATA_LEN },
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -0700347 [NL80211_ATTR_DFS_REGION] = { .type = NLA_U8 },
Ben Greear7e7c8922011-11-18 11:31:59 -0800348 [NL80211_ATTR_DISABLE_HT] = { .type = NLA_FLAG },
349 [NL80211_ATTR_HT_CAPABILITY_MASK] = {
350 .len = NL80211_HT_CAPABILITY_LEN
351 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +0100352 [NL80211_ATTR_NOACK_MAP] = { .type = NLA_U16 },
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +0530353 [NL80211_ATTR_INACTIVITY_TIMEOUT] = { .type = NLA_U16 },
Bala Shanmugam4486ea92012-03-07 17:27:12 +0530354 [NL80211_ATTR_BG_SCAN_PERIOD] = { .type = NLA_U16 },
Johannes Berg89a54e42012-06-15 14:33:17 +0200355 [NL80211_ATTR_WDEV] = { .type = NLA_U64 },
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -0700356 [NL80211_ATTR_USER_REG_HINT_TYPE] = { .type = NLA_U32 },
Jouni Malinene39e5b52012-09-30 19:29:39 +0300357 [NL80211_ATTR_SAE_DATA] = { .type = NLA_BINARY, },
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +0000358 [NL80211_ATTR_VHT_CAPABILITY] = { .len = NL80211_VHT_CAPABILITY_LEN },
Sam Lefflered4737712012-10-11 21:03:31 -0700359 [NL80211_ATTR_SCAN_FLAGS] = { .type = NLA_U32 },
Johannes Berg55682962007-09-20 13:09:35 -0400360};
361
Johannes Berge31b8212010-10-05 19:39:30 +0200362/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000363static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200364 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200365 [NL80211_KEY_IDX] = { .type = NLA_U8 },
366 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
Jouni Malinen81962262011-11-02 23:36:31 +0200367 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 16 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200368 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
369 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200370 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100371 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
372};
373
374/* policy for the key default flags */
375static const struct nla_policy
376nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
377 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
378 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200379};
380
Johannes Bergff1b6e62011-05-04 15:37:28 +0200381/* policy for WoWLAN attributes */
382static const struct nla_policy
383nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
384 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
385 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
386 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
387 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200388 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
389 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
390 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
391 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200392};
393
Johannes Berge5497d72011-07-05 16:35:40 +0200394/* policy for GTK rekey offload attributes */
395static const struct nla_policy
396nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
397 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
398 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
399 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
400};
401
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300402static const struct nla_policy
403nl80211_match_policy[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1] = {
Johannes Berg4a4ab0d2012-06-13 11:17:11 +0200404 [NL80211_SCHED_SCAN_MATCH_ATTR_SSID] = { .type = NLA_BINARY,
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300405 .len = IEEE80211_MAX_SSID_LEN },
Thomas Pedersen88e920b2012-06-21 11:09:54 -0700406 [NL80211_SCHED_SCAN_MATCH_ATTR_RSSI] = { .type = NLA_U32 },
Luciano Coelhoa1f1c212011-08-31 16:01:48 +0300407};
408
Holger Schuriga0438972009-11-11 11:30:02 +0100409/* ifidx get helper */
410static int nl80211_get_ifidx(struct netlink_callback *cb)
411{
412 int res;
413
414 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
415 nl80211_fam.attrbuf, nl80211_fam.maxattr,
416 nl80211_policy);
417 if (res)
418 return res;
419
420 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
421 return -EINVAL;
422
423 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
424 if (!res)
425 return -EINVAL;
426 return res;
427}
428
Johannes Berg67748892010-10-04 21:14:06 +0200429static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
430 struct netlink_callback *cb,
431 struct cfg80211_registered_device **rdev,
432 struct net_device **dev)
433{
434 int ifidx = cb->args[0];
435 int err;
436
437 if (!ifidx)
438 ifidx = nl80211_get_ifidx(cb);
439 if (ifidx < 0)
440 return ifidx;
441
442 cb->args[0] = ifidx;
443
444 rtnl_lock();
445
446 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
447 if (!*dev) {
448 err = -ENODEV;
449 goto out_rtnl;
450 }
451
452 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100453 if (IS_ERR(*rdev)) {
454 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200455 goto out_rtnl;
456 }
457
458 return 0;
459 out_rtnl:
460 rtnl_unlock();
461 return err;
462}
463
464static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
465{
466 cfg80211_unlock_rdev(rdev);
467 rtnl_unlock();
468}
469
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100470/* IE validation */
471static bool is_valid_ie_attr(const struct nlattr *attr)
472{
473 const u8 *pos;
474 int len;
475
476 if (!attr)
477 return true;
478
479 pos = nla_data(attr);
480 len = nla_len(attr);
481
482 while (len) {
483 u8 elemlen;
484
485 if (len < 2)
486 return false;
487 len -= 2;
488
489 elemlen = pos[1];
490 if (elemlen > len)
491 return false;
492
493 len -= elemlen;
494 pos += 2 + elemlen;
495 }
496
497 return true;
498}
499
Johannes Berg55682962007-09-20 13:09:35 -0400500/* message building helper */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000501static inline void *nl80211hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
Johannes Berg55682962007-09-20 13:09:35 -0400502 int flags, u8 cmd)
503{
504 /* since there is no private header just add the generic one */
Eric W. Biederman15e47302012-09-07 20:12:54 +0000505 return genlmsg_put(skb, portid, seq, &nl80211_fam, flags, cmd);
Johannes Berg55682962007-09-20 13:09:35 -0400506}
507
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400508static int nl80211_msg_put_channel(struct sk_buff *msg,
509 struct ieee80211_channel *chan)
510{
David S. Miller9360ffd2012-03-29 04:41:26 -0400511 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_FREQ,
512 chan->center_freq))
513 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400514
David S. Miller9360ffd2012-03-29 04:41:26 -0400515 if ((chan->flags & IEEE80211_CHAN_DISABLED) &&
516 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_DISABLED))
517 goto nla_put_failure;
518 if ((chan->flags & IEEE80211_CHAN_PASSIVE_SCAN) &&
519 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN))
520 goto nla_put_failure;
521 if ((chan->flags & IEEE80211_CHAN_NO_IBSS) &&
522 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_NO_IBSS))
523 goto nla_put_failure;
524 if ((chan->flags & IEEE80211_CHAN_RADAR) &&
525 nla_put_flag(msg, NL80211_FREQUENCY_ATTR_RADAR))
526 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400527
David S. Miller9360ffd2012-03-29 04:41:26 -0400528 if (nla_put_u32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
529 DBM_TO_MBM(chan->max_power)))
530 goto nla_put_failure;
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400531
532 return 0;
533
534 nla_put_failure:
535 return -ENOBUFS;
536}
537
Johannes Berg55682962007-09-20 13:09:35 -0400538/* netlink command implementations */
539
Johannes Bergb9454e82009-07-08 13:29:08 +0200540struct key_parse {
541 struct key_params p;
542 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200543 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200544 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100545 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200546};
547
548static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
549{
550 struct nlattr *tb[NL80211_KEY_MAX + 1];
551 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
552 nl80211_key_policy);
553 if (err)
554 return err;
555
556 k->def = !!tb[NL80211_KEY_DEFAULT];
557 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
558
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100559 if (k->def) {
560 k->def_uni = true;
561 k->def_multi = true;
562 }
563 if (k->defmgmt)
564 k->def_multi = true;
565
Johannes Bergb9454e82009-07-08 13:29:08 +0200566 if (tb[NL80211_KEY_IDX])
567 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
568
569 if (tb[NL80211_KEY_DATA]) {
570 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
571 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
572 }
573
574 if (tb[NL80211_KEY_SEQ]) {
575 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
576 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
577 }
578
579 if (tb[NL80211_KEY_CIPHER])
580 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
581
Johannes Berge31b8212010-10-05 19:39:30 +0200582 if (tb[NL80211_KEY_TYPE]) {
583 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
584 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
585 return -EINVAL;
586 }
587
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100588 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
589 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
Johannes Berg2da8f412012-01-20 13:52:37 +0100590 err = nla_parse_nested(kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
591 tb[NL80211_KEY_DEFAULT_TYPES],
592 nl80211_key_default_policy);
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100593 if (err)
594 return err;
595
596 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
597 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
598 }
599
Johannes Bergb9454e82009-07-08 13:29:08 +0200600 return 0;
601}
602
603static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
604{
605 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
606 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
607 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
608 }
609
610 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
611 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
612 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
613 }
614
615 if (info->attrs[NL80211_ATTR_KEY_IDX])
616 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
617
618 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
619 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
620
621 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
622 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
623
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100624 if (k->def) {
625 k->def_uni = true;
626 k->def_multi = true;
627 }
628 if (k->defmgmt)
629 k->def_multi = true;
630
Johannes Berge31b8212010-10-05 19:39:30 +0200631 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
632 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
633 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
634 return -EINVAL;
635 }
636
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100637 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
638 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
639 int err = nla_parse_nested(
640 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
641 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
642 nl80211_key_default_policy);
643 if (err)
644 return err;
645
646 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
647 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
648 }
649
Johannes Bergb9454e82009-07-08 13:29:08 +0200650 return 0;
651}
652
653static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
654{
655 int err;
656
657 memset(k, 0, sizeof(*k));
658 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200659 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200660
661 if (info->attrs[NL80211_ATTR_KEY])
662 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
663 else
664 err = nl80211_parse_key_old(info, k);
665
666 if (err)
667 return err;
668
669 if (k->def && k->defmgmt)
670 return -EINVAL;
671
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100672 if (k->defmgmt) {
673 if (k->def_uni || !k->def_multi)
674 return -EINVAL;
675 }
676
Johannes Bergb9454e82009-07-08 13:29:08 +0200677 if (k->idx != -1) {
678 if (k->defmgmt) {
679 if (k->idx < 4 || k->idx > 5)
680 return -EINVAL;
681 } else if (k->def) {
682 if (k->idx < 0 || k->idx > 3)
683 return -EINVAL;
684 } else {
685 if (k->idx < 0 || k->idx > 5)
686 return -EINVAL;
687 }
688 }
689
690 return 0;
691}
692
Johannes Bergfffd0932009-07-08 14:22:54 +0200693static struct cfg80211_cached_keys *
694nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
695 struct nlattr *keys)
696{
697 struct key_parse parse;
698 struct nlattr *key;
699 struct cfg80211_cached_keys *result;
700 int rem, err, def = 0;
701
702 result = kzalloc(sizeof(*result), GFP_KERNEL);
703 if (!result)
704 return ERR_PTR(-ENOMEM);
705
706 result->def = -1;
707 result->defmgmt = -1;
708
709 nla_for_each_nested(key, keys, rem) {
710 memset(&parse, 0, sizeof(parse));
711 parse.idx = -1;
712
713 err = nl80211_parse_key_new(key, &parse);
714 if (err)
715 goto error;
716 err = -EINVAL;
717 if (!parse.p.key)
718 goto error;
719 if (parse.idx < 0 || parse.idx > 4)
720 goto error;
721 if (parse.def) {
722 if (def)
723 goto error;
724 def = 1;
725 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100726 if (!parse.def_uni || !parse.def_multi)
727 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200728 } else if (parse.defmgmt)
729 goto error;
730 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200731 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200732 if (err)
733 goto error;
734 result->params[parse.idx].cipher = parse.p.cipher;
735 result->params[parse.idx].key_len = parse.p.key_len;
736 result->params[parse.idx].key = result->data[parse.idx];
737 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
738 }
739
740 return result;
741 error:
742 kfree(result);
743 return ERR_PTR(err);
744}
745
746static int nl80211_key_allowed(struct wireless_dev *wdev)
747{
748 ASSERT_WDEV_LOCK(wdev);
749
Johannes Bergfffd0932009-07-08 14:22:54 +0200750 switch (wdev->iftype) {
751 case NL80211_IFTYPE_AP:
752 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200753 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700754 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200755 break;
756 case NL80211_IFTYPE_ADHOC:
757 if (!wdev->current_bss)
758 return -ENOLINK;
759 break;
760 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200761 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200762 if (wdev->sme_state != CFG80211_SME_CONNECTED)
763 return -ENOLINK;
764 break;
765 default:
766 return -EINVAL;
767 }
768
769 return 0;
770}
771
Johannes Berg7527a782011-05-13 10:58:57 +0200772static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
773{
774 struct nlattr *nl_modes = nla_nest_start(msg, attr);
775 int i;
776
777 if (!nl_modes)
778 goto nla_put_failure;
779
780 i = 0;
781 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400782 if ((ifmodes & 1) && nla_put_flag(msg, i))
783 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200784 ifmodes >>= 1;
785 i++;
786 }
787
788 nla_nest_end(msg, nl_modes);
789 return 0;
790
791nla_put_failure:
792 return -ENOBUFS;
793}
794
795static int nl80211_put_iface_combinations(struct wiphy *wiphy,
796 struct sk_buff *msg)
797{
798 struct nlattr *nl_combis;
799 int i, j;
800
801 nl_combis = nla_nest_start(msg,
802 NL80211_ATTR_INTERFACE_COMBINATIONS);
803 if (!nl_combis)
804 goto nla_put_failure;
805
806 for (i = 0; i < wiphy->n_iface_combinations; i++) {
807 const struct ieee80211_iface_combination *c;
808 struct nlattr *nl_combi, *nl_limits;
809
810 c = &wiphy->iface_combinations[i];
811
812 nl_combi = nla_nest_start(msg, i + 1);
813 if (!nl_combi)
814 goto nla_put_failure;
815
816 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
817 if (!nl_limits)
818 goto nla_put_failure;
819
820 for (j = 0; j < c->n_limits; j++) {
821 struct nlattr *nl_limit;
822
823 nl_limit = nla_nest_start(msg, j + 1);
824 if (!nl_limit)
825 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400826 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
827 c->limits[j].max))
828 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200829 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
830 c->limits[j].types))
831 goto nla_put_failure;
832 nla_nest_end(msg, nl_limit);
833 }
834
835 nla_nest_end(msg, nl_limits);
836
David S. Miller9360ffd2012-03-29 04:41:26 -0400837 if (c->beacon_int_infra_match &&
838 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
839 goto nla_put_failure;
840 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
841 c->num_different_channels) ||
842 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
843 c->max_interfaces))
844 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200845
846 nla_nest_end(msg, nl_combi);
847 }
848
849 nla_nest_end(msg, nl_combis);
850
851 return 0;
852nla_put_failure:
853 return -ENOBUFS;
854}
855
Eric W. Biederman15e47302012-09-07 20:12:54 +0000856static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400857 struct cfg80211_registered_device *dev)
858{
859 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100860 struct nlattr *nl_bands, *nl_band;
861 struct nlattr *nl_freqs, *nl_freq;
862 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100863 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100864 enum ieee80211_band band;
865 struct ieee80211_channel *chan;
866 struct ieee80211_rate *rate;
867 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200868 const struct ieee80211_txrx_stypes *mgmt_stypes =
869 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400870
Eric W. Biederman15e47302012-09-07 20:12:54 +0000871 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400872 if (!hdr)
873 return -1;
874
David S. Miller9360ffd2012-03-29 04:41:26 -0400875 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
876 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
877 nla_put_u32(msg, NL80211_ATTR_GENERATION,
878 cfg80211_rdev_list_generation) ||
879 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
880 dev->wiphy.retry_short) ||
881 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
882 dev->wiphy.retry_long) ||
883 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
884 dev->wiphy.frag_threshold) ||
885 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
886 dev->wiphy.rts_threshold) ||
887 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
888 dev->wiphy.coverage_class) ||
889 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
890 dev->wiphy.max_scan_ssids) ||
891 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
892 dev->wiphy.max_sched_scan_ssids) ||
893 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
894 dev->wiphy.max_scan_ie_len) ||
895 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
896 dev->wiphy.max_sched_scan_ie_len) ||
897 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
898 dev->wiphy.max_match_sets))
899 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200900
David S. Miller9360ffd2012-03-29 04:41:26 -0400901 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
902 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
903 goto nla_put_failure;
904 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
905 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
906 goto nla_put_failure;
907 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
908 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
909 goto nla_put_failure;
910 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
911 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
912 goto nla_put_failure;
913 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
914 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
915 goto nla_put_failure;
916 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
917 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
918 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200919
David S. Miller9360ffd2012-03-29 04:41:26 -0400920 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
921 sizeof(u32) * dev->wiphy.n_cipher_suites,
922 dev->wiphy.cipher_suites))
923 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100924
David S. Miller9360ffd2012-03-29 04:41:26 -0400925 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
926 dev->wiphy.max_num_pmkids))
927 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530928
David S. Miller9360ffd2012-03-29 04:41:26 -0400929 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
930 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
931 goto nla_put_failure;
Johannes Berg25e47c182009-04-02 20:14:06 +0200932
David S. Miller9360ffd2012-03-29 04:41:26 -0400933 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
934 dev->wiphy.available_antennas_tx) ||
935 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
936 dev->wiphy.available_antennas_rx))
937 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100938
David S. Miller9360ffd2012-03-29 04:41:26 -0400939 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
940 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
941 dev->wiphy.probe_resp_offload))
942 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200943
Bruno Randolf7f531e02010-12-16 11:30:22 +0900944 if ((dev->wiphy.available_antennas_tx ||
945 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900946 u32 tx_ant = 0, rx_ant = 0;
947 int res;
948 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
949 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400950 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
951 tx_ant) ||
952 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
953 rx_ant))
954 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900955 }
956 }
957
Johannes Berg7527a782011-05-13 10:58:57 +0200958 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
959 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700960 goto nla_put_failure;
961
Johannes Bergee688b002008-01-24 19:38:39 +0100962 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
963 if (!nl_bands)
964 goto nla_put_failure;
965
966 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
967 if (!dev->wiphy.bands[band])
968 continue;
969
970 nl_band = nla_nest_start(msg, band);
971 if (!nl_band)
972 goto nla_put_failure;
973
Johannes Bergd51626d2008-10-09 12:20:13 +0200974 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400975 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
976 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
977 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
978 &dev->wiphy.bands[band]->ht_cap.mcs) ||
979 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
980 dev->wiphy.bands[band]->ht_cap.cap) ||
981 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
982 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
983 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
984 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
985 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200986
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000987 /* add VHT info */
988 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
989 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
990 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
991 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
992 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
993 dev->wiphy.bands[band]->vht_cap.cap)))
994 goto nla_put_failure;
995
Johannes Bergee688b002008-01-24 19:38:39 +0100996 /* add frequencies */
997 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
998 if (!nl_freqs)
999 goto nla_put_failure;
1000
1001 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1002 nl_freq = nla_nest_start(msg, i);
1003 if (!nl_freq)
1004 goto nla_put_failure;
1005
1006 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001007
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001008 if (nl80211_msg_put_channel(msg, chan))
1009 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001010
Johannes Bergee688b002008-01-24 19:38:39 +01001011 nla_nest_end(msg, nl_freq);
1012 }
1013
1014 nla_nest_end(msg, nl_freqs);
1015
1016 /* add bitrates */
1017 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1018 if (!nl_rates)
1019 goto nla_put_failure;
1020
1021 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1022 nl_rate = nla_nest_start(msg, i);
1023 if (!nl_rate)
1024 goto nla_put_failure;
1025
1026 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001027 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1028 rate->bitrate))
1029 goto nla_put_failure;
1030 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1031 nla_put_flag(msg,
1032 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1033 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001034
1035 nla_nest_end(msg, nl_rate);
1036 }
1037
1038 nla_nest_end(msg, nl_rates);
1039
1040 nla_nest_end(msg, nl_band);
1041 }
1042 nla_nest_end(msg, nl_bands);
1043
Johannes Berg8fdc6212009-03-14 09:34:01 +01001044 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1045 if (!nl_cmds)
1046 goto nla_put_failure;
1047
1048 i = 0;
1049#define CMD(op, n) \
1050 do { \
1051 if (dev->ops->op) { \
1052 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001053 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1054 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001055 } \
1056 } while (0)
1057
1058 CMD(add_virtual_intf, NEW_INTERFACE);
1059 CMD(change_virtual_intf, SET_INTERFACE);
1060 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001061 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001062 CMD(add_station, NEW_STATION);
1063 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001064 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001065 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001066 CMD(auth, AUTHENTICATE);
1067 CMD(assoc, ASSOCIATE);
1068 CMD(deauth, DEAUTHENTICATE);
1069 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001070 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001071 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001072 CMD(set_pmksa, SET_PMKSA);
1073 CMD(del_pmksa, DEL_PMKSA);
1074 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001075 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1076 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001077 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001078 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001079 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001080 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001081 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001082 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1083 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001084 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001085 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001086 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001087 i++;
1088 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1089 goto nla_put_failure;
1090 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001091 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001092 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1093 CMD(tdls_mgmt, TDLS_MGMT);
1094 CMD(tdls_oper, TDLS_OPER);
1095 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001096 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1097 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001098 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001099 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001100 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1101 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001102 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1103 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001104 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001105 CMD(start_p2p_device, START_P2P_DEVICE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001106
Kalle Valo4745fc02011-11-17 19:06:10 +02001107#ifdef CONFIG_NL80211_TESTMODE
1108 CMD(testmode_cmd, TESTMODE);
1109#endif
1110
Johannes Berg8fdc6212009-03-14 09:34:01 +01001111#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001112
Johannes Berg6829c8782009-07-02 09:13:27 +02001113 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001114 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001115 if (nla_put_u32(msg, i, NL80211_CMD_CONNECT))
1116 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001117 }
1118
Johannes Berg6829c8782009-07-02 09:13:27 +02001119 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001120 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001121 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1122 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001123 }
1124
Johannes Berg8fdc6212009-03-14 09:34:01 +01001125 nla_nest_end(msg, nl_cmds);
1126
Johannes Berg7c4ef712011-11-18 15:33:48 +01001127 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001128 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1129 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1130 dev->wiphy.max_remain_on_channel_duration))
1131 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001132
David S. Miller9360ffd2012-03-29 04:41:26 -04001133 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1134 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1135 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001136
Johannes Berg2e161f72010-08-12 15:38:38 +02001137 if (mgmt_stypes) {
1138 u16 stypes;
1139 struct nlattr *nl_ftypes, *nl_ifs;
1140 enum nl80211_iftype ift;
1141
1142 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1143 if (!nl_ifs)
1144 goto nla_put_failure;
1145
1146 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1147 nl_ftypes = nla_nest_start(msg, ift);
1148 if (!nl_ftypes)
1149 goto nla_put_failure;
1150 i = 0;
1151 stypes = mgmt_stypes[ift].tx;
1152 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001153 if ((stypes & 1) &&
1154 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1155 (i << 4) | IEEE80211_FTYPE_MGMT))
1156 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001157 stypes >>= 1;
1158 i++;
1159 }
1160 nla_nest_end(msg, nl_ftypes);
1161 }
1162
Johannes Berg74b70a42010-08-24 12:15:53 +02001163 nla_nest_end(msg, nl_ifs);
1164
Johannes Berg2e161f72010-08-12 15:38:38 +02001165 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1166 if (!nl_ifs)
1167 goto nla_put_failure;
1168
1169 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1170 nl_ftypes = nla_nest_start(msg, ift);
1171 if (!nl_ftypes)
1172 goto nla_put_failure;
1173 i = 0;
1174 stypes = mgmt_stypes[ift].rx;
1175 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001176 if ((stypes & 1) &&
1177 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1178 (i << 4) | IEEE80211_FTYPE_MGMT))
1179 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001180 stypes >>= 1;
1181 i++;
1182 }
1183 nla_nest_end(msg, nl_ftypes);
1184 }
1185 nla_nest_end(msg, nl_ifs);
1186 }
1187
Johannes Bergdfb89c52012-06-27 09:23:48 +02001188#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001189 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1190 struct nlattr *nl_wowlan;
1191
1192 nl_wowlan = nla_nest_start(msg,
1193 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1194 if (!nl_wowlan)
1195 goto nla_put_failure;
1196
David S. Miller9360ffd2012-03-29 04:41:26 -04001197 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1198 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1199 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1200 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1201 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1202 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1203 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1204 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1205 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1206 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1207 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1208 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1209 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1210 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1211 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1212 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1213 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001214 if (dev->wiphy.wowlan.n_patterns) {
1215 struct nl80211_wowlan_pattern_support pat = {
1216 .max_patterns = dev->wiphy.wowlan.n_patterns,
1217 .min_pattern_len =
1218 dev->wiphy.wowlan.pattern_min_len,
1219 .max_pattern_len =
1220 dev->wiphy.wowlan.pattern_max_len,
1221 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001222 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1223 sizeof(pat), &pat))
1224 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001225 }
1226
1227 nla_nest_end(msg, nl_wowlan);
1228 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001229#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001230
Johannes Berg7527a782011-05-13 10:58:57 +02001231 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1232 dev->wiphy.software_iftypes))
1233 goto nla_put_failure;
1234
1235 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1236 goto nla_put_failure;
1237
David S. Miller9360ffd2012-03-29 04:41:26 -04001238 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1239 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1240 dev->wiphy.ap_sme_capa))
1241 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001242
David S. Miller9360ffd2012-03-29 04:41:26 -04001243 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1244 dev->wiphy.features))
1245 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001246
David S. Miller9360ffd2012-03-29 04:41:26 -04001247 if (dev->wiphy.ht_capa_mod_mask &&
1248 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1249 sizeof(*dev->wiphy.ht_capa_mod_mask),
1250 dev->wiphy.ht_capa_mod_mask))
1251 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001252
Johannes Berg55682962007-09-20 13:09:35 -04001253 return genlmsg_end(msg, hdr);
1254
1255 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001256 genlmsg_cancel(msg, hdr);
1257 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001258}
1259
1260static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1261{
1262 int idx = 0;
1263 int start = cb->args[0];
1264 struct cfg80211_registered_device *dev;
1265
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001266 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001267 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001268 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1269 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001270 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001271 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001272 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001273 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001274 dev) < 0) {
1275 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001276 break;
Julius Volzb4637272008-07-08 14:02:19 +02001277 }
Johannes Berg55682962007-09-20 13:09:35 -04001278 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001279 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001280
1281 cb->args[0] = idx;
1282
1283 return skb->len;
1284}
1285
1286static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1287{
1288 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001289 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001290
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001291 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001292 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001293 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001294
Eric W. Biederman15e47302012-09-07 20:12:54 +00001295 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001296 nlmsg_free(msg);
1297 return -ENOBUFS;
1298 }
Johannes Berg55682962007-09-20 13:09:35 -04001299
Johannes Berg134e6372009-07-10 09:51:34 +00001300 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001301}
1302
Jouni Malinen31888482008-10-30 16:59:24 +02001303static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1304 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1305 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1306 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1307 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1308 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1309};
1310
1311static int parse_txq_params(struct nlattr *tb[],
1312 struct ieee80211_txq_params *txq_params)
1313{
Johannes Berga3304b02012-03-28 11:04:24 +02001314 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001315 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1316 !tb[NL80211_TXQ_ATTR_AIFS])
1317 return -EINVAL;
1318
Johannes Berga3304b02012-03-28 11:04:24 +02001319 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001320 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1321 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1322 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1323 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1324
Johannes Berga3304b02012-03-28 11:04:24 +02001325 if (txq_params->ac >= NL80211_NUM_ACS)
1326 return -EINVAL;
1327
Jouni Malinen31888482008-10-30 16:59:24 +02001328 return 0;
1329}
1330
Johannes Bergf444de02010-05-05 15:25:02 +02001331static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1332{
1333 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001334 * You can only set the channel explicitly for WDS interfaces,
1335 * all others have their channel managed via their respective
1336 * "establish a connection" command (connect, join, ...)
1337 *
1338 * For AP/GO and mesh mode, the channel can be set with the
1339 * channel userspace API, but is only stored and passed to the
1340 * low-level driver when the AP starts or the mesh is joined.
1341 * This is for backward compatibility, userspace can also give
1342 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001343 *
1344 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001345 * whatever else is going on, so they have their own special
1346 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001347 */
1348 return !wdev ||
1349 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001350 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001351 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1352 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001353}
1354
Johannes Bergcd6c6592012-05-10 21:27:18 +02001355static bool nl80211_valid_channel_type(struct genl_info *info,
1356 enum nl80211_channel_type *channel_type)
1357{
1358 enum nl80211_channel_type tmp;
1359
1360 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1361 return false;
1362
1363 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1364 if (tmp != NL80211_CHAN_NO_HT &&
1365 tmp != NL80211_CHAN_HT20 &&
1366 tmp != NL80211_CHAN_HT40PLUS &&
1367 tmp != NL80211_CHAN_HT40MINUS)
1368 return false;
1369
1370 if (channel_type)
1371 *channel_type = tmp;
1372
1373 return true;
1374}
1375
Johannes Bergf444de02010-05-05 15:25:02 +02001376static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1377 struct wireless_dev *wdev,
1378 struct genl_info *info)
1379{
Johannes Bergaa430da2012-05-16 23:50:18 +02001380 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001381 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1382 u32 freq;
1383 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001384 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1385
1386 if (wdev)
1387 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001388
1389 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1390 return -EINVAL;
1391
1392 if (!nl80211_can_set_dev_channel(wdev))
1393 return -EOPNOTSUPP;
1394
Johannes Bergcd6c6592012-05-10 21:27:18 +02001395 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1396 !nl80211_valid_channel_type(info, &channel_type))
1397 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001398
1399 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1400
1401 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001402 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001403 case NL80211_IFTYPE_AP:
1404 case NL80211_IFTYPE_P2P_GO:
1405 if (wdev->beacon_interval) {
1406 result = -EBUSY;
1407 break;
1408 }
1409 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1410 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1411 channel,
1412 channel_type)) {
1413 result = -EINVAL;
1414 break;
1415 }
1416 wdev->preset_chan = channel;
1417 wdev->preset_chantype = channel_type;
1418 result = 0;
1419 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001420 case NL80211_IFTYPE_MESH_POINT:
1421 result = cfg80211_set_mesh_freq(rdev, wdev, freq, channel_type);
1422 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001423 case NL80211_IFTYPE_MONITOR:
1424 result = cfg80211_set_monitor_channel(rdev, freq, channel_type);
1425 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001426 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001427 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001428 }
1429 mutex_unlock(&rdev->devlist_mtx);
1430
1431 return result;
1432}
1433
1434static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1435{
Johannes Berg4c476992010-10-04 21:36:35 +02001436 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1437 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001438
Johannes Berg4c476992010-10-04 21:36:35 +02001439 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001440}
1441
Bill Jordane8347eb2010-10-01 13:54:28 -04001442static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1443{
Johannes Berg43b19952010-10-07 13:10:30 +02001444 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1445 struct net_device *dev = info->user_ptr[1];
1446 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001447 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001448
1449 if (!info->attrs[NL80211_ATTR_MAC])
1450 return -EINVAL;
1451
Johannes Berg43b19952010-10-07 13:10:30 +02001452 if (netif_running(dev))
1453 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001454
Johannes Berg43b19952010-10-07 13:10:30 +02001455 if (!rdev->ops->set_wds_peer)
1456 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001457
Johannes Berg43b19952010-10-07 13:10:30 +02001458 if (wdev->iftype != NL80211_IFTYPE_WDS)
1459 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001460
1461 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001462 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001463}
1464
1465
Johannes Berg55682962007-09-20 13:09:35 -04001466static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1467{
1468 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001469 struct net_device *netdev = NULL;
1470 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001471 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001472 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001473 u32 changed;
1474 u8 retry_short = 0, retry_long = 0;
1475 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001476 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001477
Johannes Bergf444de02010-05-05 15:25:02 +02001478 /*
1479 * Try to find the wiphy and netdev. Normally this
1480 * function shouldn't need the netdev, but this is
1481 * done for backward compatibility -- previously
1482 * setting the channel was done per wiphy, but now
1483 * it is per netdev. Previous userland like hostapd
1484 * also passed a netdev to set_wiphy, so that it is
1485 * possible to let that go to the right netdev!
1486 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001487 mutex_lock(&cfg80211_mutex);
1488
Johannes Bergf444de02010-05-05 15:25:02 +02001489 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1490 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1491
1492 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1493 if (netdev && netdev->ieee80211_ptr) {
1494 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1495 mutex_lock(&rdev->mtx);
1496 } else
1497 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001498 }
1499
Johannes Bergf444de02010-05-05 15:25:02 +02001500 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001501 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1502 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001503 if (IS_ERR(rdev)) {
1504 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001505 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001506 }
1507 wdev = NULL;
1508 netdev = NULL;
1509 result = 0;
1510
1511 mutex_lock(&rdev->mtx);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001512 } else if (nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
Johannes Bergf444de02010-05-05 15:25:02 +02001513 wdev = netdev->ieee80211_ptr;
1514 else
1515 wdev = NULL;
1516
1517 /*
1518 * end workaround code, by now the rdev is available
1519 * and locked, and wdev may or may not be NULL.
1520 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001521
1522 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001523 result = cfg80211_dev_rename(
1524 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001525
1526 mutex_unlock(&cfg80211_mutex);
1527
1528 if (result)
1529 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001530
Jouni Malinen31888482008-10-30 16:59:24 +02001531 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1532 struct ieee80211_txq_params txq_params;
1533 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1534
1535 if (!rdev->ops->set_txq_params) {
1536 result = -EOPNOTSUPP;
1537 goto bad_res;
1538 }
1539
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001540 if (!netdev) {
1541 result = -EINVAL;
1542 goto bad_res;
1543 }
1544
Johannes Berg133a3ff2011-11-03 14:50:13 +01001545 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1546 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1547 result = -EINVAL;
1548 goto bad_res;
1549 }
1550
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001551 if (!netif_running(netdev)) {
1552 result = -ENETDOWN;
1553 goto bad_res;
1554 }
1555
Jouni Malinen31888482008-10-30 16:59:24 +02001556 nla_for_each_nested(nl_txq_params,
1557 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1558 rem_txq_params) {
1559 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1560 nla_data(nl_txq_params),
1561 nla_len(nl_txq_params),
1562 txq_params_policy);
1563 result = parse_txq_params(tb, &txq_params);
1564 if (result)
1565 goto bad_res;
1566
1567 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001568 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001569 &txq_params);
1570 if (result)
1571 goto bad_res;
1572 }
1573 }
1574
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001575 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001576 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001577 if (result)
1578 goto bad_res;
1579 }
1580
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001581 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1582 enum nl80211_tx_power_setting type;
1583 int idx, mbm = 0;
1584
1585 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001586 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001587 goto bad_res;
1588 }
1589
1590 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1591 type = nla_get_u32(info->attrs[idx]);
1592
1593 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1594 (type != NL80211_TX_POWER_AUTOMATIC)) {
1595 result = -EINVAL;
1596 goto bad_res;
1597 }
1598
1599 if (type != NL80211_TX_POWER_AUTOMATIC) {
1600 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1601 mbm = nla_get_u32(info->attrs[idx]);
1602 }
1603
1604 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1605 if (result)
1606 goto bad_res;
1607 }
1608
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001609 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1610 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1611 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001612 if ((!rdev->wiphy.available_antennas_tx &&
1613 !rdev->wiphy.available_antennas_rx) ||
1614 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001615 result = -EOPNOTSUPP;
1616 goto bad_res;
1617 }
1618
1619 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1620 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1621
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001622 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001623 * available antenna masks, except for the "all" mask */
1624 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1625 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001626 result = -EINVAL;
1627 goto bad_res;
1628 }
1629
Bruno Randolf7f531e02010-12-16 11:30:22 +09001630 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1631 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001632
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001633 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1634 if (result)
1635 goto bad_res;
1636 }
1637
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001638 changed = 0;
1639
1640 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1641 retry_short = nla_get_u8(
1642 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1643 if (retry_short == 0) {
1644 result = -EINVAL;
1645 goto bad_res;
1646 }
1647 changed |= WIPHY_PARAM_RETRY_SHORT;
1648 }
1649
1650 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1651 retry_long = nla_get_u8(
1652 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1653 if (retry_long == 0) {
1654 result = -EINVAL;
1655 goto bad_res;
1656 }
1657 changed |= WIPHY_PARAM_RETRY_LONG;
1658 }
1659
1660 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1661 frag_threshold = nla_get_u32(
1662 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1663 if (frag_threshold < 256) {
1664 result = -EINVAL;
1665 goto bad_res;
1666 }
1667 if (frag_threshold != (u32) -1) {
1668 /*
1669 * Fragments (apart from the last one) are required to
1670 * have even length. Make the fragmentation code
1671 * simpler by stripping LSB should someone try to use
1672 * odd threshold value.
1673 */
1674 frag_threshold &= ~0x1;
1675 }
1676 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1677 }
1678
1679 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1680 rts_threshold = nla_get_u32(
1681 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1682 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1683 }
1684
Lukáš Turek81077e82009-12-21 22:50:47 +01001685 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1686 coverage_class = nla_get_u8(
1687 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1688 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1689 }
1690
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001691 if (changed) {
1692 u8 old_retry_short, old_retry_long;
1693 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001694 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001695
1696 if (!rdev->ops->set_wiphy_params) {
1697 result = -EOPNOTSUPP;
1698 goto bad_res;
1699 }
1700
1701 old_retry_short = rdev->wiphy.retry_short;
1702 old_retry_long = rdev->wiphy.retry_long;
1703 old_frag_threshold = rdev->wiphy.frag_threshold;
1704 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001705 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001706
1707 if (changed & WIPHY_PARAM_RETRY_SHORT)
1708 rdev->wiphy.retry_short = retry_short;
1709 if (changed & WIPHY_PARAM_RETRY_LONG)
1710 rdev->wiphy.retry_long = retry_long;
1711 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1712 rdev->wiphy.frag_threshold = frag_threshold;
1713 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1714 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001715 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1716 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001717
1718 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1719 if (result) {
1720 rdev->wiphy.retry_short = old_retry_short;
1721 rdev->wiphy.retry_long = old_retry_long;
1722 rdev->wiphy.frag_threshold = old_frag_threshold;
1723 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001724 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001725 }
1726 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001727
Johannes Berg306d6112008-12-08 12:39:04 +01001728 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001729 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001730 if (netdev)
1731 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001732 return result;
1733}
1734
Johannes Berg71bbc992012-06-15 15:30:18 +02001735static inline u64 wdev_id(struct wireless_dev *wdev)
1736{
1737 return (u64)wdev->identifier |
1738 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1739}
Johannes Berg55682962007-09-20 13:09:35 -04001740
Eric W. Biederman15e47302012-09-07 20:12:54 +00001741static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001742 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001743 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001744{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001745 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001746 void *hdr;
1747
Eric W. Biederman15e47302012-09-07 20:12:54 +00001748 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001749 if (!hdr)
1750 return -1;
1751
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001752 if (dev &&
1753 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001754 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001755 goto nla_put_failure;
1756
1757 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1758 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001759 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001760 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001761 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1762 rdev->devlist_generation ^
1763 (cfg80211_rdev_list_generation << 2)))
1764 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001765
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001766 if (rdev->ops->get_channel) {
1767 struct ieee80211_channel *chan;
1768 enum nl80211_channel_type channel_type;
1769
1770 chan = rdev->ops->get_channel(&rdev->wiphy, wdev,
1771 &channel_type);
1772 if (chan &&
1773 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1774 chan->center_freq) ||
1775 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1776 channel_type)))
John W. Linville59ef43e2012-04-18 14:17:13 -04001777 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001778 }
1779
Johannes Berg55682962007-09-20 13:09:35 -04001780 return genlmsg_end(msg, hdr);
1781
1782 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001783 genlmsg_cancel(msg, hdr);
1784 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001785}
1786
1787static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1788{
1789 int wp_idx = 0;
1790 int if_idx = 0;
1791 int wp_start = cb->args[0];
1792 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001793 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001794 struct wireless_dev *wdev;
1795
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001796 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001797 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1798 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001799 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001800 if (wp_idx < wp_start) {
1801 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001802 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001803 }
Johannes Berg55682962007-09-20 13:09:35 -04001804 if_idx = 0;
1805
Johannes Bergf5ea9122009-08-07 16:17:38 +02001806 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001807 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001808 if (if_idx < if_start) {
1809 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001810 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001811 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001812 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001813 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001814 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001815 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001816 goto out;
1817 }
1818 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001819 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001820 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001821
1822 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001823 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001824 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001825 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001826
1827 cb->args[0] = wp_idx;
1828 cb->args[1] = if_idx;
1829
1830 return skb->len;
1831}
1832
1833static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1834{
1835 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001836 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001837 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001838
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001839 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001840 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001841 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001842
Eric W. Biederman15e47302012-09-07 20:12:54 +00001843 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001844 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001845 nlmsg_free(msg);
1846 return -ENOBUFS;
1847 }
Johannes Berg55682962007-09-20 13:09:35 -04001848
Johannes Berg134e6372009-07-10 09:51:34 +00001849 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001850}
1851
Michael Wu66f7ac52008-01-31 19:48:22 +01001852static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1853 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1854 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1855 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1856 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1857 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1858};
1859
1860static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1861{
1862 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1863 int flag;
1864
1865 *mntrflags = 0;
1866
1867 if (!nla)
1868 return -EINVAL;
1869
1870 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1871 nla, mntr_flags_policy))
1872 return -EINVAL;
1873
1874 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1875 if (flags[flag])
1876 *mntrflags |= (1<<flag);
1877
1878 return 0;
1879}
1880
Johannes Berg9bc383d2009-11-19 11:55:19 +01001881static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001882 struct net_device *netdev, u8 use_4addr,
1883 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001884{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001885 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001886 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001887 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001888 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001889 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001890
1891 switch (iftype) {
1892 case NL80211_IFTYPE_AP_VLAN:
1893 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1894 return 0;
1895 break;
1896 case NL80211_IFTYPE_STATION:
1897 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1898 return 0;
1899 break;
1900 default:
1901 break;
1902 }
1903
1904 return -EOPNOTSUPP;
1905}
1906
Johannes Berg55682962007-09-20 13:09:35 -04001907static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1908{
Johannes Berg4c476992010-10-04 21:36:35 +02001909 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001910 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001911 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001912 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001913 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001914 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001915 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001916
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001917 memset(&params, 0, sizeof(params));
1918
Johannes Berg04a773a2009-04-19 21:24:32 +02001919 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001920
Johannes Berg723b0382008-09-16 20:22:09 +02001921 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001922 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001923 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001924 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001925 if (ntype > NL80211_IFTYPE_MAX)
1926 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001927 }
1928
Johannes Berg92ffe052008-09-16 20:39:36 +02001929 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001930 struct wireless_dev *wdev = dev->ieee80211_ptr;
1931
Johannes Berg4c476992010-10-04 21:36:35 +02001932 if (ntype != NL80211_IFTYPE_MESH_POINT)
1933 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001934 if (netif_running(dev))
1935 return -EBUSY;
1936
1937 wdev_lock(wdev);
1938 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1939 IEEE80211_MAX_MESH_ID_LEN);
1940 wdev->mesh_id_up_len =
1941 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1942 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1943 wdev->mesh_id_up_len);
1944 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001945 }
1946
Felix Fietkau8b787642009-11-10 18:53:10 +01001947 if (info->attrs[NL80211_ATTR_4ADDR]) {
1948 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1949 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001950 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001951 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001952 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001953 } else {
1954 params.use_4addr = -1;
1955 }
1956
Johannes Berg92ffe052008-09-16 20:39:36 +02001957 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001958 if (ntype != NL80211_IFTYPE_MONITOR)
1959 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001960 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1961 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001962 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001963 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001964
1965 flags = &_flags;
1966 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001967 }
Johannes Berg3b858752009-03-12 09:55:09 +01001968
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001969 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001970 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001971 else
1972 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001973
Johannes Berg9bc383d2009-11-19 11:55:19 +01001974 if (!err && params.use_4addr != -1)
1975 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1976
Johannes Berg55682962007-09-20 13:09:35 -04001977 return err;
1978}
1979
1980static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1981{
Johannes Berg4c476992010-10-04 21:36:35 +02001982 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001983 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02001984 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02001985 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04001986 int err;
1987 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001988 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001989
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001990 memset(&params, 0, sizeof(params));
1991
Johannes Berg55682962007-09-20 13:09:35 -04001992 if (!info->attrs[NL80211_ATTR_IFNAME])
1993 return -EINVAL;
1994
1995 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1996 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1997 if (type > NL80211_IFTYPE_MAX)
1998 return -EINVAL;
1999 }
2000
Johannes Berg79c97e92009-07-07 03:56:12 +02002001 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002002 !(rdev->wiphy.interface_modes & (1 << type)))
2003 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002004
Johannes Berg9bc383d2009-11-19 11:55:19 +01002005 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002006 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002007 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002008 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002009 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002010 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002011
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002012 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2013 if (!msg)
2014 return -ENOMEM;
2015
Michael Wu66f7ac52008-01-31 19:48:22 +01002016 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2017 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2018 &flags);
Johannes Berg84efbb82012-06-16 00:00:26 +02002019 wdev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01002020 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002021 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002022 if (IS_ERR(wdev)) {
2023 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002024 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002025 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002026
Johannes Berg98104fde2012-06-16 00:19:54 +02002027 switch (type) {
2028 case NL80211_IFTYPE_MESH_POINT:
2029 if (!info->attrs[NL80211_ATTR_MESH_ID])
2030 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002031 wdev_lock(wdev);
2032 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2033 IEEE80211_MAX_MESH_ID_LEN);
2034 wdev->mesh_id_up_len =
2035 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2036 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2037 wdev->mesh_id_up_len);
2038 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002039 break;
2040 case NL80211_IFTYPE_P2P_DEVICE:
2041 /*
2042 * P2P Device doesn't have a netdev, so doesn't go
2043 * through the netdev notifier and must be added here
2044 */
2045 mutex_init(&wdev->mtx);
2046 INIT_LIST_HEAD(&wdev->event_list);
2047 spin_lock_init(&wdev->event_lock);
2048 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2049 spin_lock_init(&wdev->mgmt_registrations_lock);
2050
2051 mutex_lock(&rdev->devlist_mtx);
2052 wdev->identifier = ++rdev->wdev_id;
2053 list_add_rcu(&wdev->list, &rdev->wdev_list);
2054 rdev->devlist_generation++;
2055 mutex_unlock(&rdev->devlist_mtx);
2056 break;
2057 default:
2058 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002059 }
2060
Eric W. Biederman15e47302012-09-07 20:12:54 +00002061 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002062 rdev, wdev) < 0) {
2063 nlmsg_free(msg);
2064 return -ENOBUFS;
2065 }
2066
2067 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002068}
2069
2070static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2071{
Johannes Berg4c476992010-10-04 21:36:35 +02002072 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002073 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002074
Johannes Berg4c476992010-10-04 21:36:35 +02002075 if (!rdev->ops->del_virtual_intf)
2076 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002077
Johannes Berg84efbb82012-06-16 00:00:26 +02002078 /*
2079 * If we remove a wireless device without a netdev then clear
2080 * user_ptr[1] so that nl80211_post_doit won't dereference it
2081 * to check if it needs to do dev_put(). Otherwise it crashes
2082 * since the wdev has been freed, unlike with a netdev where
2083 * we need the dev_put() for the netdev to really be freed.
2084 */
2085 if (!wdev->netdev)
2086 info->user_ptr[1] = NULL;
2087
2088 return rdev->ops->del_virtual_intf(&rdev->wiphy, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002089}
2090
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002091static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2092{
2093 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2094 struct net_device *dev = info->user_ptr[1];
2095 u16 noack_map;
2096
2097 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2098 return -EINVAL;
2099
2100 if (!rdev->ops->set_noack_map)
2101 return -EOPNOTSUPP;
2102
2103 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2104
2105 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
2106}
2107
Johannes Berg41ade002007-12-19 02:03:29 +01002108struct get_key_cookie {
2109 struct sk_buff *msg;
2110 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002111 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002112};
2113
2114static void get_key_callback(void *c, struct key_params *params)
2115{
Johannes Bergb9454e82009-07-08 13:29:08 +02002116 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002117 struct get_key_cookie *cookie = c;
2118
David S. Miller9360ffd2012-03-29 04:41:26 -04002119 if ((params->key &&
2120 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2121 params->key_len, params->key)) ||
2122 (params->seq &&
2123 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2124 params->seq_len, params->seq)) ||
2125 (params->cipher &&
2126 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2127 params->cipher)))
2128 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002129
Johannes Bergb9454e82009-07-08 13:29:08 +02002130 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2131 if (!key)
2132 goto nla_put_failure;
2133
David S. Miller9360ffd2012-03-29 04:41:26 -04002134 if ((params->key &&
2135 nla_put(cookie->msg, NL80211_KEY_DATA,
2136 params->key_len, params->key)) ||
2137 (params->seq &&
2138 nla_put(cookie->msg, NL80211_KEY_SEQ,
2139 params->seq_len, params->seq)) ||
2140 (params->cipher &&
2141 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2142 params->cipher)))
2143 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002144
David S. Miller9360ffd2012-03-29 04:41:26 -04002145 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2146 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002147
2148 nla_nest_end(cookie->msg, key);
2149
Johannes Berg41ade002007-12-19 02:03:29 +01002150 return;
2151 nla_put_failure:
2152 cookie->error = 1;
2153}
2154
2155static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2156{
Johannes Berg4c476992010-10-04 21:36:35 +02002157 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002158 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002159 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002160 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002161 const u8 *mac_addr = NULL;
2162 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002163 struct get_key_cookie cookie = {
2164 .error = 0,
2165 };
2166 void *hdr;
2167 struct sk_buff *msg;
2168
2169 if (info->attrs[NL80211_ATTR_KEY_IDX])
2170 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2171
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002172 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002173 return -EINVAL;
2174
2175 if (info->attrs[NL80211_ATTR_MAC])
2176 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2177
Johannes Berge31b8212010-10-05 19:39:30 +02002178 pairwise = !!mac_addr;
2179 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2180 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2181 if (kt >= NUM_NL80211_KEYTYPES)
2182 return -EINVAL;
2183 if (kt != NL80211_KEYTYPE_GROUP &&
2184 kt != NL80211_KEYTYPE_PAIRWISE)
2185 return -EINVAL;
2186 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2187 }
2188
Johannes Berg4c476992010-10-04 21:36:35 +02002189 if (!rdev->ops->get_key)
2190 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002191
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002192 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002193 if (!msg)
2194 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002195
Eric W. Biederman15e47302012-09-07 20:12:54 +00002196 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002197 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002198 if (IS_ERR(hdr))
2199 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002200
2201 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002202 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002203
David S. Miller9360ffd2012-03-29 04:41:26 -04002204 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2205 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2206 goto nla_put_failure;
2207 if (mac_addr &&
2208 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2209 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002210
Johannes Berge31b8212010-10-05 19:39:30 +02002211 if (pairwise && mac_addr &&
2212 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2213 return -ENOENT;
2214
2215 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
2216 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002217
2218 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002219 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002220
2221 if (cookie.error)
2222 goto nla_put_failure;
2223
2224 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002225 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002226
2227 nla_put_failure:
2228 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002229 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002230 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002231 return err;
2232}
2233
2234static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2235{
Johannes Berg4c476992010-10-04 21:36:35 +02002236 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002237 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002238 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002239 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002240
Johannes Bergb9454e82009-07-08 13:29:08 +02002241 err = nl80211_parse_key(info, &key);
2242 if (err)
2243 return err;
2244
2245 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002246 return -EINVAL;
2247
Johannes Bergb9454e82009-07-08 13:29:08 +02002248 /* only support setting default key */
2249 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002250 return -EINVAL;
2251
Johannes Bergfffd0932009-07-08 14:22:54 +02002252 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002253
2254 if (key.def) {
2255 if (!rdev->ops->set_default_key) {
2256 err = -EOPNOTSUPP;
2257 goto out;
2258 }
2259
2260 err = nl80211_key_allowed(dev->ieee80211_ptr);
2261 if (err)
2262 goto out;
2263
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002264 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2265 key.def_uni, key.def_multi);
2266
2267 if (err)
2268 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002269
Johannes Berg3d23e342009-09-29 23:27:28 +02002270#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002271 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002272#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002273 } else {
2274 if (key.def_uni || !key.def_multi) {
2275 err = -EINVAL;
2276 goto out;
2277 }
2278
2279 if (!rdev->ops->set_default_mgmt_key) {
2280 err = -EOPNOTSUPP;
2281 goto out;
2282 }
2283
2284 err = nl80211_key_allowed(dev->ieee80211_ptr);
2285 if (err)
2286 goto out;
2287
2288 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2289 dev, key.idx);
2290 if (err)
2291 goto out;
2292
2293#ifdef CONFIG_CFG80211_WEXT
2294 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2295#endif
2296 }
2297
2298 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002299 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002300
Johannes Berg41ade002007-12-19 02:03:29 +01002301 return err;
2302}
2303
2304static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2305{
Johannes Berg4c476992010-10-04 21:36:35 +02002306 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002307 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002308 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002309 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002310 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002311
Johannes Bergb9454e82009-07-08 13:29:08 +02002312 err = nl80211_parse_key(info, &key);
2313 if (err)
2314 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002315
Johannes Bergb9454e82009-07-08 13:29:08 +02002316 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002317 return -EINVAL;
2318
Johannes Berg41ade002007-12-19 02:03:29 +01002319 if (info->attrs[NL80211_ATTR_MAC])
2320 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2321
Johannes Berge31b8212010-10-05 19:39:30 +02002322 if (key.type == -1) {
2323 if (mac_addr)
2324 key.type = NL80211_KEYTYPE_PAIRWISE;
2325 else
2326 key.type = NL80211_KEYTYPE_GROUP;
2327 }
2328
2329 /* for now */
2330 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2331 key.type != NL80211_KEYTYPE_GROUP)
2332 return -EINVAL;
2333
Johannes Berg4c476992010-10-04 21:36:35 +02002334 if (!rdev->ops->add_key)
2335 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002336
Johannes Berge31b8212010-10-05 19:39:30 +02002337 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2338 key.type == NL80211_KEYTYPE_PAIRWISE,
2339 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002340 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002341
2342 wdev_lock(dev->ieee80211_ptr);
2343 err = nl80211_key_allowed(dev->ieee80211_ptr);
2344 if (!err)
2345 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002346 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002347 mac_addr, &key.p);
2348 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002349
Johannes Berg41ade002007-12-19 02:03:29 +01002350 return err;
2351}
2352
2353static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2354{
Johannes Berg4c476992010-10-04 21:36:35 +02002355 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002356 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002357 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002358 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002359 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002360
Johannes Bergb9454e82009-07-08 13:29:08 +02002361 err = nl80211_parse_key(info, &key);
2362 if (err)
2363 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002364
2365 if (info->attrs[NL80211_ATTR_MAC])
2366 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2367
Johannes Berge31b8212010-10-05 19:39:30 +02002368 if (key.type == -1) {
2369 if (mac_addr)
2370 key.type = NL80211_KEYTYPE_PAIRWISE;
2371 else
2372 key.type = NL80211_KEYTYPE_GROUP;
2373 }
2374
2375 /* for now */
2376 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2377 key.type != NL80211_KEYTYPE_GROUP)
2378 return -EINVAL;
2379
Johannes Berg4c476992010-10-04 21:36:35 +02002380 if (!rdev->ops->del_key)
2381 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002382
Johannes Bergfffd0932009-07-08 14:22:54 +02002383 wdev_lock(dev->ieee80211_ptr);
2384 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002385
2386 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2387 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2388 err = -ENOENT;
2389
Johannes Bergfffd0932009-07-08 14:22:54 +02002390 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002391 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2392 key.type == NL80211_KEYTYPE_PAIRWISE,
2393 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002394
Johannes Berg3d23e342009-09-29 23:27:28 +02002395#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002396 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002397 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002398 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002399 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002400 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2401 }
2402#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002403 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002404
Johannes Berg41ade002007-12-19 02:03:29 +01002405 return err;
2406}
2407
Johannes Berg88600202012-02-13 15:17:18 +01002408static int nl80211_parse_beacon(struct genl_info *info,
2409 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002410{
Johannes Berg88600202012-02-13 15:17:18 +01002411 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002412
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002413 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2414 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2415 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2416 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002417 return -EINVAL;
2418
Johannes Berg88600202012-02-13 15:17:18 +01002419 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002420
Johannes Berged1b6cc2007-12-19 02:03:32 +01002421 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002422 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2423 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2424 if (!bcn->head_len)
2425 return -EINVAL;
2426 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002427 }
2428
2429 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002430 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2431 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002432 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002433 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002434 }
2435
Johannes Berg4c476992010-10-04 21:36:35 +02002436 if (!haveinfo)
2437 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002438
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002439 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002440 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2441 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002442 }
2443
2444 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002445 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002446 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002447 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002448 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2449 }
2450
2451 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002452 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002453 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002454 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002455 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2456 }
2457
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002458 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002459 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002460 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002461 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002462 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2463 }
2464
Johannes Berg88600202012-02-13 15:17:18 +01002465 return 0;
2466}
2467
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002468static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2469 struct cfg80211_ap_settings *params)
2470{
2471 struct wireless_dev *wdev;
2472 bool ret = false;
2473
2474 mutex_lock(&rdev->devlist_mtx);
2475
Johannes Berg89a54e42012-06-15 14:33:17 +02002476 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002477 if (wdev->iftype != NL80211_IFTYPE_AP &&
2478 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2479 continue;
2480
2481 if (!wdev->preset_chan)
2482 continue;
2483
2484 params->channel = wdev->preset_chan;
2485 params->channel_type = wdev->preset_chantype;
2486 ret = true;
2487 break;
2488 }
2489
2490 mutex_unlock(&rdev->devlist_mtx);
2491
2492 return ret;
2493}
2494
Jouni Malinene39e5b52012-09-30 19:29:39 +03002495static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2496 enum nl80211_auth_type auth_type,
2497 enum nl80211_commands cmd)
2498{
2499 if (auth_type > NL80211_AUTHTYPE_MAX)
2500 return false;
2501
2502 switch (cmd) {
2503 case NL80211_CMD_AUTHENTICATE:
2504 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2505 auth_type == NL80211_AUTHTYPE_SAE)
2506 return false;
2507 return true;
2508 case NL80211_CMD_CONNECT:
2509 case NL80211_CMD_START_AP:
2510 /* SAE not supported yet */
2511 if (auth_type == NL80211_AUTHTYPE_SAE)
2512 return false;
2513 return true;
2514 default:
2515 return false;
2516 }
2517}
2518
Johannes Berg88600202012-02-13 15:17:18 +01002519static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2520{
2521 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2522 struct net_device *dev = info->user_ptr[1];
2523 struct wireless_dev *wdev = dev->ieee80211_ptr;
2524 struct cfg80211_ap_settings params;
2525 int err;
2526
2527 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2528 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2529 return -EOPNOTSUPP;
2530
2531 if (!rdev->ops->start_ap)
2532 return -EOPNOTSUPP;
2533
2534 if (wdev->beacon_interval)
2535 return -EALREADY;
2536
2537 memset(&params, 0, sizeof(params));
2538
2539 /* these are required for START_AP */
2540 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2541 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2542 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2543 return -EINVAL;
2544
2545 err = nl80211_parse_beacon(info, &params.beacon);
2546 if (err)
2547 return err;
2548
2549 params.beacon_interval =
2550 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2551 params.dtim_period =
2552 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2553
2554 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2555 if (err)
2556 return err;
2557
2558 /*
2559 * In theory, some of these attributes should be required here
2560 * but since they were not used when the command was originally
2561 * added, keep them optional for old user space programs to let
2562 * them continue to work with drivers that do not need the
2563 * additional information -- drivers must check!
2564 */
2565 if (info->attrs[NL80211_ATTR_SSID]) {
2566 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2567 params.ssid_len =
2568 nla_len(info->attrs[NL80211_ATTR_SSID]);
2569 if (params.ssid_len == 0 ||
2570 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2571 return -EINVAL;
2572 }
2573
2574 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2575 params.hidden_ssid = nla_get_u32(
2576 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2577 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2578 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2579 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2580 return -EINVAL;
2581 }
2582
2583 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2584
2585 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2586 params.auth_type = nla_get_u32(
2587 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002588 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2589 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002590 return -EINVAL;
2591 } else
2592 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2593
2594 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2595 NL80211_MAX_NR_CIPHER_SUITES);
2596 if (err)
2597 return err;
2598
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302599 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2600 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2601 return -EOPNOTSUPP;
2602 params.inactivity_timeout = nla_get_u16(
2603 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2604 }
2605
Johannes Bergaa430da2012-05-16 23:50:18 +02002606 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2607 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2608
2609 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2610 !nl80211_valid_channel_type(info, &channel_type))
2611 return -EINVAL;
2612
2613 params.channel = rdev_freq_to_chan(rdev,
2614 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2615 channel_type);
2616 if (!params.channel)
2617 return -EINVAL;
2618 params.channel_type = channel_type;
2619 } else if (wdev->preset_chan) {
2620 params.channel = wdev->preset_chan;
2621 params.channel_type = wdev->preset_chantype;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002622 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002623 return -EINVAL;
2624
2625 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2626 params.channel_type))
2627 return -EINVAL;
2628
Michal Kaziore4e32452012-06-29 12:47:08 +02002629 mutex_lock(&rdev->devlist_mtx);
2630 err = cfg80211_can_use_chan(rdev, wdev, params.channel,
2631 CHAN_MODE_SHARED);
2632 mutex_unlock(&rdev->devlist_mtx);
2633
2634 if (err)
2635 return err;
2636
Johannes Berg88600202012-02-13 15:17:18 +01002637 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002638 if (!err) {
2639 wdev->preset_chan = params.channel;
2640 wdev->preset_chantype = params.channel_type;
Johannes Berg88600202012-02-13 15:17:18 +01002641 wdev->beacon_interval = params.beacon_interval;
Michal Kaziorf4489eb2012-06-29 12:46:58 +02002642 wdev->channel = params.channel;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002643 }
Johannes Berg56d18932011-05-09 18:41:15 +02002644 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002645}
2646
Johannes Berg88600202012-02-13 15:17:18 +01002647static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2648{
2649 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2650 struct net_device *dev = info->user_ptr[1];
2651 struct wireless_dev *wdev = dev->ieee80211_ptr;
2652 struct cfg80211_beacon_data params;
2653 int err;
2654
2655 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2656 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2657 return -EOPNOTSUPP;
2658
2659 if (!rdev->ops->change_beacon)
2660 return -EOPNOTSUPP;
2661
2662 if (!wdev->beacon_interval)
2663 return -EINVAL;
2664
2665 err = nl80211_parse_beacon(info, &params);
2666 if (err)
2667 return err;
2668
2669 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2670}
2671
2672static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002673{
Johannes Berg4c476992010-10-04 21:36:35 +02002674 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2675 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002676
Michal Kazior60771782012-06-29 12:46:56 +02002677 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002678}
2679
Johannes Berg5727ef12007-12-19 02:03:34 +01002680static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2681 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2682 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2683 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002684 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002685 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002686 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002687};
2688
Johannes Bergeccb8e82009-05-11 21:57:56 +03002689static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002690 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002691 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002692{
2693 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002694 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002695 int flag;
2696
Johannes Bergeccb8e82009-05-11 21:57:56 +03002697 /*
2698 * Try parsing the new attribute first so userspace
2699 * can specify both for older kernels.
2700 */
2701 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2702 if (nla) {
2703 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002704
Johannes Bergeccb8e82009-05-11 21:57:56 +03002705 sta_flags = nla_data(nla);
2706 params->sta_flags_mask = sta_flags->mask;
2707 params->sta_flags_set = sta_flags->set;
2708 if ((params->sta_flags_mask |
2709 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2710 return -EINVAL;
2711 return 0;
2712 }
2713
2714 /* if present, parse the old attribute */
2715
2716 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002717 if (!nla)
2718 return 0;
2719
2720 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2721 nla, sta_flags_policy))
2722 return -EINVAL;
2723
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002724 /*
2725 * Only allow certain flags for interface types so that
2726 * other attributes are silently ignored. Remember that
2727 * this is backward compatibility code with old userspace
2728 * and shouldn't be hit in other cases anyway.
2729 */
2730 switch (iftype) {
2731 case NL80211_IFTYPE_AP:
2732 case NL80211_IFTYPE_AP_VLAN:
2733 case NL80211_IFTYPE_P2P_GO:
2734 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2735 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2736 BIT(NL80211_STA_FLAG_WME) |
2737 BIT(NL80211_STA_FLAG_MFP);
2738 break;
2739 case NL80211_IFTYPE_P2P_CLIENT:
2740 case NL80211_IFTYPE_STATION:
2741 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2742 BIT(NL80211_STA_FLAG_TDLS_PEER);
2743 break;
2744 case NL80211_IFTYPE_MESH_POINT:
2745 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2746 BIT(NL80211_STA_FLAG_MFP) |
2747 BIT(NL80211_STA_FLAG_AUTHORIZED);
2748 default:
2749 return -EINVAL;
2750 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002751
Johannes Berg3383b5a2012-05-10 20:14:43 +02002752 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2753 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002754 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002755
Johannes Berg3383b5a2012-05-10 20:14:43 +02002756 /* no longer support new API additions in old API */
2757 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2758 return -EINVAL;
2759 }
2760 }
2761
Johannes Berg5727ef12007-12-19 02:03:34 +01002762 return 0;
2763}
2764
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002765static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2766 int attr)
2767{
2768 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002769 u32 bitrate;
2770 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002771
2772 rate = nla_nest_start(msg, attr);
2773 if (!rate)
2774 goto nla_put_failure;
2775
2776 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2777 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002778 /* report 16-bit bitrate only if we can */
2779 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002780 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002781 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2782 (bitrate_compat > 0 &&
2783 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002784 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2785 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2786 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2787 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2788 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2789 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2790 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002791
2792 nla_nest_end(msg, rate);
2793 return true;
2794
2795nla_put_failure:
2796 return false;
2797}
2798
Eric W. Biederman15e47302012-09-07 20:12:54 +00002799static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002800 int flags,
2801 struct cfg80211_registered_device *rdev,
2802 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002803 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002804{
2805 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002806 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002807
Eric W. Biederman15e47302012-09-07 20:12:54 +00002808 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002809 if (!hdr)
2810 return -1;
2811
David S. Miller9360ffd2012-03-29 04:41:26 -04002812 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2813 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2814 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2815 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002816
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002817 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2818 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002819 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002820 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2821 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2822 sinfo->connected_time))
2823 goto nla_put_failure;
2824 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2825 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2826 sinfo->inactive_time))
2827 goto nla_put_failure;
2828 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2829 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2830 sinfo->rx_bytes))
2831 goto nla_put_failure;
2832 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2833 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2834 sinfo->tx_bytes))
2835 goto nla_put_failure;
2836 if ((sinfo->filled & STATION_INFO_LLID) &&
2837 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2838 goto nla_put_failure;
2839 if ((sinfo->filled & STATION_INFO_PLID) &&
2840 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2841 goto nla_put_failure;
2842 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2843 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2844 sinfo->plink_state))
2845 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002846 switch (rdev->wiphy.signal_type) {
2847 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002848 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2849 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2850 sinfo->signal))
2851 goto nla_put_failure;
2852 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2853 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2854 sinfo->signal_avg))
2855 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002856 break;
2857 default:
2858 break;
2859 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002860 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002861 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2862 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002863 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002864 }
2865 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2866 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2867 NL80211_STA_INFO_RX_BITRATE))
2868 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002869 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002870 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2871 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2872 sinfo->rx_packets))
2873 goto nla_put_failure;
2874 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2875 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2876 sinfo->tx_packets))
2877 goto nla_put_failure;
2878 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2879 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2880 sinfo->tx_retries))
2881 goto nla_put_failure;
2882 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2883 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2884 sinfo->tx_failed))
2885 goto nla_put_failure;
2886 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2887 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2888 sinfo->beacon_loss_count))
2889 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002890 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2891 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2892 if (!bss_param)
2893 goto nla_put_failure;
2894
David S. Miller9360ffd2012-03-29 04:41:26 -04002895 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2896 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2897 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2898 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2899 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2900 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2901 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2902 sinfo->bss_param.dtim_period) ||
2903 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2904 sinfo->bss_param.beacon_interval))
2905 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002906
2907 nla_nest_end(msg, bss_param);
2908 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002909 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2910 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2911 sizeof(struct nl80211_sta_flag_update),
2912 &sinfo->sta_flags))
2913 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002914 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2915 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2916 sinfo->t_offset))
2917 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002918 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002919
David S. Miller9360ffd2012-03-29 04:41:26 -04002920 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2921 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2922 sinfo->assoc_req_ies))
2923 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002924
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002925 return genlmsg_end(msg, hdr);
2926
2927 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002928 genlmsg_cancel(msg, hdr);
2929 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002930}
2931
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002932static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002933 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002934{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002935 struct station_info sinfo;
2936 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002937 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002938 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002939 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002940 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002941
Johannes Berg67748892010-10-04 21:14:06 +02002942 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2943 if (err)
2944 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002945
2946 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002947 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002948 goto out_err;
2949 }
2950
Johannes Bergbba95fe2008-07-29 13:22:51 +02002951 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002952 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002953 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2954 mac_addr, &sinfo);
2955 if (err == -ENOENT)
2956 break;
2957 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002958 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002959
2960 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002961 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002962 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002963 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002964 &sinfo) < 0)
2965 goto out;
2966
2967 sta_idx++;
2968 }
2969
2970
2971 out:
2972 cb->args[1] = sta_idx;
2973 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002974 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002975 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002976
2977 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002978}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002979
Johannes Berg5727ef12007-12-19 02:03:34 +01002980static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2981{
Johannes Berg4c476992010-10-04 21:36:35 +02002982 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2983 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002984 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002985 struct sk_buff *msg;
2986 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002987 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002988
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002989 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002990
2991 if (!info->attrs[NL80211_ATTR_MAC])
2992 return -EINVAL;
2993
2994 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2995
Johannes Berg4c476992010-10-04 21:36:35 +02002996 if (!rdev->ops->get_station)
2997 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002998
Johannes Berg79c97e92009-07-07 03:56:12 +02002999 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003000 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003001 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003002
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003003 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003004 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003005 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003006
Eric W. Biederman15e47302012-09-07 20:12:54 +00003007 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003008 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003009 nlmsg_free(msg);
3010 return -ENOBUFS;
3011 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003012
Johannes Berg4c476992010-10-04 21:36:35 +02003013 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003014}
3015
3016/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003017 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003018 */
Johannes Berg80b99892011-11-18 16:23:01 +01003019static struct net_device *get_vlan(struct genl_info *info,
3020 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003021{
Johannes Berg463d0182009-07-14 00:33:35 +02003022 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003023 struct net_device *v;
3024 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003025
Johannes Berg80b99892011-11-18 16:23:01 +01003026 if (!vlanattr)
3027 return NULL;
3028
3029 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3030 if (!v)
3031 return ERR_PTR(-ENODEV);
3032
3033 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3034 ret = -EINVAL;
3035 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003036 }
Johannes Berg80b99892011-11-18 16:23:01 +01003037
3038 if (!netif_running(v)) {
3039 ret = -ENETDOWN;
3040 goto error;
3041 }
3042
3043 return v;
3044 error:
3045 dev_put(v);
3046 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003047}
3048
3049static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3050{
Johannes Berg4c476992010-10-04 21:36:35 +02003051 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003052 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003053 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003054 struct station_parameters params;
3055 u8 *mac_addr = NULL;
3056
3057 memset(&params, 0, sizeof(params));
3058
3059 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003060 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003061
3062 if (info->attrs[NL80211_ATTR_STA_AID])
3063 return -EINVAL;
3064
3065 if (!info->attrs[NL80211_ATTR_MAC])
3066 return -EINVAL;
3067
3068 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3069
3070 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3071 params.supported_rates =
3072 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3073 params.supported_rates_len =
3074 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3075 }
3076
3077 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3078 params.listen_interval =
3079 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3080
Jouni Malinen36aedc92008-08-25 11:58:58 +03003081 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3082 params.ht_capa =
3083 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3084
Johannes Bergbdd90d52011-12-14 12:20:27 +01003085 if (!rdev->ops->change_station)
3086 return -EOPNOTSUPP;
3087
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003088 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003089 return -EINVAL;
3090
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003091 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3092 params.plink_action =
3093 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3094
Javier Cardona9c3990a2011-05-03 16:57:11 -07003095 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3096 params.plink_state =
3097 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3098
Johannes Berga97f4422009-06-18 17:23:43 +02003099 switch (dev->ieee80211_ptr->iftype) {
3100 case NL80211_IFTYPE_AP:
3101 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003102 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003103 /* disallow mesh-specific things */
3104 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003105 return -EINVAL;
3106
3107 /* TDLS can't be set, ... */
3108 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3109 return -EINVAL;
3110 /*
3111 * ... but don't bother the driver with it. This works around
3112 * a hostapd/wpa_supplicant issue -- it always includes the
3113 * TLDS_PEER flag in the mask even for AP mode.
3114 */
3115 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3116
3117 /* accept only the listed bits */
3118 if (params.sta_flags_mask &
3119 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3120 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3121 BIT(NL80211_STA_FLAG_WME) |
3122 BIT(NL80211_STA_FLAG_MFP)))
3123 return -EINVAL;
3124
3125 /* must be last in here for error handling */
3126 params.vlan = get_vlan(info, rdev);
3127 if (IS_ERR(params.vlan))
3128 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003129 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003130 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003131 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003132 /*
3133 * Don't allow userspace to change the TDLS_PEER flag,
3134 * but silently ignore attempts to change it since we
3135 * don't have state here to verify that it doesn't try
3136 * to change the flag.
3137 */
3138 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003139 /* fall through */
3140 case NL80211_IFTYPE_ADHOC:
3141 /* disallow things sta doesn't support */
3142 if (params.plink_action)
3143 return -EINVAL;
3144 if (params.ht_capa)
3145 return -EINVAL;
3146 if (params.listen_interval >= 0)
3147 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003148 /* reject any changes other than AUTHORIZED */
3149 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3150 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003151 break;
3152 case NL80211_IFTYPE_MESH_POINT:
3153 /* disallow things mesh doesn't support */
3154 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003155 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003156 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003157 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003158 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003159 return -EINVAL;
3160 /*
3161 * No special handling for TDLS here -- the userspace
3162 * mesh code doesn't have this bug.
3163 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003164 if (params.sta_flags_mask &
3165 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003166 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003167 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003168 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003169 break;
3170 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003171 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003172 }
3173
Johannes Bergbdd90d52011-12-14 12:20:27 +01003174 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003175
Johannes Berg79c97e92009-07-07 03:56:12 +02003176 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003177
Johannes Berg5727ef12007-12-19 02:03:34 +01003178 if (params.vlan)
3179 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003180
Johannes Berg5727ef12007-12-19 02:03:34 +01003181 return err;
3182}
3183
Eliad Pellerc75786c2011-08-23 14:37:46 +03003184static struct nla_policy
3185nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3186 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3187 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3188};
3189
Johannes Berg5727ef12007-12-19 02:03:34 +01003190static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3191{
Johannes Berg4c476992010-10-04 21:36:35 +02003192 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003193 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003194 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003195 struct station_parameters params;
3196 u8 *mac_addr = NULL;
3197
3198 memset(&params, 0, sizeof(params));
3199
3200 if (!info->attrs[NL80211_ATTR_MAC])
3201 return -EINVAL;
3202
Johannes Berg5727ef12007-12-19 02:03:34 +01003203 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3204 return -EINVAL;
3205
3206 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3207 return -EINVAL;
3208
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003209 if (!info->attrs[NL80211_ATTR_STA_AID])
3210 return -EINVAL;
3211
Johannes Berg5727ef12007-12-19 02:03:34 +01003212 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3213 params.supported_rates =
3214 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3215 params.supported_rates_len =
3216 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3217 params.listen_interval =
3218 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003219
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003220 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3221 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3222 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003223
Jouni Malinen36aedc92008-08-25 11:58:58 +03003224 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3225 params.ht_capa =
3226 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003227
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003228 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3229 params.vht_capa =
3230 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3231
Javier Cardona96b78df2011-04-07 15:08:33 -07003232 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3233 params.plink_action =
3234 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3235
Johannes Bergbdd90d52011-12-14 12:20:27 +01003236 if (!rdev->ops->add_station)
3237 return -EOPNOTSUPP;
3238
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003239 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003240 return -EINVAL;
3241
Johannes Bergbdd90d52011-12-14 12:20:27 +01003242 switch (dev->ieee80211_ptr->iftype) {
3243 case NL80211_IFTYPE_AP:
3244 case NL80211_IFTYPE_AP_VLAN:
3245 case NL80211_IFTYPE_P2P_GO:
3246 /* parse WME attributes if sta is WME capable */
3247 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3248 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3249 info->attrs[NL80211_ATTR_STA_WME]) {
3250 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3251 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003252
Johannes Bergbdd90d52011-12-14 12:20:27 +01003253 nla = info->attrs[NL80211_ATTR_STA_WME];
3254 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3255 nl80211_sta_wme_policy);
3256 if (err)
3257 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003258
Johannes Bergbdd90d52011-12-14 12:20:27 +01003259 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3260 params.uapsd_queues =
3261 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3262 if (params.uapsd_queues &
3263 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3264 return -EINVAL;
3265
3266 if (tb[NL80211_STA_WME_MAX_SP])
3267 params.max_sp =
3268 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3269
3270 if (params.max_sp &
3271 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3272 return -EINVAL;
3273
3274 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3275 }
3276 /* TDLS peers cannot be added */
3277 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003278 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003279 /* but don't bother the driver with it */
3280 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003281
Johannes Bergbdd90d52011-12-14 12:20:27 +01003282 /* must be last in here for error handling */
3283 params.vlan = get_vlan(info, rdev);
3284 if (IS_ERR(params.vlan))
3285 return PTR_ERR(params.vlan);
3286 break;
3287 case NL80211_IFTYPE_MESH_POINT:
3288 /* TDLS peers cannot be added */
3289 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003290 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003291 break;
3292 case NL80211_IFTYPE_STATION:
3293 /* Only TDLS peers can be added */
3294 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3295 return -EINVAL;
3296 /* Can only add if TDLS ... */
3297 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3298 return -EOPNOTSUPP;
3299 /* ... with external setup is supported */
3300 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3301 return -EOPNOTSUPP;
3302 break;
3303 default:
3304 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003305 }
3306
Johannes Bergbdd90d52011-12-14 12:20:27 +01003307 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003308
Johannes Berg79c97e92009-07-07 03:56:12 +02003309 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003310
Johannes Berg5727ef12007-12-19 02:03:34 +01003311 if (params.vlan)
3312 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003313 return err;
3314}
3315
3316static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3317{
Johannes Berg4c476992010-10-04 21:36:35 +02003318 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3319 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003320 u8 *mac_addr = NULL;
3321
3322 if (info->attrs[NL80211_ATTR_MAC])
3323 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3324
Johannes Berge80cf852009-05-11 14:43:13 +02003325 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003326 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003327 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003328 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3329 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003330
Johannes Berg4c476992010-10-04 21:36:35 +02003331 if (!rdev->ops->del_station)
3332 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003333
Johannes Berg4c476992010-10-04 21:36:35 +02003334 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003335}
3336
Eric W. Biederman15e47302012-09-07 20:12:54 +00003337static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003338 int flags, struct net_device *dev,
3339 u8 *dst, u8 *next_hop,
3340 struct mpath_info *pinfo)
3341{
3342 void *hdr;
3343 struct nlattr *pinfoattr;
3344
Eric W. Biederman15e47302012-09-07 20:12:54 +00003345 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003346 if (!hdr)
3347 return -1;
3348
David S. Miller9360ffd2012-03-29 04:41:26 -04003349 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3350 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3351 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3352 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3353 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003354
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003355 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3356 if (!pinfoattr)
3357 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003358 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3359 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3360 pinfo->frame_qlen))
3361 goto nla_put_failure;
3362 if (((pinfo->filled & MPATH_INFO_SN) &&
3363 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3364 ((pinfo->filled & MPATH_INFO_METRIC) &&
3365 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3366 pinfo->metric)) ||
3367 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3368 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3369 pinfo->exptime)) ||
3370 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3371 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3372 pinfo->flags)) ||
3373 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3374 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3375 pinfo->discovery_timeout)) ||
3376 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3377 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3378 pinfo->discovery_retries)))
3379 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003380
3381 nla_nest_end(msg, pinfoattr);
3382
3383 return genlmsg_end(msg, hdr);
3384
3385 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003386 genlmsg_cancel(msg, hdr);
3387 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003388}
3389
3390static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003391 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003392{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003393 struct mpath_info pinfo;
3394 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003395 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003396 u8 dst[ETH_ALEN];
3397 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003398 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003399 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003400
Johannes Berg67748892010-10-04 21:14:06 +02003401 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3402 if (err)
3403 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003404
3405 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003406 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003407 goto out_err;
3408 }
3409
Jouni Malineneec60b02009-03-20 21:21:19 +02003410 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3411 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003412 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003413 }
3414
Johannes Bergbba95fe2008-07-29 13:22:51 +02003415 while (1) {
3416 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3417 dst, next_hop, &pinfo);
3418 if (err == -ENOENT)
3419 break;
3420 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003421 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003422
Eric W. Biederman15e47302012-09-07 20:12:54 +00003423 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003424 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3425 netdev, dst, next_hop,
3426 &pinfo) < 0)
3427 goto out;
3428
3429 path_idx++;
3430 }
3431
3432
3433 out:
3434 cb->args[1] = path_idx;
3435 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003436 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003437 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003438 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003439}
3440
3441static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3442{
Johannes Berg4c476992010-10-04 21:36:35 +02003443 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003444 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003445 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003446 struct mpath_info pinfo;
3447 struct sk_buff *msg;
3448 u8 *dst = NULL;
3449 u8 next_hop[ETH_ALEN];
3450
3451 memset(&pinfo, 0, sizeof(pinfo));
3452
3453 if (!info->attrs[NL80211_ATTR_MAC])
3454 return -EINVAL;
3455
3456 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3457
Johannes Berg4c476992010-10-04 21:36:35 +02003458 if (!rdev->ops->get_mpath)
3459 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003460
Johannes Berg4c476992010-10-04 21:36:35 +02003461 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3462 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003463
Johannes Berg79c97e92009-07-07 03:56:12 +02003464 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003465 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003466 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003467
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003468 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003469 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003470 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003471
Eric W. Biederman15e47302012-09-07 20:12:54 +00003472 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003473 dev, dst, next_hop, &pinfo) < 0) {
3474 nlmsg_free(msg);
3475 return -ENOBUFS;
3476 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003477
Johannes Berg4c476992010-10-04 21:36:35 +02003478 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003479}
3480
3481static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3482{
Johannes Berg4c476992010-10-04 21:36:35 +02003483 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3484 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003485 u8 *dst = NULL;
3486 u8 *next_hop = NULL;
3487
3488 if (!info->attrs[NL80211_ATTR_MAC])
3489 return -EINVAL;
3490
3491 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3492 return -EINVAL;
3493
3494 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3495 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3496
Johannes Berg4c476992010-10-04 21:36:35 +02003497 if (!rdev->ops->change_mpath)
3498 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003499
Johannes Berg4c476992010-10-04 21:36:35 +02003500 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3501 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003502
Johannes Berg4c476992010-10-04 21:36:35 +02003503 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003504}
Johannes Berg4c476992010-10-04 21:36:35 +02003505
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003506static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3507{
Johannes Berg4c476992010-10-04 21:36:35 +02003508 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3509 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003510 u8 *dst = NULL;
3511 u8 *next_hop = NULL;
3512
3513 if (!info->attrs[NL80211_ATTR_MAC])
3514 return -EINVAL;
3515
3516 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3517 return -EINVAL;
3518
3519 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3520 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3521
Johannes Berg4c476992010-10-04 21:36:35 +02003522 if (!rdev->ops->add_mpath)
3523 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003524
Johannes Berg4c476992010-10-04 21:36:35 +02003525 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3526 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003527
Johannes Berg4c476992010-10-04 21:36:35 +02003528 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003529}
3530
3531static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3532{
Johannes Berg4c476992010-10-04 21:36:35 +02003533 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3534 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003535 u8 *dst = NULL;
3536
3537 if (info->attrs[NL80211_ATTR_MAC])
3538 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3539
Johannes Berg4c476992010-10-04 21:36:35 +02003540 if (!rdev->ops->del_mpath)
3541 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003542
Johannes Berg4c476992010-10-04 21:36:35 +02003543 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003544}
3545
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003546static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3547{
Johannes Berg4c476992010-10-04 21:36:35 +02003548 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3549 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003550 struct bss_parameters params;
3551
3552 memset(&params, 0, sizeof(params));
3553 /* default to not changing parameters */
3554 params.use_cts_prot = -1;
3555 params.use_short_preamble = -1;
3556 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003557 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003558 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003559
3560 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3561 params.use_cts_prot =
3562 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3563 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3564 params.use_short_preamble =
3565 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3566 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3567 params.use_short_slot_time =
3568 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003569 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3570 params.basic_rates =
3571 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3572 params.basic_rates_len =
3573 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3574 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003575 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3576 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003577 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3578 params.ht_opmode =
3579 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003580
Johannes Berg4c476992010-10-04 21:36:35 +02003581 if (!rdev->ops->change_bss)
3582 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003583
Johannes Berg074ac8d2010-09-16 14:58:22 +02003584 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003585 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3586 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003587
Johannes Berg4c476992010-10-04 21:36:35 +02003588 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003589}
3590
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003591static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003592 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3593 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3594 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3595 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3596 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3597 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3598};
3599
3600static int parse_reg_rule(struct nlattr *tb[],
3601 struct ieee80211_reg_rule *reg_rule)
3602{
3603 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3604 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3605
3606 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3607 return -EINVAL;
3608 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3609 return -EINVAL;
3610 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3611 return -EINVAL;
3612 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3613 return -EINVAL;
3614 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3615 return -EINVAL;
3616
3617 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3618
3619 freq_range->start_freq_khz =
3620 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3621 freq_range->end_freq_khz =
3622 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3623 freq_range->max_bandwidth_khz =
3624 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3625
3626 power_rule->max_eirp =
3627 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3628
3629 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3630 power_rule->max_antenna_gain =
3631 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3632
3633 return 0;
3634}
3635
3636static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3637{
3638 int r;
3639 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003640 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003641
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003642 /*
3643 * You should only get this when cfg80211 hasn't yet initialized
3644 * completely when built-in to the kernel right between the time
3645 * window between nl80211_init() and regulatory_init(), if that is
3646 * even possible.
3647 */
3648 mutex_lock(&cfg80211_mutex);
3649 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003650 mutex_unlock(&cfg80211_mutex);
3651 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003652 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003653 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003654
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003655 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3656 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003657
3658 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3659
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003660 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3661 user_reg_hint_type =
3662 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3663 else
3664 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3665
3666 switch (user_reg_hint_type) {
3667 case NL80211_USER_REG_HINT_USER:
3668 case NL80211_USER_REG_HINT_CELL_BASE:
3669 break;
3670 default:
3671 return -EINVAL;
3672 }
3673
3674 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003675
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003676 return r;
3677}
3678
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003679static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003680 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003681{
Johannes Berg4c476992010-10-04 21:36:35 +02003682 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003683 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003684 struct wireless_dev *wdev = dev->ieee80211_ptr;
3685 struct mesh_config cur_params;
3686 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003687 void *hdr;
3688 struct nlattr *pinfoattr;
3689 struct sk_buff *msg;
3690
Johannes Berg29cbe682010-12-03 09:20:44 +01003691 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3692 return -EOPNOTSUPP;
3693
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003694 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003695 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003696
Johannes Berg29cbe682010-12-03 09:20:44 +01003697 wdev_lock(wdev);
3698 /* If not connected, get default parameters */
3699 if (!wdev->mesh_id_len)
3700 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3701 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003702 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003703 &cur_params);
3704 wdev_unlock(wdev);
3705
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003706 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003707 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003708
3709 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003710 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003711 if (!msg)
3712 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003713 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003714 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003715 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003716 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003717 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003718 if (!pinfoattr)
3719 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003720 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3721 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3722 cur_params.dot11MeshRetryTimeout) ||
3723 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3724 cur_params.dot11MeshConfirmTimeout) ||
3725 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3726 cur_params.dot11MeshHoldingTimeout) ||
3727 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3728 cur_params.dot11MeshMaxPeerLinks) ||
3729 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3730 cur_params.dot11MeshMaxRetries) ||
3731 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3732 cur_params.dot11MeshTTL) ||
3733 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3734 cur_params.element_ttl) ||
3735 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3736 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003737 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3738 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003739 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3740 cur_params.dot11MeshHWMPmaxPREQretries) ||
3741 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3742 cur_params.path_refresh_time) ||
3743 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3744 cur_params.min_discovery_timeout) ||
3745 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3746 cur_params.dot11MeshHWMPactivePathTimeout) ||
3747 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3748 cur_params.dot11MeshHWMPpreqMinInterval) ||
3749 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3750 cur_params.dot11MeshHWMPperrMinInterval) ||
3751 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3752 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3753 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3754 cur_params.dot11MeshHWMPRootMode) ||
3755 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3756 cur_params.dot11MeshHWMPRannInterval) ||
3757 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3758 cur_params.dot11MeshGateAnnouncementProtocol) ||
3759 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3760 cur_params.dot11MeshForwarding) ||
3761 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003762 cur_params.rssi_threshold) ||
3763 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003764 cur_params.ht_opmode) ||
3765 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3766 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3767 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003768 cur_params.dot11MeshHWMProotInterval) ||
3769 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3770 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003771 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003772 nla_nest_end(msg, pinfoattr);
3773 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003774 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003775
Johannes Berg3b858752009-03-12 09:55:09 +01003776 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003777 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003778 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003779 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003780 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003781}
3782
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003783static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003784 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3785 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3786 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3787 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3788 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3789 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003790 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003791 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003792 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003793 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3794 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3795 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3796 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3797 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003798 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003799 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003800 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003801 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003802 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003803 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003804 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3805 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003806 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3807 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003808 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003809};
3810
Javier Cardonac80d5452010-12-16 17:37:49 -08003811static const struct nla_policy
3812 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003813 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003814 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3815 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003816 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003817 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003818 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003819 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003820};
3821
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003822static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003823 struct mesh_config *cfg,
3824 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003825{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003826 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003827 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003828
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003829#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3830do {\
3831 if (table[attr_num]) {\
3832 cfg->param = nla_fn(table[attr_num]); \
3833 mask |= (1 << (attr_num - 1)); \
3834 } \
3835} while (0);\
3836
3837
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003838 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003839 return -EINVAL;
3840 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003841 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003842 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003843 return -EINVAL;
3844
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003845 /* This makes sure that there aren't more than 32 mesh config
3846 * parameters (otherwise our bitfield scheme would not work.) */
3847 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3848
3849 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003850 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003851 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3852 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003853 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003854 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3855 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003856 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003857 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3858 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003859 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003860 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3861 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003862 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003863 mask, NL80211_MESHCONF_MAX_RETRIES,
3864 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003865 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003866 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003867 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003868 mask, NL80211_MESHCONF_ELEMENT_TTL,
3869 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003870 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003871 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3872 nla_get_u8);
3873 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3874 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3875 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003876 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003877 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3878 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003879 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003880 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3881 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003882 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003883 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3884 nla_get_u16);
3885 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3886 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3887 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003888 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003889 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3890 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003891 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003892 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3893 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003894 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003895 dot11MeshHWMPnetDiameterTraversalTime, mask,
3896 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3897 nla_get_u16);
3898 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3899 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3900 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3901 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3902 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003903 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003904 dot11MeshGateAnnouncementProtocol, mask,
3905 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3906 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003907 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003908 mask, NL80211_MESHCONF_FORWARDING,
3909 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003910 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003911 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3912 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003914 mask, NL80211_MESHCONF_HT_OPMODE,
3915 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3917 mask,
3918 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3919 nla_get_u32);
3920 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3921 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3922 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003923 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3924 dot11MeshHWMPconfirmationInterval, mask,
3925 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3926 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003927 if (mask_out)
3928 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003929
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003930 return 0;
3931
3932#undef FILL_IN_MESH_PARAM_IF_SET
3933}
3934
Javier Cardonac80d5452010-12-16 17:37:49 -08003935static int nl80211_parse_mesh_setup(struct genl_info *info,
3936 struct mesh_setup *setup)
3937{
3938 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3939
3940 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3941 return -EINVAL;
3942 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3943 info->attrs[NL80211_ATTR_MESH_SETUP],
3944 nl80211_mesh_setup_params_policy))
3945 return -EINVAL;
3946
Javier Cardonad299a1f2012-03-31 11:31:33 -07003947 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3948 setup->sync_method =
3949 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3950 IEEE80211_SYNC_METHOD_VENDOR :
3951 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3952
Javier Cardonac80d5452010-12-16 17:37:49 -08003953 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3954 setup->path_sel_proto =
3955 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3956 IEEE80211_PATH_PROTOCOL_VENDOR :
3957 IEEE80211_PATH_PROTOCOL_HWMP;
3958
3959 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3960 setup->path_metric =
3961 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3962 IEEE80211_PATH_METRIC_VENDOR :
3963 IEEE80211_PATH_METRIC_AIRTIME;
3964
Javier Cardona581a8b02011-04-07 15:08:27 -07003965
3966 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003967 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003968 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003969 if (!is_valid_ie_attr(ieattr))
3970 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003971 setup->ie = nla_data(ieattr);
3972 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003973 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003974 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3975 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003976
3977 return 0;
3978}
3979
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003980static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003981 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003982{
3983 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3984 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003985 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003986 struct mesh_config cfg;
3987 u32 mask;
3988 int err;
3989
Johannes Berg29cbe682010-12-03 09:20:44 +01003990 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3991 return -EOPNOTSUPP;
3992
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003993 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003994 return -EOPNOTSUPP;
3995
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003996 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003997 if (err)
3998 return err;
3999
Johannes Berg29cbe682010-12-03 09:20:44 +01004000 wdev_lock(wdev);
4001 if (!wdev->mesh_id_len)
4002 err = -ENOLINK;
4003
4004 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004005 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01004006 mask, &cfg);
4007
4008 wdev_unlock(wdev);
4009
4010 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004011}
4012
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004013static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4014{
4015 struct sk_buff *msg;
4016 void *hdr = NULL;
4017 struct nlattr *nl_reg_rules;
4018 unsigned int i;
4019 int err = -EINVAL;
4020
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004021 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004022
4023 if (!cfg80211_regdomain)
4024 goto out;
4025
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004026 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004027 if (!msg) {
4028 err = -ENOBUFS;
4029 goto out;
4030 }
4031
Eric W. Biederman15e47302012-09-07 20:12:54 +00004032 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004033 NL80211_CMD_GET_REG);
4034 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004035 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004036
David S. Miller9360ffd2012-03-29 04:41:26 -04004037 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
4038 cfg80211_regdomain->alpha2) ||
4039 (cfg80211_regdomain->dfs_region &&
4040 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
4041 cfg80211_regdomain->dfs_region)))
4042 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004043
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004044 if (reg_last_request_cell_base() &&
4045 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4046 NL80211_USER_REG_HINT_CELL_BASE))
4047 goto nla_put_failure;
4048
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004049 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4050 if (!nl_reg_rules)
4051 goto nla_put_failure;
4052
4053 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
4054 struct nlattr *nl_reg_rule;
4055 const struct ieee80211_reg_rule *reg_rule;
4056 const struct ieee80211_freq_range *freq_range;
4057 const struct ieee80211_power_rule *power_rule;
4058
4059 reg_rule = &cfg80211_regdomain->reg_rules[i];
4060 freq_range = &reg_rule->freq_range;
4061 power_rule = &reg_rule->power_rule;
4062
4063 nl_reg_rule = nla_nest_start(msg, i);
4064 if (!nl_reg_rule)
4065 goto nla_put_failure;
4066
David S. Miller9360ffd2012-03-29 04:41:26 -04004067 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4068 reg_rule->flags) ||
4069 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4070 freq_range->start_freq_khz) ||
4071 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4072 freq_range->end_freq_khz) ||
4073 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4074 freq_range->max_bandwidth_khz) ||
4075 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4076 power_rule->max_antenna_gain) ||
4077 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4078 power_rule->max_eirp))
4079 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004080
4081 nla_nest_end(msg, nl_reg_rule);
4082 }
4083
4084 nla_nest_end(msg, nl_reg_rules);
4085
4086 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004087 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004088 goto out;
4089
4090nla_put_failure:
4091 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004092put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004093 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004094 err = -EMSGSIZE;
4095out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004096 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004097 return err;
4098}
4099
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004100static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4101{
4102 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4103 struct nlattr *nl_reg_rule;
4104 char *alpha2 = NULL;
4105 int rem_reg_rules = 0, r = 0;
4106 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004107 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004108 struct ieee80211_regdomain *rd = NULL;
4109
4110 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4111 return -EINVAL;
4112
4113 if (!info->attrs[NL80211_ATTR_REG_RULES])
4114 return -EINVAL;
4115
4116 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4117
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004118 if (info->attrs[NL80211_ATTR_DFS_REGION])
4119 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4120
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004121 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4122 rem_reg_rules) {
4123 num_rules++;
4124 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004125 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004126 }
4127
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004128 mutex_lock(&cfg80211_mutex);
4129
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004130 if (!reg_is_valid_request(alpha2)) {
4131 r = -EINVAL;
4132 goto bad_reg;
4133 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004134
4135 size_of_regd = sizeof(struct ieee80211_regdomain) +
4136 (num_rules * sizeof(struct ieee80211_reg_rule));
4137
4138 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004139 if (!rd) {
4140 r = -ENOMEM;
4141 goto bad_reg;
4142 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004143
4144 rd->n_reg_rules = num_rules;
4145 rd->alpha2[0] = alpha2[0];
4146 rd->alpha2[1] = alpha2[1];
4147
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004148 /*
4149 * Disable DFS master mode if the DFS region was
4150 * not supported or known on this kernel.
4151 */
4152 if (reg_supported_dfs_region(dfs_region))
4153 rd->dfs_region = dfs_region;
4154
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004155 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4156 rem_reg_rules) {
4157 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4158 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4159 reg_rule_policy);
4160 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4161 if (r)
4162 goto bad_reg;
4163
4164 rule_idx++;
4165
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004166 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4167 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004168 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004169 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004170 }
4171
4172 BUG_ON(rule_idx != num_rules);
4173
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004174 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004175
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004176 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004177
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004178 return r;
4179
Johannes Bergd2372b32008-10-24 20:32:20 +02004180 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004181 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004182 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004183 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004184}
4185
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004186static int validate_scan_freqs(struct nlattr *freqs)
4187{
4188 struct nlattr *attr1, *attr2;
4189 int n_channels = 0, tmp1, tmp2;
4190
4191 nla_for_each_nested(attr1, freqs, tmp1) {
4192 n_channels++;
4193 /*
4194 * Some hardware has a limited channel list for
4195 * scanning, and it is pretty much nonsensical
4196 * to scan for a channel twice, so disallow that
4197 * and don't require drivers to check that the
4198 * channel list they get isn't longer than what
4199 * they can scan, as long as they can scan all
4200 * the channels they registered at once.
4201 */
4202 nla_for_each_nested(attr2, freqs, tmp2)
4203 if (attr1 != attr2 &&
4204 nla_get_u32(attr1) == nla_get_u32(attr2))
4205 return 0;
4206 }
4207
4208 return n_channels;
4209}
4210
Johannes Berg2a519312009-02-10 21:25:55 +01004211static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4212{
Johannes Berg4c476992010-10-04 21:36:35 +02004213 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004214 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004215 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004216 struct nlattr *attr;
4217 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004218 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004219 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004220
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004221 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4222 return -EINVAL;
4223
Johannes Berg79c97e92009-07-07 03:56:12 +02004224 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004225
Johannes Berg4c476992010-10-04 21:36:35 +02004226 if (!rdev->ops->scan)
4227 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004228
Johannes Berg4c476992010-10-04 21:36:35 +02004229 if (rdev->scan_req)
4230 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004231
4232 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004233 n_channels = validate_scan_freqs(
4234 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004235 if (!n_channels)
4236 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004237 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004238 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004239 n_channels = 0;
4240
Johannes Berg2a519312009-02-10 21:25:55 +01004241 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4242 if (wiphy->bands[band])
4243 n_channels += wiphy->bands[band]->n_channels;
4244 }
4245
4246 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4247 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4248 n_ssids++;
4249
Johannes Berg4c476992010-10-04 21:36:35 +02004250 if (n_ssids > wiphy->max_scan_ssids)
4251 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004252
Jouni Malinen70692ad2009-02-16 19:39:13 +02004253 if (info->attrs[NL80211_ATTR_IE])
4254 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4255 else
4256 ie_len = 0;
4257
Johannes Berg4c476992010-10-04 21:36:35 +02004258 if (ie_len > wiphy->max_scan_ie_len)
4259 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004260
Johannes Berg2a519312009-02-10 21:25:55 +01004261 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004262 + sizeof(*request->ssids) * n_ssids
4263 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004264 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004265 if (!request)
4266 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004267
Johannes Berg2a519312009-02-10 21:25:55 +01004268 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004269 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004270 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004271 if (ie_len) {
4272 if (request->ssids)
4273 request->ie = (void *)(request->ssids + n_ssids);
4274 else
4275 request->ie = (void *)(request->channels + n_channels);
4276 }
Johannes Berg2a519312009-02-10 21:25:55 +01004277
Johannes Berg584991d2009-11-02 13:32:03 +01004278 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004279 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4280 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004281 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004282 struct ieee80211_channel *chan;
4283
4284 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4285
4286 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004287 err = -EINVAL;
4288 goto out_free;
4289 }
Johannes Berg584991d2009-11-02 13:32:03 +01004290
4291 /* ignore disabled channels */
4292 if (chan->flags & IEEE80211_CHAN_DISABLED)
4293 continue;
4294
4295 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004296 i++;
4297 }
4298 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004299 enum ieee80211_band band;
4300
Johannes Berg2a519312009-02-10 21:25:55 +01004301 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004302 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4303 int j;
4304 if (!wiphy->bands[band])
4305 continue;
4306 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004307 struct ieee80211_channel *chan;
4308
4309 chan = &wiphy->bands[band]->channels[j];
4310
4311 if (chan->flags & IEEE80211_CHAN_DISABLED)
4312 continue;
4313
4314 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004315 i++;
4316 }
4317 }
4318 }
4319
Johannes Berg584991d2009-11-02 13:32:03 +01004320 if (!i) {
4321 err = -EINVAL;
4322 goto out_free;
4323 }
4324
4325 request->n_channels = i;
4326
Johannes Berg2a519312009-02-10 21:25:55 +01004327 i = 0;
4328 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4329 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004330 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004331 err = -EINVAL;
4332 goto out_free;
4333 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004334 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004335 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004336 i++;
4337 }
4338 }
4339
Jouni Malinen70692ad2009-02-16 19:39:13 +02004340 if (info->attrs[NL80211_ATTR_IE]) {
4341 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004342 memcpy((void *)request->ie,
4343 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004344 request->ie_len);
4345 }
4346
Johannes Berg34850ab2011-07-18 18:08:35 +02004347 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004348 if (wiphy->bands[i])
4349 request->rates[i] =
4350 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004351
4352 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4353 nla_for_each_nested(attr,
4354 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4355 tmp) {
4356 enum ieee80211_band band = nla_type(attr);
4357
Dan Carpenter84404622011-07-29 11:52:18 +03004358 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004359 err = -EINVAL;
4360 goto out_free;
4361 }
4362 err = ieee80211_get_ratemask(wiphy->bands[band],
4363 nla_data(attr),
4364 nla_len(attr),
4365 &request->rates[band]);
4366 if (err)
4367 goto out_free;
4368 }
4369 }
4370
Sam Leffler46856bb2012-10-11 21:03:32 -07004371 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004372 request->flags = nla_get_u32(
4373 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004374 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4375 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4376 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4377 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004378 err = -EOPNOTSUPP;
4379 goto out_free;
4380 }
4381 }
Sam Lefflered4737712012-10-11 21:03:31 -07004382
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304383 request->no_cck =
4384 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4385
Johannes Bergfd014282012-06-18 19:17:03 +02004386 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004387 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004388 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004389
Johannes Berg79c97e92009-07-07 03:56:12 +02004390 rdev->scan_req = request;
Johannes Bergfd014282012-06-18 19:17:03 +02004391 err = rdev->ops->scan(&rdev->wiphy, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004392
Johannes Berg463d0182009-07-14 00:33:35 +02004393 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004394 nl80211_send_scan_start(rdev, wdev);
4395 if (wdev->netdev)
4396 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004397 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004398 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004399 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004400 kfree(request);
4401 }
Johannes Berg3b858752009-03-12 09:55:09 +01004402
Johannes Berg2a519312009-02-10 21:25:55 +01004403 return err;
4404}
4405
Luciano Coelho807f8a82011-05-11 17:09:35 +03004406static int nl80211_start_sched_scan(struct sk_buff *skb,
4407 struct genl_info *info)
4408{
4409 struct cfg80211_sched_scan_request *request;
4410 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4411 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004412 struct nlattr *attr;
4413 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004414 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004415 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004416 enum ieee80211_band band;
4417 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004418 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004419
4420 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4421 !rdev->ops->sched_scan_start)
4422 return -EOPNOTSUPP;
4423
4424 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4425 return -EINVAL;
4426
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004427 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4428 return -EINVAL;
4429
4430 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4431 if (interval == 0)
4432 return -EINVAL;
4433
Luciano Coelho807f8a82011-05-11 17:09:35 +03004434 wiphy = &rdev->wiphy;
4435
4436 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4437 n_channels = validate_scan_freqs(
4438 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4439 if (!n_channels)
4440 return -EINVAL;
4441 } else {
4442 n_channels = 0;
4443
4444 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4445 if (wiphy->bands[band])
4446 n_channels += wiphy->bands[band]->n_channels;
4447 }
4448
4449 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4450 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4451 tmp)
4452 n_ssids++;
4453
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004454 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004455 return -EINVAL;
4456
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004457 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4458 nla_for_each_nested(attr,
4459 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4460 tmp)
4461 n_match_sets++;
4462
4463 if (n_match_sets > wiphy->max_match_sets)
4464 return -EINVAL;
4465
Luciano Coelho807f8a82011-05-11 17:09:35 +03004466 if (info->attrs[NL80211_ATTR_IE])
4467 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4468 else
4469 ie_len = 0;
4470
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004471 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004472 return -EINVAL;
4473
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004474 mutex_lock(&rdev->sched_scan_mtx);
4475
4476 if (rdev->sched_scan_req) {
4477 err = -EINPROGRESS;
4478 goto out;
4479 }
4480
Luciano Coelho807f8a82011-05-11 17:09:35 +03004481 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004482 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004483 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004484 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004485 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004486 if (!request) {
4487 err = -ENOMEM;
4488 goto out;
4489 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004490
4491 if (n_ssids)
4492 request->ssids = (void *)&request->channels[n_channels];
4493 request->n_ssids = n_ssids;
4494 if (ie_len) {
4495 if (request->ssids)
4496 request->ie = (void *)(request->ssids + n_ssids);
4497 else
4498 request->ie = (void *)(request->channels + n_channels);
4499 }
4500
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004501 if (n_match_sets) {
4502 if (request->ie)
4503 request->match_sets = (void *)(request->ie + ie_len);
4504 else if (request->ssids)
4505 request->match_sets =
4506 (void *)(request->ssids + n_ssids);
4507 else
4508 request->match_sets =
4509 (void *)(request->channels + n_channels);
4510 }
4511 request->n_match_sets = n_match_sets;
4512
Luciano Coelho807f8a82011-05-11 17:09:35 +03004513 i = 0;
4514 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4515 /* user specified, bail out if channel not found */
4516 nla_for_each_nested(attr,
4517 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4518 tmp) {
4519 struct ieee80211_channel *chan;
4520
4521 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4522
4523 if (!chan) {
4524 err = -EINVAL;
4525 goto out_free;
4526 }
4527
4528 /* ignore disabled channels */
4529 if (chan->flags & IEEE80211_CHAN_DISABLED)
4530 continue;
4531
4532 request->channels[i] = chan;
4533 i++;
4534 }
4535 } else {
4536 /* all channels */
4537 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4538 int j;
4539 if (!wiphy->bands[band])
4540 continue;
4541 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4542 struct ieee80211_channel *chan;
4543
4544 chan = &wiphy->bands[band]->channels[j];
4545
4546 if (chan->flags & IEEE80211_CHAN_DISABLED)
4547 continue;
4548
4549 request->channels[i] = chan;
4550 i++;
4551 }
4552 }
4553 }
4554
4555 if (!i) {
4556 err = -EINVAL;
4557 goto out_free;
4558 }
4559
4560 request->n_channels = i;
4561
4562 i = 0;
4563 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4564 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4565 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004566 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004567 err = -EINVAL;
4568 goto out_free;
4569 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004570 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004571 memcpy(request->ssids[i].ssid, nla_data(attr),
4572 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004573 i++;
4574 }
4575 }
4576
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004577 i = 0;
4578 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4579 nla_for_each_nested(attr,
4580 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4581 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004582 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004583
4584 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4585 nla_data(attr), nla_len(attr),
4586 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004587 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004588 if (ssid) {
4589 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4590 err = -EINVAL;
4591 goto out_free;
4592 }
4593 memcpy(request->match_sets[i].ssid.ssid,
4594 nla_data(ssid), nla_len(ssid));
4595 request->match_sets[i].ssid.ssid_len =
4596 nla_len(ssid);
4597 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004598 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4599 if (rssi)
4600 request->rssi_thold = nla_get_u32(rssi);
4601 else
4602 request->rssi_thold =
4603 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004604 i++;
4605 }
4606 }
4607
Luciano Coelho807f8a82011-05-11 17:09:35 +03004608 if (info->attrs[NL80211_ATTR_IE]) {
4609 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4610 memcpy((void *)request->ie,
4611 nla_data(info->attrs[NL80211_ATTR_IE]),
4612 request->ie_len);
4613 }
4614
Sam Leffler46856bb2012-10-11 21:03:32 -07004615 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004616 request->flags = nla_get_u32(
4617 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004618 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4619 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4620 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4621 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004622 err = -EOPNOTSUPP;
4623 goto out_free;
4624 }
4625 }
Sam Lefflered4737712012-10-11 21:03:31 -07004626
Luciano Coelho807f8a82011-05-11 17:09:35 +03004627 request->dev = dev;
4628 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004629 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004630 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004631
4632 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4633 if (!err) {
4634 rdev->sched_scan_req = request;
4635 nl80211_send_sched_scan(rdev, dev,
4636 NL80211_CMD_START_SCHED_SCAN);
4637 goto out;
4638 }
4639
4640out_free:
4641 kfree(request);
4642out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004643 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004644 return err;
4645}
4646
4647static int nl80211_stop_sched_scan(struct sk_buff *skb,
4648 struct genl_info *info)
4649{
4650 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004651 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004652
4653 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4654 !rdev->ops->sched_scan_stop)
4655 return -EOPNOTSUPP;
4656
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004657 mutex_lock(&rdev->sched_scan_mtx);
4658 err = __cfg80211_stop_sched_scan(rdev, false);
4659 mutex_unlock(&rdev->sched_scan_mtx);
4660
4661 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004662}
4663
Johannes Berg9720bb32011-06-21 09:45:33 +02004664static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4665 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004666 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004667 struct wireless_dev *wdev,
4668 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004669{
Johannes Berg48ab9052009-07-10 18:42:31 +02004670 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004671 void *hdr;
4672 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004673
4674 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004675
Eric W. Biederman15e47302012-09-07 20:12:54 +00004676 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004677 NL80211_CMD_NEW_SCAN_RESULTS);
4678 if (!hdr)
4679 return -1;
4680
Johannes Berg9720bb32011-06-21 09:45:33 +02004681 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4682
David S. Miller9360ffd2012-03-29 04:41:26 -04004683 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4684 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4685 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004686
4687 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4688 if (!bss)
4689 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004690 if ((!is_zero_ether_addr(res->bssid) &&
4691 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4692 (res->information_elements && res->len_information_elements &&
4693 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4694 res->len_information_elements,
4695 res->information_elements)) ||
4696 (res->beacon_ies && res->len_beacon_ies &&
4697 res->beacon_ies != res->information_elements &&
4698 nla_put(msg, NL80211_BSS_BEACON_IES,
4699 res->len_beacon_ies, res->beacon_ies)))
4700 goto nla_put_failure;
4701 if (res->tsf &&
4702 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4703 goto nla_put_failure;
4704 if (res->beacon_interval &&
4705 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4706 goto nla_put_failure;
4707 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4708 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4709 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4710 jiffies_to_msecs(jiffies - intbss->ts)))
4711 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004712
Johannes Berg77965c92009-02-18 18:45:06 +01004713 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004714 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004715 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4716 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004717 break;
4718 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004719 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4720 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004721 break;
4722 default:
4723 break;
4724 }
4725
Johannes Berg48ab9052009-07-10 18:42:31 +02004726 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004727 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004728 case NL80211_IFTYPE_STATION:
David S. Miller9360ffd2012-03-29 04:41:26 -04004729 if (intbss == wdev->current_bss &&
4730 nla_put_u32(msg, NL80211_BSS_STATUS,
4731 NL80211_BSS_STATUS_ASSOCIATED))
4732 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004733 break;
4734 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004735 if (intbss == wdev->current_bss &&
4736 nla_put_u32(msg, NL80211_BSS_STATUS,
4737 NL80211_BSS_STATUS_IBSS_JOINED))
4738 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004739 break;
4740 default:
4741 break;
4742 }
4743
Johannes Berg2a519312009-02-10 21:25:55 +01004744 nla_nest_end(msg, bss);
4745
4746 return genlmsg_end(msg, hdr);
4747
4748 nla_put_failure:
4749 genlmsg_cancel(msg, hdr);
4750 return -EMSGSIZE;
4751}
4752
4753static int nl80211_dump_scan(struct sk_buff *skb,
4754 struct netlink_callback *cb)
4755{
Johannes Berg48ab9052009-07-10 18:42:31 +02004756 struct cfg80211_registered_device *rdev;
4757 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004758 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004759 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004760 int start = cb->args[1], idx = 0;
4761 int err;
4762
Johannes Berg67748892010-10-04 21:14:06 +02004763 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4764 if (err)
4765 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004766
Johannes Berg48ab9052009-07-10 18:42:31 +02004767 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004768
Johannes Berg48ab9052009-07-10 18:42:31 +02004769 wdev_lock(wdev);
4770 spin_lock_bh(&rdev->bss_lock);
4771 cfg80211_bss_expire(rdev);
4772
Johannes Berg9720bb32011-06-21 09:45:33 +02004773 cb->seq = rdev->bss_generation;
4774
Johannes Berg48ab9052009-07-10 18:42:31 +02004775 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004776 if (++idx <= start)
4777 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004778 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004779 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004780 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004781 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004782 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004783 }
4784 }
4785
Johannes Berg48ab9052009-07-10 18:42:31 +02004786 spin_unlock_bh(&rdev->bss_lock);
4787 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004788
4789 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004790 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004791
Johannes Berg67748892010-10-04 21:14:06 +02004792 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004793}
4794
Eric W. Biederman15e47302012-09-07 20:12:54 +00004795static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004796 int flags, struct net_device *dev,
4797 struct survey_info *survey)
4798{
4799 void *hdr;
4800 struct nlattr *infoattr;
4801
Eric W. Biederman15e47302012-09-07 20:12:54 +00004802 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004803 NL80211_CMD_NEW_SURVEY_RESULTS);
4804 if (!hdr)
4805 return -ENOMEM;
4806
David S. Miller9360ffd2012-03-29 04:41:26 -04004807 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4808 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004809
4810 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4811 if (!infoattr)
4812 goto nla_put_failure;
4813
David S. Miller9360ffd2012-03-29 04:41:26 -04004814 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4815 survey->channel->center_freq))
4816 goto nla_put_failure;
4817
4818 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4819 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4820 goto nla_put_failure;
4821 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4822 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4823 goto nla_put_failure;
4824 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4825 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4826 survey->channel_time))
4827 goto nla_put_failure;
4828 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4829 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4830 survey->channel_time_busy))
4831 goto nla_put_failure;
4832 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4833 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4834 survey->channel_time_ext_busy))
4835 goto nla_put_failure;
4836 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4837 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4838 survey->channel_time_rx))
4839 goto nla_put_failure;
4840 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4841 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4842 survey->channel_time_tx))
4843 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004844
4845 nla_nest_end(msg, infoattr);
4846
4847 return genlmsg_end(msg, hdr);
4848
4849 nla_put_failure:
4850 genlmsg_cancel(msg, hdr);
4851 return -EMSGSIZE;
4852}
4853
4854static int nl80211_dump_survey(struct sk_buff *skb,
4855 struct netlink_callback *cb)
4856{
4857 struct survey_info survey;
4858 struct cfg80211_registered_device *dev;
4859 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004860 int survey_idx = cb->args[1];
4861 int res;
4862
Johannes Berg67748892010-10-04 21:14:06 +02004863 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4864 if (res)
4865 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004866
4867 if (!dev->ops->dump_survey) {
4868 res = -EOPNOTSUPP;
4869 goto out_err;
4870 }
4871
4872 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004873 struct ieee80211_channel *chan;
4874
Holger Schurig61fa7132009-11-11 12:25:40 +01004875 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4876 &survey);
4877 if (res == -ENOENT)
4878 break;
4879 if (res)
4880 goto out_err;
4881
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004882 /* Survey without a channel doesn't make sense */
4883 if (!survey.channel) {
4884 res = -EINVAL;
4885 goto out;
4886 }
4887
4888 chan = ieee80211_get_channel(&dev->wiphy,
4889 survey.channel->center_freq);
4890 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4891 survey_idx++;
4892 continue;
4893 }
4894
Holger Schurig61fa7132009-11-11 12:25:40 +01004895 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004896 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01004897 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4898 netdev,
4899 &survey) < 0)
4900 goto out;
4901 survey_idx++;
4902 }
4903
4904 out:
4905 cb->args[1] = survey_idx;
4906 res = skb->len;
4907 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004908 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004909 return res;
4910}
4911
Samuel Ortizb23aa672009-07-01 21:26:54 +02004912static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4913{
4914 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4915 NL80211_WPA_VERSION_2));
4916}
4917
Jouni Malinen636a5d32009-03-19 13:39:22 +02004918static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4919{
Johannes Berg4c476992010-10-04 21:36:35 +02004920 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4921 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004922 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03004923 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
4924 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004925 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004926 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004927 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004928
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004929 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4930 return -EINVAL;
4931
4932 if (!info->attrs[NL80211_ATTR_MAC])
4933 return -EINVAL;
4934
Jouni Malinen17780922009-03-27 20:52:47 +02004935 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4936 return -EINVAL;
4937
Johannes Berg19957bb2009-07-02 17:20:43 +02004938 if (!info->attrs[NL80211_ATTR_SSID])
4939 return -EINVAL;
4940
4941 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4942 return -EINVAL;
4943
Johannes Bergfffd0932009-07-08 14:22:54 +02004944 err = nl80211_parse_key(info, &key);
4945 if (err)
4946 return err;
4947
4948 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004949 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4950 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004951 if (!key.p.key || !key.p.key_len)
4952 return -EINVAL;
4953 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4954 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4955 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4956 key.p.key_len != WLAN_KEY_LEN_WEP104))
4957 return -EINVAL;
4958 if (key.idx > 4)
4959 return -EINVAL;
4960 } else {
4961 key.p.key_len = 0;
4962 key.p.key = NULL;
4963 }
4964
Johannes Bergafea0b72010-08-10 09:46:42 +02004965 if (key.idx >= 0) {
4966 int i;
4967 bool ok = false;
4968 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4969 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4970 ok = true;
4971 break;
4972 }
4973 }
Johannes Berg4c476992010-10-04 21:36:35 +02004974 if (!ok)
4975 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004976 }
4977
Johannes Berg4c476992010-10-04 21:36:35 +02004978 if (!rdev->ops->auth)
4979 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004980
Johannes Berg074ac8d2010-09-16 14:58:22 +02004981 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004982 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4983 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004984
Johannes Berg19957bb2009-07-02 17:20:43 +02004985 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004986 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004987 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004988 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4989 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004990
Johannes Berg19957bb2009-07-02 17:20:43 +02004991 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4992 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4993
4994 if (info->attrs[NL80211_ATTR_IE]) {
4995 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4996 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4997 }
4998
4999 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005000 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005001 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005002
Jouni Malinene39e5b52012-09-30 19:29:39 +03005003 if (auth_type == NL80211_AUTHTYPE_SAE &&
5004 !info->attrs[NL80211_ATTR_SAE_DATA])
5005 return -EINVAL;
5006
5007 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5008 if (auth_type != NL80211_AUTHTYPE_SAE)
5009 return -EINVAL;
5010 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5011 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5012 /* need to include at least Auth Transaction and Status Code */
5013 if (sae_data_len < 4)
5014 return -EINVAL;
5015 }
5016
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005017 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5018
Johannes Berg95de8172012-01-20 13:55:25 +01005019 /*
5020 * Since we no longer track auth state, ignore
5021 * requests to only change local state.
5022 */
5023 if (local_state_change)
5024 return 0;
5025
Johannes Berg4c476992010-10-04 21:36:35 +02005026 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5027 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005028 key.p.key, key.p.key_len, key.idx,
5029 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005030}
5031
Johannes Bergc0692b82010-08-27 14:26:53 +03005032static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5033 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005034 struct cfg80211_crypto_settings *settings,
5035 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005036{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005037 memset(settings, 0, sizeof(*settings));
5038
Samuel Ortizb23aa672009-07-01 21:26:54 +02005039 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5040
Johannes Bergc0692b82010-08-27 14:26:53 +03005041 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5042 u16 proto;
5043 proto = nla_get_u16(
5044 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5045 settings->control_port_ethertype = cpu_to_be16(proto);
5046 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5047 proto != ETH_P_PAE)
5048 return -EINVAL;
5049 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5050 settings->control_port_no_encrypt = true;
5051 } else
5052 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5053
Samuel Ortizb23aa672009-07-01 21:26:54 +02005054 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5055 void *data;
5056 int len, i;
5057
5058 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5059 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5060 settings->n_ciphers_pairwise = len / sizeof(u32);
5061
5062 if (len % sizeof(u32))
5063 return -EINVAL;
5064
Johannes Berg3dc27d22009-07-02 21:36:37 +02005065 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005066 return -EINVAL;
5067
5068 memcpy(settings->ciphers_pairwise, data, len);
5069
5070 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005071 if (!cfg80211_supported_cipher_suite(
5072 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005073 settings->ciphers_pairwise[i]))
5074 return -EINVAL;
5075 }
5076
5077 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5078 settings->cipher_group =
5079 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005080 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5081 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005082 return -EINVAL;
5083 }
5084
5085 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5086 settings->wpa_versions =
5087 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5088 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5089 return -EINVAL;
5090 }
5091
5092 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5093 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005094 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005095
5096 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5097 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5098 settings->n_akm_suites = len / sizeof(u32);
5099
5100 if (len % sizeof(u32))
5101 return -EINVAL;
5102
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005103 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5104 return -EINVAL;
5105
Samuel Ortizb23aa672009-07-01 21:26:54 +02005106 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005107 }
5108
5109 return 0;
5110}
5111
Jouni Malinen636a5d32009-03-19 13:39:22 +02005112static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5113{
Johannes Berg4c476992010-10-04 21:36:35 +02005114 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5115 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005116 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005117 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005118 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005119 int err, ssid_len, ie_len = 0;
5120 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005121 u32 flags = 0;
5122 struct ieee80211_ht_cap *ht_capa = NULL;
5123 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005124
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005125 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5126 return -EINVAL;
5127
5128 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005129 !info->attrs[NL80211_ATTR_SSID] ||
5130 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005131 return -EINVAL;
5132
Johannes Berg4c476992010-10-04 21:36:35 +02005133 if (!rdev->ops->assoc)
5134 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005135
Johannes Berg074ac8d2010-09-16 14:58:22 +02005136 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005137 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5138 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005139
Johannes Berg19957bb2009-07-02 17:20:43 +02005140 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005141
Johannes Berg19957bb2009-07-02 17:20:43 +02005142 chan = ieee80211_get_channel(&rdev->wiphy,
5143 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005144 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5145 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005146
Johannes Berg19957bb2009-07-02 17:20:43 +02005147 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5148 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005149
5150 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005151 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5152 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005153 }
5154
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005155 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005156 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005157 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005158 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005159 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005160 else if (mfp != NL80211_MFP_NO)
5161 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005162 }
5163
Johannes Berg3e5d7642009-07-07 14:37:26 +02005164 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5165 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5166
Ben Greear7e7c8922011-11-18 11:31:59 -08005167 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5168 flags |= ASSOC_REQ_DISABLE_HT;
5169
5170 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5171 ht_capa_mask =
5172 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5173
5174 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5175 if (!ht_capa_mask)
5176 return -EINVAL;
5177 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5178 }
5179
Johannes Bergc0692b82010-08-27 14:26:53 +03005180 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005181 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005182 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5183 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005184 &crypto, flags, ht_capa,
5185 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005186
Jouni Malinen636a5d32009-03-19 13:39:22 +02005187 return err;
5188}
5189
5190static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5191{
Johannes Berg4c476992010-10-04 21:36:35 +02005192 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5193 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005194 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005195 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005196 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005197 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005198
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005199 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5200 return -EINVAL;
5201
5202 if (!info->attrs[NL80211_ATTR_MAC])
5203 return -EINVAL;
5204
5205 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5206 return -EINVAL;
5207
Johannes Berg4c476992010-10-04 21:36:35 +02005208 if (!rdev->ops->deauth)
5209 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005210
Johannes Berg074ac8d2010-09-16 14:58:22 +02005211 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005212 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5213 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005214
Johannes Berg19957bb2009-07-02 17:20:43 +02005215 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005216
Johannes Berg19957bb2009-07-02 17:20:43 +02005217 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5218 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005219 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005220 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005221 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005222
5223 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005224 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5225 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005226 }
5227
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005228 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5229
Johannes Berg4c476992010-10-04 21:36:35 +02005230 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5231 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005232}
5233
5234static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5235{
Johannes Berg4c476992010-10-04 21:36:35 +02005236 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5237 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005238 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005239 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005240 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005241 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005242
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005243 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5244 return -EINVAL;
5245
5246 if (!info->attrs[NL80211_ATTR_MAC])
5247 return -EINVAL;
5248
5249 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5250 return -EINVAL;
5251
Johannes Berg4c476992010-10-04 21:36:35 +02005252 if (!rdev->ops->disassoc)
5253 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005254
Johannes Berg074ac8d2010-09-16 14:58:22 +02005255 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005256 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5257 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005258
Johannes Berg19957bb2009-07-02 17:20:43 +02005259 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005260
Johannes Berg19957bb2009-07-02 17:20:43 +02005261 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5262 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005263 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005264 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005265 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005266
5267 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005268 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5269 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005270 }
5271
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005272 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5273
Johannes Berg4c476992010-10-04 21:36:35 +02005274 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5275 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005276}
5277
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005278static bool
5279nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5280 int mcast_rate[IEEE80211_NUM_BANDS],
5281 int rateval)
5282{
5283 struct wiphy *wiphy = &rdev->wiphy;
5284 bool found = false;
5285 int band, i;
5286
5287 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5288 struct ieee80211_supported_band *sband;
5289
5290 sband = wiphy->bands[band];
5291 if (!sband)
5292 continue;
5293
5294 for (i = 0; i < sband->n_bitrates; i++) {
5295 if (sband->bitrates[i].bitrate == rateval) {
5296 mcast_rate[band] = i + 1;
5297 found = true;
5298 break;
5299 }
5300 }
5301 }
5302
5303 return found;
5304}
5305
Johannes Berg04a773a2009-04-19 21:24:32 +02005306static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5307{
Johannes Berg4c476992010-10-04 21:36:35 +02005308 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5309 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005310 struct cfg80211_ibss_params ibss;
5311 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005312 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005313 int err;
5314
Johannes Berg8e30bc52009-04-22 17:45:38 +02005315 memset(&ibss, 0, sizeof(ibss));
5316
Johannes Berg04a773a2009-04-19 21:24:32 +02005317 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5318 return -EINVAL;
5319
5320 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5321 !info->attrs[NL80211_ATTR_SSID] ||
5322 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5323 return -EINVAL;
5324
Johannes Berg8e30bc52009-04-22 17:45:38 +02005325 ibss.beacon_interval = 100;
5326
5327 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5328 ibss.beacon_interval =
5329 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5330 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5331 return -EINVAL;
5332 }
5333
Johannes Berg4c476992010-10-04 21:36:35 +02005334 if (!rdev->ops->join_ibss)
5335 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005336
Johannes Berg4c476992010-10-04 21:36:35 +02005337 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5338 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005339
Johannes Berg79c97e92009-07-07 03:56:12 +02005340 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005341
Johannes Berg39193492011-09-16 13:45:25 +02005342 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005343 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005344
5345 if (!is_valid_ether_addr(ibss.bssid))
5346 return -EINVAL;
5347 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005348 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5349 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5350
5351 if (info->attrs[NL80211_ATTR_IE]) {
5352 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5353 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5354 }
5355
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005356 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5357 enum nl80211_channel_type channel_type;
5358
Johannes Bergcd6c6592012-05-10 21:27:18 +02005359 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005360 return -EINVAL;
5361
5362 if (channel_type != NL80211_CHAN_NO_HT &&
5363 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5364 return -EINVAL;
5365
5366 ibss.channel_type = channel_type;
5367 } else {
5368 ibss.channel_type = NL80211_CHAN_NO_HT;
5369 }
5370
5371 ibss.channel = rdev_freq_to_chan(rdev,
5372 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5373 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005374 if (!ibss.channel ||
5375 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005376 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5377 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005378
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005379 /* Both channels should be able to initiate communication */
5380 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5381 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5382 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5383 ibss.channel_type))
5384 return -EINVAL;
5385
Johannes Berg04a773a2009-04-19 21:24:32 +02005386 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005387 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005388
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005389 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5390 u8 *rates =
5391 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5392 int n_rates =
5393 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5394 struct ieee80211_supported_band *sband =
5395 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005396
Johannes Berg34850ab2011-07-18 18:08:35 +02005397 err = ieee80211_get_ratemask(sband, rates, n_rates,
5398 &ibss.basic_rates);
5399 if (err)
5400 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005401 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005402
5403 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5404 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5405 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5406 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005407
Johannes Berg4c476992010-10-04 21:36:35 +02005408 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5409 connkeys = nl80211_parse_connkeys(rdev,
5410 info->attrs[NL80211_ATTR_KEYS]);
5411 if (IS_ERR(connkeys))
5412 return PTR_ERR(connkeys);
5413 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005414
Antonio Quartulli267335d2012-01-31 20:25:47 +01005415 ibss.control_port =
5416 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5417
Johannes Berg4c476992010-10-04 21:36:35 +02005418 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005419 if (err)
5420 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005421 return err;
5422}
5423
5424static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5425{
Johannes Berg4c476992010-10-04 21:36:35 +02005426 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5427 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005428
Johannes Berg4c476992010-10-04 21:36:35 +02005429 if (!rdev->ops->leave_ibss)
5430 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005431
Johannes Berg4c476992010-10-04 21:36:35 +02005432 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5433 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005434
Johannes Berg4c476992010-10-04 21:36:35 +02005435 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005436}
5437
Johannes Bergaff89a92009-07-01 21:26:51 +02005438#ifdef CONFIG_NL80211_TESTMODE
5439static struct genl_multicast_group nl80211_testmode_mcgrp = {
5440 .name = "testmode",
5441};
5442
5443static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5444{
Johannes Berg4c476992010-10-04 21:36:35 +02005445 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005446 int err;
5447
5448 if (!info->attrs[NL80211_ATTR_TESTDATA])
5449 return -EINVAL;
5450
Johannes Bergaff89a92009-07-01 21:26:51 +02005451 err = -EOPNOTSUPP;
5452 if (rdev->ops->testmode_cmd) {
5453 rdev->testmode_info = info;
5454 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5455 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5456 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5457 rdev->testmode_info = NULL;
5458 }
5459
Johannes Bergaff89a92009-07-01 21:26:51 +02005460 return err;
5461}
5462
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005463static int nl80211_testmode_dump(struct sk_buff *skb,
5464 struct netlink_callback *cb)
5465{
Johannes Berg00918d32011-12-13 17:22:05 +01005466 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005467 int err;
5468 long phy_idx;
5469 void *data = NULL;
5470 int data_len = 0;
5471
5472 if (cb->args[0]) {
5473 /*
5474 * 0 is a valid index, but not valid for args[0],
5475 * so we need to offset by 1.
5476 */
5477 phy_idx = cb->args[0] - 1;
5478 } else {
5479 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5480 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5481 nl80211_policy);
5482 if (err)
5483 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005484
Johannes Berg2bd7e352012-06-15 14:23:16 +02005485 mutex_lock(&cfg80211_mutex);
5486 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5487 nl80211_fam.attrbuf);
5488 if (IS_ERR(rdev)) {
5489 mutex_unlock(&cfg80211_mutex);
5490 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005491 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005492 phy_idx = rdev->wiphy_idx;
5493 rdev = NULL;
5494 mutex_unlock(&cfg80211_mutex);
5495
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005496 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5497 cb->args[1] =
5498 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5499 }
5500
5501 if (cb->args[1]) {
5502 data = nla_data((void *)cb->args[1]);
5503 data_len = nla_len((void *)cb->args[1]);
5504 }
5505
5506 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005507 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5508 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005509 mutex_unlock(&cfg80211_mutex);
5510 return -ENOENT;
5511 }
Johannes Berg00918d32011-12-13 17:22:05 +01005512 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005513 mutex_unlock(&cfg80211_mutex);
5514
Johannes Berg00918d32011-12-13 17:22:05 +01005515 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005516 err = -EOPNOTSUPP;
5517 goto out_err;
5518 }
5519
5520 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005521 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005522 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5523 NL80211_CMD_TESTMODE);
5524 struct nlattr *tmdata;
5525
David S. Miller9360ffd2012-03-29 04:41:26 -04005526 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005527 genlmsg_cancel(skb, hdr);
5528 break;
5529 }
5530
5531 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5532 if (!tmdata) {
5533 genlmsg_cancel(skb, hdr);
5534 break;
5535 }
Johannes Berg00918d32011-12-13 17:22:05 +01005536 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5537 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005538 nla_nest_end(skb, tmdata);
5539
5540 if (err == -ENOBUFS || err == -ENOENT) {
5541 genlmsg_cancel(skb, hdr);
5542 break;
5543 } else if (err) {
5544 genlmsg_cancel(skb, hdr);
5545 goto out_err;
5546 }
5547
5548 genlmsg_end(skb, hdr);
5549 }
5550
5551 err = skb->len;
5552 /* see above */
5553 cb->args[0] = phy_idx + 1;
5554 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005555 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005556 return err;
5557}
5558
Johannes Bergaff89a92009-07-01 21:26:51 +02005559static struct sk_buff *
5560__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005561 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005562{
5563 struct sk_buff *skb;
5564 void *hdr;
5565 struct nlattr *data;
5566
5567 skb = nlmsg_new(approxlen + 100, gfp);
5568 if (!skb)
5569 return NULL;
5570
Eric W. Biederman15e47302012-09-07 20:12:54 +00005571 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005572 if (!hdr) {
5573 kfree_skb(skb);
5574 return NULL;
5575 }
5576
David S. Miller9360ffd2012-03-29 04:41:26 -04005577 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5578 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005579 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5580
5581 ((void **)skb->cb)[0] = rdev;
5582 ((void **)skb->cb)[1] = hdr;
5583 ((void **)skb->cb)[2] = data;
5584
5585 return skb;
5586
5587 nla_put_failure:
5588 kfree_skb(skb);
5589 return NULL;
5590}
5591
5592struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5593 int approxlen)
5594{
5595 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5596
5597 if (WARN_ON(!rdev->testmode_info))
5598 return NULL;
5599
5600 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005601 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005602 rdev->testmode_info->snd_seq,
5603 GFP_KERNEL);
5604}
5605EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5606
5607int cfg80211_testmode_reply(struct sk_buff *skb)
5608{
5609 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5610 void *hdr = ((void **)skb->cb)[1];
5611 struct nlattr *data = ((void **)skb->cb)[2];
5612
5613 if (WARN_ON(!rdev->testmode_info)) {
5614 kfree_skb(skb);
5615 return -EINVAL;
5616 }
5617
5618 nla_nest_end(skb, data);
5619 genlmsg_end(skb, hdr);
5620 return genlmsg_reply(skb, rdev->testmode_info);
5621}
5622EXPORT_SYMBOL(cfg80211_testmode_reply);
5623
5624struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5625 int approxlen, gfp_t gfp)
5626{
5627 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5628
5629 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5630}
5631EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5632
5633void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5634{
5635 void *hdr = ((void **)skb->cb)[1];
5636 struct nlattr *data = ((void **)skb->cb)[2];
5637
5638 nla_nest_end(skb, data);
5639 genlmsg_end(skb, hdr);
5640 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5641}
5642EXPORT_SYMBOL(cfg80211_testmode_event);
5643#endif
5644
Samuel Ortizb23aa672009-07-01 21:26:54 +02005645static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5646{
Johannes Berg4c476992010-10-04 21:36:35 +02005647 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5648 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005649 struct cfg80211_connect_params connect;
5650 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005651 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005652 int err;
5653
5654 memset(&connect, 0, sizeof(connect));
5655
5656 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5657 return -EINVAL;
5658
5659 if (!info->attrs[NL80211_ATTR_SSID] ||
5660 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5661 return -EINVAL;
5662
5663 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5664 connect.auth_type =
5665 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005666 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5667 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005668 return -EINVAL;
5669 } else
5670 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5671
5672 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5673
Johannes Bergc0692b82010-08-27 14:26:53 +03005674 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005675 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005676 if (err)
5677 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005678
Johannes Berg074ac8d2010-09-16 14:58:22 +02005679 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005680 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5681 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005682
Johannes Berg79c97e92009-07-07 03:56:12 +02005683 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005684
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305685 connect.bg_scan_period = -1;
5686 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5687 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5688 connect.bg_scan_period =
5689 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5690 }
5691
Samuel Ortizb23aa672009-07-01 21:26:54 +02005692 if (info->attrs[NL80211_ATTR_MAC])
5693 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5694 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5695 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5696
5697 if (info->attrs[NL80211_ATTR_IE]) {
5698 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5699 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5700 }
5701
5702 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5703 connect.channel =
5704 ieee80211_get_channel(wiphy,
5705 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5706 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005707 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5708 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005709 }
5710
Johannes Bergfffd0932009-07-08 14:22:54 +02005711 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5712 connkeys = nl80211_parse_connkeys(rdev,
5713 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02005714 if (IS_ERR(connkeys))
5715 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005716 }
5717
Ben Greear7e7c8922011-11-18 11:31:59 -08005718 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5719 connect.flags |= ASSOC_REQ_DISABLE_HT;
5720
5721 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5722 memcpy(&connect.ht_capa_mask,
5723 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5724 sizeof(connect.ht_capa_mask));
5725
5726 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005727 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5728 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005729 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005730 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005731 memcpy(&connect.ht_capa,
5732 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5733 sizeof(connect.ht_capa));
5734 }
5735
Johannes Bergfffd0932009-07-08 14:22:54 +02005736 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005737 if (err)
5738 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005739 return err;
5740}
5741
5742static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5743{
Johannes Berg4c476992010-10-04 21:36:35 +02005744 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5745 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005746 u16 reason;
5747
5748 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5749 reason = WLAN_REASON_DEAUTH_LEAVING;
5750 else
5751 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5752
5753 if (reason == 0)
5754 return -EINVAL;
5755
Johannes Berg074ac8d2010-09-16 14:58:22 +02005756 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005757 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5758 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005759
Johannes Berg4c476992010-10-04 21:36:35 +02005760 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005761}
5762
Johannes Berg463d0182009-07-14 00:33:35 +02005763static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5764{
Johannes Berg4c476992010-10-04 21:36:35 +02005765 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005766 struct net *net;
5767 int err;
5768 u32 pid;
5769
5770 if (!info->attrs[NL80211_ATTR_PID])
5771 return -EINVAL;
5772
5773 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5774
Johannes Berg463d0182009-07-14 00:33:35 +02005775 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005776 if (IS_ERR(net))
5777 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005778
5779 err = 0;
5780
5781 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005782 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5783 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005784
Johannes Berg463d0182009-07-14 00:33:35 +02005785 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005786 return err;
5787}
5788
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005789static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5790{
Johannes Berg4c476992010-10-04 21:36:35 +02005791 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005792 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5793 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005794 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005795 struct cfg80211_pmksa pmksa;
5796
5797 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5798
5799 if (!info->attrs[NL80211_ATTR_MAC])
5800 return -EINVAL;
5801
5802 if (!info->attrs[NL80211_ATTR_PMKID])
5803 return -EINVAL;
5804
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005805 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5806 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5807
Johannes Berg074ac8d2010-09-16 14:58:22 +02005808 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005809 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5810 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005811
5812 switch (info->genlhdr->cmd) {
5813 case NL80211_CMD_SET_PMKSA:
5814 rdev_ops = rdev->ops->set_pmksa;
5815 break;
5816 case NL80211_CMD_DEL_PMKSA:
5817 rdev_ops = rdev->ops->del_pmksa;
5818 break;
5819 default:
5820 WARN_ON(1);
5821 break;
5822 }
5823
Johannes Berg4c476992010-10-04 21:36:35 +02005824 if (!rdev_ops)
5825 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005826
Johannes Berg4c476992010-10-04 21:36:35 +02005827 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005828}
5829
5830static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5831{
Johannes Berg4c476992010-10-04 21:36:35 +02005832 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5833 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005834
Johannes Berg074ac8d2010-09-16 14:58:22 +02005835 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005836 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5837 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005838
Johannes Berg4c476992010-10-04 21:36:35 +02005839 if (!rdev->ops->flush_pmksa)
5840 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005841
Johannes Berg4c476992010-10-04 21:36:35 +02005842 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005843}
5844
Arik Nemtsov109086c2011-09-28 14:12:50 +03005845static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5846{
5847 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5848 struct net_device *dev = info->user_ptr[1];
5849 u8 action_code, dialog_token;
5850 u16 status_code;
5851 u8 *peer;
5852
5853 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5854 !rdev->ops->tdls_mgmt)
5855 return -EOPNOTSUPP;
5856
5857 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5858 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5859 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5860 !info->attrs[NL80211_ATTR_IE] ||
5861 !info->attrs[NL80211_ATTR_MAC])
5862 return -EINVAL;
5863
5864 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5865 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5866 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5867 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5868
5869 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5870 dialog_token, status_code,
5871 nla_data(info->attrs[NL80211_ATTR_IE]),
5872 nla_len(info->attrs[NL80211_ATTR_IE]));
5873}
5874
5875static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5876{
5877 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5878 struct net_device *dev = info->user_ptr[1];
5879 enum nl80211_tdls_operation operation;
5880 u8 *peer;
5881
5882 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5883 !rdev->ops->tdls_oper)
5884 return -EOPNOTSUPP;
5885
5886 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5887 !info->attrs[NL80211_ATTR_MAC])
5888 return -EINVAL;
5889
5890 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5891 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5892
5893 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5894}
5895
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005896static int nl80211_remain_on_channel(struct sk_buff *skb,
5897 struct genl_info *info)
5898{
Johannes Berg4c476992010-10-04 21:36:35 +02005899 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005900 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005901 struct ieee80211_channel *chan;
5902 struct sk_buff *msg;
5903 void *hdr;
5904 u64 cookie;
5905 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5906 u32 freq, duration;
5907 int err;
5908
5909 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5910 !info->attrs[NL80211_ATTR_DURATION])
5911 return -EINVAL;
5912
5913 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5914
Johannes Berg7c4ef712011-11-18 15:33:48 +01005915 if (!rdev->ops->remain_on_channel ||
5916 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005917 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005918
Johannes Bergebf348f2012-06-01 12:50:54 +02005919 /*
5920 * We should be on that channel for at least a minimum amount of
5921 * time (10ms) but no longer than the driver supports.
5922 */
5923 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5924 duration > rdev->wiphy.max_remain_on_channel_duration)
5925 return -EINVAL;
5926
Johannes Bergcd6c6592012-05-10 21:27:18 +02005927 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5928 !nl80211_valid_channel_type(info, &channel_type))
5929 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005930
5931 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5932 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005933 if (chan == NULL)
5934 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005935
5936 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005937 if (!msg)
5938 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005939
Eric W. Biederman15e47302012-09-07 20:12:54 +00005940 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005941 NL80211_CMD_REMAIN_ON_CHANNEL);
5942
5943 if (IS_ERR(hdr)) {
5944 err = PTR_ERR(hdr);
5945 goto free_msg;
5946 }
5947
Johannes Berg71bbc992012-06-15 15:30:18 +02005948 err = rdev->ops->remain_on_channel(&rdev->wiphy, wdev, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005949 channel_type, duration, &cookie);
5950
5951 if (err)
5952 goto free_msg;
5953
David S. Miller9360ffd2012-03-29 04:41:26 -04005954 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5955 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005956
5957 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005958
5959 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005960
5961 nla_put_failure:
5962 err = -ENOBUFS;
5963 free_msg:
5964 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005965 return err;
5966}
5967
5968static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5969 struct genl_info *info)
5970{
Johannes Berg4c476992010-10-04 21:36:35 +02005971 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005972 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005973 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005974
5975 if (!info->attrs[NL80211_ATTR_COOKIE])
5976 return -EINVAL;
5977
Johannes Berg4c476992010-10-04 21:36:35 +02005978 if (!rdev->ops->cancel_remain_on_channel)
5979 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005980
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005981 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5982
Johannes Berg71bbc992012-06-15 15:30:18 +02005983 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005984}
5985
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005986static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
5987 u8 *rates, u8 rates_len)
5988{
5989 u8 i;
5990 u32 mask = 0;
5991
5992 for (i = 0; i < rates_len; i++) {
5993 int rate = (rates[i] & 0x7f) * 5;
5994 int ridx;
5995 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
5996 struct ieee80211_rate *srate =
5997 &sband->bitrates[ridx];
5998 if (rate == srate->bitrate) {
5999 mask |= 1 << ridx;
6000 break;
6001 }
6002 }
6003 if (ridx == sband->n_bitrates)
6004 return 0; /* rate not found */
6005 }
6006
6007 return mask;
6008}
6009
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006010static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6011 u8 *rates, u8 rates_len,
6012 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6013{
6014 u8 i;
6015
6016 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6017
6018 for (i = 0; i < rates_len; i++) {
6019 int ridx, rbit;
6020
6021 ridx = rates[i] / 8;
6022 rbit = BIT(rates[i] % 8);
6023
6024 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006025 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006026 return false;
6027
6028 /* check availability */
6029 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6030 mcs[ridx] |= rbit;
6031 else
6032 return false;
6033 }
6034
6035 return true;
6036}
6037
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006038static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006039 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6040 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006041 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6042 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006043};
6044
6045static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6046 struct genl_info *info)
6047{
6048 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006049 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006050 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006051 int rem, i;
6052 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006053 struct nlattr *tx_rates;
6054 struct ieee80211_supported_band *sband;
6055
6056 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6057 return -EINVAL;
6058
Johannes Berg4c476992010-10-04 21:36:35 +02006059 if (!rdev->ops->set_bitrate_mask)
6060 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006061
6062 memset(&mask, 0, sizeof(mask));
6063 /* Default to all rates enabled */
6064 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6065 sband = rdev->wiphy.bands[i];
6066 mask.control[i].legacy =
6067 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006068 if (sband)
6069 memcpy(mask.control[i].mcs,
6070 sband->ht_cap.mcs.rx_mask,
6071 sizeof(mask.control[i].mcs));
6072 else
6073 memset(mask.control[i].mcs, 0,
6074 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006075 }
6076
6077 /*
6078 * The nested attribute uses enum nl80211_band as the index. This maps
6079 * directly to the enum ieee80211_band values used in cfg80211.
6080 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006081 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006082 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6083 {
6084 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006085 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6086 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006087 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006088 if (sband == NULL)
6089 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006090 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6091 nla_len(tx_rates), nl80211_txattr_policy);
6092 if (tb[NL80211_TXRATE_LEGACY]) {
6093 mask.control[band].legacy = rateset_to_mask(
6094 sband,
6095 nla_data(tb[NL80211_TXRATE_LEGACY]),
6096 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306097 if ((mask.control[band].legacy == 0) &&
6098 nla_len(tb[NL80211_TXRATE_LEGACY]))
6099 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006100 }
6101 if (tb[NL80211_TXRATE_MCS]) {
6102 if (!ht_rateset_to_mask(
6103 sband,
6104 nla_data(tb[NL80211_TXRATE_MCS]),
6105 nla_len(tb[NL80211_TXRATE_MCS]),
6106 mask.control[band].mcs))
6107 return -EINVAL;
6108 }
6109
6110 if (mask.control[band].legacy == 0) {
6111 /* don't allow empty legacy rates if HT
6112 * is not even supported. */
6113 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6114 return -EINVAL;
6115
6116 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6117 if (mask.control[band].mcs[i])
6118 break;
6119
6120 /* legacy and mcs rates may not be both empty */
6121 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006122 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006123 }
6124 }
6125
Johannes Berg4c476992010-10-04 21:36:35 +02006126 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006127}
6128
Johannes Berg2e161f72010-08-12 15:38:38 +02006129static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006130{
Johannes Berg4c476992010-10-04 21:36:35 +02006131 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006132 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006133 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006134
6135 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6136 return -EINVAL;
6137
Johannes Berg2e161f72010-08-12 15:38:38 +02006138 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6139 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006140
Johannes Berg71bbc992012-06-15 15:30:18 +02006141 switch (wdev->iftype) {
6142 case NL80211_IFTYPE_STATION:
6143 case NL80211_IFTYPE_ADHOC:
6144 case NL80211_IFTYPE_P2P_CLIENT:
6145 case NL80211_IFTYPE_AP:
6146 case NL80211_IFTYPE_AP_VLAN:
6147 case NL80211_IFTYPE_MESH_POINT:
6148 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006149 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006150 break;
6151 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006152 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006153 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006154
6155 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006156 if (!rdev->ops->mgmt_tx)
6157 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006158
Eric W. Biederman15e47302012-09-07 20:12:54 +00006159 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006160 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6161 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006162}
6163
Johannes Berg2e161f72010-08-12 15:38:38 +02006164static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006165{
Johannes Berg4c476992010-10-04 21:36:35 +02006166 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006167 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02006168 struct ieee80211_channel *chan;
6169 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02006170 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02006171 u32 freq;
6172 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006173 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006174 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006175 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006176 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006177 bool offchan, no_cck, dont_wait_for_ack;
6178
6179 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006180
6181 if (!info->attrs[NL80211_ATTR_FRAME] ||
6182 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6183 return -EINVAL;
6184
Johannes Berg4c476992010-10-04 21:36:35 +02006185 if (!rdev->ops->mgmt_tx)
6186 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006187
Johannes Berg71bbc992012-06-15 15:30:18 +02006188 switch (wdev->iftype) {
6189 case NL80211_IFTYPE_STATION:
6190 case NL80211_IFTYPE_ADHOC:
6191 case NL80211_IFTYPE_P2P_CLIENT:
6192 case NL80211_IFTYPE_AP:
6193 case NL80211_IFTYPE_AP_VLAN:
6194 case NL80211_IFTYPE_MESH_POINT:
6195 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006196 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006197 break;
6198 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006199 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006200 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006201
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006202 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006203 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006204 return -EINVAL;
6205 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006206
6207 /*
6208 * We should wait on the channel for at least a minimum amount
6209 * of time (10ms) but no longer than the driver supports.
6210 */
6211 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6212 wait > rdev->wiphy.max_remain_on_channel_duration)
6213 return -EINVAL;
6214
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006215 }
6216
Jouni Malinen026331c2010-02-15 12:53:10 +02006217 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02006218 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02006219 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02006220 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02006221 }
6222
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006223 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6224
Johannes Berg7c4ef712011-11-18 15:33:48 +01006225 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6226 return -EINVAL;
6227
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306228 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6229
Jouni Malinen026331c2010-02-15 12:53:10 +02006230 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6231 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02006232 if (chan == NULL)
6233 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006234
Johannes Berge247bd902011-11-04 11:18:21 +01006235 if (!dont_wait_for_ack) {
6236 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6237 if (!msg)
6238 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006239
Eric W. Biederman15e47302012-09-07 20:12:54 +00006240 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006241 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006242
Johannes Berge247bd902011-11-04 11:18:21 +01006243 if (IS_ERR(hdr)) {
6244 err = PTR_ERR(hdr);
6245 goto free_msg;
6246 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006247 }
Johannes Berge247bd902011-11-04 11:18:21 +01006248
Johannes Berg71bbc992012-06-15 15:30:18 +02006249 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chan, offchan, channel_type,
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006250 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006251 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6252 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006253 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006254 if (err)
6255 goto free_msg;
6256
Johannes Berge247bd902011-11-04 11:18:21 +01006257 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006258 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6259 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006260
Johannes Berge247bd902011-11-04 11:18:21 +01006261 genlmsg_end(msg, hdr);
6262 return genlmsg_reply(msg, info);
6263 }
6264
6265 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006266
6267 nla_put_failure:
6268 err = -ENOBUFS;
6269 free_msg:
6270 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006271 return err;
6272}
6273
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006274static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6275{
6276 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006277 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006278 u64 cookie;
6279
6280 if (!info->attrs[NL80211_ATTR_COOKIE])
6281 return -EINVAL;
6282
6283 if (!rdev->ops->mgmt_tx_cancel_wait)
6284 return -EOPNOTSUPP;
6285
Johannes Berg71bbc992012-06-15 15:30:18 +02006286 switch (wdev->iftype) {
6287 case NL80211_IFTYPE_STATION:
6288 case NL80211_IFTYPE_ADHOC:
6289 case NL80211_IFTYPE_P2P_CLIENT:
6290 case NL80211_IFTYPE_AP:
6291 case NL80211_IFTYPE_AP_VLAN:
6292 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006293 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006294 break;
6295 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006296 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006297 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006298
6299 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6300
Johannes Berg71bbc992012-06-15 15:30:18 +02006301 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006302}
6303
Kalle Valoffb9eb32010-02-17 17:58:10 +02006304static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6305{
Johannes Berg4c476992010-10-04 21:36:35 +02006306 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006307 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006308 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006309 u8 ps_state;
6310 bool state;
6311 int err;
6312
Johannes Berg4c476992010-10-04 21:36:35 +02006313 if (!info->attrs[NL80211_ATTR_PS_STATE])
6314 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006315
6316 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6317
Johannes Berg4c476992010-10-04 21:36:35 +02006318 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6319 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006320
6321 wdev = dev->ieee80211_ptr;
6322
Johannes Berg4c476992010-10-04 21:36:35 +02006323 if (!rdev->ops->set_power_mgmt)
6324 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006325
6326 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6327
6328 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006329 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006330
Johannes Berg4c476992010-10-04 21:36:35 +02006331 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
6332 wdev->ps_timeout);
6333 if (!err)
6334 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006335 return err;
6336}
6337
6338static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6339{
Johannes Berg4c476992010-10-04 21:36:35 +02006340 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006341 enum nl80211_ps_state ps_state;
6342 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006343 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006344 struct sk_buff *msg;
6345 void *hdr;
6346 int err;
6347
Kalle Valoffb9eb32010-02-17 17:58:10 +02006348 wdev = dev->ieee80211_ptr;
6349
Johannes Berg4c476992010-10-04 21:36:35 +02006350 if (!rdev->ops->set_power_mgmt)
6351 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006352
6353 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006354 if (!msg)
6355 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006356
Eric W. Biederman15e47302012-09-07 20:12:54 +00006357 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006358 NL80211_CMD_GET_POWER_SAVE);
6359 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006360 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006361 goto free_msg;
6362 }
6363
6364 if (wdev->ps)
6365 ps_state = NL80211_PS_ENABLED;
6366 else
6367 ps_state = NL80211_PS_DISABLED;
6368
David S. Miller9360ffd2012-03-29 04:41:26 -04006369 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6370 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006371
6372 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006373 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006374
Johannes Berg4c476992010-10-04 21:36:35 +02006375 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006376 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006377 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006378 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006379 return err;
6380}
6381
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006382static struct nla_policy
6383nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6384 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6385 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6386 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006387 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6388 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6389 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006390};
6391
Thomas Pedersen84f10702012-07-12 16:17:33 -07006392static int nl80211_set_cqm_txe(struct genl_info *info,
6393 u32 rate, u32 pkts, u32 intvl)
6394{
6395 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6396 struct wireless_dev *wdev;
6397 struct net_device *dev = info->user_ptr[1];
6398
6399 if ((rate < 0 || rate > 100) ||
6400 (intvl < 0 || intvl > NL80211_CQM_TXE_MAX_INTVL))
6401 return -EINVAL;
6402
6403 wdev = dev->ieee80211_ptr;
6404
6405 if (!rdev->ops->set_cqm_txe_config)
6406 return -EOPNOTSUPP;
6407
6408 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6409 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6410 return -EOPNOTSUPP;
6411
6412 return rdev->ops->set_cqm_txe_config(wdev->wiphy, dev,
6413 rate, pkts, intvl);
6414}
6415
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006416static int nl80211_set_cqm_rssi(struct genl_info *info,
6417 s32 threshold, u32 hysteresis)
6418{
Johannes Berg4c476992010-10-04 21:36:35 +02006419 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006420 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006421 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006422
6423 if (threshold > 0)
6424 return -EINVAL;
6425
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006426 wdev = dev->ieee80211_ptr;
6427
Johannes Berg4c476992010-10-04 21:36:35 +02006428 if (!rdev->ops->set_cqm_rssi_config)
6429 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006430
Johannes Berg074ac8d2010-09-16 14:58:22 +02006431 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006432 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6433 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006434
Johannes Berg4c476992010-10-04 21:36:35 +02006435 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6436 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006437}
6438
6439static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6440{
6441 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6442 struct nlattr *cqm;
6443 int err;
6444
6445 cqm = info->attrs[NL80211_ATTR_CQM];
6446 if (!cqm) {
6447 err = -EINVAL;
6448 goto out;
6449 }
6450
6451 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6452 nl80211_attr_cqm_policy);
6453 if (err)
6454 goto out;
6455
6456 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6457 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6458 s32 threshold;
6459 u32 hysteresis;
6460 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6461 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6462 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006463 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6464 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6465 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6466 u32 rate, pkts, intvl;
6467 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6468 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6469 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6470 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006471 } else
6472 err = -EINVAL;
6473
6474out:
6475 return err;
6476}
6477
Johannes Berg29cbe682010-12-03 09:20:44 +01006478static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6479{
6480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6481 struct net_device *dev = info->user_ptr[1];
6482 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006483 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006484 int err;
6485
6486 /* start with default */
6487 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006488 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006489
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006490 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006491 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006492 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006493 if (err)
6494 return err;
6495 }
6496
6497 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6498 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6499 return -EINVAL;
6500
Javier Cardonac80d5452010-12-16 17:37:49 -08006501 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6502 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6503
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006504 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6505 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6506 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6507 return -EINVAL;
6508
Javier Cardonac80d5452010-12-16 17:37:49 -08006509 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6510 /* parse additional setup parameters if given */
6511 err = nl80211_parse_mesh_setup(info, &setup);
6512 if (err)
6513 return err;
6514 }
6515
Johannes Bergcc1d2802012-05-16 23:50:20 +02006516 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6517 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6518
6519 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6520 !nl80211_valid_channel_type(info, &channel_type))
6521 return -EINVAL;
6522
6523 setup.channel = rdev_freq_to_chan(rdev,
6524 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6525 channel_type);
6526 if (!setup.channel)
6527 return -EINVAL;
6528 setup.channel_type = channel_type;
6529 } else {
6530 /* cfg80211_join_mesh() will sort it out */
6531 setup.channel = NULL;
6532 }
6533
Javier Cardonac80d5452010-12-16 17:37:49 -08006534 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006535}
6536
6537static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6538{
6539 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6540 struct net_device *dev = info->user_ptr[1];
6541
6542 return cfg80211_leave_mesh(rdev, dev);
6543}
6544
Johannes Bergdfb89c52012-06-27 09:23:48 +02006545#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006546static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6547{
6548 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6549 struct sk_buff *msg;
6550 void *hdr;
6551
6552 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6553 return -EOPNOTSUPP;
6554
6555 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6556 if (!msg)
6557 return -ENOMEM;
6558
Eric W. Biederman15e47302012-09-07 20:12:54 +00006559 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006560 NL80211_CMD_GET_WOWLAN);
6561 if (!hdr)
6562 goto nla_put_failure;
6563
6564 if (rdev->wowlan) {
6565 struct nlattr *nl_wowlan;
6566
6567 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6568 if (!nl_wowlan)
6569 goto nla_put_failure;
6570
David S. Miller9360ffd2012-03-29 04:41:26 -04006571 if ((rdev->wowlan->any &&
6572 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6573 (rdev->wowlan->disconnect &&
6574 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6575 (rdev->wowlan->magic_pkt &&
6576 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6577 (rdev->wowlan->gtk_rekey_failure &&
6578 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6579 (rdev->wowlan->eap_identity_req &&
6580 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6581 (rdev->wowlan->four_way_handshake &&
6582 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6583 (rdev->wowlan->rfkill_release &&
6584 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6585 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006586 if (rdev->wowlan->n_patterns) {
6587 struct nlattr *nl_pats, *nl_pat;
6588 int i, pat_len;
6589
6590 nl_pats = nla_nest_start(msg,
6591 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6592 if (!nl_pats)
6593 goto nla_put_failure;
6594
6595 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6596 nl_pat = nla_nest_start(msg, i + 1);
6597 if (!nl_pat)
6598 goto nla_put_failure;
6599 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006600 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6601 DIV_ROUND_UP(pat_len, 8),
6602 rdev->wowlan->patterns[i].mask) ||
6603 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6604 pat_len,
6605 rdev->wowlan->patterns[i].pattern))
6606 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006607 nla_nest_end(msg, nl_pat);
6608 }
6609 nla_nest_end(msg, nl_pats);
6610 }
6611
6612 nla_nest_end(msg, nl_wowlan);
6613 }
6614
6615 genlmsg_end(msg, hdr);
6616 return genlmsg_reply(msg, info);
6617
6618nla_put_failure:
6619 nlmsg_free(msg);
6620 return -ENOBUFS;
6621}
6622
6623static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6624{
6625 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6626 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006627 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006628 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006629 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6630 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006631 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006632
6633 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6634 return -EOPNOTSUPP;
6635
Johannes Bergae33bd82012-07-12 16:25:02 +02006636 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6637 cfg80211_rdev_free_wowlan(rdev);
6638 rdev->wowlan = NULL;
6639 goto set_wakeup;
6640 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006641
6642 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6643 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6644 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6645 nl80211_wowlan_policy);
6646 if (err)
6647 return err;
6648
6649 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6650 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6651 return -EINVAL;
6652 new_triggers.any = true;
6653 }
6654
6655 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6656 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6657 return -EINVAL;
6658 new_triggers.disconnect = true;
6659 }
6660
6661 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6662 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6663 return -EINVAL;
6664 new_triggers.magic_pkt = true;
6665 }
6666
Johannes Berg77dbbb12011-07-13 10:48:55 +02006667 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6668 return -EINVAL;
6669
6670 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6671 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6672 return -EINVAL;
6673 new_triggers.gtk_rekey_failure = true;
6674 }
6675
6676 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6677 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6678 return -EINVAL;
6679 new_triggers.eap_identity_req = true;
6680 }
6681
6682 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6683 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6684 return -EINVAL;
6685 new_triggers.four_way_handshake = true;
6686 }
6687
6688 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6689 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6690 return -EINVAL;
6691 new_triggers.rfkill_release = true;
6692 }
6693
Johannes Bergff1b6e62011-05-04 15:37:28 +02006694 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6695 struct nlattr *pat;
6696 int n_patterns = 0;
6697 int rem, pat_len, mask_len;
6698 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6699
6700 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6701 rem)
6702 n_patterns++;
6703 if (n_patterns > wowlan->n_patterns)
6704 return -EINVAL;
6705
6706 new_triggers.patterns = kcalloc(n_patterns,
6707 sizeof(new_triggers.patterns[0]),
6708 GFP_KERNEL);
6709 if (!new_triggers.patterns)
6710 return -ENOMEM;
6711
6712 new_triggers.n_patterns = n_patterns;
6713 i = 0;
6714
6715 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6716 rem) {
6717 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6718 nla_data(pat), nla_len(pat), NULL);
6719 err = -EINVAL;
6720 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6721 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6722 goto error;
6723 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6724 mask_len = DIV_ROUND_UP(pat_len, 8);
6725 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6726 mask_len)
6727 goto error;
6728 if (pat_len > wowlan->pattern_max_len ||
6729 pat_len < wowlan->pattern_min_len)
6730 goto error;
6731
6732 new_triggers.patterns[i].mask =
6733 kmalloc(mask_len + pat_len, GFP_KERNEL);
6734 if (!new_triggers.patterns[i].mask) {
6735 err = -ENOMEM;
6736 goto error;
6737 }
6738 new_triggers.patterns[i].pattern =
6739 new_triggers.patterns[i].mask + mask_len;
6740 memcpy(new_triggers.patterns[i].mask,
6741 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6742 mask_len);
6743 new_triggers.patterns[i].pattern_len = pat_len;
6744 memcpy(new_triggers.patterns[i].pattern,
6745 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6746 pat_len);
6747 i++;
6748 }
6749 }
6750
Johannes Bergae33bd82012-07-12 16:25:02 +02006751 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6752 if (!ntrig) {
6753 err = -ENOMEM;
6754 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006755 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006756 cfg80211_rdev_free_wowlan(rdev);
6757 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006758
Johannes Bergae33bd82012-07-12 16:25:02 +02006759 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006760 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6761 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6762
Johannes Bergff1b6e62011-05-04 15:37:28 +02006763 return 0;
6764 error:
6765 for (i = 0; i < new_triggers.n_patterns; i++)
6766 kfree(new_triggers.patterns[i].mask);
6767 kfree(new_triggers.patterns);
6768 return err;
6769}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006770#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006771
Johannes Berge5497d72011-07-05 16:35:40 +02006772static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6773{
6774 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6775 struct net_device *dev = info->user_ptr[1];
6776 struct wireless_dev *wdev = dev->ieee80211_ptr;
6777 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6778 struct cfg80211_gtk_rekey_data rekey_data;
6779 int err;
6780
6781 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6782 return -EINVAL;
6783
6784 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6785 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6786 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6787 nl80211_rekey_policy);
6788 if (err)
6789 return err;
6790
6791 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6792 return -ERANGE;
6793 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6794 return -ERANGE;
6795 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6796 return -ERANGE;
6797
6798 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6799 NL80211_KEK_LEN);
6800 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6801 NL80211_KCK_LEN);
6802 memcpy(rekey_data.replay_ctr,
6803 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6804 NL80211_REPLAY_CTR_LEN);
6805
6806 wdev_lock(wdev);
6807 if (!wdev->current_bss) {
6808 err = -ENOTCONN;
6809 goto out;
6810 }
6811
6812 if (!rdev->ops->set_rekey_data) {
6813 err = -EOPNOTSUPP;
6814 goto out;
6815 }
6816
6817 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6818 out:
6819 wdev_unlock(wdev);
6820 return err;
6821}
6822
Johannes Berg28946da2011-11-04 11:18:12 +01006823static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6824 struct genl_info *info)
6825{
6826 struct net_device *dev = info->user_ptr[1];
6827 struct wireless_dev *wdev = dev->ieee80211_ptr;
6828
6829 if (wdev->iftype != NL80211_IFTYPE_AP &&
6830 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6831 return -EINVAL;
6832
Eric W. Biederman15e47302012-09-07 20:12:54 +00006833 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01006834 return -EBUSY;
6835
Eric W. Biederman15e47302012-09-07 20:12:54 +00006836 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01006837 return 0;
6838}
6839
Johannes Berg7f6cf312011-11-04 11:18:15 +01006840static int nl80211_probe_client(struct sk_buff *skb,
6841 struct genl_info *info)
6842{
6843 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6844 struct net_device *dev = info->user_ptr[1];
6845 struct wireless_dev *wdev = dev->ieee80211_ptr;
6846 struct sk_buff *msg;
6847 void *hdr;
6848 const u8 *addr;
6849 u64 cookie;
6850 int err;
6851
6852 if (wdev->iftype != NL80211_IFTYPE_AP &&
6853 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6854 return -EOPNOTSUPP;
6855
6856 if (!info->attrs[NL80211_ATTR_MAC])
6857 return -EINVAL;
6858
6859 if (!rdev->ops->probe_client)
6860 return -EOPNOTSUPP;
6861
6862 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6863 if (!msg)
6864 return -ENOMEM;
6865
Eric W. Biederman15e47302012-09-07 20:12:54 +00006866 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01006867 NL80211_CMD_PROBE_CLIENT);
6868
6869 if (IS_ERR(hdr)) {
6870 err = PTR_ERR(hdr);
6871 goto free_msg;
6872 }
6873
6874 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6875
6876 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6877 if (err)
6878 goto free_msg;
6879
David S. Miller9360ffd2012-03-29 04:41:26 -04006880 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6881 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006882
6883 genlmsg_end(msg, hdr);
6884
6885 return genlmsg_reply(msg, info);
6886
6887 nla_put_failure:
6888 err = -ENOBUFS;
6889 free_msg:
6890 nlmsg_free(msg);
6891 return err;
6892}
6893
Johannes Berg5e760232011-11-04 11:18:17 +01006894static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6895{
6896 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6897
6898 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6899 return -EOPNOTSUPP;
6900
Eric W. Biederman15e47302012-09-07 20:12:54 +00006901 if (rdev->ap_beacons_nlportid)
Johannes Berg5e760232011-11-04 11:18:17 +01006902 return -EBUSY;
6903
Eric W. Biederman15e47302012-09-07 20:12:54 +00006904 rdev->ap_beacons_nlportid = info->snd_portid;
Johannes Berg5e760232011-11-04 11:18:17 +01006905
6906 return 0;
6907}
6908
Johannes Berg98104fde2012-06-16 00:19:54 +02006909static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
6910{
6911 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6912 struct wireless_dev *wdev = info->user_ptr[1];
6913 int err;
6914
6915 if (!rdev->ops->start_p2p_device)
6916 return -EOPNOTSUPP;
6917
6918 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
6919 return -EOPNOTSUPP;
6920
6921 if (wdev->p2p_started)
6922 return 0;
6923
6924 mutex_lock(&rdev->devlist_mtx);
6925 err = cfg80211_can_add_interface(rdev, wdev->iftype);
6926 mutex_unlock(&rdev->devlist_mtx);
6927 if (err)
6928 return err;
6929
6930 err = rdev->ops->start_p2p_device(&rdev->wiphy, wdev);
6931 if (err)
6932 return err;
6933
6934 wdev->p2p_started = true;
6935 mutex_lock(&rdev->devlist_mtx);
6936 rdev->opencount++;
6937 mutex_unlock(&rdev->devlist_mtx);
6938
6939 return 0;
6940}
6941
6942static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
6943{
6944 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6945 struct wireless_dev *wdev = info->user_ptr[1];
6946
6947 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
6948 return -EOPNOTSUPP;
6949
6950 if (!rdev->ops->stop_p2p_device)
6951 return -EOPNOTSUPP;
6952
6953 if (!wdev->p2p_started)
6954 return 0;
6955
6956 rdev->ops->stop_p2p_device(&rdev->wiphy, wdev);
6957 wdev->p2p_started = false;
6958
6959 mutex_lock(&rdev->devlist_mtx);
6960 rdev->opencount--;
6961 mutex_unlock(&rdev->devlist_mtx);
6962
6963 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
6964 rdev->scan_req->aborted = true;
6965 ___cfg80211_scan_done(rdev, true);
6966 }
6967
6968 return 0;
6969}
6970
Johannes Berg4c476992010-10-04 21:36:35 +02006971#define NL80211_FLAG_NEED_WIPHY 0x01
6972#define NL80211_FLAG_NEED_NETDEV 0x02
6973#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006974#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6975#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6976 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02006977#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02006978/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02006979#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
6980 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006981
6982static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6983 struct genl_info *info)
6984{
6985 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02006986 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006987 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02006988 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
6989
6990 if (rtnl)
6991 rtnl_lock();
6992
6993 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02006994 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02006995 if (IS_ERR(rdev)) {
6996 if (rtnl)
6997 rtnl_unlock();
6998 return PTR_ERR(rdev);
6999 }
7000 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007001 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7002 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007003 mutex_lock(&cfg80211_mutex);
7004 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7005 info->attrs);
7006 if (IS_ERR(wdev)) {
7007 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007008 if (rtnl)
7009 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007010 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007011 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007012
Johannes Berg89a54e42012-06-15 14:33:17 +02007013 dev = wdev->netdev;
7014 rdev = wiphy_to_dev(wdev->wiphy);
7015
Johannes Berg1bf614e2012-06-15 15:23:36 +02007016 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7017 if (!dev) {
7018 mutex_unlock(&cfg80211_mutex);
7019 if (rtnl)
7020 rtnl_unlock();
7021 return -EINVAL;
7022 }
7023
7024 info->user_ptr[1] = dev;
7025 } else {
7026 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007027 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007028
Johannes Berg1bf614e2012-06-15 15:23:36 +02007029 if (dev) {
7030 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7031 !netif_running(dev)) {
7032 mutex_unlock(&cfg80211_mutex);
7033 if (rtnl)
7034 rtnl_unlock();
7035 return -ENETDOWN;
7036 }
7037
7038 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007039 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7040 if (!wdev->p2p_started) {
7041 mutex_unlock(&cfg80211_mutex);
7042 if (rtnl)
7043 rtnl_unlock();
7044 return -ENETDOWN;
7045 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007046 }
7047
Johannes Berg89a54e42012-06-15 14:33:17 +02007048 cfg80211_lock_rdev(rdev);
7049
7050 mutex_unlock(&cfg80211_mutex);
7051
Johannes Berg4c476992010-10-04 21:36:35 +02007052 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007053 }
7054
7055 return 0;
7056}
7057
7058static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7059 struct genl_info *info)
7060{
7061 if (info->user_ptr[0])
7062 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007063 if (info->user_ptr[1]) {
7064 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7065 struct wireless_dev *wdev = info->user_ptr[1];
7066
7067 if (wdev->netdev)
7068 dev_put(wdev->netdev);
7069 } else {
7070 dev_put(info->user_ptr[1]);
7071 }
7072 }
Johannes Berg4c476992010-10-04 21:36:35 +02007073 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7074 rtnl_unlock();
7075}
7076
Johannes Berg55682962007-09-20 13:09:35 -04007077static struct genl_ops nl80211_ops[] = {
7078 {
7079 .cmd = NL80211_CMD_GET_WIPHY,
7080 .doit = nl80211_get_wiphy,
7081 .dumpit = nl80211_dump_wiphy,
7082 .policy = nl80211_policy,
7083 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007084 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007085 },
7086 {
7087 .cmd = NL80211_CMD_SET_WIPHY,
7088 .doit = nl80211_set_wiphy,
7089 .policy = nl80211_policy,
7090 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007091 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007092 },
7093 {
7094 .cmd = NL80211_CMD_GET_INTERFACE,
7095 .doit = nl80211_get_interface,
7096 .dumpit = nl80211_dump_interface,
7097 .policy = nl80211_policy,
7098 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007099 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007100 },
7101 {
7102 .cmd = NL80211_CMD_SET_INTERFACE,
7103 .doit = nl80211_set_interface,
7104 .policy = nl80211_policy,
7105 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007106 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7107 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007108 },
7109 {
7110 .cmd = NL80211_CMD_NEW_INTERFACE,
7111 .doit = nl80211_new_interface,
7112 .policy = nl80211_policy,
7113 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007114 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7115 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007116 },
7117 {
7118 .cmd = NL80211_CMD_DEL_INTERFACE,
7119 .doit = nl80211_del_interface,
7120 .policy = nl80211_policy,
7121 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007122 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007123 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007124 },
Johannes Berg41ade002007-12-19 02:03:29 +01007125 {
7126 .cmd = NL80211_CMD_GET_KEY,
7127 .doit = nl80211_get_key,
7128 .policy = nl80211_policy,
7129 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007130 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007131 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007132 },
7133 {
7134 .cmd = NL80211_CMD_SET_KEY,
7135 .doit = nl80211_set_key,
7136 .policy = nl80211_policy,
7137 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007138 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007139 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007140 },
7141 {
7142 .cmd = NL80211_CMD_NEW_KEY,
7143 .doit = nl80211_new_key,
7144 .policy = nl80211_policy,
7145 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007146 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007147 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007148 },
7149 {
7150 .cmd = NL80211_CMD_DEL_KEY,
7151 .doit = nl80211_del_key,
7152 .policy = nl80211_policy,
7153 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007154 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007155 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007156 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007157 {
7158 .cmd = NL80211_CMD_SET_BEACON,
7159 .policy = nl80211_policy,
7160 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007161 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007162 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007163 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007164 },
7165 {
Johannes Berg88600202012-02-13 15:17:18 +01007166 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007167 .policy = nl80211_policy,
7168 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007169 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007170 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007171 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007172 },
7173 {
Johannes Berg88600202012-02-13 15:17:18 +01007174 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007175 .policy = nl80211_policy,
7176 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007177 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007178 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007179 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007180 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007181 {
7182 .cmd = NL80211_CMD_GET_STATION,
7183 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007184 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007185 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007186 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7187 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007188 },
7189 {
7190 .cmd = NL80211_CMD_SET_STATION,
7191 .doit = nl80211_set_station,
7192 .policy = nl80211_policy,
7193 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007194 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007195 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007196 },
7197 {
7198 .cmd = NL80211_CMD_NEW_STATION,
7199 .doit = nl80211_new_station,
7200 .policy = nl80211_policy,
7201 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007202 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007203 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007204 },
7205 {
7206 .cmd = NL80211_CMD_DEL_STATION,
7207 .doit = nl80211_del_station,
7208 .policy = nl80211_policy,
7209 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007210 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007211 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007212 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007213 {
7214 .cmd = NL80211_CMD_GET_MPATH,
7215 .doit = nl80211_get_mpath,
7216 .dumpit = nl80211_dump_mpath,
7217 .policy = nl80211_policy,
7218 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007219 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007220 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007221 },
7222 {
7223 .cmd = NL80211_CMD_SET_MPATH,
7224 .doit = nl80211_set_mpath,
7225 .policy = nl80211_policy,
7226 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007227 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007228 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007229 },
7230 {
7231 .cmd = NL80211_CMD_NEW_MPATH,
7232 .doit = nl80211_new_mpath,
7233 .policy = nl80211_policy,
7234 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007235 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007236 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007237 },
7238 {
7239 .cmd = NL80211_CMD_DEL_MPATH,
7240 .doit = nl80211_del_mpath,
7241 .policy = nl80211_policy,
7242 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007243 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007244 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007245 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007246 {
7247 .cmd = NL80211_CMD_SET_BSS,
7248 .doit = nl80211_set_bss,
7249 .policy = nl80211_policy,
7250 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007251 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007252 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007253 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007254 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007255 .cmd = NL80211_CMD_GET_REG,
7256 .doit = nl80211_get_reg,
7257 .policy = nl80211_policy,
7258 /* can be retrieved by unprivileged users */
7259 },
7260 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007261 .cmd = NL80211_CMD_SET_REG,
7262 .doit = nl80211_set_reg,
7263 .policy = nl80211_policy,
7264 .flags = GENL_ADMIN_PERM,
7265 },
7266 {
7267 .cmd = NL80211_CMD_REQ_SET_REG,
7268 .doit = nl80211_req_set_reg,
7269 .policy = nl80211_policy,
7270 .flags = GENL_ADMIN_PERM,
7271 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007272 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007273 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7274 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007275 .policy = nl80211_policy,
7276 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007277 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007278 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007279 },
7280 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007281 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7282 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007283 .policy = nl80211_policy,
7284 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007285 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007286 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007287 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007288 {
Johannes Berg2a519312009-02-10 21:25:55 +01007289 .cmd = NL80211_CMD_TRIGGER_SCAN,
7290 .doit = nl80211_trigger_scan,
7291 .policy = nl80211_policy,
7292 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007293 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007294 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007295 },
7296 {
7297 .cmd = NL80211_CMD_GET_SCAN,
7298 .policy = nl80211_policy,
7299 .dumpit = nl80211_dump_scan,
7300 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007301 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007302 .cmd = NL80211_CMD_START_SCHED_SCAN,
7303 .doit = nl80211_start_sched_scan,
7304 .policy = nl80211_policy,
7305 .flags = GENL_ADMIN_PERM,
7306 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7307 NL80211_FLAG_NEED_RTNL,
7308 },
7309 {
7310 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7311 .doit = nl80211_stop_sched_scan,
7312 .policy = nl80211_policy,
7313 .flags = GENL_ADMIN_PERM,
7314 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7315 NL80211_FLAG_NEED_RTNL,
7316 },
7317 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007318 .cmd = NL80211_CMD_AUTHENTICATE,
7319 .doit = nl80211_authenticate,
7320 .policy = nl80211_policy,
7321 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007322 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007323 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007324 },
7325 {
7326 .cmd = NL80211_CMD_ASSOCIATE,
7327 .doit = nl80211_associate,
7328 .policy = nl80211_policy,
7329 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007330 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007331 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007332 },
7333 {
7334 .cmd = NL80211_CMD_DEAUTHENTICATE,
7335 .doit = nl80211_deauthenticate,
7336 .policy = nl80211_policy,
7337 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007338 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007339 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007340 },
7341 {
7342 .cmd = NL80211_CMD_DISASSOCIATE,
7343 .doit = nl80211_disassociate,
7344 .policy = nl80211_policy,
7345 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007346 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007347 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007348 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007349 {
7350 .cmd = NL80211_CMD_JOIN_IBSS,
7351 .doit = nl80211_join_ibss,
7352 .policy = nl80211_policy,
7353 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007354 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007355 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007356 },
7357 {
7358 .cmd = NL80211_CMD_LEAVE_IBSS,
7359 .doit = nl80211_leave_ibss,
7360 .policy = nl80211_policy,
7361 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007362 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007363 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007364 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007365#ifdef CONFIG_NL80211_TESTMODE
7366 {
7367 .cmd = NL80211_CMD_TESTMODE,
7368 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007369 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007370 .policy = nl80211_policy,
7371 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007372 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7373 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007374 },
7375#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007376 {
7377 .cmd = NL80211_CMD_CONNECT,
7378 .doit = nl80211_connect,
7379 .policy = nl80211_policy,
7380 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007381 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007382 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007383 },
7384 {
7385 .cmd = NL80211_CMD_DISCONNECT,
7386 .doit = nl80211_disconnect,
7387 .policy = nl80211_policy,
7388 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007389 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007390 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007391 },
Johannes Berg463d0182009-07-14 00:33:35 +02007392 {
7393 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7394 .doit = nl80211_wiphy_netns,
7395 .policy = nl80211_policy,
7396 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007397 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7398 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007399 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007400 {
7401 .cmd = NL80211_CMD_GET_SURVEY,
7402 .policy = nl80211_policy,
7403 .dumpit = nl80211_dump_survey,
7404 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007405 {
7406 .cmd = NL80211_CMD_SET_PMKSA,
7407 .doit = nl80211_setdel_pmksa,
7408 .policy = nl80211_policy,
7409 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007410 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007411 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007412 },
7413 {
7414 .cmd = NL80211_CMD_DEL_PMKSA,
7415 .doit = nl80211_setdel_pmksa,
7416 .policy = nl80211_policy,
7417 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007418 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007419 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007420 },
7421 {
7422 .cmd = NL80211_CMD_FLUSH_PMKSA,
7423 .doit = nl80211_flush_pmksa,
7424 .policy = nl80211_policy,
7425 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007426 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007427 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007428 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007429 {
7430 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7431 .doit = nl80211_remain_on_channel,
7432 .policy = nl80211_policy,
7433 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007434 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007435 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007436 },
7437 {
7438 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7439 .doit = nl80211_cancel_remain_on_channel,
7440 .policy = nl80211_policy,
7441 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007442 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007443 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007444 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007445 {
7446 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7447 .doit = nl80211_set_tx_bitrate_mask,
7448 .policy = nl80211_policy,
7449 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007450 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7451 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007452 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007453 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007454 .cmd = NL80211_CMD_REGISTER_FRAME,
7455 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007456 .policy = nl80211_policy,
7457 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007458 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007459 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007460 },
7461 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007462 .cmd = NL80211_CMD_FRAME,
7463 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007464 .policy = nl80211_policy,
7465 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007466 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007467 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007468 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007469 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007470 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7471 .doit = nl80211_tx_mgmt_cancel_wait,
7472 .policy = nl80211_policy,
7473 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007474 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007475 NL80211_FLAG_NEED_RTNL,
7476 },
7477 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007478 .cmd = NL80211_CMD_SET_POWER_SAVE,
7479 .doit = nl80211_set_power_save,
7480 .policy = nl80211_policy,
7481 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007482 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7483 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007484 },
7485 {
7486 .cmd = NL80211_CMD_GET_POWER_SAVE,
7487 .doit = nl80211_get_power_save,
7488 .policy = nl80211_policy,
7489 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007490 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7491 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007492 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007493 {
7494 .cmd = NL80211_CMD_SET_CQM,
7495 .doit = nl80211_set_cqm,
7496 .policy = nl80211_policy,
7497 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007498 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7499 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007500 },
Johannes Bergf444de02010-05-05 15:25:02 +02007501 {
7502 .cmd = NL80211_CMD_SET_CHANNEL,
7503 .doit = nl80211_set_channel,
7504 .policy = nl80211_policy,
7505 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007506 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7507 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007508 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007509 {
7510 .cmd = NL80211_CMD_SET_WDS_PEER,
7511 .doit = nl80211_set_wds_peer,
7512 .policy = nl80211_policy,
7513 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007514 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7515 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007516 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007517 {
7518 .cmd = NL80211_CMD_JOIN_MESH,
7519 .doit = nl80211_join_mesh,
7520 .policy = nl80211_policy,
7521 .flags = GENL_ADMIN_PERM,
7522 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7523 NL80211_FLAG_NEED_RTNL,
7524 },
7525 {
7526 .cmd = NL80211_CMD_LEAVE_MESH,
7527 .doit = nl80211_leave_mesh,
7528 .policy = nl80211_policy,
7529 .flags = GENL_ADMIN_PERM,
7530 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7531 NL80211_FLAG_NEED_RTNL,
7532 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007533#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007534 {
7535 .cmd = NL80211_CMD_GET_WOWLAN,
7536 .doit = nl80211_get_wowlan,
7537 .policy = nl80211_policy,
7538 /* can be retrieved by unprivileged users */
7539 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7540 NL80211_FLAG_NEED_RTNL,
7541 },
7542 {
7543 .cmd = NL80211_CMD_SET_WOWLAN,
7544 .doit = nl80211_set_wowlan,
7545 .policy = nl80211_policy,
7546 .flags = GENL_ADMIN_PERM,
7547 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7548 NL80211_FLAG_NEED_RTNL,
7549 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007550#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007551 {
7552 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7553 .doit = nl80211_set_rekey_data,
7554 .policy = nl80211_policy,
7555 .flags = GENL_ADMIN_PERM,
7556 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7557 NL80211_FLAG_NEED_RTNL,
7558 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007559 {
7560 .cmd = NL80211_CMD_TDLS_MGMT,
7561 .doit = nl80211_tdls_mgmt,
7562 .policy = nl80211_policy,
7563 .flags = GENL_ADMIN_PERM,
7564 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7565 NL80211_FLAG_NEED_RTNL,
7566 },
7567 {
7568 .cmd = NL80211_CMD_TDLS_OPER,
7569 .doit = nl80211_tdls_oper,
7570 .policy = nl80211_policy,
7571 .flags = GENL_ADMIN_PERM,
7572 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7573 NL80211_FLAG_NEED_RTNL,
7574 },
Johannes Berg28946da2011-11-04 11:18:12 +01007575 {
7576 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7577 .doit = nl80211_register_unexpected_frame,
7578 .policy = nl80211_policy,
7579 .flags = GENL_ADMIN_PERM,
7580 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7581 NL80211_FLAG_NEED_RTNL,
7582 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007583 {
7584 .cmd = NL80211_CMD_PROBE_CLIENT,
7585 .doit = nl80211_probe_client,
7586 .policy = nl80211_policy,
7587 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007588 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007589 NL80211_FLAG_NEED_RTNL,
7590 },
Johannes Berg5e760232011-11-04 11:18:17 +01007591 {
7592 .cmd = NL80211_CMD_REGISTER_BEACONS,
7593 .doit = nl80211_register_beacons,
7594 .policy = nl80211_policy,
7595 .flags = GENL_ADMIN_PERM,
7596 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7597 NL80211_FLAG_NEED_RTNL,
7598 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007599 {
7600 .cmd = NL80211_CMD_SET_NOACK_MAP,
7601 .doit = nl80211_set_noack_map,
7602 .policy = nl80211_policy,
7603 .flags = GENL_ADMIN_PERM,
7604 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7605 NL80211_FLAG_NEED_RTNL,
7606 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007607 {
7608 .cmd = NL80211_CMD_START_P2P_DEVICE,
7609 .doit = nl80211_start_p2p_device,
7610 .policy = nl80211_policy,
7611 .flags = GENL_ADMIN_PERM,
7612 .internal_flags = NL80211_FLAG_NEED_WDEV |
7613 NL80211_FLAG_NEED_RTNL,
7614 },
7615 {
7616 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7617 .doit = nl80211_stop_p2p_device,
7618 .policy = nl80211_policy,
7619 .flags = GENL_ADMIN_PERM,
7620 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7621 NL80211_FLAG_NEED_RTNL,
7622 },
Johannes Berg55682962007-09-20 13:09:35 -04007623};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007624
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007625static struct genl_multicast_group nl80211_mlme_mcgrp = {
7626 .name = "mlme",
7627};
Johannes Berg55682962007-09-20 13:09:35 -04007628
7629/* multicast groups */
7630static struct genl_multicast_group nl80211_config_mcgrp = {
7631 .name = "config",
7632};
Johannes Berg2a519312009-02-10 21:25:55 +01007633static struct genl_multicast_group nl80211_scan_mcgrp = {
7634 .name = "scan",
7635};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007636static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7637 .name = "regulatory",
7638};
Johannes Berg55682962007-09-20 13:09:35 -04007639
7640/* notification functions */
7641
7642void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7643{
7644 struct sk_buff *msg;
7645
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007646 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007647 if (!msg)
7648 return;
7649
7650 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7651 nlmsg_free(msg);
7652 return;
7653 }
7654
Johannes Berg463d0182009-07-14 00:33:35 +02007655 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7656 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007657}
7658
Johannes Berg362a4152009-05-24 16:43:15 +02007659static int nl80211_add_scan_req(struct sk_buff *msg,
7660 struct cfg80211_registered_device *rdev)
7661{
7662 struct cfg80211_scan_request *req = rdev->scan_req;
7663 struct nlattr *nest;
7664 int i;
7665
Johannes Berg667503d2009-07-07 03:56:11 +02007666 ASSERT_RDEV_LOCK(rdev);
7667
Johannes Berg362a4152009-05-24 16:43:15 +02007668 if (WARN_ON(!req))
7669 return 0;
7670
7671 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7672 if (!nest)
7673 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007674 for (i = 0; i < req->n_ssids; i++) {
7675 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7676 goto nla_put_failure;
7677 }
Johannes Berg362a4152009-05-24 16:43:15 +02007678 nla_nest_end(msg, nest);
7679
7680 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7681 if (!nest)
7682 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007683 for (i = 0; i < req->n_channels; i++) {
7684 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7685 goto nla_put_failure;
7686 }
Johannes Berg362a4152009-05-24 16:43:15 +02007687 nla_nest_end(msg, nest);
7688
David S. Miller9360ffd2012-03-29 04:41:26 -04007689 if (req->ie &&
7690 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7691 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007692
Sam Lefflered4737712012-10-11 21:03:31 -07007693 if (req->flags)
7694 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7695
Johannes Berg362a4152009-05-24 16:43:15 +02007696 return 0;
7697 nla_put_failure:
7698 return -ENOBUFS;
7699}
7700
Johannes Berga538e2d2009-06-16 19:56:42 +02007701static int nl80211_send_scan_msg(struct sk_buff *msg,
7702 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007703 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007704 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007705 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007706{
7707 void *hdr;
7708
Eric W. Biederman15e47302012-09-07 20:12:54 +00007709 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007710 if (!hdr)
7711 return -1;
7712
David S. Miller9360ffd2012-03-29 04:41:26 -04007713 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007714 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7715 wdev->netdev->ifindex)) ||
7716 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007717 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007718
Johannes Berg362a4152009-05-24 16:43:15 +02007719 /* ignore errors and send incomplete event anyway */
7720 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007721
7722 return genlmsg_end(msg, hdr);
7723
7724 nla_put_failure:
7725 genlmsg_cancel(msg, hdr);
7726 return -EMSGSIZE;
7727}
7728
Luciano Coelho807f8a82011-05-11 17:09:35 +03007729static int
7730nl80211_send_sched_scan_msg(struct sk_buff *msg,
7731 struct cfg80211_registered_device *rdev,
7732 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007733 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007734{
7735 void *hdr;
7736
Eric W. Biederman15e47302012-09-07 20:12:54 +00007737 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007738 if (!hdr)
7739 return -1;
7740
David S. Miller9360ffd2012-03-29 04:41:26 -04007741 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7742 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7743 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007744
7745 return genlmsg_end(msg, hdr);
7746
7747 nla_put_failure:
7748 genlmsg_cancel(msg, hdr);
7749 return -EMSGSIZE;
7750}
7751
Johannes Berga538e2d2009-06-16 19:56:42 +02007752void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007753 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007754{
7755 struct sk_buff *msg;
7756
Thomas Graf58050fc2012-06-28 03:57:45 +00007757 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007758 if (!msg)
7759 return;
7760
Johannes Bergfd014282012-06-18 19:17:03 +02007761 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007762 NL80211_CMD_TRIGGER_SCAN) < 0) {
7763 nlmsg_free(msg);
7764 return;
7765 }
7766
Johannes Berg463d0182009-07-14 00:33:35 +02007767 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7768 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007769}
7770
Johannes Berg2a519312009-02-10 21:25:55 +01007771void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007772 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007773{
7774 struct sk_buff *msg;
7775
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007776 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007777 if (!msg)
7778 return;
7779
Johannes Bergfd014282012-06-18 19:17:03 +02007780 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007781 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007782 nlmsg_free(msg);
7783 return;
7784 }
7785
Johannes Berg463d0182009-07-14 00:33:35 +02007786 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7787 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007788}
7789
7790void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007791 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007792{
7793 struct sk_buff *msg;
7794
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007795 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007796 if (!msg)
7797 return;
7798
Johannes Bergfd014282012-06-18 19:17:03 +02007799 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007800 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007801 nlmsg_free(msg);
7802 return;
7803 }
7804
Johannes Berg463d0182009-07-14 00:33:35 +02007805 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7806 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007807}
7808
Luciano Coelho807f8a82011-05-11 17:09:35 +03007809void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7810 struct net_device *netdev)
7811{
7812 struct sk_buff *msg;
7813
7814 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7815 if (!msg)
7816 return;
7817
7818 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7819 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7820 nlmsg_free(msg);
7821 return;
7822 }
7823
7824 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7825 nl80211_scan_mcgrp.id, GFP_KERNEL);
7826}
7827
7828void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7829 struct net_device *netdev, u32 cmd)
7830{
7831 struct sk_buff *msg;
7832
Thomas Graf58050fc2012-06-28 03:57:45 +00007833 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007834 if (!msg)
7835 return;
7836
7837 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7838 nlmsg_free(msg);
7839 return;
7840 }
7841
7842 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7843 nl80211_scan_mcgrp.id, GFP_KERNEL);
7844}
7845
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007846/*
7847 * This can happen on global regulatory changes or device specific settings
7848 * based on custom world regulatory domains.
7849 */
7850void nl80211_send_reg_change_event(struct regulatory_request *request)
7851{
7852 struct sk_buff *msg;
7853 void *hdr;
7854
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007855 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007856 if (!msg)
7857 return;
7858
7859 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7860 if (!hdr) {
7861 nlmsg_free(msg);
7862 return;
7863 }
7864
7865 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007866 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7867 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007868
David S. Miller9360ffd2012-03-29 04:41:26 -04007869 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7870 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7871 NL80211_REGDOM_TYPE_WORLD))
7872 goto nla_put_failure;
7873 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7874 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7875 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7876 goto nla_put_failure;
7877 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7878 request->intersect) {
7879 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7880 NL80211_REGDOM_TYPE_INTERSECTION))
7881 goto nla_put_failure;
7882 } else {
7883 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7884 NL80211_REGDOM_TYPE_COUNTRY) ||
7885 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7886 request->alpha2))
7887 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007888 }
7889
David S. Miller9360ffd2012-03-29 04:41:26 -04007890 if (wiphy_idx_valid(request->wiphy_idx) &&
7891 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7892 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007893
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007894 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007895
Johannes Bergbc43b282009-07-25 10:54:13 +02007896 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007897 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007898 GFP_ATOMIC);
7899 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007900
7901 return;
7902
7903nla_put_failure:
7904 genlmsg_cancel(msg, hdr);
7905 nlmsg_free(msg);
7906}
7907
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007908static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7909 struct net_device *netdev,
7910 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007911 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007912{
7913 struct sk_buff *msg;
7914 void *hdr;
7915
Johannes Berge6d6e342009-07-01 21:26:47 +02007916 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007917 if (!msg)
7918 return;
7919
7920 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7921 if (!hdr) {
7922 nlmsg_free(msg);
7923 return;
7924 }
7925
David S. Miller9360ffd2012-03-29 04:41:26 -04007926 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7927 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7928 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7929 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007930
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007931 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007932
Johannes Berg463d0182009-07-14 00:33:35 +02007933 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7934 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007935 return;
7936
7937 nla_put_failure:
7938 genlmsg_cancel(msg, hdr);
7939 nlmsg_free(msg);
7940}
7941
7942void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007943 struct net_device *netdev, const u8 *buf,
7944 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007945{
7946 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007947 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007948}
7949
7950void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7951 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007952 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007953{
Johannes Berge6d6e342009-07-01 21:26:47 +02007954 nl80211_send_mlme_event(rdev, netdev, buf, len,
7955 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007956}
7957
Jouni Malinen53b46b82009-03-27 20:53:56 +02007958void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007959 struct net_device *netdev, const u8 *buf,
7960 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007961{
7962 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007963 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007964}
7965
Jouni Malinen53b46b82009-03-27 20:53:56 +02007966void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7967 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007968 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007969{
7970 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007971 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007972}
7973
Jouni Malinencf4e5942010-12-16 00:52:40 +02007974void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7975 struct net_device *netdev, const u8 *buf,
7976 size_t len, gfp_t gfp)
7977{
7978 nl80211_send_mlme_event(rdev, netdev, buf, len,
7979 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7980}
7981
7982void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7983 struct net_device *netdev, const u8 *buf,
7984 size_t len, gfp_t gfp)
7985{
7986 nl80211_send_mlme_event(rdev, netdev, buf, len,
7987 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
7988}
7989
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04007990static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
7991 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02007992 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03007993{
7994 struct sk_buff *msg;
7995 void *hdr;
7996
Johannes Berge6d6e342009-07-01 21:26:47 +02007997 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03007998 if (!msg)
7999 return;
8000
8001 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8002 if (!hdr) {
8003 nlmsg_free(msg);
8004 return;
8005 }
8006
David S. Miller9360ffd2012-03-29 04:41:26 -04008007 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8008 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8009 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8010 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8011 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008012
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008013 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008014
Johannes Berg463d0182009-07-14 00:33:35 +02008015 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8016 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008017 return;
8018
8019 nla_put_failure:
8020 genlmsg_cancel(msg, hdr);
8021 nlmsg_free(msg);
8022}
8023
8024void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008025 struct net_device *netdev, const u8 *addr,
8026 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008027{
8028 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008029 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008030}
8031
8032void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008033 struct net_device *netdev, const u8 *addr,
8034 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008035{
Johannes Berge6d6e342009-07-01 21:26:47 +02008036 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8037 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008038}
8039
Samuel Ortizb23aa672009-07-01 21:26:54 +02008040void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8041 struct net_device *netdev, const u8 *bssid,
8042 const u8 *req_ie, size_t req_ie_len,
8043 const u8 *resp_ie, size_t resp_ie_len,
8044 u16 status, gfp_t gfp)
8045{
8046 struct sk_buff *msg;
8047 void *hdr;
8048
Thomas Graf58050fc2012-06-28 03:57:45 +00008049 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008050 if (!msg)
8051 return;
8052
8053 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8054 if (!hdr) {
8055 nlmsg_free(msg);
8056 return;
8057 }
8058
David S. Miller9360ffd2012-03-29 04:41:26 -04008059 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8060 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8061 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8062 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8063 (req_ie &&
8064 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8065 (resp_ie &&
8066 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8067 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008068
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008069 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008070
Johannes Berg463d0182009-07-14 00:33:35 +02008071 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8072 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008073 return;
8074
8075 nla_put_failure:
8076 genlmsg_cancel(msg, hdr);
8077 nlmsg_free(msg);
8078
8079}
8080
8081void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8082 struct net_device *netdev, const u8 *bssid,
8083 const u8 *req_ie, size_t req_ie_len,
8084 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8085{
8086 struct sk_buff *msg;
8087 void *hdr;
8088
Thomas Graf58050fc2012-06-28 03:57:45 +00008089 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008090 if (!msg)
8091 return;
8092
8093 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8094 if (!hdr) {
8095 nlmsg_free(msg);
8096 return;
8097 }
8098
David S. Miller9360ffd2012-03-29 04:41:26 -04008099 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8100 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8101 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8102 (req_ie &&
8103 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8104 (resp_ie &&
8105 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8106 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008107
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008108 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008109
Johannes Berg463d0182009-07-14 00:33:35 +02008110 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8111 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008112 return;
8113
8114 nla_put_failure:
8115 genlmsg_cancel(msg, hdr);
8116 nlmsg_free(msg);
8117
8118}
8119
8120void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8121 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02008122 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008123{
8124 struct sk_buff *msg;
8125 void *hdr;
8126
Thomas Graf58050fc2012-06-28 03:57:45 +00008127 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008128 if (!msg)
8129 return;
8130
8131 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8132 if (!hdr) {
8133 nlmsg_free(msg);
8134 return;
8135 }
8136
David S. Miller9360ffd2012-03-29 04:41:26 -04008137 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8138 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8139 (from_ap && reason &&
8140 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8141 (from_ap &&
8142 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8143 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8144 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008145
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008146 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008147
Johannes Berg463d0182009-07-14 00:33:35 +02008148 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8149 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008150 return;
8151
8152 nla_put_failure:
8153 genlmsg_cancel(msg, hdr);
8154 nlmsg_free(msg);
8155
8156}
8157
Johannes Berg04a773a2009-04-19 21:24:32 +02008158void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8159 struct net_device *netdev, const u8 *bssid,
8160 gfp_t gfp)
8161{
8162 struct sk_buff *msg;
8163 void *hdr;
8164
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008165 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008166 if (!msg)
8167 return;
8168
8169 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8170 if (!hdr) {
8171 nlmsg_free(msg);
8172 return;
8173 }
8174
David S. Miller9360ffd2012-03-29 04:41:26 -04008175 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8176 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8177 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8178 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008179
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008180 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008181
Johannes Berg463d0182009-07-14 00:33:35 +02008182 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8183 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008184 return;
8185
8186 nla_put_failure:
8187 genlmsg_cancel(msg, hdr);
8188 nlmsg_free(msg);
8189}
8190
Javier Cardonac93b5e72011-04-07 15:08:34 -07008191void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8192 struct net_device *netdev,
8193 const u8 *macaddr, const u8* ie, u8 ie_len,
8194 gfp_t gfp)
8195{
8196 struct sk_buff *msg;
8197 void *hdr;
8198
8199 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8200 if (!msg)
8201 return;
8202
8203 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8204 if (!hdr) {
8205 nlmsg_free(msg);
8206 return;
8207 }
8208
David S. Miller9360ffd2012-03-29 04:41:26 -04008209 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8210 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8211 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8212 (ie_len && ie &&
8213 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8214 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008215
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008216 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008217
8218 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8219 nl80211_mlme_mcgrp.id, gfp);
8220 return;
8221
8222 nla_put_failure:
8223 genlmsg_cancel(msg, hdr);
8224 nlmsg_free(msg);
8225}
8226
Jouni Malinena3b8b052009-03-27 21:59:49 +02008227void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8228 struct net_device *netdev, const u8 *addr,
8229 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008230 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008231{
8232 struct sk_buff *msg;
8233 void *hdr;
8234
Johannes Berge6d6e342009-07-01 21:26:47 +02008235 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008236 if (!msg)
8237 return;
8238
8239 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8240 if (!hdr) {
8241 nlmsg_free(msg);
8242 return;
8243 }
8244
David S. Miller9360ffd2012-03-29 04:41:26 -04008245 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8246 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8247 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8248 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8249 (key_id != -1 &&
8250 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8251 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8252 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008253
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008254 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008255
Johannes Berg463d0182009-07-14 00:33:35 +02008256 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8257 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008258 return;
8259
8260 nla_put_failure:
8261 genlmsg_cancel(msg, hdr);
8262 nlmsg_free(msg);
8263}
8264
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008265void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8266 struct ieee80211_channel *channel_before,
8267 struct ieee80211_channel *channel_after)
8268{
8269 struct sk_buff *msg;
8270 void *hdr;
8271 struct nlattr *nl_freq;
8272
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008273 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008274 if (!msg)
8275 return;
8276
8277 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8278 if (!hdr) {
8279 nlmsg_free(msg);
8280 return;
8281 }
8282
8283 /*
8284 * Since we are applying the beacon hint to a wiphy we know its
8285 * wiphy_idx is valid
8286 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008287 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8288 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008289
8290 /* Before */
8291 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8292 if (!nl_freq)
8293 goto nla_put_failure;
8294 if (nl80211_msg_put_channel(msg, channel_before))
8295 goto nla_put_failure;
8296 nla_nest_end(msg, nl_freq);
8297
8298 /* After */
8299 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8300 if (!nl_freq)
8301 goto nla_put_failure;
8302 if (nl80211_msg_put_channel(msg, channel_after))
8303 goto nla_put_failure;
8304 nla_nest_end(msg, nl_freq);
8305
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008306 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008307
Johannes Berg463d0182009-07-14 00:33:35 +02008308 rcu_read_lock();
8309 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8310 GFP_ATOMIC);
8311 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008312
8313 return;
8314
8315nla_put_failure:
8316 genlmsg_cancel(msg, hdr);
8317 nlmsg_free(msg);
8318}
8319
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008320static void nl80211_send_remain_on_chan_event(
8321 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008322 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008323 struct ieee80211_channel *chan,
8324 enum nl80211_channel_type channel_type,
8325 unsigned int duration, gfp_t gfp)
8326{
8327 struct sk_buff *msg;
8328 void *hdr;
8329
8330 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8331 if (!msg)
8332 return;
8333
8334 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8335 if (!hdr) {
8336 nlmsg_free(msg);
8337 return;
8338 }
8339
David S. Miller9360ffd2012-03-29 04:41:26 -04008340 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008341 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8342 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008343 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008344 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
8345 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
8346 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8347 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008348
David S. Miller9360ffd2012-03-29 04:41:26 -04008349 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8350 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8351 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008352
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008353 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008354
8355 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8356 nl80211_mlme_mcgrp.id, gfp);
8357 return;
8358
8359 nla_put_failure:
8360 genlmsg_cancel(msg, hdr);
8361 nlmsg_free(msg);
8362}
8363
8364void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008365 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008366 struct ieee80211_channel *chan,
8367 enum nl80211_channel_type channel_type,
8368 unsigned int duration, gfp_t gfp)
8369{
8370 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008371 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008372 channel_type, duration, gfp);
8373}
8374
8375void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008376 struct cfg80211_registered_device *rdev,
8377 struct wireless_dev *wdev,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008378 u64 cookie, struct ieee80211_channel *chan,
8379 enum nl80211_channel_type channel_type, gfp_t gfp)
8380{
8381 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008382 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008383 channel_type, 0, gfp);
8384}
8385
Johannes Berg98b62182009-12-23 13:15:44 +01008386void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8387 struct net_device *dev, const u8 *mac_addr,
8388 struct station_info *sinfo, gfp_t gfp)
8389{
8390 struct sk_buff *msg;
8391
Thomas Graf58050fc2012-06-28 03:57:45 +00008392 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008393 if (!msg)
8394 return;
8395
John W. Linville66266b32012-03-15 13:25:41 -04008396 if (nl80211_send_station(msg, 0, 0, 0,
8397 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008398 nlmsg_free(msg);
8399 return;
8400 }
8401
8402 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8403 nl80211_mlme_mcgrp.id, gfp);
8404}
8405
Jouni Malinenec15e682011-03-23 15:29:52 +02008406void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8407 struct net_device *dev, const u8 *mac_addr,
8408 gfp_t gfp)
8409{
8410 struct sk_buff *msg;
8411 void *hdr;
8412
Thomas Graf58050fc2012-06-28 03:57:45 +00008413 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008414 if (!msg)
8415 return;
8416
8417 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8418 if (!hdr) {
8419 nlmsg_free(msg);
8420 return;
8421 }
8422
David S. Miller9360ffd2012-03-29 04:41:26 -04008423 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8424 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8425 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008426
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008427 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008428
8429 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8430 nl80211_mlme_mcgrp.id, gfp);
8431 return;
8432
8433 nla_put_failure:
8434 genlmsg_cancel(msg, hdr);
8435 nlmsg_free(msg);
8436}
8437
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308438void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8439 struct net_device *dev, const u8 *mac_addr,
8440 enum nl80211_connect_failed_reason reason,
8441 gfp_t gfp)
8442{
8443 struct sk_buff *msg;
8444 void *hdr;
8445
8446 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8447 if (!msg)
8448 return;
8449
8450 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8451 if (!hdr) {
8452 nlmsg_free(msg);
8453 return;
8454 }
8455
8456 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8457 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8458 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8459 goto nla_put_failure;
8460
8461 genlmsg_end(msg, hdr);
8462
8463 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8464 nl80211_mlme_mcgrp.id, gfp);
8465 return;
8466
8467 nla_put_failure:
8468 genlmsg_cancel(msg, hdr);
8469 nlmsg_free(msg);
8470}
8471
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008472static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8473 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008474{
8475 struct wireless_dev *wdev = dev->ieee80211_ptr;
8476 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8477 struct sk_buff *msg;
8478 void *hdr;
8479 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008480 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008481
Eric W. Biederman15e47302012-09-07 20:12:54 +00008482 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008483 return false;
8484
8485 msg = nlmsg_new(100, gfp);
8486 if (!msg)
8487 return true;
8488
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008489 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008490 if (!hdr) {
8491 nlmsg_free(msg);
8492 return true;
8493 }
8494
David S. Miller9360ffd2012-03-29 04:41:26 -04008495 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8496 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8497 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8498 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008499
8500 err = genlmsg_end(msg, hdr);
8501 if (err < 0) {
8502 nlmsg_free(msg);
8503 return true;
8504 }
8505
Eric W. Biederman15e47302012-09-07 20:12:54 +00008506 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008507 return true;
8508
8509 nla_put_failure:
8510 genlmsg_cancel(msg, hdr);
8511 nlmsg_free(msg);
8512 return true;
8513}
8514
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008515bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8516{
8517 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8518 addr, gfp);
8519}
8520
8521bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8522 const u8 *addr, gfp_t gfp)
8523{
8524 return __nl80211_unexpected_frame(dev,
8525 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8526 addr, gfp);
8527}
8528
Johannes Berg2e161f72010-08-12 15:38:38 +02008529int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008530 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008531 int freq, int sig_dbm,
8532 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008533{
Johannes Berg71bbc992012-06-15 15:30:18 +02008534 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008535 struct sk_buff *msg;
8536 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008537
8538 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8539 if (!msg)
8540 return -ENOMEM;
8541
Johannes Berg2e161f72010-08-12 15:38:38 +02008542 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008543 if (!hdr) {
8544 nlmsg_free(msg);
8545 return -ENOMEM;
8546 }
8547
David S. Miller9360ffd2012-03-29 04:41:26 -04008548 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008549 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8550 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008551 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8552 (sig_dbm &&
8553 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8554 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8555 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008556
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008557 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008558
Eric W. Biederman15e47302012-09-07 20:12:54 +00008559 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008560
8561 nla_put_failure:
8562 genlmsg_cancel(msg, hdr);
8563 nlmsg_free(msg);
8564 return -ENOBUFS;
8565}
8566
Johannes Berg2e161f72010-08-12 15:38:38 +02008567void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008568 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008569 const u8 *buf, size_t len, bool ack,
8570 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008571{
Johannes Berg71bbc992012-06-15 15:30:18 +02008572 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008573 struct sk_buff *msg;
8574 void *hdr;
8575
8576 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8577 if (!msg)
8578 return;
8579
Johannes Berg2e161f72010-08-12 15:38:38 +02008580 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008581 if (!hdr) {
8582 nlmsg_free(msg);
8583 return;
8584 }
8585
David S. Miller9360ffd2012-03-29 04:41:26 -04008586 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008587 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8588 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008589 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8590 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8591 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8592 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008593
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008594 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008595
8596 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8597 return;
8598
8599 nla_put_failure:
8600 genlmsg_cancel(msg, hdr);
8601 nlmsg_free(msg);
8602}
8603
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008604void
8605nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8606 struct net_device *netdev,
8607 enum nl80211_cqm_rssi_threshold_event rssi_event,
8608 gfp_t gfp)
8609{
8610 struct sk_buff *msg;
8611 struct nlattr *pinfoattr;
8612 void *hdr;
8613
Thomas Graf58050fc2012-06-28 03:57:45 +00008614 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008615 if (!msg)
8616 return;
8617
8618 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8619 if (!hdr) {
8620 nlmsg_free(msg);
8621 return;
8622 }
8623
David S. Miller9360ffd2012-03-29 04:41:26 -04008624 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8625 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8626 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008627
8628 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8629 if (!pinfoattr)
8630 goto nla_put_failure;
8631
David S. Miller9360ffd2012-03-29 04:41:26 -04008632 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8633 rssi_event))
8634 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008635
8636 nla_nest_end(msg, pinfoattr);
8637
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008638 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008639
8640 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8641 nl80211_mlme_mcgrp.id, gfp);
8642 return;
8643
8644 nla_put_failure:
8645 genlmsg_cancel(msg, hdr);
8646 nlmsg_free(msg);
8647}
8648
Johannes Berge5497d72011-07-05 16:35:40 +02008649void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8650 struct net_device *netdev, const u8 *bssid,
8651 const u8 *replay_ctr, gfp_t gfp)
8652{
8653 struct sk_buff *msg;
8654 struct nlattr *rekey_attr;
8655 void *hdr;
8656
Thomas Graf58050fc2012-06-28 03:57:45 +00008657 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008658 if (!msg)
8659 return;
8660
8661 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8662 if (!hdr) {
8663 nlmsg_free(msg);
8664 return;
8665 }
8666
David S. Miller9360ffd2012-03-29 04:41:26 -04008667 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8668 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8669 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8670 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008671
8672 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8673 if (!rekey_attr)
8674 goto nla_put_failure;
8675
David S. Miller9360ffd2012-03-29 04:41:26 -04008676 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8677 NL80211_REPLAY_CTR_LEN, replay_ctr))
8678 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008679
8680 nla_nest_end(msg, rekey_attr);
8681
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008682 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008683
8684 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8685 nl80211_mlme_mcgrp.id, gfp);
8686 return;
8687
8688 nla_put_failure:
8689 genlmsg_cancel(msg, hdr);
8690 nlmsg_free(msg);
8691}
8692
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008693void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8694 struct net_device *netdev, int index,
8695 const u8 *bssid, bool preauth, gfp_t gfp)
8696{
8697 struct sk_buff *msg;
8698 struct nlattr *attr;
8699 void *hdr;
8700
Thomas Graf58050fc2012-06-28 03:57:45 +00008701 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008702 if (!msg)
8703 return;
8704
8705 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8706 if (!hdr) {
8707 nlmsg_free(msg);
8708 return;
8709 }
8710
David S. Miller9360ffd2012-03-29 04:41:26 -04008711 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8712 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8713 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008714
8715 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8716 if (!attr)
8717 goto nla_put_failure;
8718
David S. Miller9360ffd2012-03-29 04:41:26 -04008719 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8720 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8721 (preauth &&
8722 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8723 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008724
8725 nla_nest_end(msg, attr);
8726
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008727 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008728
8729 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8730 nl80211_mlme_mcgrp.id, gfp);
8731 return;
8732
8733 nla_put_failure:
8734 genlmsg_cancel(msg, hdr);
8735 nlmsg_free(msg);
8736}
8737
Thomas Pedersen53145262012-04-06 13:35:47 -07008738void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8739 struct net_device *netdev, int freq,
8740 enum nl80211_channel_type type, gfp_t gfp)
8741{
8742 struct sk_buff *msg;
8743 void *hdr;
8744
Thomas Graf58050fc2012-06-28 03:57:45 +00008745 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008746 if (!msg)
8747 return;
8748
8749 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8750 if (!hdr) {
8751 nlmsg_free(msg);
8752 return;
8753 }
8754
John W. Linville7eab0f62012-04-12 14:25:14 -04008755 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8756 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8757 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8758 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008759
8760 genlmsg_end(msg, hdr);
8761
8762 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8763 nl80211_mlme_mcgrp.id, gfp);
8764 return;
8765
8766 nla_put_failure:
8767 genlmsg_cancel(msg, hdr);
8768 nlmsg_free(msg);
8769}
8770
Johannes Bergc063dbf2010-11-24 08:10:05 +01008771void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008772nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8773 struct net_device *netdev, const u8 *peer,
8774 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8775{
8776 struct sk_buff *msg;
8777 struct nlattr *pinfoattr;
8778 void *hdr;
8779
8780 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8781 if (!msg)
8782 return;
8783
8784 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8785 if (!hdr) {
8786 nlmsg_free(msg);
8787 return;
8788 }
8789
8790 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8791 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8792 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8793 goto nla_put_failure;
8794
8795 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8796 if (!pinfoattr)
8797 goto nla_put_failure;
8798
8799 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8800 goto nla_put_failure;
8801
8802 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8803 goto nla_put_failure;
8804
8805 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8806 goto nla_put_failure;
8807
8808 nla_nest_end(msg, pinfoattr);
8809
8810 genlmsg_end(msg, hdr);
8811
8812 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8813 nl80211_mlme_mcgrp.id, gfp);
8814 return;
8815
8816 nla_put_failure:
8817 genlmsg_cancel(msg, hdr);
8818 nlmsg_free(msg);
8819}
8820
8821void
Johannes Bergc063dbf2010-11-24 08:10:05 +01008822nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8823 struct net_device *netdev, const u8 *peer,
8824 u32 num_packets, gfp_t gfp)
8825{
8826 struct sk_buff *msg;
8827 struct nlattr *pinfoattr;
8828 void *hdr;
8829
Thomas Graf58050fc2012-06-28 03:57:45 +00008830 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008831 if (!msg)
8832 return;
8833
8834 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8835 if (!hdr) {
8836 nlmsg_free(msg);
8837 return;
8838 }
8839
David S. Miller9360ffd2012-03-29 04:41:26 -04008840 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8841 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8842 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8843 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008844
8845 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8846 if (!pinfoattr)
8847 goto nla_put_failure;
8848
David S. Miller9360ffd2012-03-29 04:41:26 -04008849 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8850 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008851
8852 nla_nest_end(msg, pinfoattr);
8853
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008854 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008855
8856 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8857 nl80211_mlme_mcgrp.id, gfp);
8858 return;
8859
8860 nla_put_failure:
8861 genlmsg_cancel(msg, hdr);
8862 nlmsg_free(msg);
8863}
8864
Johannes Berg7f6cf312011-11-04 11:18:15 +01008865void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8866 u64 cookie, bool acked, gfp_t gfp)
8867{
8868 struct wireless_dev *wdev = dev->ieee80211_ptr;
8869 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8870 struct sk_buff *msg;
8871 void *hdr;
8872 int err;
8873
Thomas Graf58050fc2012-06-28 03:57:45 +00008874 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008875 if (!msg)
8876 return;
8877
8878 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8879 if (!hdr) {
8880 nlmsg_free(msg);
8881 return;
8882 }
8883
David S. Miller9360ffd2012-03-29 04:41:26 -04008884 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8885 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8886 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8887 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8888 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8889 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008890
8891 err = genlmsg_end(msg, hdr);
8892 if (err < 0) {
8893 nlmsg_free(msg);
8894 return;
8895 }
8896
8897 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8898 nl80211_mlme_mcgrp.id, gfp);
8899 return;
8900
8901 nla_put_failure:
8902 genlmsg_cancel(msg, hdr);
8903 nlmsg_free(msg);
8904}
8905EXPORT_SYMBOL(cfg80211_probe_status);
8906
Johannes Berg5e760232011-11-04 11:18:17 +01008907void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8908 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008909 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008910{
8911 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8912 struct sk_buff *msg;
8913 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008914 u32 nlportid = ACCESS_ONCE(rdev->ap_beacons_nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01008915
Eric W. Biederman15e47302012-09-07 20:12:54 +00008916 if (!nlportid)
Johannes Berg5e760232011-11-04 11:18:17 +01008917 return;
8918
8919 msg = nlmsg_new(len + 100, gfp);
8920 if (!msg)
8921 return;
8922
8923 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8924 if (!hdr) {
8925 nlmsg_free(msg);
8926 return;
8927 }
8928
David S. Miller9360ffd2012-03-29 04:41:26 -04008929 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8930 (freq &&
8931 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8932 (sig_dbm &&
8933 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8934 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8935 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008936
8937 genlmsg_end(msg, hdr);
8938
Eric W. Biederman15e47302012-09-07 20:12:54 +00008939 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01008940 return;
8941
8942 nla_put_failure:
8943 genlmsg_cancel(msg, hdr);
8944 nlmsg_free(msg);
8945}
8946EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8947
Jouni Malinen026331c2010-02-15 12:53:10 +02008948static int nl80211_netlink_notify(struct notifier_block * nb,
8949 unsigned long state,
8950 void *_notify)
8951{
8952 struct netlink_notify *notify = _notify;
8953 struct cfg80211_registered_device *rdev;
8954 struct wireless_dev *wdev;
8955
8956 if (state != NETLINK_URELEASE)
8957 return NOTIFY_DONE;
8958
8959 rcu_read_lock();
8960
Johannes Berg5e760232011-11-04 11:18:17 +01008961 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008962 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00008963 cfg80211_mlme_unregister_socket(wdev, notify->portid);
8964 if (rdev->ap_beacons_nlportid == notify->portid)
8965 rdev->ap_beacons_nlportid = 0;
Johannes Berg5e760232011-11-04 11:18:17 +01008966 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008967
8968 rcu_read_unlock();
8969
8970 return NOTIFY_DONE;
8971}
8972
8973static struct notifier_block nl80211_netlink_notifier = {
8974 .notifier_call = nl80211_netlink_notify,
8975};
8976
Johannes Berg55682962007-09-20 13:09:35 -04008977/* initialisation/exit functions */
8978
8979int nl80211_init(void)
8980{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008981 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008982
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008983 err = genl_register_family_with_ops(&nl80211_fam,
8984 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008985 if (err)
8986 return err;
8987
Johannes Berg55682962007-09-20 13:09:35 -04008988 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
8989 if (err)
8990 goto err_out;
8991
Johannes Berg2a519312009-02-10 21:25:55 +01008992 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
8993 if (err)
8994 goto err_out;
8995
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04008996 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
8997 if (err)
8998 goto err_out;
8999
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009000 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9001 if (err)
9002 goto err_out;
9003
Johannes Bergaff89a92009-07-01 21:26:51 +02009004#ifdef CONFIG_NL80211_TESTMODE
9005 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9006 if (err)
9007 goto err_out;
9008#endif
9009
Jouni Malinen026331c2010-02-15 12:53:10 +02009010 err = netlink_register_notifier(&nl80211_netlink_notifier);
9011 if (err)
9012 goto err_out;
9013
Johannes Berg55682962007-09-20 13:09:35 -04009014 return 0;
9015 err_out:
9016 genl_unregister_family(&nl80211_fam);
9017 return err;
9018}
9019
9020void nl80211_exit(void)
9021{
Jouni Malinen026331c2010-02-15 12:53:10 +02009022 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009023 genl_unregister_family(&nl80211_fam);
9024}