blob: 48d754c9adb86167a898d983fd11c6ff62737a3e [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,
Sujith Manoharande7044e2012-10-18 10:19:28 +0530695 struct nlattr *keys, bool *no_ht)
Johannes Bergfffd0932009-07-08 14:22:54 +0200696{
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);
Sujith Manoharande7044e2012-10-18 10:19:28 +0530738
739 if (parse.p.cipher == WLAN_CIPHER_SUITE_WEP40 ||
740 parse.p.cipher == WLAN_CIPHER_SUITE_WEP104) {
741 if (no_ht)
742 *no_ht = true;
743 }
Johannes Bergfffd0932009-07-08 14:22:54 +0200744 }
745
746 return result;
747 error:
748 kfree(result);
749 return ERR_PTR(err);
750}
751
752static int nl80211_key_allowed(struct wireless_dev *wdev)
753{
754 ASSERT_WDEV_LOCK(wdev);
755
Johannes Bergfffd0932009-07-08 14:22:54 +0200756 switch (wdev->iftype) {
757 case NL80211_IFTYPE_AP:
758 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200759 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700760 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200761 break;
762 case NL80211_IFTYPE_ADHOC:
763 if (!wdev->current_bss)
764 return -ENOLINK;
765 break;
766 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200767 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200768 if (wdev->sme_state != CFG80211_SME_CONNECTED)
769 return -ENOLINK;
770 break;
771 default:
772 return -EINVAL;
773 }
774
775 return 0;
776}
777
Johannes Berg7527a782011-05-13 10:58:57 +0200778static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
779{
780 struct nlattr *nl_modes = nla_nest_start(msg, attr);
781 int i;
782
783 if (!nl_modes)
784 goto nla_put_failure;
785
786 i = 0;
787 while (ifmodes) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400788 if ((ifmodes & 1) && nla_put_flag(msg, i))
789 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200790 ifmodes >>= 1;
791 i++;
792 }
793
794 nla_nest_end(msg, nl_modes);
795 return 0;
796
797nla_put_failure:
798 return -ENOBUFS;
799}
800
801static int nl80211_put_iface_combinations(struct wiphy *wiphy,
802 struct sk_buff *msg)
803{
804 struct nlattr *nl_combis;
805 int i, j;
806
807 nl_combis = nla_nest_start(msg,
808 NL80211_ATTR_INTERFACE_COMBINATIONS);
809 if (!nl_combis)
810 goto nla_put_failure;
811
812 for (i = 0; i < wiphy->n_iface_combinations; i++) {
813 const struct ieee80211_iface_combination *c;
814 struct nlattr *nl_combi, *nl_limits;
815
816 c = &wiphy->iface_combinations[i];
817
818 nl_combi = nla_nest_start(msg, i + 1);
819 if (!nl_combi)
820 goto nla_put_failure;
821
822 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
823 if (!nl_limits)
824 goto nla_put_failure;
825
826 for (j = 0; j < c->n_limits; j++) {
827 struct nlattr *nl_limit;
828
829 nl_limit = nla_nest_start(msg, j + 1);
830 if (!nl_limit)
831 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -0400832 if (nla_put_u32(msg, NL80211_IFACE_LIMIT_MAX,
833 c->limits[j].max))
834 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200835 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
836 c->limits[j].types))
837 goto nla_put_failure;
838 nla_nest_end(msg, nl_limit);
839 }
840
841 nla_nest_end(msg, nl_limits);
842
David S. Miller9360ffd2012-03-29 04:41:26 -0400843 if (c->beacon_int_infra_match &&
844 nla_put_flag(msg, NL80211_IFACE_COMB_STA_AP_BI_MATCH))
845 goto nla_put_failure;
846 if (nla_put_u32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
847 c->num_different_channels) ||
848 nla_put_u32(msg, NL80211_IFACE_COMB_MAXNUM,
849 c->max_interfaces))
850 goto nla_put_failure;
Johannes Berg7527a782011-05-13 10:58:57 +0200851
852 nla_nest_end(msg, nl_combi);
853 }
854
855 nla_nest_end(msg, nl_combis);
856
857 return 0;
858nla_put_failure:
859 return -ENOBUFS;
860}
861
Eric W. Biederman15e47302012-09-07 20:12:54 +0000862static int nl80211_send_wiphy(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Berg55682962007-09-20 13:09:35 -0400863 struct cfg80211_registered_device *dev)
864{
865 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100866 struct nlattr *nl_bands, *nl_band;
867 struct nlattr *nl_freqs, *nl_freq;
868 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100869 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100870 enum ieee80211_band band;
871 struct ieee80211_channel *chan;
872 struct ieee80211_rate *rate;
873 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200874 const struct ieee80211_txrx_stypes *mgmt_stypes =
875 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400876
Eric W. Biederman15e47302012-09-07 20:12:54 +0000877 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_WIPHY);
Johannes Berg55682962007-09-20 13:09:35 -0400878 if (!hdr)
879 return -1;
880
David S. Miller9360ffd2012-03-29 04:41:26 -0400881 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx) ||
882 nla_put_string(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy)) ||
883 nla_put_u32(msg, NL80211_ATTR_GENERATION,
884 cfg80211_rdev_list_generation) ||
885 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
886 dev->wiphy.retry_short) ||
887 nla_put_u8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
888 dev->wiphy.retry_long) ||
889 nla_put_u32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
890 dev->wiphy.frag_threshold) ||
891 nla_put_u32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
892 dev->wiphy.rts_threshold) ||
893 nla_put_u8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
894 dev->wiphy.coverage_class) ||
895 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
896 dev->wiphy.max_scan_ssids) ||
897 nla_put_u8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
898 dev->wiphy.max_sched_scan_ssids) ||
899 nla_put_u16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
900 dev->wiphy.max_scan_ie_len) ||
901 nla_put_u16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
902 dev->wiphy.max_sched_scan_ie_len) ||
903 nla_put_u8(msg, NL80211_ATTR_MAX_MATCH_SETS,
904 dev->wiphy.max_match_sets))
905 goto nla_put_failure;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200906
David S. Miller9360ffd2012-03-29 04:41:26 -0400907 if ((dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN) &&
908 nla_put_flag(msg, NL80211_ATTR_SUPPORT_IBSS_RSN))
909 goto nla_put_failure;
910 if ((dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
911 nla_put_flag(msg, NL80211_ATTR_SUPPORT_MESH_AUTH))
912 goto nla_put_failure;
913 if ((dev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
914 nla_put_flag(msg, NL80211_ATTR_SUPPORT_AP_UAPSD))
915 goto nla_put_failure;
916 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_FW_ROAM) &&
917 nla_put_flag(msg, NL80211_ATTR_ROAM_SUPPORT))
918 goto nla_put_failure;
919 if ((dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) &&
920 nla_put_flag(msg, NL80211_ATTR_TDLS_SUPPORT))
921 goto nla_put_failure;
922 if ((dev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP) &&
923 nla_put_flag(msg, NL80211_ATTR_TDLS_EXTERNAL_SETUP))
924 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +0200925
David S. Miller9360ffd2012-03-29 04:41:26 -0400926 if (nla_put(msg, NL80211_ATTR_CIPHER_SUITES,
927 sizeof(u32) * dev->wiphy.n_cipher_suites,
928 dev->wiphy.cipher_suites))
929 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +0100930
David S. Miller9360ffd2012-03-29 04:41:26 -0400931 if (nla_put_u8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
932 dev->wiphy.max_num_pmkids))
933 goto nla_put_failure;
Vivek Natarajanf4b34b52011-08-29 14:23:03 +0530934
David S. Miller9360ffd2012-03-29 04:41:26 -0400935 if ((dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
936 nla_put_flag(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE))
937 goto nla_put_failure;
Johannes Berg25e47c182009-04-02 20:14:06 +0200938
David S. Miller9360ffd2012-03-29 04:41:26 -0400939 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
940 dev->wiphy.available_antennas_tx) ||
941 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
942 dev->wiphy.available_antennas_rx))
943 goto nla_put_failure;
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100944
David S. Miller9360ffd2012-03-29 04:41:26 -0400945 if ((dev->wiphy.flags & WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD) &&
946 nla_put_u32(msg, NL80211_ATTR_PROBE_RESP_OFFLOAD,
947 dev->wiphy.probe_resp_offload))
948 goto nla_put_failure;
Arik Nemtsov87bbbe22011-11-10 11:28:55 +0200949
Bruno Randolf7f531e02010-12-16 11:30:22 +0900950 if ((dev->wiphy.available_antennas_tx ||
951 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900952 u32 tx_ant = 0, rx_ant = 0;
953 int res;
954 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
955 if (!res) {
David S. Miller9360ffd2012-03-29 04:41:26 -0400956 if (nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX,
957 tx_ant) ||
958 nla_put_u32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX,
959 rx_ant))
960 goto nla_put_failure;
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900961 }
962 }
963
Johannes Berg7527a782011-05-13 10:58:57 +0200964 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
965 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700966 goto nla_put_failure;
967
Johannes Bergee688b002008-01-24 19:38:39 +0100968 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
969 if (!nl_bands)
970 goto nla_put_failure;
971
972 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
973 if (!dev->wiphy.bands[band])
974 continue;
975
976 nl_band = nla_nest_start(msg, band);
977 if (!nl_band)
978 goto nla_put_failure;
979
Johannes Bergd51626d2008-10-09 12:20:13 +0200980 /* add HT info */
David S. Miller9360ffd2012-03-29 04:41:26 -0400981 if (dev->wiphy.bands[band]->ht_cap.ht_supported &&
982 (nla_put(msg, NL80211_BAND_ATTR_HT_MCS_SET,
983 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
984 &dev->wiphy.bands[band]->ht_cap.mcs) ||
985 nla_put_u16(msg, NL80211_BAND_ATTR_HT_CAPA,
986 dev->wiphy.bands[band]->ht_cap.cap) ||
987 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
988 dev->wiphy.bands[band]->ht_cap.ampdu_factor) ||
989 nla_put_u8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
990 dev->wiphy.bands[band]->ht_cap.ampdu_density)))
991 goto nla_put_failure;
Johannes Bergd51626d2008-10-09 12:20:13 +0200992
Mahesh Palivelabf0c111e2012-06-22 07:27:46 +0000993 /* add VHT info */
994 if (dev->wiphy.bands[band]->vht_cap.vht_supported &&
995 (nla_put(msg, NL80211_BAND_ATTR_VHT_MCS_SET,
996 sizeof(dev->wiphy.bands[band]->vht_cap.vht_mcs),
997 &dev->wiphy.bands[band]->vht_cap.vht_mcs) ||
998 nla_put_u32(msg, NL80211_BAND_ATTR_VHT_CAPA,
999 dev->wiphy.bands[band]->vht_cap.cap)))
1000 goto nla_put_failure;
1001
Johannes Bergee688b002008-01-24 19:38:39 +01001002 /* add frequencies */
1003 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
1004 if (!nl_freqs)
1005 goto nla_put_failure;
1006
1007 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
1008 nl_freq = nla_nest_start(msg, i);
1009 if (!nl_freq)
1010 goto nla_put_failure;
1011
1012 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +01001013
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -04001014 if (nl80211_msg_put_channel(msg, chan))
1015 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +02001016
Johannes Bergee688b002008-01-24 19:38:39 +01001017 nla_nest_end(msg, nl_freq);
1018 }
1019
1020 nla_nest_end(msg, nl_freqs);
1021
1022 /* add bitrates */
1023 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
1024 if (!nl_rates)
1025 goto nla_put_failure;
1026
1027 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
1028 nl_rate = nla_nest_start(msg, i);
1029 if (!nl_rate)
1030 goto nla_put_failure;
1031
1032 rate = &dev->wiphy.bands[band]->bitrates[i];
David S. Miller9360ffd2012-03-29 04:41:26 -04001033 if (nla_put_u32(msg, NL80211_BITRATE_ATTR_RATE,
1034 rate->bitrate))
1035 goto nla_put_failure;
1036 if ((rate->flags & IEEE80211_RATE_SHORT_PREAMBLE) &&
1037 nla_put_flag(msg,
1038 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE))
1039 goto nla_put_failure;
Johannes Bergee688b002008-01-24 19:38:39 +01001040
1041 nla_nest_end(msg, nl_rate);
1042 }
1043
1044 nla_nest_end(msg, nl_rates);
1045
1046 nla_nest_end(msg, nl_band);
1047 }
1048 nla_nest_end(msg, nl_bands);
1049
Johannes Berg8fdc6212009-03-14 09:34:01 +01001050 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
1051 if (!nl_cmds)
1052 goto nla_put_failure;
1053
1054 i = 0;
1055#define CMD(op, n) \
1056 do { \
1057 if (dev->ops->op) { \
1058 i++; \
David S. Miller9360ffd2012-03-29 04:41:26 -04001059 if (nla_put_u32(msg, i, NL80211_CMD_ ## n)) \
1060 goto nla_put_failure; \
Johannes Berg8fdc6212009-03-14 09:34:01 +01001061 } \
1062 } while (0)
1063
1064 CMD(add_virtual_intf, NEW_INTERFACE);
1065 CMD(change_virtual_intf, SET_INTERFACE);
1066 CMD(add_key, NEW_KEY);
Johannes Berg88600202012-02-13 15:17:18 +01001067 CMD(start_ap, START_AP);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001068 CMD(add_station, NEW_STATION);
1069 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -08001070 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001071 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +02001072 CMD(auth, AUTHENTICATE);
1073 CMD(assoc, ASSOCIATE);
1074 CMD(deauth, DEAUTHENTICATE);
1075 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +02001076 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +01001077 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01001078 CMD(set_pmksa, SET_PMKSA);
1079 CMD(del_pmksa, DEL_PMKSA);
1080 CMD(flush_pmksa, FLUSH_PMKSA);
Johannes Berg7c4ef712011-11-18 15:33:48 +01001081 if (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL)
1082 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02001083 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +02001084 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001085 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +01001086 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +02001087 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001088 if (nla_put_u32(msg, i, NL80211_CMD_SET_WIPHY_NETNS))
1089 goto nla_put_failure;
Johannes Berg463d0182009-07-14 00:33:35 +02001090 }
Johannes Berge8c9bd52012-06-06 08:18:22 +02001091 if (dev->ops->set_monitor_channel || dev->ops->start_ap ||
Johannes Bergcc1d2802012-05-16 23:50:20 +02001092 dev->ops->join_mesh) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001093 i++;
1094 if (nla_put_u32(msg, i, NL80211_CMD_SET_CHANNEL))
1095 goto nla_put_failure;
1096 }
Bill Jordane8347eb2010-10-01 13:54:28 -04001097 CMD(set_wds_peer, SET_WDS_PEER);
Arik Nemtsov109086c2011-09-28 14:12:50 +03001098 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1099 CMD(tdls_mgmt, TDLS_MGMT);
1100 CMD(tdls_oper, TDLS_OPER);
1101 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03001102 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
1103 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg7f6cf312011-11-04 11:18:15 +01001104 CMD(probe_client, PROBE_CLIENT);
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01001105 CMD(set_noack_map, SET_NOACK_MAP);
Johannes Berg5e760232011-11-04 11:18:17 +01001106 if (dev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS) {
1107 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001108 if (nla_put_u32(msg, i, NL80211_CMD_REGISTER_BEACONS))
1109 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01001110 }
Johannes Berg98104fde2012-06-16 00:19:54 +02001111 CMD(start_p2p_device, START_P2P_DEVICE);
Johannes Berg8fdc6212009-03-14 09:34:01 +01001112
Kalle Valo4745fc02011-11-17 19:06:10 +02001113#ifdef CONFIG_NL80211_TESTMODE
1114 CMD(testmode_cmd, TESTMODE);
1115#endif
1116
Johannes Berg8fdc6212009-03-14 09:34:01 +01001117#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +02001118
Johannes Berg6829c8782009-07-02 09:13:27 +02001119 if (dev->ops->connect || dev->ops->auth) {
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_CONNECT))
1122 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001123 }
1124
Johannes Berg6829c8782009-07-02 09:13:27 +02001125 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +02001126 i++;
David S. Miller9360ffd2012-03-29 04:41:26 -04001127 if (nla_put_u32(msg, i, NL80211_CMD_DISCONNECT))
1128 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02001129 }
1130
Johannes Berg8fdc6212009-03-14 09:34:01 +01001131 nla_nest_end(msg, nl_cmds);
1132
Johannes Berg7c4ef712011-11-18 15:33:48 +01001133 if (dev->ops->remain_on_channel &&
David S. Miller9360ffd2012-03-29 04:41:26 -04001134 (dev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL) &&
1135 nla_put_u32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
1136 dev->wiphy.max_remain_on_channel_duration))
1137 goto nla_put_failure;
Johannes Berga2939112010-12-14 17:54:28 +01001138
David S. Miller9360ffd2012-03-29 04:41:26 -04001139 if ((dev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX) &&
1140 nla_put_flag(msg, NL80211_ATTR_OFFCHANNEL_TX_OK))
1141 goto nla_put_failure;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01001142
Johannes Berg2e161f72010-08-12 15:38:38 +02001143 if (mgmt_stypes) {
1144 u16 stypes;
1145 struct nlattr *nl_ftypes, *nl_ifs;
1146 enum nl80211_iftype ift;
1147
1148 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
1149 if (!nl_ifs)
1150 goto nla_put_failure;
1151
1152 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1153 nl_ftypes = nla_nest_start(msg, ift);
1154 if (!nl_ftypes)
1155 goto nla_put_failure;
1156 i = 0;
1157 stypes = mgmt_stypes[ift].tx;
1158 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001159 if ((stypes & 1) &&
1160 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1161 (i << 4) | IEEE80211_FTYPE_MGMT))
1162 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001163 stypes >>= 1;
1164 i++;
1165 }
1166 nla_nest_end(msg, nl_ftypes);
1167 }
1168
Johannes Berg74b70a42010-08-24 12:15:53 +02001169 nla_nest_end(msg, nl_ifs);
1170
Johannes Berg2e161f72010-08-12 15:38:38 +02001171 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
1172 if (!nl_ifs)
1173 goto nla_put_failure;
1174
1175 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
1176 nl_ftypes = nla_nest_start(msg, ift);
1177 if (!nl_ftypes)
1178 goto nla_put_failure;
1179 i = 0;
1180 stypes = mgmt_stypes[ift].rx;
1181 while (stypes) {
David S. Miller9360ffd2012-03-29 04:41:26 -04001182 if ((stypes & 1) &&
1183 nla_put_u16(msg, NL80211_ATTR_FRAME_TYPE,
1184 (i << 4) | IEEE80211_FTYPE_MGMT))
1185 goto nla_put_failure;
Johannes Berg2e161f72010-08-12 15:38:38 +02001186 stypes >>= 1;
1187 i++;
1188 }
1189 nla_nest_end(msg, nl_ftypes);
1190 }
1191 nla_nest_end(msg, nl_ifs);
1192 }
1193
Johannes Bergdfb89c52012-06-27 09:23:48 +02001194#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02001195 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
1196 struct nlattr *nl_wowlan;
1197
1198 nl_wowlan = nla_nest_start(msg,
1199 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
1200 if (!nl_wowlan)
1201 goto nla_put_failure;
1202
David S. Miller9360ffd2012-03-29 04:41:26 -04001203 if (((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY) &&
1204 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
1205 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT) &&
1206 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
1207 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT) &&
1208 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
1209 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY) &&
1210 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED)) ||
1211 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
1212 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
1213 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ) &&
1214 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
1215 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE) &&
1216 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
1217 ((dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE) &&
1218 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
1219 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001220 if (dev->wiphy.wowlan.n_patterns) {
1221 struct nl80211_wowlan_pattern_support pat = {
1222 .max_patterns = dev->wiphy.wowlan.n_patterns,
1223 .min_pattern_len =
1224 dev->wiphy.wowlan.pattern_min_len,
1225 .max_pattern_len =
1226 dev->wiphy.wowlan.pattern_max_len,
1227 };
David S. Miller9360ffd2012-03-29 04:41:26 -04001228 if (nla_put(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
1229 sizeof(pat), &pat))
1230 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02001231 }
1232
1233 nla_nest_end(msg, nl_wowlan);
1234 }
Johannes Bergdfb89c52012-06-27 09:23:48 +02001235#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02001236
Johannes Berg7527a782011-05-13 10:58:57 +02001237 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
1238 dev->wiphy.software_iftypes))
1239 goto nla_put_failure;
1240
1241 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
1242 goto nla_put_failure;
1243
David S. Miller9360ffd2012-03-29 04:41:26 -04001244 if ((dev->wiphy.flags & WIPHY_FLAG_HAVE_AP_SME) &&
1245 nla_put_u32(msg, NL80211_ATTR_DEVICE_AP_SME,
1246 dev->wiphy.ap_sme_capa))
1247 goto nla_put_failure;
Johannes Berg562a7482011-11-07 12:39:33 +01001248
David S. Miller9360ffd2012-03-29 04:41:26 -04001249 if (nla_put_u32(msg, NL80211_ATTR_FEATURE_FLAGS,
1250 dev->wiphy.features))
1251 goto nla_put_failure;
Johannes Berg1f074bd2011-11-06 14:13:33 +01001252
David S. Miller9360ffd2012-03-29 04:41:26 -04001253 if (dev->wiphy.ht_capa_mod_mask &&
1254 nla_put(msg, NL80211_ATTR_HT_CAPABILITY_MASK,
1255 sizeof(*dev->wiphy.ht_capa_mod_mask),
1256 dev->wiphy.ht_capa_mod_mask))
1257 goto nla_put_failure;
Ben Greear7e7c8922011-11-18 11:31:59 -08001258
Johannes Berg55682962007-09-20 13:09:35 -04001259 return genlmsg_end(msg, hdr);
1260
1261 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001262 genlmsg_cancel(msg, hdr);
1263 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001264}
1265
1266static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
1267{
1268 int idx = 0;
1269 int start = cb->args[0];
1270 struct cfg80211_registered_device *dev;
1271
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001272 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +02001273 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +02001274 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
1275 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001276 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001277 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001278 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001279 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001280 dev) < 0) {
1281 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001282 break;
Julius Volzb4637272008-07-08 14:02:19 +02001283 }
Johannes Berg55682962007-09-20 13:09:35 -04001284 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001285 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001286
1287 cb->args[0] = idx;
1288
1289 return skb->len;
1290}
1291
1292static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1293{
1294 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001295 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001296
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001297 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001298 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001299 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001300
Eric W. Biederman15e47302012-09-07 20:12:54 +00001301 if (nl80211_send_wiphy(msg, info->snd_portid, info->snd_seq, 0, dev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001302 nlmsg_free(msg);
1303 return -ENOBUFS;
1304 }
Johannes Berg55682962007-09-20 13:09:35 -04001305
Johannes Berg134e6372009-07-10 09:51:34 +00001306 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001307}
1308
Jouni Malinen31888482008-10-30 16:59:24 +02001309static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1310 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1311 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1312 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1313 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1314 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1315};
1316
1317static int parse_txq_params(struct nlattr *tb[],
1318 struct ieee80211_txq_params *txq_params)
1319{
Johannes Berga3304b02012-03-28 11:04:24 +02001320 if (!tb[NL80211_TXQ_ATTR_AC] || !tb[NL80211_TXQ_ATTR_TXOP] ||
Jouni Malinen31888482008-10-30 16:59:24 +02001321 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1322 !tb[NL80211_TXQ_ATTR_AIFS])
1323 return -EINVAL;
1324
Johannes Berga3304b02012-03-28 11:04:24 +02001325 txq_params->ac = nla_get_u8(tb[NL80211_TXQ_ATTR_AC]);
Jouni Malinen31888482008-10-30 16:59:24 +02001326 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1327 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1328 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1329 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1330
Johannes Berga3304b02012-03-28 11:04:24 +02001331 if (txq_params->ac >= NL80211_NUM_ACS)
1332 return -EINVAL;
1333
Jouni Malinen31888482008-10-30 16:59:24 +02001334 return 0;
1335}
1336
Johannes Bergf444de02010-05-05 15:25:02 +02001337static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1338{
1339 /*
Johannes Bergcc1d2802012-05-16 23:50:20 +02001340 * You can only set the channel explicitly for WDS interfaces,
1341 * all others have their channel managed via their respective
1342 * "establish a connection" command (connect, join, ...)
1343 *
1344 * For AP/GO and mesh mode, the channel can be set with the
1345 * channel userspace API, but is only stored and passed to the
1346 * low-level driver when the AP starts or the mesh is joined.
1347 * This is for backward compatibility, userspace can also give
1348 * the channel in the start-ap or join-mesh commands instead.
Johannes Bergf444de02010-05-05 15:25:02 +02001349 *
1350 * Monitors are special as they are normally slaved to
Johannes Berge8c9bd52012-06-06 08:18:22 +02001351 * whatever else is going on, so they have their own special
1352 * operation to set the monitor channel if possible.
Johannes Bergf444de02010-05-05 15:25:02 +02001353 */
1354 return !wdev ||
1355 wdev->iftype == NL80211_IFTYPE_AP ||
Johannes Bergf444de02010-05-05 15:25:02 +02001356 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001357 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1358 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001359}
1360
Johannes Bergcd6c6592012-05-10 21:27:18 +02001361static bool nl80211_valid_channel_type(struct genl_info *info,
1362 enum nl80211_channel_type *channel_type)
1363{
1364 enum nl80211_channel_type tmp;
1365
1366 if (!info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE])
1367 return false;
1368
1369 tmp = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1370 if (tmp != NL80211_CHAN_NO_HT &&
1371 tmp != NL80211_CHAN_HT20 &&
1372 tmp != NL80211_CHAN_HT40PLUS &&
1373 tmp != NL80211_CHAN_HT40MINUS)
1374 return false;
1375
1376 if (channel_type)
1377 *channel_type = tmp;
1378
1379 return true;
1380}
1381
Johannes Bergf444de02010-05-05 15:25:02 +02001382static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1383 struct wireless_dev *wdev,
1384 struct genl_info *info)
1385{
Johannes Bergaa430da2012-05-16 23:50:18 +02001386 struct ieee80211_channel *channel;
Johannes Bergf444de02010-05-05 15:25:02 +02001387 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1388 u32 freq;
1389 int result;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001390 enum nl80211_iftype iftype = NL80211_IFTYPE_MONITOR;
1391
1392 if (wdev)
1393 iftype = wdev->iftype;
Johannes Bergf444de02010-05-05 15:25:02 +02001394
1395 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1396 return -EINVAL;
1397
1398 if (!nl80211_can_set_dev_channel(wdev))
1399 return -EOPNOTSUPP;
1400
Johannes Bergcd6c6592012-05-10 21:27:18 +02001401 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
1402 !nl80211_valid_channel_type(info, &channel_type))
1403 return -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001404
1405 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1406
1407 mutex_lock(&rdev->devlist_mtx);
Johannes Berge8c9bd52012-06-06 08:18:22 +02001408 switch (iftype) {
Johannes Bergaa430da2012-05-16 23:50:18 +02001409 case NL80211_IFTYPE_AP:
1410 case NL80211_IFTYPE_P2P_GO:
1411 if (wdev->beacon_interval) {
1412 result = -EBUSY;
1413 break;
1414 }
1415 channel = rdev_freq_to_chan(rdev, freq, channel_type);
1416 if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
1417 channel,
1418 channel_type)) {
1419 result = -EINVAL;
1420 break;
1421 }
1422 wdev->preset_chan = channel;
1423 wdev->preset_chantype = channel_type;
1424 result = 0;
1425 break;
Johannes Bergcc1d2802012-05-16 23:50:20 +02001426 case NL80211_IFTYPE_MESH_POINT:
1427 result = cfg80211_set_mesh_freq(rdev, wdev, freq, channel_type);
1428 break;
Johannes Berge8c9bd52012-06-06 08:18:22 +02001429 case NL80211_IFTYPE_MONITOR:
1430 result = cfg80211_set_monitor_channel(rdev, freq, channel_type);
1431 break;
Johannes Bergaa430da2012-05-16 23:50:18 +02001432 default:
Johannes Berge8c9bd52012-06-06 08:18:22 +02001433 result = -EINVAL;
Johannes Bergf444de02010-05-05 15:25:02 +02001434 }
1435 mutex_unlock(&rdev->devlist_mtx);
1436
1437 return result;
1438}
1439
1440static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1441{
Johannes Berg4c476992010-10-04 21:36:35 +02001442 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1443 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001444
Johannes Berg4c476992010-10-04 21:36:35 +02001445 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001446}
1447
Bill Jordane8347eb2010-10-01 13:54:28 -04001448static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1449{
Johannes Berg43b19952010-10-07 13:10:30 +02001450 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1451 struct net_device *dev = info->user_ptr[1];
1452 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001453 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001454
1455 if (!info->attrs[NL80211_ATTR_MAC])
1456 return -EINVAL;
1457
Johannes Berg43b19952010-10-07 13:10:30 +02001458 if (netif_running(dev))
1459 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001460
Johannes Berg43b19952010-10-07 13:10:30 +02001461 if (!rdev->ops->set_wds_peer)
1462 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001463
Johannes Berg43b19952010-10-07 13:10:30 +02001464 if (wdev->iftype != NL80211_IFTYPE_WDS)
1465 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001466
1467 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001468 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001469}
1470
1471
Johannes Berg55682962007-09-20 13:09:35 -04001472static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1473{
1474 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001475 struct net_device *netdev = NULL;
1476 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001477 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001478 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001479 u32 changed;
1480 u8 retry_short = 0, retry_long = 0;
1481 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001482 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001483
Johannes Bergf444de02010-05-05 15:25:02 +02001484 /*
1485 * Try to find the wiphy and netdev. Normally this
1486 * function shouldn't need the netdev, but this is
1487 * done for backward compatibility -- previously
1488 * setting the channel was done per wiphy, but now
1489 * it is per netdev. Previous userland like hostapd
1490 * also passed a netdev to set_wiphy, so that it is
1491 * possible to let that go to the right netdev!
1492 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001493 mutex_lock(&cfg80211_mutex);
1494
Johannes Bergf444de02010-05-05 15:25:02 +02001495 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1496 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1497
1498 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1499 if (netdev && netdev->ieee80211_ptr) {
1500 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1501 mutex_lock(&rdev->mtx);
1502 } else
1503 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001504 }
1505
Johannes Bergf444de02010-05-05 15:25:02 +02001506 if (!netdev) {
Johannes Berg878d9ec2012-06-15 14:18:32 +02001507 rdev = __cfg80211_rdev_from_attrs(genl_info_net(info),
1508 info->attrs);
Johannes Bergf444de02010-05-05 15:25:02 +02001509 if (IS_ERR(rdev)) {
1510 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001511 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001512 }
1513 wdev = NULL;
1514 netdev = NULL;
1515 result = 0;
1516
1517 mutex_lock(&rdev->mtx);
Johannes Bergcc1d2802012-05-16 23:50:20 +02001518 } else if (nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
Johannes Bergf444de02010-05-05 15:25:02 +02001519 wdev = netdev->ieee80211_ptr;
1520 else
1521 wdev = NULL;
1522
1523 /*
1524 * end workaround code, by now the rdev is available
1525 * and locked, and wdev may or may not be NULL.
1526 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001527
1528 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001529 result = cfg80211_dev_rename(
1530 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001531
1532 mutex_unlock(&cfg80211_mutex);
1533
1534 if (result)
1535 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001536
Jouni Malinen31888482008-10-30 16:59:24 +02001537 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1538 struct ieee80211_txq_params txq_params;
1539 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1540
1541 if (!rdev->ops->set_txq_params) {
1542 result = -EOPNOTSUPP;
1543 goto bad_res;
1544 }
1545
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001546 if (!netdev) {
1547 result = -EINVAL;
1548 goto bad_res;
1549 }
1550
Johannes Berg133a3ff2011-11-03 14:50:13 +01001551 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
1552 netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) {
1553 result = -EINVAL;
1554 goto bad_res;
1555 }
1556
Johannes Berg2b5f8b02012-04-02 10:51:55 +02001557 if (!netif_running(netdev)) {
1558 result = -ENETDOWN;
1559 goto bad_res;
1560 }
1561
Jouni Malinen31888482008-10-30 16:59:24 +02001562 nla_for_each_nested(nl_txq_params,
1563 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1564 rem_txq_params) {
1565 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1566 nla_data(nl_txq_params),
1567 nla_len(nl_txq_params),
1568 txq_params_policy);
1569 result = parse_txq_params(tb, &txq_params);
1570 if (result)
1571 goto bad_res;
1572
1573 result = rdev->ops->set_txq_params(&rdev->wiphy,
Eliad Pellerf70f01c2011-09-25 20:06:53 +03001574 netdev,
Jouni Malinen31888482008-10-30 16:59:24 +02001575 &txq_params);
1576 if (result)
1577 goto bad_res;
1578 }
1579 }
1580
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001581 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001582 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001583 if (result)
1584 goto bad_res;
1585 }
1586
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001587 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1588 enum nl80211_tx_power_setting type;
1589 int idx, mbm = 0;
1590
1591 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001592 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001593 goto bad_res;
1594 }
1595
1596 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1597 type = nla_get_u32(info->attrs[idx]);
1598
1599 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1600 (type != NL80211_TX_POWER_AUTOMATIC)) {
1601 result = -EINVAL;
1602 goto bad_res;
1603 }
1604
1605 if (type != NL80211_TX_POWER_AUTOMATIC) {
1606 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1607 mbm = nla_get_u32(info->attrs[idx]);
1608 }
1609
1610 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1611 if (result)
1612 goto bad_res;
1613 }
1614
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001615 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1616 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1617 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001618 if ((!rdev->wiphy.available_antennas_tx &&
1619 !rdev->wiphy.available_antennas_rx) ||
1620 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001621 result = -EOPNOTSUPP;
1622 goto bad_res;
1623 }
1624
1625 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1626 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1627
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001628 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001629 * available antenna masks, except for the "all" mask */
1630 if ((~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 result = -EINVAL;
1633 goto bad_res;
1634 }
1635
Bruno Randolf7f531e02010-12-16 11:30:22 +09001636 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1637 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001638
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001639 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1640 if (result)
1641 goto bad_res;
1642 }
1643
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001644 changed = 0;
1645
1646 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1647 retry_short = nla_get_u8(
1648 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1649 if (retry_short == 0) {
1650 result = -EINVAL;
1651 goto bad_res;
1652 }
1653 changed |= WIPHY_PARAM_RETRY_SHORT;
1654 }
1655
1656 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1657 retry_long = nla_get_u8(
1658 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1659 if (retry_long == 0) {
1660 result = -EINVAL;
1661 goto bad_res;
1662 }
1663 changed |= WIPHY_PARAM_RETRY_LONG;
1664 }
1665
1666 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1667 frag_threshold = nla_get_u32(
1668 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1669 if (frag_threshold < 256) {
1670 result = -EINVAL;
1671 goto bad_res;
1672 }
1673 if (frag_threshold != (u32) -1) {
1674 /*
1675 * Fragments (apart from the last one) are required to
1676 * have even length. Make the fragmentation code
1677 * simpler by stripping LSB should someone try to use
1678 * odd threshold value.
1679 */
1680 frag_threshold &= ~0x1;
1681 }
1682 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1683 }
1684
1685 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1686 rts_threshold = nla_get_u32(
1687 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1688 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1689 }
1690
Lukáš Turek81077e82009-12-21 22:50:47 +01001691 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1692 coverage_class = nla_get_u8(
1693 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1694 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1695 }
1696
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001697 if (changed) {
1698 u8 old_retry_short, old_retry_long;
1699 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001700 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001701
1702 if (!rdev->ops->set_wiphy_params) {
1703 result = -EOPNOTSUPP;
1704 goto bad_res;
1705 }
1706
1707 old_retry_short = rdev->wiphy.retry_short;
1708 old_retry_long = rdev->wiphy.retry_long;
1709 old_frag_threshold = rdev->wiphy.frag_threshold;
1710 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001711 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001712
1713 if (changed & WIPHY_PARAM_RETRY_SHORT)
1714 rdev->wiphy.retry_short = retry_short;
1715 if (changed & WIPHY_PARAM_RETRY_LONG)
1716 rdev->wiphy.retry_long = retry_long;
1717 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1718 rdev->wiphy.frag_threshold = frag_threshold;
1719 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1720 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001721 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1722 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001723
1724 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1725 if (result) {
1726 rdev->wiphy.retry_short = old_retry_short;
1727 rdev->wiphy.retry_long = old_retry_long;
1728 rdev->wiphy.frag_threshold = old_frag_threshold;
1729 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001730 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001731 }
1732 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001733
Johannes Berg306d6112008-12-08 12:39:04 +01001734 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001735 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001736 if (netdev)
1737 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001738 return result;
1739}
1740
Johannes Berg71bbc992012-06-15 15:30:18 +02001741static inline u64 wdev_id(struct wireless_dev *wdev)
1742{
1743 return (u64)wdev->identifier |
1744 ((u64)wiphy_to_dev(wdev->wiphy)->wiphy_idx << 32);
1745}
Johannes Berg55682962007-09-20 13:09:35 -04001746
Eric W. Biederman15e47302012-09-07 20:12:54 +00001747static int nl80211_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001748 struct cfg80211_registered_device *rdev,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001749 struct wireless_dev *wdev)
Johannes Berg55682962007-09-20 13:09:35 -04001750{
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001751 struct net_device *dev = wdev->netdev;
Johannes Berg55682962007-09-20 13:09:35 -04001752 void *hdr;
1753
Eric W. Biederman15e47302012-09-07 20:12:54 +00001754 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_INTERFACE);
Johannes Berg55682962007-09-20 13:09:35 -04001755 if (!hdr)
1756 return -1;
1757
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001758 if (dev &&
1759 (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001760 nla_put_string(msg, NL80211_ATTR_IFNAME, dev->name)))
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001761 goto nla_put_failure;
1762
1763 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
1764 nla_put_u32(msg, NL80211_ATTR_IFTYPE, wdev->iftype) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02001765 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
Johannes Berg98104fde2012-06-16 00:19:54 +02001766 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, wdev_address(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04001767 nla_put_u32(msg, NL80211_ATTR_GENERATION,
1768 rdev->devlist_generation ^
1769 (cfg80211_rdev_list_generation << 2)))
1770 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02001771
Johannes Berg5b7ccaf2012-07-12 19:45:08 +02001772 if (rdev->ops->get_channel) {
1773 struct ieee80211_channel *chan;
1774 enum nl80211_channel_type channel_type;
1775
1776 chan = rdev->ops->get_channel(&rdev->wiphy, wdev,
1777 &channel_type);
1778 if (chan &&
1779 (nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ,
1780 chan->center_freq) ||
1781 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE,
1782 channel_type)))
John W. Linville59ef43e2012-04-18 14:17:13 -04001783 goto nla_put_failure;
Pontus Fuchsd91df0e2012-04-03 16:39:58 +02001784 }
1785
Johannes Berg55682962007-09-20 13:09:35 -04001786 return genlmsg_end(msg, hdr);
1787
1788 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001789 genlmsg_cancel(msg, hdr);
1790 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001791}
1792
1793static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1794{
1795 int wp_idx = 0;
1796 int if_idx = 0;
1797 int wp_start = cb->args[0];
1798 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001799 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001800 struct wireless_dev *wdev;
1801
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001802 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001803 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1804 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001805 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001806 if (wp_idx < wp_start) {
1807 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001808 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001809 }
Johannes Berg55682962007-09-20 13:09:35 -04001810 if_idx = 0;
1811
Johannes Bergf5ea9122009-08-07 16:17:38 +02001812 mutex_lock(&rdev->devlist_mtx);
Johannes Berg89a54e42012-06-15 14:33:17 +02001813 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001814 if (if_idx < if_start) {
1815 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001816 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001817 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001818 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).portid,
Johannes Berg55682962007-09-20 13:09:35 -04001819 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001820 rdev, wdev) < 0) {
Johannes Bergf5ea9122009-08-07 16:17:38 +02001821 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001822 goto out;
1823 }
1824 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001825 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001826 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001827
1828 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001829 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001830 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001831 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001832
1833 cb->args[0] = wp_idx;
1834 cb->args[1] = if_idx;
1835
1836 return skb->len;
1837}
1838
1839static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1840{
1841 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001842 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001843 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001844
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001845 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001846 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001847 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001848
Eric W. Biederman15e47302012-09-07 20:12:54 +00001849 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg72fb2ab2012-06-15 17:52:47 +02001850 dev, wdev) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02001851 nlmsg_free(msg);
1852 return -ENOBUFS;
1853 }
Johannes Berg55682962007-09-20 13:09:35 -04001854
Johannes Berg134e6372009-07-10 09:51:34 +00001855 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001856}
1857
Michael Wu66f7ac52008-01-31 19:48:22 +01001858static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1859 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1860 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1861 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1862 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1863 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1864};
1865
1866static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1867{
1868 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1869 int flag;
1870
1871 *mntrflags = 0;
1872
1873 if (!nla)
1874 return -EINVAL;
1875
1876 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1877 nla, mntr_flags_policy))
1878 return -EINVAL;
1879
1880 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1881 if (flags[flag])
1882 *mntrflags |= (1<<flag);
1883
1884 return 0;
1885}
1886
Johannes Berg9bc383d2009-11-19 11:55:19 +01001887static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001888 struct net_device *netdev, u8 use_4addr,
1889 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001890{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001891 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001892 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001893 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001894 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001895 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001896
1897 switch (iftype) {
1898 case NL80211_IFTYPE_AP_VLAN:
1899 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1900 return 0;
1901 break;
1902 case NL80211_IFTYPE_STATION:
1903 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1904 return 0;
1905 break;
1906 default:
1907 break;
1908 }
1909
1910 return -EOPNOTSUPP;
1911}
1912
Johannes Berg55682962007-09-20 13:09:35 -04001913static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1914{
Johannes Berg4c476992010-10-04 21:36:35 +02001915 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001916 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001917 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001918 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001919 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001920 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001921 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001922
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001923 memset(&params, 0, sizeof(params));
1924
Johannes Berg04a773a2009-04-19 21:24:32 +02001925 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001926
Johannes Berg723b0382008-09-16 20:22:09 +02001927 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001928 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001929 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001930 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001931 if (ntype > NL80211_IFTYPE_MAX)
1932 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001933 }
1934
Johannes Berg92ffe052008-09-16 20:39:36 +02001935 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001936 struct wireless_dev *wdev = dev->ieee80211_ptr;
1937
Johannes Berg4c476992010-10-04 21:36:35 +02001938 if (ntype != NL80211_IFTYPE_MESH_POINT)
1939 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001940 if (netif_running(dev))
1941 return -EBUSY;
1942
1943 wdev_lock(wdev);
1944 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1945 IEEE80211_MAX_MESH_ID_LEN);
1946 wdev->mesh_id_up_len =
1947 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1948 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1949 wdev->mesh_id_up_len);
1950 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001951 }
1952
Felix Fietkau8b787642009-11-10 18:53:10 +01001953 if (info->attrs[NL80211_ATTR_4ADDR]) {
1954 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1955 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001956 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001957 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001958 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001959 } else {
1960 params.use_4addr = -1;
1961 }
1962
Johannes Berg92ffe052008-09-16 20:39:36 +02001963 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001964 if (ntype != NL80211_IFTYPE_MONITOR)
1965 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001966 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1967 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001968 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001969 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001970
1971 flags = &_flags;
1972 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001973 }
Johannes Berg3b858752009-03-12 09:55:09 +01001974
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001975 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001976 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001977 else
1978 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001979
Johannes Berg9bc383d2009-11-19 11:55:19 +01001980 if (!err && params.use_4addr != -1)
1981 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1982
Johannes Berg55682962007-09-20 13:09:35 -04001983 return err;
1984}
1985
1986static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1987{
Johannes Berg4c476992010-10-04 21:36:35 +02001988 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001989 struct vif_params params;
Johannes Berg84efbb82012-06-16 00:00:26 +02001990 struct wireless_dev *wdev;
Johannes Berg1c90f9d2012-06-16 00:05:37 +02001991 struct sk_buff *msg;
Johannes Berg55682962007-09-20 13:09:35 -04001992 int err;
1993 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001994 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001995
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001996 memset(&params, 0, sizeof(params));
1997
Johannes Berg55682962007-09-20 13:09:35 -04001998 if (!info->attrs[NL80211_ATTR_IFNAME])
1999 return -EINVAL;
2000
2001 if (info->attrs[NL80211_ATTR_IFTYPE]) {
2002 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
2003 if (type > NL80211_IFTYPE_MAX)
2004 return -EINVAL;
2005 }
2006
Johannes Berg79c97e92009-07-07 03:56:12 +02002007 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02002008 !(rdev->wiphy.interface_modes & (1 << type)))
2009 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04002010
Johannes Berg9bc383d2009-11-19 11:55:19 +01002011 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01002012 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01002013 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01002014 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002015 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01002016 }
Felix Fietkau8b787642009-11-10 18:53:10 +01002017
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002018 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2019 if (!msg)
2020 return -ENOMEM;
2021
Michael Wu66f7ac52008-01-31 19:48:22 +01002022 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
2023 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
2024 &flags);
Johannes Berg84efbb82012-06-16 00:00:26 +02002025 wdev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01002026 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002027 type, err ? NULL : &flags, &params);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002028 if (IS_ERR(wdev)) {
2029 nlmsg_free(msg);
Johannes Berg84efbb82012-06-16 00:00:26 +02002030 return PTR_ERR(wdev);
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002031 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002032
Johannes Berg98104fde2012-06-16 00:19:54 +02002033 switch (type) {
2034 case NL80211_IFTYPE_MESH_POINT:
2035 if (!info->attrs[NL80211_ATTR_MESH_ID])
2036 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002037 wdev_lock(wdev);
2038 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
2039 IEEE80211_MAX_MESH_ID_LEN);
2040 wdev->mesh_id_up_len =
2041 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
2042 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
2043 wdev->mesh_id_up_len);
2044 wdev_unlock(wdev);
Johannes Berg98104fde2012-06-16 00:19:54 +02002045 break;
2046 case NL80211_IFTYPE_P2P_DEVICE:
2047 /*
2048 * P2P Device doesn't have a netdev, so doesn't go
2049 * through the netdev notifier and must be added here
2050 */
2051 mutex_init(&wdev->mtx);
2052 INIT_LIST_HEAD(&wdev->event_list);
2053 spin_lock_init(&wdev->event_lock);
2054 INIT_LIST_HEAD(&wdev->mgmt_registrations);
2055 spin_lock_init(&wdev->mgmt_registrations_lock);
2056
2057 mutex_lock(&rdev->devlist_mtx);
2058 wdev->identifier = ++rdev->wdev_id;
2059 list_add_rcu(&wdev->list, &rdev->wdev_list);
2060 rdev->devlist_generation++;
2061 mutex_unlock(&rdev->devlist_mtx);
2062 break;
2063 default:
2064 break;
Johannes Berg29cbe682010-12-03 09:20:44 +01002065 }
2066
Eric W. Biederman15e47302012-09-07 20:12:54 +00002067 if (nl80211_send_iface(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg1c90f9d2012-06-16 00:05:37 +02002068 rdev, wdev) < 0) {
2069 nlmsg_free(msg);
2070 return -ENOBUFS;
2071 }
2072
2073 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04002074}
2075
2076static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
2077{
Johannes Berg4c476992010-10-04 21:36:35 +02002078 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg84efbb82012-06-16 00:00:26 +02002079 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04002080
Johannes Berg4c476992010-10-04 21:36:35 +02002081 if (!rdev->ops->del_virtual_intf)
2082 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002083
Johannes Berg84efbb82012-06-16 00:00:26 +02002084 /*
2085 * If we remove a wireless device without a netdev then clear
2086 * user_ptr[1] so that nl80211_post_doit won't dereference it
2087 * to check if it needs to do dev_put(). Otherwise it crashes
2088 * since the wdev has been freed, unlike with a netdev where
2089 * we need the dev_put() for the netdev to really be freed.
2090 */
2091 if (!wdev->netdev)
2092 info->user_ptr[1] = NULL;
2093
2094 return rdev->ops->del_virtual_intf(&rdev->wiphy, wdev);
Johannes Berg55682962007-09-20 13:09:35 -04002095}
2096
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01002097static int nl80211_set_noack_map(struct sk_buff *skb, struct genl_info *info)
2098{
2099 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2100 struct net_device *dev = info->user_ptr[1];
2101 u16 noack_map;
2102
2103 if (!info->attrs[NL80211_ATTR_NOACK_MAP])
2104 return -EINVAL;
2105
2106 if (!rdev->ops->set_noack_map)
2107 return -EOPNOTSUPP;
2108
2109 noack_map = nla_get_u16(info->attrs[NL80211_ATTR_NOACK_MAP]);
2110
2111 return rdev->ops->set_noack_map(&rdev->wiphy, dev, noack_map);
2112}
2113
Johannes Berg41ade002007-12-19 02:03:29 +01002114struct get_key_cookie {
2115 struct sk_buff *msg;
2116 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02002117 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002118};
2119
2120static void get_key_callback(void *c, struct key_params *params)
2121{
Johannes Bergb9454e82009-07-08 13:29:08 +02002122 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01002123 struct get_key_cookie *cookie = c;
2124
David S. Miller9360ffd2012-03-29 04:41:26 -04002125 if ((params->key &&
2126 nla_put(cookie->msg, NL80211_ATTR_KEY_DATA,
2127 params->key_len, params->key)) ||
2128 (params->seq &&
2129 nla_put(cookie->msg, NL80211_ATTR_KEY_SEQ,
2130 params->seq_len, params->seq)) ||
2131 (params->cipher &&
2132 nla_put_u32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
2133 params->cipher)))
2134 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002135
Johannes Bergb9454e82009-07-08 13:29:08 +02002136 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
2137 if (!key)
2138 goto nla_put_failure;
2139
David S. Miller9360ffd2012-03-29 04:41:26 -04002140 if ((params->key &&
2141 nla_put(cookie->msg, NL80211_KEY_DATA,
2142 params->key_len, params->key)) ||
2143 (params->seq &&
2144 nla_put(cookie->msg, NL80211_KEY_SEQ,
2145 params->seq_len, params->seq)) ||
2146 (params->cipher &&
2147 nla_put_u32(cookie->msg, NL80211_KEY_CIPHER,
2148 params->cipher)))
2149 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002150
David S. Miller9360ffd2012-03-29 04:41:26 -04002151 if (nla_put_u8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx))
2152 goto nla_put_failure;
Johannes Bergb9454e82009-07-08 13:29:08 +02002153
2154 nla_nest_end(cookie->msg, key);
2155
Johannes Berg41ade002007-12-19 02:03:29 +01002156 return;
2157 nla_put_failure:
2158 cookie->error = 1;
2159}
2160
2161static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2162{
Johannes Berg4c476992010-10-04 21:36:35 +02002163 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002164 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002165 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002166 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02002167 const u8 *mac_addr = NULL;
2168 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01002169 struct get_key_cookie cookie = {
2170 .error = 0,
2171 };
2172 void *hdr;
2173 struct sk_buff *msg;
2174
2175 if (info->attrs[NL80211_ATTR_KEY_IDX])
2176 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
2177
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02002178 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01002179 return -EINVAL;
2180
2181 if (info->attrs[NL80211_ATTR_MAC])
2182 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2183
Johannes Berge31b8212010-10-05 19:39:30 +02002184 pairwise = !!mac_addr;
2185 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
2186 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
2187 if (kt >= NUM_NL80211_KEYTYPES)
2188 return -EINVAL;
2189 if (kt != NL80211_KEYTYPE_GROUP &&
2190 kt != NL80211_KEYTYPE_PAIRWISE)
2191 return -EINVAL;
2192 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
2193 }
2194
Johannes Berg4c476992010-10-04 21:36:35 +02002195 if (!rdev->ops->get_key)
2196 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002197
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002198 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002199 if (!msg)
2200 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01002201
Eric W. Biederman15e47302012-09-07 20:12:54 +00002202 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg41ade002007-12-19 02:03:29 +01002203 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02002204 if (IS_ERR(hdr))
2205 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01002206
2207 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02002208 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01002209
David S. Miller9360ffd2012-03-29 04:41:26 -04002210 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2211 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_idx))
2212 goto nla_put_failure;
2213 if (mac_addr &&
2214 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
2215 goto nla_put_failure;
Johannes Berg41ade002007-12-19 02:03:29 +01002216
Johannes Berge31b8212010-10-05 19:39:30 +02002217 if (pairwise && mac_addr &&
2218 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2219 return -ENOENT;
2220
2221 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
2222 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01002223
2224 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002225 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01002226
2227 if (cookie.error)
2228 goto nla_put_failure;
2229
2230 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002231 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01002232
2233 nla_put_failure:
2234 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03002235 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01002236 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01002237 return err;
2238}
2239
2240static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
2241{
Johannes Berg4c476992010-10-04 21:36:35 +02002242 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02002243 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002244 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002245 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002246
Johannes Bergb9454e82009-07-08 13:29:08 +02002247 err = nl80211_parse_key(info, &key);
2248 if (err)
2249 return err;
2250
2251 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01002252 return -EINVAL;
2253
Johannes Bergb9454e82009-07-08 13:29:08 +02002254 /* only support setting default key */
2255 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01002256 return -EINVAL;
2257
Johannes Bergfffd0932009-07-08 14:22:54 +02002258 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002259
2260 if (key.def) {
2261 if (!rdev->ops->set_default_key) {
2262 err = -EOPNOTSUPP;
2263 goto out;
2264 }
2265
2266 err = nl80211_key_allowed(dev->ieee80211_ptr);
2267 if (err)
2268 goto out;
2269
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002270 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
2271 key.def_uni, key.def_multi);
2272
2273 if (err)
2274 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02002275
Johannes Berg3d23e342009-09-29 23:27:28 +02002276#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002277 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02002278#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01002279 } else {
2280 if (key.def_uni || !key.def_multi) {
2281 err = -EINVAL;
2282 goto out;
2283 }
2284
2285 if (!rdev->ops->set_default_mgmt_key) {
2286 err = -EOPNOTSUPP;
2287 goto out;
2288 }
2289
2290 err = nl80211_key_allowed(dev->ieee80211_ptr);
2291 if (err)
2292 goto out;
2293
2294 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
2295 dev, key.idx);
2296 if (err)
2297 goto out;
2298
2299#ifdef CONFIG_CFG80211_WEXT
2300 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
2301#endif
2302 }
2303
2304 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02002305 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002306
Johannes Berg41ade002007-12-19 02:03:29 +01002307 return err;
2308}
2309
2310static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
2311{
Johannes Berg4c476992010-10-04 21:36:35 +02002312 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02002313 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002314 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02002315 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02002316 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01002317
Johannes Bergb9454e82009-07-08 13:29:08 +02002318 err = nl80211_parse_key(info, &key);
2319 if (err)
2320 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002321
Johannes Bergb9454e82009-07-08 13:29:08 +02002322 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01002323 return -EINVAL;
2324
Johannes Berg41ade002007-12-19 02:03:29 +01002325 if (info->attrs[NL80211_ATTR_MAC])
2326 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2327
Johannes Berge31b8212010-10-05 19:39:30 +02002328 if (key.type == -1) {
2329 if (mac_addr)
2330 key.type = NL80211_KEYTYPE_PAIRWISE;
2331 else
2332 key.type = NL80211_KEYTYPE_GROUP;
2333 }
2334
2335 /* for now */
2336 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2337 key.type != NL80211_KEYTYPE_GROUP)
2338 return -EINVAL;
2339
Johannes Berg4c476992010-10-04 21:36:35 +02002340 if (!rdev->ops->add_key)
2341 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002342
Johannes Berge31b8212010-10-05 19:39:30 +02002343 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
2344 key.type == NL80211_KEYTYPE_PAIRWISE,
2345 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02002346 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02002347
2348 wdev_lock(dev->ieee80211_ptr);
2349 err = nl80211_key_allowed(dev->ieee80211_ptr);
2350 if (!err)
2351 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02002352 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02002353 mac_addr, &key.p);
2354 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01002355
Johannes Berg41ade002007-12-19 02:03:29 +01002356 return err;
2357}
2358
2359static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
2360{
Johannes Berg4c476992010-10-04 21:36:35 +02002361 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01002362 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002363 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01002364 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02002365 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01002366
Johannes Bergb9454e82009-07-08 13:29:08 +02002367 err = nl80211_parse_key(info, &key);
2368 if (err)
2369 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01002370
2371 if (info->attrs[NL80211_ATTR_MAC])
2372 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2373
Johannes Berge31b8212010-10-05 19:39:30 +02002374 if (key.type == -1) {
2375 if (mac_addr)
2376 key.type = NL80211_KEYTYPE_PAIRWISE;
2377 else
2378 key.type = NL80211_KEYTYPE_GROUP;
2379 }
2380
2381 /* for now */
2382 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
2383 key.type != NL80211_KEYTYPE_GROUP)
2384 return -EINVAL;
2385
Johannes Berg4c476992010-10-04 21:36:35 +02002386 if (!rdev->ops->del_key)
2387 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01002388
Johannes Bergfffd0932009-07-08 14:22:54 +02002389 wdev_lock(dev->ieee80211_ptr);
2390 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02002391
2392 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
2393 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
2394 err = -ENOENT;
2395
Johannes Bergfffd0932009-07-08 14:22:54 +02002396 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02002397 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
2398 key.type == NL80211_KEYTYPE_PAIRWISE,
2399 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01002400
Johannes Berg3d23e342009-09-29 23:27:28 +02002401#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02002402 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02002403 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02002404 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02002405 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02002406 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
2407 }
2408#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02002409 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02002410
Johannes Berg41ade002007-12-19 02:03:29 +01002411 return err;
2412}
2413
Johannes Berg88600202012-02-13 15:17:18 +01002414static int nl80211_parse_beacon(struct genl_info *info,
2415 struct cfg80211_beacon_data *bcn)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002416{
Johannes Berg88600202012-02-13 15:17:18 +01002417 bool haveinfo = false;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002418
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002419 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
2420 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2421 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2422 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002423 return -EINVAL;
2424
Johannes Berg88600202012-02-13 15:17:18 +01002425 memset(bcn, 0, sizeof(*bcn));
Johannes Berged1b6cc2007-12-19 02:03:32 +01002426
Johannes Berged1b6cc2007-12-19 02:03:32 +01002427 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
Johannes Berg88600202012-02-13 15:17:18 +01002428 bcn->head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2429 bcn->head_len = nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2430 if (!bcn->head_len)
2431 return -EINVAL;
2432 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002433 }
2434
2435 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
Johannes Berg88600202012-02-13 15:17:18 +01002436 bcn->tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2437 bcn->tail_len =
Johannes Berged1b6cc2007-12-19 02:03:32 +01002438 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
Johannes Berg88600202012-02-13 15:17:18 +01002439 haveinfo = true;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002440 }
2441
Johannes Berg4c476992010-10-04 21:36:35 +02002442 if (!haveinfo)
2443 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002444
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002445 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg88600202012-02-13 15:17:18 +01002446 bcn->beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2447 bcn->beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002448 }
2449
2450 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002451 bcn->proberesp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002452 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002453 bcn->proberesp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002454 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2455 }
2456
2457 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002458 bcn->assocresp_ies =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002459 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002460 bcn->assocresp_ies_len =
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002461 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2462 }
2463
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002464 if (info->attrs[NL80211_ATTR_PROBE_RESP]) {
Johannes Berg88600202012-02-13 15:17:18 +01002465 bcn->probe_resp =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002466 nla_data(info->attrs[NL80211_ATTR_PROBE_RESP]);
Johannes Berg88600202012-02-13 15:17:18 +01002467 bcn->probe_resp_len =
Arik Nemtsov00f740e2011-11-10 11:28:56 +02002468 nla_len(info->attrs[NL80211_ATTR_PROBE_RESP]);
2469 }
2470
Johannes Berg88600202012-02-13 15:17:18 +01002471 return 0;
2472}
2473
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002474static bool nl80211_get_ap_channel(struct cfg80211_registered_device *rdev,
2475 struct cfg80211_ap_settings *params)
2476{
2477 struct wireless_dev *wdev;
2478 bool ret = false;
2479
2480 mutex_lock(&rdev->devlist_mtx);
2481
Johannes Berg89a54e42012-06-15 14:33:17 +02002482 list_for_each_entry(wdev, &rdev->wdev_list, list) {
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002483 if (wdev->iftype != NL80211_IFTYPE_AP &&
2484 wdev->iftype != NL80211_IFTYPE_P2P_GO)
2485 continue;
2486
2487 if (!wdev->preset_chan)
2488 continue;
2489
2490 params->channel = wdev->preset_chan;
2491 params->channel_type = wdev->preset_chantype;
2492 ret = true;
2493 break;
2494 }
2495
2496 mutex_unlock(&rdev->devlist_mtx);
2497
2498 return ret;
2499}
2500
Jouni Malinene39e5b52012-09-30 19:29:39 +03002501static bool nl80211_valid_auth_type(struct cfg80211_registered_device *rdev,
2502 enum nl80211_auth_type auth_type,
2503 enum nl80211_commands cmd)
2504{
2505 if (auth_type > NL80211_AUTHTYPE_MAX)
2506 return false;
2507
2508 switch (cmd) {
2509 case NL80211_CMD_AUTHENTICATE:
2510 if (!(rdev->wiphy.features & NL80211_FEATURE_SAE) &&
2511 auth_type == NL80211_AUTHTYPE_SAE)
2512 return false;
2513 return true;
2514 case NL80211_CMD_CONNECT:
2515 case NL80211_CMD_START_AP:
2516 /* SAE not supported yet */
2517 if (auth_type == NL80211_AUTHTYPE_SAE)
2518 return false;
2519 return true;
2520 default:
2521 return false;
2522 }
2523}
2524
Johannes Berg88600202012-02-13 15:17:18 +01002525static int nl80211_start_ap(struct sk_buff *skb, struct genl_info *info)
2526{
2527 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2528 struct net_device *dev = info->user_ptr[1];
2529 struct wireless_dev *wdev = dev->ieee80211_ptr;
2530 struct cfg80211_ap_settings params;
2531 int err;
2532
2533 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2534 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2535 return -EOPNOTSUPP;
2536
2537 if (!rdev->ops->start_ap)
2538 return -EOPNOTSUPP;
2539
2540 if (wdev->beacon_interval)
2541 return -EALREADY;
2542
2543 memset(&params, 0, sizeof(params));
2544
2545 /* these are required for START_AP */
2546 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2547 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
2548 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2549 return -EINVAL;
2550
2551 err = nl80211_parse_beacon(info, &params.beacon);
2552 if (err)
2553 return err;
2554
2555 params.beacon_interval =
2556 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2557 params.dtim_period =
2558 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2559
2560 err = cfg80211_validate_beacon_int(rdev, params.beacon_interval);
2561 if (err)
2562 return err;
2563
2564 /*
2565 * In theory, some of these attributes should be required here
2566 * but since they were not used when the command was originally
2567 * added, keep them optional for old user space programs to let
2568 * them continue to work with drivers that do not need the
2569 * additional information -- drivers must check!
2570 */
2571 if (info->attrs[NL80211_ATTR_SSID]) {
2572 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2573 params.ssid_len =
2574 nla_len(info->attrs[NL80211_ATTR_SSID]);
2575 if (params.ssid_len == 0 ||
2576 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2577 return -EINVAL;
2578 }
2579
2580 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2581 params.hidden_ssid = nla_get_u32(
2582 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2583 if (params.hidden_ssid != NL80211_HIDDEN_SSID_NOT_IN_USE &&
2584 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_LEN &&
2585 params.hidden_ssid != NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2586 return -EINVAL;
2587 }
2588
2589 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2590
2591 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2592 params.auth_type = nla_get_u32(
2593 info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03002594 if (!nl80211_valid_auth_type(rdev, params.auth_type,
2595 NL80211_CMD_START_AP))
Johannes Berg88600202012-02-13 15:17:18 +01002596 return -EINVAL;
2597 } else
2598 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2599
2600 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2601 NL80211_MAX_NR_CIPHER_SUITES);
2602 if (err)
2603 return err;
2604
Vasanthakumar Thiagarajan1b658f12012-03-02 15:50:02 +05302605 if (info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]) {
2606 if (!(rdev->wiphy.features & NL80211_FEATURE_INACTIVITY_TIMER))
2607 return -EOPNOTSUPP;
2608 params.inactivity_timeout = nla_get_u16(
2609 info->attrs[NL80211_ATTR_INACTIVITY_TIMEOUT]);
2610 }
2611
Johannes Bergaa430da2012-05-16 23:50:18 +02002612 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
2613 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
2614
2615 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
2616 !nl80211_valid_channel_type(info, &channel_type))
2617 return -EINVAL;
2618
2619 params.channel = rdev_freq_to_chan(rdev,
2620 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
2621 channel_type);
2622 if (!params.channel)
2623 return -EINVAL;
2624 params.channel_type = channel_type;
2625 } else if (wdev->preset_chan) {
2626 params.channel = wdev->preset_chan;
2627 params.channel_type = wdev->preset_chantype;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002628 } else if (!nl80211_get_ap_channel(rdev, &params))
Johannes Bergaa430da2012-05-16 23:50:18 +02002629 return -EINVAL;
2630
2631 if (!cfg80211_can_beacon_sec_chan(&rdev->wiphy, params.channel,
2632 params.channel_type))
2633 return -EINVAL;
2634
Michal Kaziore4e32452012-06-29 12:47:08 +02002635 mutex_lock(&rdev->devlist_mtx);
2636 err = cfg80211_can_use_chan(rdev, wdev, params.channel,
2637 CHAN_MODE_SHARED);
2638 mutex_unlock(&rdev->devlist_mtx);
2639
2640 if (err)
2641 return err;
2642
Johannes Berg88600202012-02-13 15:17:18 +01002643 err = rdev->ops->start_ap(&rdev->wiphy, dev, &params);
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002644 if (!err) {
2645 wdev->preset_chan = params.channel;
2646 wdev->preset_chantype = params.channel_type;
Johannes Berg88600202012-02-13 15:17:18 +01002647 wdev->beacon_interval = params.beacon_interval;
Michal Kaziorf4489eb2012-06-29 12:46:58 +02002648 wdev->channel = params.channel;
Felix Fietkau46c1dd02012-06-19 02:50:57 +02002649 }
Johannes Berg56d18932011-05-09 18:41:15 +02002650 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002651}
2652
Johannes Berg88600202012-02-13 15:17:18 +01002653static int nl80211_set_beacon(struct sk_buff *skb, struct genl_info *info)
2654{
2655 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2656 struct net_device *dev = info->user_ptr[1];
2657 struct wireless_dev *wdev = dev->ieee80211_ptr;
2658 struct cfg80211_beacon_data params;
2659 int err;
2660
2661 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
2662 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2663 return -EOPNOTSUPP;
2664
2665 if (!rdev->ops->change_beacon)
2666 return -EOPNOTSUPP;
2667
2668 if (!wdev->beacon_interval)
2669 return -EINVAL;
2670
2671 err = nl80211_parse_beacon(info, &params);
2672 if (err)
2673 return err;
2674
2675 return rdev->ops->change_beacon(&rdev->wiphy, dev, &params);
2676}
2677
2678static int nl80211_stop_ap(struct sk_buff *skb, struct genl_info *info)
Johannes Berged1b6cc2007-12-19 02:03:32 +01002679{
Johannes Berg4c476992010-10-04 21:36:35 +02002680 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2681 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01002682
Michal Kazior60771782012-06-29 12:46:56 +02002683 return cfg80211_stop_ap(rdev, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01002684}
2685
Johannes Berg5727ef12007-12-19 02:03:34 +01002686static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2687 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2688 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2689 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002690 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002691 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Bergd83023d2011-12-14 09:29:15 +01002692 [NL80211_STA_FLAG_TDLS_PEER] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002693};
2694
Johannes Bergeccb8e82009-05-11 21:57:56 +03002695static int parse_station_flags(struct genl_info *info,
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002696 enum nl80211_iftype iftype,
Johannes Bergeccb8e82009-05-11 21:57:56 +03002697 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002698{
2699 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002700 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002701 int flag;
2702
Johannes Bergeccb8e82009-05-11 21:57:56 +03002703 /*
2704 * Try parsing the new attribute first so userspace
2705 * can specify both for older kernels.
2706 */
2707 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2708 if (nla) {
2709 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002710
Johannes Bergeccb8e82009-05-11 21:57:56 +03002711 sta_flags = nla_data(nla);
2712 params->sta_flags_mask = sta_flags->mask;
2713 params->sta_flags_set = sta_flags->set;
2714 if ((params->sta_flags_mask |
2715 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2716 return -EINVAL;
2717 return 0;
2718 }
2719
2720 /* if present, parse the old attribute */
2721
2722 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002723 if (!nla)
2724 return 0;
2725
2726 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2727 nla, sta_flags_policy))
2728 return -EINVAL;
2729
Johannes Bergbdd3ae32012-01-02 13:30:03 +01002730 /*
2731 * Only allow certain flags for interface types so that
2732 * other attributes are silently ignored. Remember that
2733 * this is backward compatibility code with old userspace
2734 * and shouldn't be hit in other cases anyway.
2735 */
2736 switch (iftype) {
2737 case NL80211_IFTYPE_AP:
2738 case NL80211_IFTYPE_AP_VLAN:
2739 case NL80211_IFTYPE_P2P_GO:
2740 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2741 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
2742 BIT(NL80211_STA_FLAG_WME) |
2743 BIT(NL80211_STA_FLAG_MFP);
2744 break;
2745 case NL80211_IFTYPE_P2P_CLIENT:
2746 case NL80211_IFTYPE_STATION:
2747 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHORIZED) |
2748 BIT(NL80211_STA_FLAG_TDLS_PEER);
2749 break;
2750 case NL80211_IFTYPE_MESH_POINT:
2751 params->sta_flags_mask = BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2752 BIT(NL80211_STA_FLAG_MFP) |
2753 BIT(NL80211_STA_FLAG_AUTHORIZED);
2754 default:
2755 return -EINVAL;
2756 }
Johannes Berg5727ef12007-12-19 02:03:34 +01002757
Johannes Berg3383b5a2012-05-10 20:14:43 +02002758 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++) {
2759 if (flags[flag]) {
Johannes Bergeccb8e82009-05-11 21:57:56 +03002760 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002761
Johannes Berg3383b5a2012-05-10 20:14:43 +02002762 /* no longer support new API additions in old API */
2763 if (flag > NL80211_STA_FLAG_MAX_OLD_API)
2764 return -EINVAL;
2765 }
2766 }
2767
Johannes Berg5727ef12007-12-19 02:03:34 +01002768 return 0;
2769}
2770
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002771static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2772 int attr)
2773{
2774 struct nlattr *rate;
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002775 u32 bitrate;
2776 u16 bitrate_compat;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002777
2778 rate = nla_nest_start(msg, attr);
2779 if (!rate)
2780 goto nla_put_failure;
2781
2782 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2783 bitrate = cfg80211_calculate_bitrate(info);
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002784 /* report 16-bit bitrate only if we can */
2785 bitrate_compat = bitrate < (1UL << 16) ? bitrate : 0;
David S. Miller9360ffd2012-03-29 04:41:26 -04002786 if ((bitrate > 0 &&
Vladimir Kondratiev8eb41c82012-07-05 14:25:49 +03002787 nla_put_u32(msg, NL80211_RATE_INFO_BITRATE32, bitrate)) ||
2788 (bitrate_compat > 0 &&
2789 nla_put_u16(msg, NL80211_RATE_INFO_BITRATE, bitrate_compat)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04002790 ((info->flags & RATE_INFO_FLAGS_MCS) &&
2791 nla_put_u8(msg, NL80211_RATE_INFO_MCS, info->mcs)) ||
2792 ((info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH) &&
2793 nla_put_flag(msg, NL80211_RATE_INFO_40_MHZ_WIDTH)) ||
2794 ((info->flags & RATE_INFO_FLAGS_SHORT_GI) &&
2795 nla_put_flag(msg, NL80211_RATE_INFO_SHORT_GI)))
2796 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002797
2798 nla_nest_end(msg, rate);
2799 return true;
2800
2801nla_put_failure:
2802 return false;
2803}
2804
Eric W. Biederman15e47302012-09-07 20:12:54 +00002805static int nl80211_send_station(struct sk_buff *msg, u32 portid, u32 seq,
John W. Linville66266b32012-03-15 13:25:41 -04002806 int flags,
2807 struct cfg80211_registered_device *rdev,
2808 struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002809 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002810{
2811 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002812 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002813
Eric W. Biederman15e47302012-09-07 20:12:54 +00002814 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002815 if (!hdr)
2816 return -1;
2817
David S. Miller9360ffd2012-03-29 04:41:26 -04002818 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
2819 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
2820 nla_put_u32(msg, NL80211_ATTR_GENERATION, sinfo->generation))
2821 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02002822
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002823 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2824 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002825 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04002826 if ((sinfo->filled & STATION_INFO_CONNECTED_TIME) &&
2827 nla_put_u32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2828 sinfo->connected_time))
2829 goto nla_put_failure;
2830 if ((sinfo->filled & STATION_INFO_INACTIVE_TIME) &&
2831 nla_put_u32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2832 sinfo->inactive_time))
2833 goto nla_put_failure;
2834 if ((sinfo->filled & STATION_INFO_RX_BYTES) &&
2835 nla_put_u32(msg, NL80211_STA_INFO_RX_BYTES,
2836 sinfo->rx_bytes))
2837 goto nla_put_failure;
2838 if ((sinfo->filled & STATION_INFO_TX_BYTES) &&
2839 nla_put_u32(msg, NL80211_STA_INFO_TX_BYTES,
2840 sinfo->tx_bytes))
2841 goto nla_put_failure;
2842 if ((sinfo->filled & STATION_INFO_LLID) &&
2843 nla_put_u16(msg, NL80211_STA_INFO_LLID, sinfo->llid))
2844 goto nla_put_failure;
2845 if ((sinfo->filled & STATION_INFO_PLID) &&
2846 nla_put_u16(msg, NL80211_STA_INFO_PLID, sinfo->plid))
2847 goto nla_put_failure;
2848 if ((sinfo->filled & STATION_INFO_PLINK_STATE) &&
2849 nla_put_u8(msg, NL80211_STA_INFO_PLINK_STATE,
2850 sinfo->plink_state))
2851 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002852 switch (rdev->wiphy.signal_type) {
2853 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04002854 if ((sinfo->filled & STATION_INFO_SIGNAL) &&
2855 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL,
2856 sinfo->signal))
2857 goto nla_put_failure;
2858 if ((sinfo->filled & STATION_INFO_SIGNAL_AVG) &&
2859 nla_put_u8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2860 sinfo->signal_avg))
2861 goto nla_put_failure;
John W. Linville66266b32012-03-15 13:25:41 -04002862 break;
2863 default:
2864 break;
2865 }
Henning Rogge420e7fa2008-12-11 22:04:19 +01002866 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002867 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2868 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002869 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002870 }
2871 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2872 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2873 NL80211_STA_INFO_RX_BITRATE))
2874 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002875 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002876 if ((sinfo->filled & STATION_INFO_RX_PACKETS) &&
2877 nla_put_u32(msg, NL80211_STA_INFO_RX_PACKETS,
2878 sinfo->rx_packets))
2879 goto nla_put_failure;
2880 if ((sinfo->filled & STATION_INFO_TX_PACKETS) &&
2881 nla_put_u32(msg, NL80211_STA_INFO_TX_PACKETS,
2882 sinfo->tx_packets))
2883 goto nla_put_failure;
2884 if ((sinfo->filled & STATION_INFO_TX_RETRIES) &&
2885 nla_put_u32(msg, NL80211_STA_INFO_TX_RETRIES,
2886 sinfo->tx_retries))
2887 goto nla_put_failure;
2888 if ((sinfo->filled & STATION_INFO_TX_FAILED) &&
2889 nla_put_u32(msg, NL80211_STA_INFO_TX_FAILED,
2890 sinfo->tx_failed))
2891 goto nla_put_failure;
2892 if ((sinfo->filled & STATION_INFO_BEACON_LOSS_COUNT) &&
2893 nla_put_u32(msg, NL80211_STA_INFO_BEACON_LOSS,
2894 sinfo->beacon_loss_count))
2895 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002896 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2897 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2898 if (!bss_param)
2899 goto nla_put_failure;
2900
David S. Miller9360ffd2012-03-29 04:41:26 -04002901 if (((sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT) &&
2902 nla_put_flag(msg, NL80211_STA_BSS_PARAM_CTS_PROT)) ||
2903 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE) &&
2904 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE)) ||
2905 ((sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME) &&
2906 nla_put_flag(msg, NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME)) ||
2907 nla_put_u8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2908 sinfo->bss_param.dtim_period) ||
2909 nla_put_u16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2910 sinfo->bss_param.beacon_interval))
2911 goto nla_put_failure;
Paul Stewartf4263c92011-03-31 09:25:41 -07002912
2913 nla_nest_end(msg, bss_param);
2914 }
David S. Miller9360ffd2012-03-29 04:41:26 -04002915 if ((sinfo->filled & STATION_INFO_STA_FLAGS) &&
2916 nla_put(msg, NL80211_STA_INFO_STA_FLAGS,
2917 sizeof(struct nl80211_sta_flag_update),
2918 &sinfo->sta_flags))
2919 goto nla_put_failure;
John W. Linville7eab0f62012-04-12 14:25:14 -04002920 if ((sinfo->filled & STATION_INFO_T_OFFSET) &&
2921 nla_put_u64(msg, NL80211_STA_INFO_T_OFFSET,
2922 sinfo->t_offset))
2923 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002924 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002925
David S. Miller9360ffd2012-03-29 04:41:26 -04002926 if ((sinfo->filled & STATION_INFO_ASSOC_REQ_IES) &&
2927 nla_put(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2928 sinfo->assoc_req_ies))
2929 goto nla_put_failure;
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002930
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002931 return genlmsg_end(msg, hdr);
2932
2933 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002934 genlmsg_cancel(msg, hdr);
2935 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002936}
2937
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002938static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002939 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002940{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002941 struct station_info sinfo;
2942 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002943 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002944 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002945 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002946 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002947
Johannes Berg67748892010-10-04 21:14:06 +02002948 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2949 if (err)
2950 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002951
2952 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002953 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002954 goto out_err;
2955 }
2956
Johannes Bergbba95fe2008-07-29 13:22:51 +02002957 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002958 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002959 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2960 mac_addr, &sinfo);
2961 if (err == -ENOENT)
2962 break;
2963 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002964 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002965
2966 if (nl80211_send_station(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002967 NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002968 cb->nlh->nlmsg_seq, NLM_F_MULTI,
John W. Linville66266b32012-03-15 13:25:41 -04002969 dev, netdev, mac_addr,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002970 &sinfo) < 0)
2971 goto out;
2972
2973 sta_idx++;
2974 }
2975
2976
2977 out:
2978 cb->args[1] = sta_idx;
2979 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002980 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002981 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002982
2983 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002984}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002985
Johannes Berg5727ef12007-12-19 02:03:34 +01002986static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2987{
Johannes Berg4c476992010-10-04 21:36:35 +02002988 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2989 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002990 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002991 struct sk_buff *msg;
2992 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002993 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002994
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002995 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002996
2997 if (!info->attrs[NL80211_ATTR_MAC])
2998 return -EINVAL;
2999
3000 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3001
Johannes Berg4c476992010-10-04 21:36:35 +02003002 if (!rdev->ops->get_station)
3003 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003004
Johannes Berg79c97e92009-07-07 03:56:12 +02003005 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003006 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003007 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003008
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003009 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003010 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003011 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003012
Eric W. Biederman15e47302012-09-07 20:12:54 +00003013 if (nl80211_send_station(msg, info->snd_portid, info->snd_seq, 0,
John W. Linville66266b32012-03-15 13:25:41 -04003014 rdev, dev, mac_addr, &sinfo) < 0) {
Johannes Berg4c476992010-10-04 21:36:35 +02003015 nlmsg_free(msg);
3016 return -ENOBUFS;
3017 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01003018
Johannes Berg4c476992010-10-04 21:36:35 +02003019 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01003020}
3021
3022/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01003023 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01003024 */
Johannes Berg80b99892011-11-18 16:23:01 +01003025static struct net_device *get_vlan(struct genl_info *info,
3026 struct cfg80211_registered_device *rdev)
Johannes Berg5727ef12007-12-19 02:03:34 +01003027{
Johannes Berg463d0182009-07-14 00:33:35 +02003028 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg80b99892011-11-18 16:23:01 +01003029 struct net_device *v;
3030 int ret;
Johannes Berg5727ef12007-12-19 02:03:34 +01003031
Johannes Berg80b99892011-11-18 16:23:01 +01003032 if (!vlanattr)
3033 return NULL;
3034
3035 v = dev_get_by_index(genl_info_net(info), nla_get_u32(vlanattr));
3036 if (!v)
3037 return ERR_PTR(-ENODEV);
3038
3039 if (!v->ieee80211_ptr || v->ieee80211_ptr->wiphy != &rdev->wiphy) {
3040 ret = -EINVAL;
3041 goto error;
Johannes Berg5727ef12007-12-19 02:03:34 +01003042 }
Johannes Berg80b99892011-11-18 16:23:01 +01003043
3044 if (!netif_running(v)) {
3045 ret = -ENETDOWN;
3046 goto error;
3047 }
3048
3049 return v;
3050 error:
3051 dev_put(v);
3052 return ERR_PTR(ret);
Johannes Berg5727ef12007-12-19 02:03:34 +01003053}
3054
3055static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
3056{
Johannes Berg4c476992010-10-04 21:36:35 +02003057 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003058 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003059 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003060 struct station_parameters params;
3061 u8 *mac_addr = NULL;
3062
3063 memset(&params, 0, sizeof(params));
3064
3065 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07003066 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01003067
3068 if (info->attrs[NL80211_ATTR_STA_AID])
3069 return -EINVAL;
3070
3071 if (!info->attrs[NL80211_ATTR_MAC])
3072 return -EINVAL;
3073
3074 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3075
3076 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
3077 params.supported_rates =
3078 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3079 params.supported_rates_len =
3080 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3081 }
3082
3083 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3084 params.listen_interval =
3085 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
3086
Jouni Malinen36aedc92008-08-25 11:58:58 +03003087 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3088 params.ht_capa =
3089 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
3090
Johannes Bergbdd90d52011-12-14 12:20:27 +01003091 if (!rdev->ops->change_station)
3092 return -EOPNOTSUPP;
3093
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003094 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003095 return -EINVAL;
3096
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003097 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3098 params.plink_action =
3099 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3100
Javier Cardona9c3990a2011-05-03 16:57:11 -07003101 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
3102 params.plink_state =
3103 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
3104
Johannes Berga97f4422009-06-18 17:23:43 +02003105 switch (dev->ieee80211_ptr->iftype) {
3106 case NL80211_IFTYPE_AP:
3107 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02003108 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02003109 /* disallow mesh-specific things */
3110 if (params.plink_action)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003111 return -EINVAL;
3112
3113 /* TDLS can't be set, ... */
3114 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
3115 return -EINVAL;
3116 /*
3117 * ... but don't bother the driver with it. This works around
3118 * a hostapd/wpa_supplicant issue -- it always includes the
3119 * TLDS_PEER flag in the mask even for AP mode.
3120 */
3121 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
3122
3123 /* accept only the listed bits */
3124 if (params.sta_flags_mask &
3125 ~(BIT(NL80211_STA_FLAG_AUTHORIZED) |
3126 BIT(NL80211_STA_FLAG_SHORT_PREAMBLE) |
3127 BIT(NL80211_STA_FLAG_WME) |
3128 BIT(NL80211_STA_FLAG_MFP)))
3129 return -EINVAL;
3130
3131 /* must be last in here for error handling */
3132 params.vlan = get_vlan(info, rdev);
3133 if (IS_ERR(params.vlan))
3134 return PTR_ERR(params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02003135 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02003136 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02003137 case NL80211_IFTYPE_STATION:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003138 /*
3139 * Don't allow userspace to change the TDLS_PEER flag,
3140 * but silently ignore attempts to change it since we
3141 * don't have state here to verify that it doesn't try
3142 * to change the flag.
3143 */
3144 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Antonio Quartulli267335d2012-01-31 20:25:47 +01003145 /* fall through */
3146 case NL80211_IFTYPE_ADHOC:
3147 /* disallow things sta doesn't support */
3148 if (params.plink_action)
3149 return -EINVAL;
3150 if (params.ht_capa)
3151 return -EINVAL;
3152 if (params.listen_interval >= 0)
3153 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003154 /* reject any changes other than AUTHORIZED */
3155 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
3156 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003157 break;
3158 case NL80211_IFTYPE_MESH_POINT:
3159 /* disallow things mesh doesn't support */
3160 if (params.vlan)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003161 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003162 if (params.ht_capa)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003163 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003164 if (params.listen_interval >= 0)
Johannes Bergbdd90d52011-12-14 12:20:27 +01003165 return -EINVAL;
3166 /*
3167 * No special handling for TDLS here -- the userspace
3168 * mesh code doesn't have this bug.
3169 */
Javier Cardonab39c48f2011-04-07 15:08:30 -07003170 if (params.sta_flags_mask &
3171 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07003172 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07003173 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Bergbdd90d52011-12-14 12:20:27 +01003174 return -EINVAL;
Johannes Berga97f4422009-06-18 17:23:43 +02003175 break;
3176 default:
Johannes Bergbdd90d52011-12-14 12:20:27 +01003177 return -EOPNOTSUPP;
Johannes Berg034d6552009-05-27 10:35:29 +02003178 }
3179
Johannes Bergbdd90d52011-12-14 12:20:27 +01003180 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003181
Johannes Berg79c97e92009-07-07 03:56:12 +02003182 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003183
Johannes Berg5727ef12007-12-19 02:03:34 +01003184 if (params.vlan)
3185 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01003186
Johannes Berg5727ef12007-12-19 02:03:34 +01003187 return err;
3188}
3189
Eliad Pellerc75786c2011-08-23 14:37:46 +03003190static struct nla_policy
3191nl80211_sta_wme_policy[NL80211_STA_WME_MAX + 1] __read_mostly = {
3192 [NL80211_STA_WME_UAPSD_QUEUES] = { .type = NLA_U8 },
3193 [NL80211_STA_WME_MAX_SP] = { .type = NLA_U8 },
3194};
3195
Johannes Berg5727ef12007-12-19 02:03:34 +01003196static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
3197{
Johannes Berg4c476992010-10-04 21:36:35 +02003198 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01003199 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003200 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003201 struct station_parameters params;
3202 u8 *mac_addr = NULL;
3203
3204 memset(&params, 0, sizeof(params));
3205
3206 if (!info->attrs[NL80211_ATTR_MAC])
3207 return -EINVAL;
3208
Johannes Berg5727ef12007-12-19 02:03:34 +01003209 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
3210 return -EINVAL;
3211
3212 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
3213 return -EINVAL;
3214
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003215 if (!info->attrs[NL80211_ATTR_STA_AID])
3216 return -EINVAL;
3217
Johannes Berg5727ef12007-12-19 02:03:34 +01003218 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3219 params.supported_rates =
3220 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3221 params.supported_rates_len =
3222 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
3223 params.listen_interval =
3224 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02003225
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02003226 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
3227 if (!params.aid || params.aid > IEEE80211_MAX_AID)
3228 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02003229
Jouni Malinen36aedc92008-08-25 11:58:58 +03003230 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
3231 params.ht_capa =
3232 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01003233
Mahesh Palivelaf461be3e2012-10-11 08:04:52 +00003234 if (info->attrs[NL80211_ATTR_VHT_CAPABILITY])
3235 params.vht_capa =
3236 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3237
Javier Cardona96b78df2011-04-07 15:08:33 -07003238 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
3239 params.plink_action =
3240 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
3241
Johannes Bergbdd90d52011-12-14 12:20:27 +01003242 if (!rdev->ops->add_station)
3243 return -EOPNOTSUPP;
3244
Johannes Bergbdd3ae32012-01-02 13:30:03 +01003245 if (parse_station_flags(info, dev->ieee80211_ptr->iftype, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01003246 return -EINVAL;
3247
Johannes Bergbdd90d52011-12-14 12:20:27 +01003248 switch (dev->ieee80211_ptr->iftype) {
3249 case NL80211_IFTYPE_AP:
3250 case NL80211_IFTYPE_AP_VLAN:
3251 case NL80211_IFTYPE_P2P_GO:
3252 /* parse WME attributes if sta is WME capable */
3253 if ((rdev->wiphy.flags & WIPHY_FLAG_AP_UAPSD) &&
3254 (params.sta_flags_set & BIT(NL80211_STA_FLAG_WME)) &&
3255 info->attrs[NL80211_ATTR_STA_WME]) {
3256 struct nlattr *tb[NL80211_STA_WME_MAX + 1];
3257 struct nlattr *nla;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003258
Johannes Bergbdd90d52011-12-14 12:20:27 +01003259 nla = info->attrs[NL80211_ATTR_STA_WME];
3260 err = nla_parse_nested(tb, NL80211_STA_WME_MAX, nla,
3261 nl80211_sta_wme_policy);
3262 if (err)
3263 return err;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003264
Johannes Bergbdd90d52011-12-14 12:20:27 +01003265 if (tb[NL80211_STA_WME_UAPSD_QUEUES])
3266 params.uapsd_queues =
3267 nla_get_u8(tb[NL80211_STA_WME_UAPSD_QUEUES]);
3268 if (params.uapsd_queues &
3269 ~IEEE80211_WMM_IE_STA_QOSINFO_AC_MASK)
3270 return -EINVAL;
3271
3272 if (tb[NL80211_STA_WME_MAX_SP])
3273 params.max_sp =
3274 nla_get_u8(tb[NL80211_STA_WME_MAX_SP]);
3275
3276 if (params.max_sp &
3277 ~IEEE80211_WMM_IE_STA_QOSINFO_SP_MASK)
3278 return -EINVAL;
3279
3280 params.sta_modify_mask |= STATION_PARAM_APPLY_UAPSD;
3281 }
3282 /* TDLS peers cannot be added */
3283 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003284 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003285 /* but don't bother the driver with it */
3286 params.sta_flags_mask &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
Eliad Pellerc75786c2011-08-23 14:37:46 +03003287
Johannes Bergbdd90d52011-12-14 12:20:27 +01003288 /* must be last in here for error handling */
3289 params.vlan = get_vlan(info, rdev);
3290 if (IS_ERR(params.vlan))
3291 return PTR_ERR(params.vlan);
3292 break;
3293 case NL80211_IFTYPE_MESH_POINT:
3294 /* TDLS peers cannot be added */
3295 if (params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER))
Johannes Berg4319e192011-09-07 11:50:48 +02003296 return -EINVAL;
Johannes Bergbdd90d52011-12-14 12:20:27 +01003297 break;
3298 case NL80211_IFTYPE_STATION:
3299 /* Only TDLS peers can be added */
3300 if (!(params.sta_flags_set & BIT(NL80211_STA_FLAG_TDLS_PEER)))
3301 return -EINVAL;
3302 /* Can only add if TDLS ... */
3303 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS))
3304 return -EOPNOTSUPP;
3305 /* ... with external setup is supported */
3306 if (!(rdev->wiphy.flags & WIPHY_FLAG_TDLS_EXTERNAL_SETUP))
3307 return -EOPNOTSUPP;
3308 break;
3309 default:
3310 return -EOPNOTSUPP;
Eliad Pellerc75786c2011-08-23 14:37:46 +03003311 }
3312
Johannes Bergbdd90d52011-12-14 12:20:27 +01003313 /* be aware of params.vlan when changing code here */
Johannes Berg5727ef12007-12-19 02:03:34 +01003314
Johannes Berg79c97e92009-07-07 03:56:12 +02003315 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01003316
Johannes Berg5727ef12007-12-19 02:03:34 +01003317 if (params.vlan)
3318 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01003319 return err;
3320}
3321
3322static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
3323{
Johannes Berg4c476992010-10-04 21:36:35 +02003324 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3325 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01003326 u8 *mac_addr = NULL;
3327
3328 if (info->attrs[NL80211_ATTR_MAC])
3329 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
3330
Johannes Berge80cf852009-05-11 14:43:13 +02003331 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02003332 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02003333 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02003334 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3335 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02003336
Johannes Berg4c476992010-10-04 21:36:35 +02003337 if (!rdev->ops->del_station)
3338 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01003339
Johannes Berg4c476992010-10-04 21:36:35 +02003340 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01003341}
3342
Eric W. Biederman15e47302012-09-07 20:12:54 +00003343static int nl80211_send_mpath(struct sk_buff *msg, u32 portid, u32 seq,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003344 int flags, struct net_device *dev,
3345 u8 *dst, u8 *next_hop,
3346 struct mpath_info *pinfo)
3347{
3348 void *hdr;
3349 struct nlattr *pinfoattr;
3350
Eric W. Biederman15e47302012-09-07 20:12:54 +00003351 hdr = nl80211hdr_put(msg, portid, seq, flags, NL80211_CMD_NEW_STATION);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003352 if (!hdr)
3353 return -1;
3354
David S. Miller9360ffd2012-03-29 04:41:26 -04003355 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3356 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, dst) ||
3357 nla_put(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop) ||
3358 nla_put_u32(msg, NL80211_ATTR_GENERATION, pinfo->generation))
3359 goto nla_put_failure;
Johannes Bergf5ea9122009-08-07 16:17:38 +02003360
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003361 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
3362 if (!pinfoattr)
3363 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003364 if ((pinfo->filled & MPATH_INFO_FRAME_QLEN) &&
3365 nla_put_u32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
3366 pinfo->frame_qlen))
3367 goto nla_put_failure;
3368 if (((pinfo->filled & MPATH_INFO_SN) &&
3369 nla_put_u32(msg, NL80211_MPATH_INFO_SN, pinfo->sn)) ||
3370 ((pinfo->filled & MPATH_INFO_METRIC) &&
3371 nla_put_u32(msg, NL80211_MPATH_INFO_METRIC,
3372 pinfo->metric)) ||
3373 ((pinfo->filled & MPATH_INFO_EXPTIME) &&
3374 nla_put_u32(msg, NL80211_MPATH_INFO_EXPTIME,
3375 pinfo->exptime)) ||
3376 ((pinfo->filled & MPATH_INFO_FLAGS) &&
3377 nla_put_u8(msg, NL80211_MPATH_INFO_FLAGS,
3378 pinfo->flags)) ||
3379 ((pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT) &&
3380 nla_put_u32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
3381 pinfo->discovery_timeout)) ||
3382 ((pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES) &&
3383 nla_put_u8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
3384 pinfo->discovery_retries)))
3385 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003386
3387 nla_nest_end(msg, pinfoattr);
3388
3389 return genlmsg_end(msg, hdr);
3390
3391 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07003392 genlmsg_cancel(msg, hdr);
3393 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003394}
3395
3396static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003397 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003398{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003399 struct mpath_info pinfo;
3400 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003401 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003402 u8 dst[ETH_ALEN];
3403 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02003404 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003405 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003406
Johannes Berg67748892010-10-04 21:14:06 +02003407 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3408 if (err)
3409 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003410
3411 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02003412 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003413 goto out_err;
3414 }
3415
Jouni Malineneec60b02009-03-20 21:21:19 +02003416 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
3417 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02003418 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02003419 }
3420
Johannes Bergbba95fe2008-07-29 13:22:51 +02003421 while (1) {
3422 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
3423 dst, next_hop, &pinfo);
3424 if (err == -ENOENT)
3425 break;
3426 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01003427 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003428
Eric W. Biederman15e47302012-09-07 20:12:54 +00003429 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).portid,
Johannes Bergbba95fe2008-07-29 13:22:51 +02003430 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3431 netdev, dst, next_hop,
3432 &pinfo) < 0)
3433 goto out;
3434
3435 path_idx++;
3436 }
3437
3438
3439 out:
3440 cb->args[1] = path_idx;
3441 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02003442 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003443 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02003444 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003445}
3446
3447static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
3448{
Johannes Berg4c476992010-10-04 21:36:35 +02003449 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003450 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02003451 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003452 struct mpath_info pinfo;
3453 struct sk_buff *msg;
3454 u8 *dst = NULL;
3455 u8 next_hop[ETH_ALEN];
3456
3457 memset(&pinfo, 0, sizeof(pinfo));
3458
3459 if (!info->attrs[NL80211_ATTR_MAC])
3460 return -EINVAL;
3461
3462 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3463
Johannes Berg4c476992010-10-04 21:36:35 +02003464 if (!rdev->ops->get_mpath)
3465 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003466
Johannes Berg4c476992010-10-04 21:36:35 +02003467 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3468 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003469
Johannes Berg79c97e92009-07-07 03:56:12 +02003470 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003471 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003472 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003473
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003474 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003475 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02003476 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003477
Eric W. Biederman15e47302012-09-07 20:12:54 +00003478 if (nl80211_send_mpath(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02003479 dev, dst, next_hop, &pinfo) < 0) {
3480 nlmsg_free(msg);
3481 return -ENOBUFS;
3482 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003483
Johannes Berg4c476992010-10-04 21:36:35 +02003484 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003485}
3486
3487static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
3488{
Johannes Berg4c476992010-10-04 21:36:35 +02003489 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3490 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003491 u8 *dst = NULL;
3492 u8 *next_hop = NULL;
3493
3494 if (!info->attrs[NL80211_ATTR_MAC])
3495 return -EINVAL;
3496
3497 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3498 return -EINVAL;
3499
3500 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3501 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3502
Johannes Berg4c476992010-10-04 21:36:35 +02003503 if (!rdev->ops->change_mpath)
3504 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003505
Johannes Berg4c476992010-10-04 21:36:35 +02003506 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3507 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003508
Johannes Berg4c476992010-10-04 21:36:35 +02003509 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003510}
Johannes Berg4c476992010-10-04 21:36:35 +02003511
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003512static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
3513{
Johannes Berg4c476992010-10-04 21:36:35 +02003514 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3515 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003516 u8 *dst = NULL;
3517 u8 *next_hop = NULL;
3518
3519 if (!info->attrs[NL80211_ATTR_MAC])
3520 return -EINVAL;
3521
3522 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
3523 return -EINVAL;
3524
3525 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3526 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
3527
Johannes Berg4c476992010-10-04 21:36:35 +02003528 if (!rdev->ops->add_mpath)
3529 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003530
Johannes Berg4c476992010-10-04 21:36:35 +02003531 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
3532 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003533
Johannes Berg4c476992010-10-04 21:36:35 +02003534 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003535}
3536
3537static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
3538{
Johannes Berg4c476992010-10-04 21:36:35 +02003539 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3540 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003541 u8 *dst = NULL;
3542
3543 if (info->attrs[NL80211_ATTR_MAC])
3544 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
3545
Johannes Berg4c476992010-10-04 21:36:35 +02003546 if (!rdev->ops->del_mpath)
3547 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01003548
Johannes Berg4c476992010-10-04 21:36:35 +02003549 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01003550}
3551
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003552static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
3553{
Johannes Berg4c476992010-10-04 21:36:35 +02003554 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3555 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003556 struct bss_parameters params;
3557
3558 memset(&params, 0, sizeof(params));
3559 /* default to not changing parameters */
3560 params.use_cts_prot = -1;
3561 params.use_short_preamble = -1;
3562 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003563 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01003564 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003565
3566 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
3567 params.use_cts_prot =
3568 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
3569 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
3570 params.use_short_preamble =
3571 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
3572 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
3573 params.use_short_slot_time =
3574 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02003575 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3576 params.basic_rates =
3577 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3578 params.basic_rates_len =
3579 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3580 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02003581 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
3582 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01003583 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
3584 params.ht_opmode =
3585 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003586
Johannes Berg4c476992010-10-04 21:36:35 +02003587 if (!rdev->ops->change_bss)
3588 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003589
Johannes Berg074ac8d2010-09-16 14:58:22 +02003590 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02003591 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
3592 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003593
Johannes Berg4c476992010-10-04 21:36:35 +02003594 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03003595}
3596
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003597static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003598 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
3599 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
3600 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
3601 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
3602 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
3603 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
3604};
3605
3606static int parse_reg_rule(struct nlattr *tb[],
3607 struct ieee80211_reg_rule *reg_rule)
3608{
3609 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
3610 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
3611
3612 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
3613 return -EINVAL;
3614 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
3615 return -EINVAL;
3616 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
3617 return -EINVAL;
3618 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
3619 return -EINVAL;
3620 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
3621 return -EINVAL;
3622
3623 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
3624
3625 freq_range->start_freq_khz =
3626 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
3627 freq_range->end_freq_khz =
3628 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
3629 freq_range->max_bandwidth_khz =
3630 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
3631
3632 power_rule->max_eirp =
3633 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
3634
3635 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
3636 power_rule->max_antenna_gain =
3637 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
3638
3639 return 0;
3640}
3641
3642static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
3643{
3644 int r;
3645 char *data = NULL;
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003646 enum nl80211_user_reg_hint_type user_reg_hint_type;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003647
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003648 /*
3649 * You should only get this when cfg80211 hasn't yet initialized
3650 * completely when built-in to the kernel right between the time
3651 * window between nl80211_init() and regulatory_init(), if that is
3652 * even possible.
3653 */
3654 mutex_lock(&cfg80211_mutex);
3655 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003656 mutex_unlock(&cfg80211_mutex);
3657 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003658 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003659 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05003660
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003661 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3662 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003663
3664 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3665
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07003666 if (info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE])
3667 user_reg_hint_type =
3668 nla_get_u32(info->attrs[NL80211_ATTR_USER_REG_HINT_TYPE]);
3669 else
3670 user_reg_hint_type = NL80211_USER_REG_HINT_USER;
3671
3672 switch (user_reg_hint_type) {
3673 case NL80211_USER_REG_HINT_USER:
3674 case NL80211_USER_REG_HINT_CELL_BASE:
3675 break;
3676 default:
3677 return -EINVAL;
3678 }
3679
3680 r = regulatory_hint_user(data, user_reg_hint_type);
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05003681
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003682 return r;
3683}
3684
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003685static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003686 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003687{
Johannes Berg4c476992010-10-04 21:36:35 +02003688 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02003689 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003690 struct wireless_dev *wdev = dev->ieee80211_ptr;
3691 struct mesh_config cur_params;
3692 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003693 void *hdr;
3694 struct nlattr *pinfoattr;
3695 struct sk_buff *msg;
3696
Johannes Berg29cbe682010-12-03 09:20:44 +01003697 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3698 return -EOPNOTSUPP;
3699
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003700 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02003701 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02003702
Johannes Berg29cbe682010-12-03 09:20:44 +01003703 wdev_lock(wdev);
3704 /* If not connected, get default parameters */
3705 if (!wdev->mesh_id_len)
3706 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
3707 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003708 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003709 &cur_params);
3710 wdev_unlock(wdev);
3711
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003712 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02003713 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003714
3715 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003716 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003717 if (!msg)
3718 return -ENOMEM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00003719 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003720 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003721 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003722 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003723 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003724 if (!pinfoattr)
3725 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04003726 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
3727 nla_put_u16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3728 cur_params.dot11MeshRetryTimeout) ||
3729 nla_put_u16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3730 cur_params.dot11MeshConfirmTimeout) ||
3731 nla_put_u16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3732 cur_params.dot11MeshHoldingTimeout) ||
3733 nla_put_u16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3734 cur_params.dot11MeshMaxPeerLinks) ||
3735 nla_put_u8(msg, NL80211_MESHCONF_MAX_RETRIES,
3736 cur_params.dot11MeshMaxRetries) ||
3737 nla_put_u8(msg, NL80211_MESHCONF_TTL,
3738 cur_params.dot11MeshTTL) ||
3739 nla_put_u8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3740 cur_params.element_ttl) ||
3741 nla_put_u8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3742 cur_params.auto_open_plinks) ||
John W. Linville7eab0f62012-04-12 14:25:14 -04003743 nla_put_u32(msg, NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3744 cur_params.dot11MeshNbrOffsetMaxNeighbor) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04003745 nla_put_u8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3746 cur_params.dot11MeshHWMPmaxPREQretries) ||
3747 nla_put_u32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3748 cur_params.path_refresh_time) ||
3749 nla_put_u16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3750 cur_params.min_discovery_timeout) ||
3751 nla_put_u32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3752 cur_params.dot11MeshHWMPactivePathTimeout) ||
3753 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3754 cur_params.dot11MeshHWMPpreqMinInterval) ||
3755 nla_put_u16(msg, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3756 cur_params.dot11MeshHWMPperrMinInterval) ||
3757 nla_put_u16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3758 cur_params.dot11MeshHWMPnetDiameterTraversalTime) ||
3759 nla_put_u8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3760 cur_params.dot11MeshHWMPRootMode) ||
3761 nla_put_u16(msg, NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3762 cur_params.dot11MeshHWMPRannInterval) ||
3763 nla_put_u8(msg, NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3764 cur_params.dot11MeshGateAnnouncementProtocol) ||
3765 nla_put_u8(msg, NL80211_MESHCONF_FORWARDING,
3766 cur_params.dot11MeshForwarding) ||
3767 nla_put_u32(msg, NL80211_MESHCONF_RSSI_THRESHOLD,
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003768 cur_params.rssi_threshold) ||
3769 nla_put_u32(msg, NL80211_MESHCONF_HT_OPMODE,
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003770 cur_params.ht_opmode) ||
3771 nla_put_u32(msg, NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3772 cur_params.dot11MeshHWMPactivePathToRootTimeout) ||
3773 nla_put_u16(msg, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003774 cur_params.dot11MeshHWMProotInterval) ||
3775 nla_put_u16(msg, NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3776 cur_params.dot11MeshHWMPconfirmationInterval))
David S. Miller9360ffd2012-03-29 04:41:26 -04003777 goto nla_put_failure;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003778 nla_nest_end(msg, pinfoattr);
3779 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003780 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003781
Johannes Berg3b858752009-03-12 09:55:09 +01003782 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003783 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003784 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003785 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003786 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003787}
3788
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003789static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003790 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3791 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3792 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3793 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3794 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3795 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003796 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003797 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
Javier Cardonad299a1f2012-03-31 11:31:33 -07003798 [NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR] = { .type = NLA_U32 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003799 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3800 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3801 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3802 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3803 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
Thomas Pedersendca7e942011-11-24 17:15:24 -08003804 [NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003805 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
Javier Cardona699403d2011-08-09 16:45:09 -07003806 [NL80211_MESHCONF_HWMP_ROOTMODE] = { .type = NLA_U8 },
Javier Cardona0507e152011-08-09 16:45:10 -07003807 [NL80211_MESHCONF_HWMP_RANN_INTERVAL] = { .type = NLA_U16 },
Javier Cardona16dd7262011-08-09 16:45:11 -07003808 [NL80211_MESHCONF_GATE_ANNOUNCEMENTS] = { .type = NLA_U8 },
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003809 [NL80211_MESHCONF_FORWARDING] = { .type = NLA_U8 },
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003810 [NL80211_MESHCONF_RSSI_THRESHOLD] = { .type = NLA_U32 },
3811 [NL80211_MESHCONF_HT_OPMODE] = { .type = NLA_U16 },
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003812 [NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT] = { .type = NLA_U32 },
3813 [NL80211_MESHCONF_HWMP_ROOT_INTERVAL] = { .type = NLA_U16 },
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003814 [NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL] = { .type = NLA_U16 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003815};
3816
Javier Cardonac80d5452010-12-16 17:37:49 -08003817static const struct nla_policy
3818 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
Javier Cardonad299a1f2012-03-31 11:31:33 -07003819 [NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC] = { .type = NLA_U8 },
Javier Cardonac80d5452010-12-16 17:37:49 -08003820 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3821 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003822 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003823 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003824 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003825 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003826};
3827
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003828static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003829 struct mesh_config *cfg,
3830 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003831{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003832 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003833 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003834
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003835#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3836do {\
3837 if (table[attr_num]) {\
3838 cfg->param = nla_fn(table[attr_num]); \
3839 mask |= (1 << (attr_num - 1)); \
3840 } \
3841} while (0);\
3842
3843
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003844 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003845 return -EINVAL;
3846 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003847 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003848 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003849 return -EINVAL;
3850
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003851 /* This makes sure that there aren't more than 32 mesh config
3852 * parameters (otherwise our bitfield scheme would not work.) */
3853 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3854
3855 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003856 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003857 mask, NL80211_MESHCONF_RETRY_TIMEOUT,
3858 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003859 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003860 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3861 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003862 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003863 mask, NL80211_MESHCONF_HOLDING_TIMEOUT,
3864 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003865 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003866 mask, NL80211_MESHCONF_MAX_PEER_LINKS,
3867 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003868 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003869 mask, NL80211_MESHCONF_MAX_RETRIES,
3870 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003871 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003872 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003873 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003874 mask, NL80211_MESHCONF_ELEMENT_TTL,
3875 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003876 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003877 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3878 nla_get_u8);
3879 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshNbrOffsetMaxNeighbor, mask,
3880 NL80211_MESHCONF_SYNC_OFFSET_MAX_NEIGHBOR,
3881 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003882 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003883 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3884 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003885 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003886 mask, NL80211_MESHCONF_PATH_REFRESH_TIME,
3887 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003888 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003889 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3890 nla_get_u16);
3891 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout, mask,
3892 NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3893 nla_get_u32);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003894 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003895 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3896 nla_get_u16);
Thomas Pedersendca7e942011-11-24 17:15:24 -08003897 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPperrMinInterval,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003898 mask, NL80211_MESHCONF_HWMP_PERR_MIN_INTERVAL,
3899 nla_get_u16);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003900 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003901 dot11MeshHWMPnetDiameterTraversalTime, mask,
3902 NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3903 nla_get_u16);
3904 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRootMode, mask,
3905 NL80211_MESHCONF_HWMP_ROOTMODE, nla_get_u8);
3906 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPRannInterval, mask,
3907 NL80211_MESHCONF_HWMP_RANN_INTERVAL,
3908 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003909 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003910 dot11MeshGateAnnouncementProtocol, mask,
3911 NL80211_MESHCONF_GATE_ANNOUNCEMENTS,
3912 nla_get_u8);
Chun-Yeow Yeoh94f90652012-01-21 01:02:16 +08003913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshForwarding,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003914 mask, NL80211_MESHCONF_FORWARDING,
3915 nla_get_u8);
Ashok Nagarajan55335132012-02-28 17:04:08 -08003916 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, rssi_threshold,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003917 mask, NL80211_MESHCONF_RSSI_THRESHOLD,
3918 nla_get_u32);
Ashok Nagarajan70c33ea2012-04-30 14:20:32 -07003919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, ht_opmode,
Chun-Yeow Yeoha4f606e2012-06-11 11:59:36 +08003920 mask, NL80211_MESHCONF_HT_OPMODE,
3921 nla_get_u16);
Chun-Yeow Yeohac1073a2012-06-14 02:06:06 +08003922 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathToRootTimeout,
3923 mask,
3924 NL80211_MESHCONF_HWMP_PATH_TO_ROOT_TIMEOUT,
3925 nla_get_u32);
3926 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMProotInterval,
3927 mask, NL80211_MESHCONF_HWMP_ROOT_INTERVAL,
3928 nla_get_u16);
Chun-Yeow Yeoh728b19e2012-06-14 02:06:10 +08003929 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3930 dot11MeshHWMPconfirmationInterval, mask,
3931 NL80211_MESHCONF_HWMP_CONFIRMATION_INTERVAL,
3932 nla_get_u16);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003933 if (mask_out)
3934 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003935
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003936 return 0;
3937
3938#undef FILL_IN_MESH_PARAM_IF_SET
3939}
3940
Javier Cardonac80d5452010-12-16 17:37:49 -08003941static int nl80211_parse_mesh_setup(struct genl_info *info,
3942 struct mesh_setup *setup)
3943{
3944 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3945
3946 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3947 return -EINVAL;
3948 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3949 info->attrs[NL80211_ATTR_MESH_SETUP],
3950 nl80211_mesh_setup_params_policy))
3951 return -EINVAL;
3952
Javier Cardonad299a1f2012-03-31 11:31:33 -07003953 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])
3954 setup->sync_method =
3955 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_SYNC])) ?
3956 IEEE80211_SYNC_METHOD_VENDOR :
3957 IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET;
3958
Javier Cardonac80d5452010-12-16 17:37:49 -08003959 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3960 setup->path_sel_proto =
3961 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3962 IEEE80211_PATH_PROTOCOL_VENDOR :
3963 IEEE80211_PATH_PROTOCOL_HWMP;
3964
3965 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3966 setup->path_metric =
3967 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3968 IEEE80211_PATH_METRIC_VENDOR :
3969 IEEE80211_PATH_METRIC_AIRTIME;
3970
Javier Cardona581a8b02011-04-07 15:08:27 -07003971
3972 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003973 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003974 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003975 if (!is_valid_ie_attr(ieattr))
3976 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003977 setup->ie = nla_data(ieattr);
3978 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003979 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003980 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3981 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003982
3983 return 0;
3984}
3985
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003986static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003987 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003988{
3989 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3990 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003991 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003992 struct mesh_config cfg;
3993 u32 mask;
3994 int err;
3995
Johannes Berg29cbe682010-12-03 09:20:44 +01003996 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3997 return -EOPNOTSUPP;
3998
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003999 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004000 return -EOPNOTSUPP;
4001
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004002 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01004003 if (err)
4004 return err;
4005
Johannes Berg29cbe682010-12-03 09:20:44 +01004006 wdev_lock(wdev);
4007 if (!wdev->mesh_id_len)
4008 err = -ENOLINK;
4009
4010 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004011 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01004012 mask, &cfg);
4013
4014 wdev_unlock(wdev);
4015
4016 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004017}
4018
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004019static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
4020{
4021 struct sk_buff *msg;
4022 void *hdr = NULL;
4023 struct nlattr *nl_reg_rules;
4024 unsigned int i;
4025 int err = -EINVAL;
4026
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004027 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004028
4029 if (!cfg80211_regdomain)
4030 goto out;
4031
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07004032 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004033 if (!msg) {
4034 err = -ENOBUFS;
4035 goto out;
4036 }
4037
Eric W. Biederman15e47302012-09-07 20:12:54 +00004038 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004039 NL80211_CMD_GET_REG);
4040 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01004041 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004042
David S. Miller9360ffd2012-03-29 04:41:26 -04004043 if (nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
4044 cfg80211_regdomain->alpha2) ||
4045 (cfg80211_regdomain->dfs_region &&
4046 nla_put_u8(msg, NL80211_ATTR_DFS_REGION,
4047 cfg80211_regdomain->dfs_region)))
4048 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004049
Luis R. Rodriguez57b5ce02012-07-12 11:49:18 -07004050 if (reg_last_request_cell_base() &&
4051 nla_put_u32(msg, NL80211_ATTR_USER_REG_HINT_TYPE,
4052 NL80211_USER_REG_HINT_CELL_BASE))
4053 goto nla_put_failure;
4054
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004055 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
4056 if (!nl_reg_rules)
4057 goto nla_put_failure;
4058
4059 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
4060 struct nlattr *nl_reg_rule;
4061 const struct ieee80211_reg_rule *reg_rule;
4062 const struct ieee80211_freq_range *freq_range;
4063 const struct ieee80211_power_rule *power_rule;
4064
4065 reg_rule = &cfg80211_regdomain->reg_rules[i];
4066 freq_range = &reg_rule->freq_range;
4067 power_rule = &reg_rule->power_rule;
4068
4069 nl_reg_rule = nla_nest_start(msg, i);
4070 if (!nl_reg_rule)
4071 goto nla_put_failure;
4072
David S. Miller9360ffd2012-03-29 04:41:26 -04004073 if (nla_put_u32(msg, NL80211_ATTR_REG_RULE_FLAGS,
4074 reg_rule->flags) ||
4075 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_START,
4076 freq_range->start_freq_khz) ||
4077 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_END,
4078 freq_range->end_freq_khz) ||
4079 nla_put_u32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
4080 freq_range->max_bandwidth_khz) ||
4081 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
4082 power_rule->max_antenna_gain) ||
4083 nla_put_u32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
4084 power_rule->max_eirp))
4085 goto nla_put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004086
4087 nla_nest_end(msg, nl_reg_rule);
4088 }
4089
4090 nla_nest_end(msg, nl_reg_rules);
4091
4092 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00004093 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004094 goto out;
4095
4096nla_put_failure:
4097 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01004098put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04004099 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004100 err = -EMSGSIZE;
4101out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004102 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004103 return err;
4104}
4105
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004106static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
4107{
4108 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
4109 struct nlattr *nl_reg_rule;
4110 char *alpha2 = NULL;
4111 int rem_reg_rules = 0, r = 0;
4112 u32 num_rules = 0, rule_idx = 0, size_of_regd;
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004113 u8 dfs_region = 0;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004114 struct ieee80211_regdomain *rd = NULL;
4115
4116 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
4117 return -EINVAL;
4118
4119 if (!info->attrs[NL80211_ATTR_REG_RULES])
4120 return -EINVAL;
4121
4122 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4123
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004124 if (info->attrs[NL80211_ATTR_DFS_REGION])
4125 dfs_region = nla_get_u8(info->attrs[NL80211_ATTR_DFS_REGION]);
4126
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004127 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4128 rem_reg_rules) {
4129 num_rules++;
4130 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04004131 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004132 }
4133
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004134 mutex_lock(&cfg80211_mutex);
4135
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004136 if (!reg_is_valid_request(alpha2)) {
4137 r = -EINVAL;
4138 goto bad_reg;
4139 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004140
4141 size_of_regd = sizeof(struct ieee80211_regdomain) +
4142 (num_rules * sizeof(struct ieee80211_reg_rule));
4143
4144 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004145 if (!rd) {
4146 r = -ENOMEM;
4147 goto bad_reg;
4148 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004149
4150 rd->n_reg_rules = num_rules;
4151 rd->alpha2[0] = alpha2[0];
4152 rd->alpha2[1] = alpha2[1];
4153
Luis R. Rodriguez8b60b072011-10-11 10:59:02 -07004154 /*
4155 * Disable DFS master mode if the DFS region was
4156 * not supported or known on this kernel.
4157 */
4158 if (reg_supported_dfs_region(dfs_region))
4159 rd->dfs_region = dfs_region;
4160
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004161 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
4162 rem_reg_rules) {
4163 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
4164 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
4165 reg_rule_policy);
4166 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
4167 if (r)
4168 goto bad_reg;
4169
4170 rule_idx++;
4171
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004172 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
4173 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004174 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004175 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004176 }
4177
4178 BUG_ON(rule_idx != num_rules);
4179
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004180 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004181
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05004182 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004183
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004184 return r;
4185
Johannes Bergd2372b32008-10-24 20:32:20 +02004186 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04004187 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004188 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04004189 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004190}
4191
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004192static int validate_scan_freqs(struct nlattr *freqs)
4193{
4194 struct nlattr *attr1, *attr2;
4195 int n_channels = 0, tmp1, tmp2;
4196
4197 nla_for_each_nested(attr1, freqs, tmp1) {
4198 n_channels++;
4199 /*
4200 * Some hardware has a limited channel list for
4201 * scanning, and it is pretty much nonsensical
4202 * to scan for a channel twice, so disallow that
4203 * and don't require drivers to check that the
4204 * channel list they get isn't longer than what
4205 * they can scan, as long as they can scan all
4206 * the channels they registered at once.
4207 */
4208 nla_for_each_nested(attr2, freqs, tmp2)
4209 if (attr1 != attr2 &&
4210 nla_get_u32(attr1) == nla_get_u32(attr2))
4211 return 0;
4212 }
4213
4214 return n_channels;
4215}
4216
Johannes Berg2a519312009-02-10 21:25:55 +01004217static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
4218{
Johannes Berg4c476992010-10-04 21:36:35 +02004219 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfd014282012-06-18 19:17:03 +02004220 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01004221 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01004222 struct nlattr *attr;
4223 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004224 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004225 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01004226
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004227 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4228 return -EINVAL;
4229
Johannes Berg79c97e92009-07-07 03:56:12 +02004230 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01004231
Johannes Berg4c476992010-10-04 21:36:35 +02004232 if (!rdev->ops->scan)
4233 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01004234
Johannes Berg4c476992010-10-04 21:36:35 +02004235 if (rdev->scan_req)
4236 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01004237
4238 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004239 n_channels = validate_scan_freqs(
4240 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02004241 if (!n_channels)
4242 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004243 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004244 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02004245 n_channels = 0;
4246
Johannes Berg2a519312009-02-10 21:25:55 +01004247 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4248 if (wiphy->bands[band])
4249 n_channels += wiphy->bands[band]->n_channels;
4250 }
4251
4252 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4253 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
4254 n_ssids++;
4255
Johannes Berg4c476992010-10-04 21:36:35 +02004256 if (n_ssids > wiphy->max_scan_ssids)
4257 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01004258
Jouni Malinen70692ad2009-02-16 19:39:13 +02004259 if (info->attrs[NL80211_ATTR_IE])
4260 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4261 else
4262 ie_len = 0;
4263
Johannes Berg4c476992010-10-04 21:36:35 +02004264 if (ie_len > wiphy->max_scan_ie_len)
4265 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02004266
Johannes Berg2a519312009-02-10 21:25:55 +01004267 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004268 + sizeof(*request->ssids) * n_ssids
4269 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02004270 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004271 if (!request)
4272 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01004273
Johannes Berg2a519312009-02-10 21:25:55 +01004274 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02004275 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01004276 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02004277 if (ie_len) {
4278 if (request->ssids)
4279 request->ie = (void *)(request->ssids + n_ssids);
4280 else
4281 request->ie = (void *)(request->channels + n_channels);
4282 }
Johannes Berg2a519312009-02-10 21:25:55 +01004283
Johannes Berg584991d2009-11-02 13:32:03 +01004284 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01004285 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4286 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01004287 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01004288 struct ieee80211_channel *chan;
4289
4290 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4291
4292 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01004293 err = -EINVAL;
4294 goto out_free;
4295 }
Johannes Berg584991d2009-11-02 13:32:03 +01004296
4297 /* ignore disabled channels */
4298 if (chan->flags & IEEE80211_CHAN_DISABLED)
4299 continue;
4300
4301 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004302 i++;
4303 }
4304 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02004305 enum ieee80211_band band;
4306
Johannes Berg2a519312009-02-10 21:25:55 +01004307 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01004308 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4309 int j;
4310 if (!wiphy->bands[band])
4311 continue;
4312 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01004313 struct ieee80211_channel *chan;
4314
4315 chan = &wiphy->bands[band]->channels[j];
4316
4317 if (chan->flags & IEEE80211_CHAN_DISABLED)
4318 continue;
4319
4320 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01004321 i++;
4322 }
4323 }
4324 }
4325
Johannes Berg584991d2009-11-02 13:32:03 +01004326 if (!i) {
4327 err = -EINVAL;
4328 goto out_free;
4329 }
4330
4331 request->n_channels = i;
4332
Johannes Berg2a519312009-02-10 21:25:55 +01004333 i = 0;
4334 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4335 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004336 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01004337 err = -EINVAL;
4338 goto out_free;
4339 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004340 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01004341 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01004342 i++;
4343 }
4344 }
4345
Jouni Malinen70692ad2009-02-16 19:39:13 +02004346 if (info->attrs[NL80211_ATTR_IE]) {
4347 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02004348 memcpy((void *)request->ie,
4349 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02004350 request->ie_len);
4351 }
4352
Johannes Berg34850ab2011-07-18 18:08:35 +02004353 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02004354 if (wiphy->bands[i])
4355 request->rates[i] =
4356 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02004357
4358 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
4359 nla_for_each_nested(attr,
4360 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
4361 tmp) {
4362 enum ieee80211_band band = nla_type(attr);
4363
Dan Carpenter84404622011-07-29 11:52:18 +03004364 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02004365 err = -EINVAL;
4366 goto out_free;
4367 }
4368 err = ieee80211_get_ratemask(wiphy->bands[band],
4369 nla_data(attr),
4370 nla_len(attr),
4371 &request->rates[band]);
4372 if (err)
4373 goto out_free;
4374 }
4375 }
4376
Sam Leffler46856bb2012-10-11 21:03:32 -07004377 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004378 request->flags = nla_get_u32(
4379 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004380 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4381 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4382 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4383 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004384 err = -EOPNOTSUPP;
4385 goto out_free;
4386 }
4387 }
Sam Lefflered4737712012-10-11 21:03:31 -07004388
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05304389 request->no_cck =
4390 nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
4391
Johannes Bergfd014282012-06-18 19:17:03 +02004392 request->wdev = wdev;
Johannes Berg79c97e92009-07-07 03:56:12 +02004393 request->wiphy = &rdev->wiphy;
Sam Leffler15d60302012-10-11 21:03:34 -07004394 request->scan_start = jiffies;
Johannes Berg2a519312009-02-10 21:25:55 +01004395
Johannes Berg79c97e92009-07-07 03:56:12 +02004396 rdev->scan_req = request;
Johannes Bergfd014282012-06-18 19:17:03 +02004397 err = rdev->ops->scan(&rdev->wiphy, request);
Johannes Berg2a519312009-02-10 21:25:55 +01004398
Johannes Berg463d0182009-07-14 00:33:35 +02004399 if (!err) {
Johannes Bergfd014282012-06-18 19:17:03 +02004400 nl80211_send_scan_start(rdev, wdev);
4401 if (wdev->netdev)
4402 dev_hold(wdev->netdev);
Johannes Berg4c476992010-10-04 21:36:35 +02004403 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01004404 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02004405 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01004406 kfree(request);
4407 }
Johannes Berg3b858752009-03-12 09:55:09 +01004408
Johannes Berg2a519312009-02-10 21:25:55 +01004409 return err;
4410}
4411
Luciano Coelho807f8a82011-05-11 17:09:35 +03004412static int nl80211_start_sched_scan(struct sk_buff *skb,
4413 struct genl_info *info)
4414{
4415 struct cfg80211_sched_scan_request *request;
4416 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4417 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004418 struct nlattr *attr;
4419 struct wiphy *wiphy;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004420 int err, tmp, n_ssids = 0, n_match_sets = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004421 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004422 enum ieee80211_band band;
4423 size_t ie_len;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004424 struct nlattr *tb[NL80211_SCHED_SCAN_MATCH_ATTR_MAX + 1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03004425
4426 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4427 !rdev->ops->sched_scan_start)
4428 return -EOPNOTSUPP;
4429
4430 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4431 return -EINVAL;
4432
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004433 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
4434 return -EINVAL;
4435
4436 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
4437 if (interval == 0)
4438 return -EINVAL;
4439
Luciano Coelho807f8a82011-05-11 17:09:35 +03004440 wiphy = &rdev->wiphy;
4441
4442 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4443 n_channels = validate_scan_freqs(
4444 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
4445 if (!n_channels)
4446 return -EINVAL;
4447 } else {
4448 n_channels = 0;
4449
4450 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
4451 if (wiphy->bands[band])
4452 n_channels += wiphy->bands[band]->n_channels;
4453 }
4454
4455 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
4456 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4457 tmp)
4458 n_ssids++;
4459
Luciano Coelho93b6aa62011-07-13 14:57:28 +03004460 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004461 return -EINVAL;
4462
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004463 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH])
4464 nla_for_each_nested(attr,
4465 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4466 tmp)
4467 n_match_sets++;
4468
4469 if (n_match_sets > wiphy->max_match_sets)
4470 return -EINVAL;
4471
Luciano Coelho807f8a82011-05-11 17:09:35 +03004472 if (info->attrs[NL80211_ATTR_IE])
4473 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4474 else
4475 ie_len = 0;
4476
Luciano Coelho5a865ba2011-07-13 14:57:29 +03004477 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03004478 return -EINVAL;
4479
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004480 mutex_lock(&rdev->sched_scan_mtx);
4481
4482 if (rdev->sched_scan_req) {
4483 err = -EINPROGRESS;
4484 goto out;
4485 }
4486
Luciano Coelho807f8a82011-05-11 17:09:35 +03004487 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004488 + sizeof(*request->ssids) * n_ssids
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004489 + sizeof(*request->match_sets) * n_match_sets
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03004490 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03004491 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004492 if (!request) {
4493 err = -ENOMEM;
4494 goto out;
4495 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03004496
4497 if (n_ssids)
4498 request->ssids = (void *)&request->channels[n_channels];
4499 request->n_ssids = n_ssids;
4500 if (ie_len) {
4501 if (request->ssids)
4502 request->ie = (void *)(request->ssids + n_ssids);
4503 else
4504 request->ie = (void *)(request->channels + n_channels);
4505 }
4506
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004507 if (n_match_sets) {
4508 if (request->ie)
4509 request->match_sets = (void *)(request->ie + ie_len);
4510 else if (request->ssids)
4511 request->match_sets =
4512 (void *)(request->ssids + n_ssids);
4513 else
4514 request->match_sets =
4515 (void *)(request->channels + n_channels);
4516 }
4517 request->n_match_sets = n_match_sets;
4518
Luciano Coelho807f8a82011-05-11 17:09:35 +03004519 i = 0;
4520 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
4521 /* user specified, bail out if channel not found */
4522 nla_for_each_nested(attr,
4523 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
4524 tmp) {
4525 struct ieee80211_channel *chan;
4526
4527 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
4528
4529 if (!chan) {
4530 err = -EINVAL;
4531 goto out_free;
4532 }
4533
4534 /* ignore disabled channels */
4535 if (chan->flags & IEEE80211_CHAN_DISABLED)
4536 continue;
4537
4538 request->channels[i] = chan;
4539 i++;
4540 }
4541 } else {
4542 /* all channels */
4543 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4544 int j;
4545 if (!wiphy->bands[band])
4546 continue;
4547 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
4548 struct ieee80211_channel *chan;
4549
4550 chan = &wiphy->bands[band]->channels[j];
4551
4552 if (chan->flags & IEEE80211_CHAN_DISABLED)
4553 continue;
4554
4555 request->channels[i] = chan;
4556 i++;
4557 }
4558 }
4559 }
4560
4561 if (!i) {
4562 err = -EINVAL;
4563 goto out_free;
4564 }
4565
4566 request->n_channels = i;
4567
4568 i = 0;
4569 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
4570 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
4571 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03004572 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03004573 err = -EINVAL;
4574 goto out_free;
4575 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03004576 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004577 memcpy(request->ssids[i].ssid, nla_data(attr),
4578 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03004579 i++;
4580 }
4581 }
4582
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004583 i = 0;
4584 if (info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH]) {
4585 nla_for_each_nested(attr,
4586 info->attrs[NL80211_ATTR_SCHED_SCAN_MATCH],
4587 tmp) {
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004588 struct nlattr *ssid, *rssi;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004589
4590 nla_parse(tb, NL80211_SCHED_SCAN_MATCH_ATTR_MAX,
4591 nla_data(attr), nla_len(attr),
4592 nl80211_match_policy);
Johannes Berg4a4ab0d2012-06-13 11:17:11 +02004593 ssid = tb[NL80211_SCHED_SCAN_MATCH_ATTR_SSID];
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004594 if (ssid) {
4595 if (nla_len(ssid) > IEEE80211_MAX_SSID_LEN) {
4596 err = -EINVAL;
4597 goto out_free;
4598 }
4599 memcpy(request->match_sets[i].ssid.ssid,
4600 nla_data(ssid), nla_len(ssid));
4601 request->match_sets[i].ssid.ssid_len =
4602 nla_len(ssid);
4603 }
Thomas Pedersen88e920b2012-06-21 11:09:54 -07004604 rssi = tb[NL80211_SCHED_SCAN_MATCH_ATTR_RSSI];
4605 if (rssi)
4606 request->rssi_thold = nla_get_u32(rssi);
4607 else
4608 request->rssi_thold =
4609 NL80211_SCAN_RSSI_THOLD_OFF;
Luciano Coelhoa1f1c212011-08-31 16:01:48 +03004610 i++;
4611 }
4612 }
4613
Luciano Coelho807f8a82011-05-11 17:09:35 +03004614 if (info->attrs[NL80211_ATTR_IE]) {
4615 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4616 memcpy((void *)request->ie,
4617 nla_data(info->attrs[NL80211_ATTR_IE]),
4618 request->ie_len);
4619 }
4620
Sam Leffler46856bb2012-10-11 21:03:32 -07004621 if (info->attrs[NL80211_ATTR_SCAN_FLAGS]) {
Sam Lefflered4737712012-10-11 21:03:31 -07004622 request->flags = nla_get_u32(
4623 info->attrs[NL80211_ATTR_SCAN_FLAGS]);
Sam Leffler15d60302012-10-11 21:03:34 -07004624 if (((request->flags & NL80211_SCAN_FLAG_LOW_PRIORITY) &&
4625 !(wiphy->features & NL80211_FEATURE_LOW_PRIORITY_SCAN)) ||
4626 ((request->flags & NL80211_SCAN_FLAG_FLUSH) &&
4627 !(wiphy->features & NL80211_FEATURE_SCAN_FLUSH))) {
Sam Leffler46856bb2012-10-11 21:03:32 -07004628 err = -EOPNOTSUPP;
4629 goto out_free;
4630 }
4631 }
Sam Lefflered4737712012-10-11 21:03:31 -07004632
Luciano Coelho807f8a82011-05-11 17:09:35 +03004633 request->dev = dev;
4634 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03004635 request->interval = interval;
Sam Leffler15d60302012-10-11 21:03:34 -07004636 request->scan_start = jiffies;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004637
4638 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
4639 if (!err) {
4640 rdev->sched_scan_req = request;
4641 nl80211_send_sched_scan(rdev, dev,
4642 NL80211_CMD_START_SCHED_SCAN);
4643 goto out;
4644 }
4645
4646out_free:
4647 kfree(request);
4648out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004649 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03004650 return err;
4651}
4652
4653static int nl80211_stop_sched_scan(struct sk_buff *skb,
4654 struct genl_info *info)
4655{
4656 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004657 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004658
4659 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
4660 !rdev->ops->sched_scan_stop)
4661 return -EOPNOTSUPP;
4662
Luciano Coelhoc10841c2011-06-30 08:32:41 +03004663 mutex_lock(&rdev->sched_scan_mtx);
4664 err = __cfg80211_stop_sched_scan(rdev, false);
4665 mutex_unlock(&rdev->sched_scan_mtx);
4666
4667 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03004668}
4669
Johannes Berg9720bb32011-06-21 09:45:33 +02004670static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
4671 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004672 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02004673 struct wireless_dev *wdev,
4674 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01004675{
Johannes Berg48ab9052009-07-10 18:42:31 +02004676 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01004677 void *hdr;
4678 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02004679
4680 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004681
Eric W. Biederman15e47302012-09-07 20:12:54 +00004682 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).portid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01004683 NL80211_CMD_NEW_SCAN_RESULTS);
4684 if (!hdr)
4685 return -1;
4686
Johannes Berg9720bb32011-06-21 09:45:33 +02004687 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
4688
David S. Miller9360ffd2012-03-29 04:41:26 -04004689 if (nla_put_u32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation) ||
4690 nla_put_u32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex))
4691 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004692
4693 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
4694 if (!bss)
4695 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04004696 if ((!is_zero_ether_addr(res->bssid) &&
4697 nla_put(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid)) ||
4698 (res->information_elements && res->len_information_elements &&
4699 nla_put(msg, NL80211_BSS_INFORMATION_ELEMENTS,
4700 res->len_information_elements,
4701 res->information_elements)) ||
4702 (res->beacon_ies && res->len_beacon_ies &&
4703 res->beacon_ies != res->information_elements &&
4704 nla_put(msg, NL80211_BSS_BEACON_IES,
4705 res->len_beacon_ies, res->beacon_ies)))
4706 goto nla_put_failure;
4707 if (res->tsf &&
4708 nla_put_u64(msg, NL80211_BSS_TSF, res->tsf))
4709 goto nla_put_failure;
4710 if (res->beacon_interval &&
4711 nla_put_u16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval))
4712 goto nla_put_failure;
4713 if (nla_put_u16(msg, NL80211_BSS_CAPABILITY, res->capability) ||
4714 nla_put_u32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq) ||
4715 nla_put_u32(msg, NL80211_BSS_SEEN_MS_AGO,
4716 jiffies_to_msecs(jiffies - intbss->ts)))
4717 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004718
Johannes Berg77965c92009-02-18 18:45:06 +01004719 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01004720 case CFG80211_SIGNAL_TYPE_MBM:
David S. Miller9360ffd2012-03-29 04:41:26 -04004721 if (nla_put_u32(msg, NL80211_BSS_SIGNAL_MBM, res->signal))
4722 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004723 break;
4724 case CFG80211_SIGNAL_TYPE_UNSPEC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004725 if (nla_put_u8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal))
4726 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01004727 break;
4728 default:
4729 break;
4730 }
4731
Johannes Berg48ab9052009-07-10 18:42:31 +02004732 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02004733 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02004734 case NL80211_IFTYPE_STATION:
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_ASSOCIATED))
4738 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004739 break;
4740 case NL80211_IFTYPE_ADHOC:
David S. Miller9360ffd2012-03-29 04:41:26 -04004741 if (intbss == wdev->current_bss &&
4742 nla_put_u32(msg, NL80211_BSS_STATUS,
4743 NL80211_BSS_STATUS_IBSS_JOINED))
4744 goto nla_put_failure;
Johannes Berg48ab9052009-07-10 18:42:31 +02004745 break;
4746 default:
4747 break;
4748 }
4749
Johannes Berg2a519312009-02-10 21:25:55 +01004750 nla_nest_end(msg, bss);
4751
4752 return genlmsg_end(msg, hdr);
4753
4754 nla_put_failure:
4755 genlmsg_cancel(msg, hdr);
4756 return -EMSGSIZE;
4757}
4758
4759static int nl80211_dump_scan(struct sk_buff *skb,
4760 struct netlink_callback *cb)
4761{
Johannes Berg48ab9052009-07-10 18:42:31 +02004762 struct cfg80211_registered_device *rdev;
4763 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01004764 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02004765 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01004766 int start = cb->args[1], idx = 0;
4767 int err;
4768
Johannes Berg67748892010-10-04 21:14:06 +02004769 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
4770 if (err)
4771 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01004772
Johannes Berg48ab9052009-07-10 18:42:31 +02004773 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01004774
Johannes Berg48ab9052009-07-10 18:42:31 +02004775 wdev_lock(wdev);
4776 spin_lock_bh(&rdev->bss_lock);
4777 cfg80211_bss_expire(rdev);
4778
Johannes Berg9720bb32011-06-21 09:45:33 +02004779 cb->seq = rdev->bss_generation;
4780
Johannes Berg48ab9052009-07-10 18:42:31 +02004781 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01004782 if (++idx <= start)
4783 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02004784 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01004785 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02004786 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01004787 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02004788 break;
Johannes Berg2a519312009-02-10 21:25:55 +01004789 }
4790 }
4791
Johannes Berg48ab9052009-07-10 18:42:31 +02004792 spin_unlock_bh(&rdev->bss_lock);
4793 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004794
4795 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02004796 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01004797
Johannes Berg67748892010-10-04 21:14:06 +02004798 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01004799}
4800
Eric W. Biederman15e47302012-09-07 20:12:54 +00004801static int nl80211_send_survey(struct sk_buff *msg, u32 portid, u32 seq,
Holger Schurig61fa7132009-11-11 12:25:40 +01004802 int flags, struct net_device *dev,
4803 struct survey_info *survey)
4804{
4805 void *hdr;
4806 struct nlattr *infoattr;
4807
Eric W. Biederman15e47302012-09-07 20:12:54 +00004808 hdr = nl80211hdr_put(msg, portid, seq, flags,
Holger Schurig61fa7132009-11-11 12:25:40 +01004809 NL80211_CMD_NEW_SURVEY_RESULTS);
4810 if (!hdr)
4811 return -ENOMEM;
4812
David S. Miller9360ffd2012-03-29 04:41:26 -04004813 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex))
4814 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004815
4816 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
4817 if (!infoattr)
4818 goto nla_put_failure;
4819
David S. Miller9360ffd2012-03-29 04:41:26 -04004820 if (nla_put_u32(msg, NL80211_SURVEY_INFO_FREQUENCY,
4821 survey->channel->center_freq))
4822 goto nla_put_failure;
4823
4824 if ((survey->filled & SURVEY_INFO_NOISE_DBM) &&
4825 nla_put_u8(msg, NL80211_SURVEY_INFO_NOISE, survey->noise))
4826 goto nla_put_failure;
4827 if ((survey->filled & SURVEY_INFO_IN_USE) &&
4828 nla_put_flag(msg, NL80211_SURVEY_INFO_IN_USE))
4829 goto nla_put_failure;
4830 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME) &&
4831 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
4832 survey->channel_time))
4833 goto nla_put_failure;
4834 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY) &&
4835 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
4836 survey->channel_time_busy))
4837 goto nla_put_failure;
4838 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY) &&
4839 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
4840 survey->channel_time_ext_busy))
4841 goto nla_put_failure;
4842 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_RX) &&
4843 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
4844 survey->channel_time_rx))
4845 goto nla_put_failure;
4846 if ((survey->filled & SURVEY_INFO_CHANNEL_TIME_TX) &&
4847 nla_put_u64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
4848 survey->channel_time_tx))
4849 goto nla_put_failure;
Holger Schurig61fa7132009-11-11 12:25:40 +01004850
4851 nla_nest_end(msg, infoattr);
4852
4853 return genlmsg_end(msg, hdr);
4854
4855 nla_put_failure:
4856 genlmsg_cancel(msg, hdr);
4857 return -EMSGSIZE;
4858}
4859
4860static int nl80211_dump_survey(struct sk_buff *skb,
4861 struct netlink_callback *cb)
4862{
4863 struct survey_info survey;
4864 struct cfg80211_registered_device *dev;
4865 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01004866 int survey_idx = cb->args[1];
4867 int res;
4868
Johannes Berg67748892010-10-04 21:14:06 +02004869 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
4870 if (res)
4871 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01004872
4873 if (!dev->ops->dump_survey) {
4874 res = -EOPNOTSUPP;
4875 goto out_err;
4876 }
4877
4878 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004879 struct ieee80211_channel *chan;
4880
Holger Schurig61fa7132009-11-11 12:25:40 +01004881 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
4882 &survey);
4883 if (res == -ENOENT)
4884 break;
4885 if (res)
4886 goto out_err;
4887
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07004888 /* Survey without a channel doesn't make sense */
4889 if (!survey.channel) {
4890 res = -EINVAL;
4891 goto out;
4892 }
4893
4894 chan = ieee80211_get_channel(&dev->wiphy,
4895 survey.channel->center_freq);
4896 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
4897 survey_idx++;
4898 continue;
4899 }
4900
Holger Schurig61fa7132009-11-11 12:25:40 +01004901 if (nl80211_send_survey(skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00004902 NETLINK_CB(cb->skb).portid,
Holger Schurig61fa7132009-11-11 12:25:40 +01004903 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4904 netdev,
4905 &survey) < 0)
4906 goto out;
4907 survey_idx++;
4908 }
4909
4910 out:
4911 cb->args[1] = survey_idx;
4912 res = skb->len;
4913 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004914 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004915 return res;
4916}
4917
Samuel Ortizb23aa672009-07-01 21:26:54 +02004918static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4919{
4920 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4921 NL80211_WPA_VERSION_2));
4922}
4923
Jouni Malinen636a5d32009-03-19 13:39:22 +02004924static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4925{
Johannes Berg4c476992010-10-04 21:36:35 +02004926 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4927 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004928 struct ieee80211_channel *chan;
Jouni Malinene39e5b52012-09-30 19:29:39 +03004929 const u8 *bssid, *ssid, *ie = NULL, *sae_data = NULL;
4930 int err, ssid_len, ie_len = 0, sae_data_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004931 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004932 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004933 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004934
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004935 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4936 return -EINVAL;
4937
4938 if (!info->attrs[NL80211_ATTR_MAC])
4939 return -EINVAL;
4940
Jouni Malinen17780922009-03-27 20:52:47 +02004941 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4942 return -EINVAL;
4943
Johannes Berg19957bb2009-07-02 17:20:43 +02004944 if (!info->attrs[NL80211_ATTR_SSID])
4945 return -EINVAL;
4946
4947 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4948 return -EINVAL;
4949
Johannes Bergfffd0932009-07-08 14:22:54 +02004950 err = nl80211_parse_key(info, &key);
4951 if (err)
4952 return err;
4953
4954 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004955 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4956 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004957 if (!key.p.key || !key.p.key_len)
4958 return -EINVAL;
4959 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4960 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4961 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4962 key.p.key_len != WLAN_KEY_LEN_WEP104))
4963 return -EINVAL;
4964 if (key.idx > 4)
4965 return -EINVAL;
4966 } else {
4967 key.p.key_len = 0;
4968 key.p.key = NULL;
4969 }
4970
Johannes Bergafea0b72010-08-10 09:46:42 +02004971 if (key.idx >= 0) {
4972 int i;
4973 bool ok = false;
4974 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4975 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4976 ok = true;
4977 break;
4978 }
4979 }
Johannes Berg4c476992010-10-04 21:36:35 +02004980 if (!ok)
4981 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004982 }
4983
Johannes Berg4c476992010-10-04 21:36:35 +02004984 if (!rdev->ops->auth)
4985 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004986
Johannes Berg074ac8d2010-09-16 14:58:22 +02004987 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004988 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4989 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004990
Johannes Berg19957bb2009-07-02 17:20:43 +02004991 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004992 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004993 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004994 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4995 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004996
Johannes Berg19957bb2009-07-02 17:20:43 +02004997 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4998 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4999
5000 if (info->attrs[NL80211_ATTR_IE]) {
5001 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5002 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5003 }
5004
5005 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005006 if (!nl80211_valid_auth_type(rdev, auth_type, NL80211_CMD_AUTHENTICATE))
Johannes Berg4c476992010-10-04 21:36:35 +02005007 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005008
Jouni Malinene39e5b52012-09-30 19:29:39 +03005009 if (auth_type == NL80211_AUTHTYPE_SAE &&
5010 !info->attrs[NL80211_ATTR_SAE_DATA])
5011 return -EINVAL;
5012
5013 if (info->attrs[NL80211_ATTR_SAE_DATA]) {
5014 if (auth_type != NL80211_AUTHTYPE_SAE)
5015 return -EINVAL;
5016 sae_data = nla_data(info->attrs[NL80211_ATTR_SAE_DATA]);
5017 sae_data_len = nla_len(info->attrs[NL80211_ATTR_SAE_DATA]);
5018 /* need to include at least Auth Transaction and Status Code */
5019 if (sae_data_len < 4)
5020 return -EINVAL;
5021 }
5022
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005023 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5024
Johannes Berg95de8172012-01-20 13:55:25 +01005025 /*
5026 * Since we no longer track auth state, ignore
5027 * requests to only change local state.
5028 */
5029 if (local_state_change)
5030 return 0;
5031
Johannes Berg4c476992010-10-04 21:36:35 +02005032 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
5033 ssid, ssid_len, ie, ie_len,
Jouni Malinene39e5b52012-09-30 19:29:39 +03005034 key.p.key, key.p.key_len, key.idx,
5035 sae_data, sae_data_len);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005036}
5037
Johannes Bergc0692b82010-08-27 14:26:53 +03005038static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
5039 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005040 struct cfg80211_crypto_settings *settings,
5041 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005042{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02005043 memset(settings, 0, sizeof(*settings));
5044
Samuel Ortizb23aa672009-07-01 21:26:54 +02005045 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
5046
Johannes Bergc0692b82010-08-27 14:26:53 +03005047 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
5048 u16 proto;
5049 proto = nla_get_u16(
5050 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
5051 settings->control_port_ethertype = cpu_to_be16(proto);
5052 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
5053 proto != ETH_P_PAE)
5054 return -EINVAL;
5055 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
5056 settings->control_port_no_encrypt = true;
5057 } else
5058 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
5059
Samuel Ortizb23aa672009-07-01 21:26:54 +02005060 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
5061 void *data;
5062 int len, i;
5063
5064 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5065 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
5066 settings->n_ciphers_pairwise = len / sizeof(u32);
5067
5068 if (len % sizeof(u32))
5069 return -EINVAL;
5070
Johannes Berg3dc27d22009-07-02 21:36:37 +02005071 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005072 return -EINVAL;
5073
5074 memcpy(settings->ciphers_pairwise, data, len);
5075
5076 for (i = 0; i < settings->n_ciphers_pairwise; i++)
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005077 if (!cfg80211_supported_cipher_suite(
5078 &rdev->wiphy,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005079 settings->ciphers_pairwise[i]))
5080 return -EINVAL;
5081 }
5082
5083 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
5084 settings->cipher_group =
5085 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
Jouni Malinen38ba3c52011-09-21 18:14:56 +03005086 if (!cfg80211_supported_cipher_suite(&rdev->wiphy,
5087 settings->cipher_group))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005088 return -EINVAL;
5089 }
5090
5091 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
5092 settings->wpa_versions =
5093 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
5094 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
5095 return -EINVAL;
5096 }
5097
5098 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
5099 void *data;
Jouni Malinen6d302402011-09-21 18:11:33 +03005100 int len;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005101
5102 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
5103 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
5104 settings->n_akm_suites = len / sizeof(u32);
5105
5106 if (len % sizeof(u32))
5107 return -EINVAL;
5108
Jouni Malinen1b9ca022011-09-21 16:13:07 +03005109 if (settings->n_akm_suites > NL80211_MAX_NR_AKM_SUITES)
5110 return -EINVAL;
5111
Samuel Ortizb23aa672009-07-01 21:26:54 +02005112 memcpy(settings->akm_suites, data, len);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005113 }
5114
5115 return 0;
5116}
5117
Jouni Malinen636a5d32009-03-19 13:39:22 +02005118static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
5119{
Johannes Berg4c476992010-10-04 21:36:35 +02005120 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5121 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005122 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02005123 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02005124 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02005125 int err, ssid_len, ie_len = 0;
5126 bool use_mfp = false;
Ben Greear7e7c8922011-11-18 11:31:59 -08005127 u32 flags = 0;
5128 struct ieee80211_ht_cap *ht_capa = NULL;
5129 struct ieee80211_ht_cap *ht_capa_mask = NULL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005130
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005131 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5132 return -EINVAL;
5133
5134 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02005135 !info->attrs[NL80211_ATTR_SSID] ||
5136 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005137 return -EINVAL;
5138
Johannes Berg4c476992010-10-04 21:36:35 +02005139 if (!rdev->ops->assoc)
5140 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005141
Johannes Berg074ac8d2010-09-16 14:58:22 +02005142 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005143 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5144 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005145
Johannes Berg19957bb2009-07-02 17:20:43 +02005146 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005147
Johannes Berg19957bb2009-07-02 17:20:43 +02005148 chan = ieee80211_get_channel(&rdev->wiphy,
5149 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02005150 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
5151 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005152
Johannes Berg19957bb2009-07-02 17:20:43 +02005153 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5154 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005155
5156 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005157 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5158 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005159 }
5160
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005161 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005162 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005163 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02005164 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02005165 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02005166 else if (mfp != NL80211_MFP_NO)
5167 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03005168 }
5169
Johannes Berg3e5d7642009-07-07 14:37:26 +02005170 if (info->attrs[NL80211_ATTR_PREV_BSSID])
5171 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
5172
Ben Greear7e7c8922011-11-18 11:31:59 -08005173 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5174 flags |= ASSOC_REQ_DISABLE_HT;
5175
5176 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5177 ht_capa_mask =
5178 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]);
5179
5180 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
5181 if (!ht_capa_mask)
5182 return -EINVAL;
5183 ht_capa = nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
5184 }
5185
Johannes Bergc0692b82010-08-27 14:26:53 +03005186 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005187 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02005188 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
5189 ssid, ssid_len, ie, ie_len, use_mfp,
Ben Greear7e7c8922011-11-18 11:31:59 -08005190 &crypto, flags, ht_capa,
5191 ht_capa_mask);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005192
Jouni Malinen636a5d32009-03-19 13:39:22 +02005193 return err;
5194}
5195
5196static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
5197{
Johannes Berg4c476992010-10-04 21:36:35 +02005198 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5199 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005200 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005201 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005202 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005203 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005204
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005205 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5206 return -EINVAL;
5207
5208 if (!info->attrs[NL80211_ATTR_MAC])
5209 return -EINVAL;
5210
5211 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5212 return -EINVAL;
5213
Johannes Berg4c476992010-10-04 21:36:35 +02005214 if (!rdev->ops->deauth)
5215 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005216
Johannes Berg074ac8d2010-09-16 14:58:22 +02005217 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005218 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5219 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005220
Johannes Berg19957bb2009-07-02 17:20:43 +02005221 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005222
Johannes Berg19957bb2009-07-02 17:20:43 +02005223 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5224 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005225 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005226 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005227 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005228
5229 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005230 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5231 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005232 }
5233
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005234 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5235
Johannes Berg4c476992010-10-04 21:36:35 +02005236 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
5237 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005238}
5239
5240static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
5241{
Johannes Berg4c476992010-10-04 21:36:35 +02005242 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5243 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02005244 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02005245 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02005246 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005247 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005248
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005249 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5250 return -EINVAL;
5251
5252 if (!info->attrs[NL80211_ATTR_MAC])
5253 return -EINVAL;
5254
5255 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5256 return -EINVAL;
5257
Johannes Berg4c476992010-10-04 21:36:35 +02005258 if (!rdev->ops->disassoc)
5259 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02005260
Johannes Berg074ac8d2010-09-16 14:58:22 +02005261 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005262 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5263 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02005264
Johannes Berg19957bb2009-07-02 17:20:43 +02005265 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005266
Johannes Berg19957bb2009-07-02 17:20:43 +02005267 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5268 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01005269 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02005270 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02005271 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02005272
5273 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02005274 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5275 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005276 }
5277
Jouni Malinend5cdfac2010-04-04 09:37:19 +03005278 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
5279
Johannes Berg4c476992010-10-04 21:36:35 +02005280 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
5281 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02005282}
5283
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005284static bool
5285nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
5286 int mcast_rate[IEEE80211_NUM_BANDS],
5287 int rateval)
5288{
5289 struct wiphy *wiphy = &rdev->wiphy;
5290 bool found = false;
5291 int band, i;
5292
5293 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
5294 struct ieee80211_supported_band *sband;
5295
5296 sband = wiphy->bands[band];
5297 if (!sband)
5298 continue;
5299
5300 for (i = 0; i < sband->n_bitrates; i++) {
5301 if (sband->bitrates[i].bitrate == rateval) {
5302 mcast_rate[band] = i + 1;
5303 found = true;
5304 break;
5305 }
5306 }
5307 }
5308
5309 return found;
5310}
5311
Johannes Berg04a773a2009-04-19 21:24:32 +02005312static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
5313{
Johannes Berg4c476992010-10-04 21:36:35 +02005314 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5315 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005316 struct cfg80211_ibss_params ibss;
5317 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005318 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005319 int err;
5320
Johannes Berg8e30bc52009-04-22 17:45:38 +02005321 memset(&ibss, 0, sizeof(ibss));
5322
Johannes Berg04a773a2009-04-19 21:24:32 +02005323 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5324 return -EINVAL;
5325
5326 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5327 !info->attrs[NL80211_ATTR_SSID] ||
5328 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5329 return -EINVAL;
5330
Johannes Berg8e30bc52009-04-22 17:45:38 +02005331 ibss.beacon_interval = 100;
5332
5333 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
5334 ibss.beacon_interval =
5335 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
5336 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
5337 return -EINVAL;
5338 }
5339
Johannes Berg4c476992010-10-04 21:36:35 +02005340 if (!rdev->ops->join_ibss)
5341 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005342
Johannes Berg4c476992010-10-04 21:36:35 +02005343 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5344 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005345
Johannes Berg79c97e92009-07-07 03:56:12 +02005346 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02005347
Johannes Berg39193492011-09-16 13:45:25 +02005348 if (info->attrs[NL80211_ATTR_MAC]) {
Johannes Berg04a773a2009-04-19 21:24:32 +02005349 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg39193492011-09-16 13:45:25 +02005350
5351 if (!is_valid_ether_addr(ibss.bssid))
5352 return -EINVAL;
5353 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005354 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5355 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5356
5357 if (info->attrs[NL80211_ATTR_IE]) {
5358 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5359 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5360 }
5361
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005362 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5363 enum nl80211_channel_type channel_type;
5364
Johannes Bergcd6c6592012-05-10 21:27:18 +02005365 if (!nl80211_valid_channel_type(info, &channel_type))
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005366 return -EINVAL;
5367
5368 if (channel_type != NL80211_CHAN_NO_HT &&
5369 !(wiphy->features & NL80211_FEATURE_HT_IBSS))
5370 return -EINVAL;
5371
5372 ibss.channel_type = channel_type;
5373 } else {
5374 ibss.channel_type = NL80211_CHAN_NO_HT;
5375 }
5376
5377 ibss.channel = rdev_freq_to_chan(rdev,
5378 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
5379 ibss.channel_type);
Johannes Berg04a773a2009-04-19 21:24:32 +02005380 if (!ibss.channel ||
5381 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02005382 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
5383 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02005384
Alexander Simon54858ee5b2011-11-30 16:56:32 +01005385 /* Both channels should be able to initiate communication */
5386 if ((ibss.channel_type == NL80211_CHAN_HT40PLUS ||
5387 ibss.channel_type == NL80211_CHAN_HT40MINUS) &&
5388 !cfg80211_can_beacon_sec_chan(&rdev->wiphy, ibss.channel,
5389 ibss.channel_type))
5390 return -EINVAL;
5391
Johannes Berg04a773a2009-04-19 21:24:32 +02005392 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02005393 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02005394
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005395 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
5396 u8 *rates =
5397 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5398 int n_rates =
5399 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
5400 struct ieee80211_supported_band *sband =
5401 wiphy->bands[ibss.channel->band];
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005402
Johannes Berg34850ab2011-07-18 18:08:35 +02005403 err = ieee80211_get_ratemask(sband, rates, n_rates,
5404 &ibss.basic_rates);
5405 if (err)
5406 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005407 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01005408
5409 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
5410 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
5411 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
5412 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03005413
Johannes Berg4c476992010-10-04 21:36:35 +02005414 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
Sujith Manoharande7044e2012-10-18 10:19:28 +05305415 bool no_ht = false;
5416
Johannes Berg4c476992010-10-04 21:36:35 +02005417 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305418 info->attrs[NL80211_ATTR_KEYS],
5419 &no_ht);
Johannes Berg4c476992010-10-04 21:36:35 +02005420 if (IS_ERR(connkeys))
5421 return PTR_ERR(connkeys);
Sujith Manoharande7044e2012-10-18 10:19:28 +05305422
5423 if ((ibss.channel_type != NL80211_CHAN_NO_HT) && no_ht) {
5424 kfree(connkeys);
5425 return -EINVAL;
5426 }
Johannes Berg4c476992010-10-04 21:36:35 +02005427 }
Johannes Berg04a773a2009-04-19 21:24:32 +02005428
Antonio Quartulli267335d2012-01-31 20:25:47 +01005429 ibss.control_port =
5430 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
5431
Johannes Berg4c476992010-10-04 21:36:35 +02005432 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005433 if (err)
5434 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02005435 return err;
5436}
5437
5438static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
5439{
Johannes Berg4c476992010-10-04 21:36:35 +02005440 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5441 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02005442
Johannes Berg4c476992010-10-04 21:36:35 +02005443 if (!rdev->ops->leave_ibss)
5444 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005445
Johannes Berg4c476992010-10-04 21:36:35 +02005446 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
5447 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02005448
Johannes Berg4c476992010-10-04 21:36:35 +02005449 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02005450}
5451
Johannes Bergaff89a92009-07-01 21:26:51 +02005452#ifdef CONFIG_NL80211_TESTMODE
5453static struct genl_multicast_group nl80211_testmode_mcgrp = {
5454 .name = "testmode",
5455};
5456
5457static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
5458{
Johannes Berg4c476992010-10-04 21:36:35 +02005459 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02005460 int err;
5461
5462 if (!info->attrs[NL80211_ATTR_TESTDATA])
5463 return -EINVAL;
5464
Johannes Bergaff89a92009-07-01 21:26:51 +02005465 err = -EOPNOTSUPP;
5466 if (rdev->ops->testmode_cmd) {
5467 rdev->testmode_info = info;
5468 err = rdev->ops->testmode_cmd(&rdev->wiphy,
5469 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
5470 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
5471 rdev->testmode_info = NULL;
5472 }
5473
Johannes Bergaff89a92009-07-01 21:26:51 +02005474 return err;
5475}
5476
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005477static int nl80211_testmode_dump(struct sk_buff *skb,
5478 struct netlink_callback *cb)
5479{
Johannes Berg00918d32011-12-13 17:22:05 +01005480 struct cfg80211_registered_device *rdev;
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005481 int err;
5482 long phy_idx;
5483 void *data = NULL;
5484 int data_len = 0;
5485
5486 if (cb->args[0]) {
5487 /*
5488 * 0 is a valid index, but not valid for args[0],
5489 * so we need to offset by 1.
5490 */
5491 phy_idx = cb->args[0] - 1;
5492 } else {
5493 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
5494 nl80211_fam.attrbuf, nl80211_fam.maxattr,
5495 nl80211_policy);
5496 if (err)
5497 return err;
Johannes Berg00918d32011-12-13 17:22:05 +01005498
Johannes Berg2bd7e352012-06-15 14:23:16 +02005499 mutex_lock(&cfg80211_mutex);
5500 rdev = __cfg80211_rdev_from_attrs(sock_net(skb->sk),
5501 nl80211_fam.attrbuf);
5502 if (IS_ERR(rdev)) {
5503 mutex_unlock(&cfg80211_mutex);
5504 return PTR_ERR(rdev);
Johannes Berg00918d32011-12-13 17:22:05 +01005505 }
Johannes Berg2bd7e352012-06-15 14:23:16 +02005506 phy_idx = rdev->wiphy_idx;
5507 rdev = NULL;
5508 mutex_unlock(&cfg80211_mutex);
5509
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005510 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
5511 cb->args[1] =
5512 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
5513 }
5514
5515 if (cb->args[1]) {
5516 data = nla_data((void *)cb->args[1]);
5517 data_len = nla_len((void *)cb->args[1]);
5518 }
5519
5520 mutex_lock(&cfg80211_mutex);
Johannes Berg00918d32011-12-13 17:22:05 +01005521 rdev = cfg80211_rdev_by_wiphy_idx(phy_idx);
5522 if (!rdev) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005523 mutex_unlock(&cfg80211_mutex);
5524 return -ENOENT;
5525 }
Johannes Berg00918d32011-12-13 17:22:05 +01005526 cfg80211_lock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005527 mutex_unlock(&cfg80211_mutex);
5528
Johannes Berg00918d32011-12-13 17:22:05 +01005529 if (!rdev->ops->testmode_dump) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005530 err = -EOPNOTSUPP;
5531 goto out_err;
5532 }
5533
5534 while (1) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00005535 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).portid,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005536 cb->nlh->nlmsg_seq, NLM_F_MULTI,
5537 NL80211_CMD_TESTMODE);
5538 struct nlattr *tmdata;
5539
David S. Miller9360ffd2012-03-29 04:41:26 -04005540 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, phy_idx)) {
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005541 genlmsg_cancel(skb, hdr);
5542 break;
5543 }
5544
5545 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5546 if (!tmdata) {
5547 genlmsg_cancel(skb, hdr);
5548 break;
5549 }
Johannes Berg00918d32011-12-13 17:22:05 +01005550 err = rdev->ops->testmode_dump(&rdev->wiphy, skb, cb,
5551 data, data_len);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005552 nla_nest_end(skb, tmdata);
5553
5554 if (err == -ENOBUFS || err == -ENOENT) {
5555 genlmsg_cancel(skb, hdr);
5556 break;
5557 } else if (err) {
5558 genlmsg_cancel(skb, hdr);
5559 goto out_err;
5560 }
5561
5562 genlmsg_end(skb, hdr);
5563 }
5564
5565 err = skb->len;
5566 /* see above */
5567 cb->args[0] = phy_idx + 1;
5568 out_err:
Johannes Berg00918d32011-12-13 17:22:05 +01005569 cfg80211_unlock_rdev(rdev);
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005570 return err;
5571}
5572
Johannes Bergaff89a92009-07-01 21:26:51 +02005573static struct sk_buff *
5574__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005575 int approxlen, u32 portid, u32 seq, gfp_t gfp)
Johannes Bergaff89a92009-07-01 21:26:51 +02005576{
5577 struct sk_buff *skb;
5578 void *hdr;
5579 struct nlattr *data;
5580
5581 skb = nlmsg_new(approxlen + 100, gfp);
5582 if (!skb)
5583 return NULL;
5584
Eric W. Biederman15e47302012-09-07 20:12:54 +00005585 hdr = nl80211hdr_put(skb, portid, seq, 0, NL80211_CMD_TESTMODE);
Johannes Bergaff89a92009-07-01 21:26:51 +02005586 if (!hdr) {
5587 kfree_skb(skb);
5588 return NULL;
5589 }
5590
David S. Miller9360ffd2012-03-29 04:41:26 -04005591 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx))
5592 goto nla_put_failure;
Johannes Bergaff89a92009-07-01 21:26:51 +02005593 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
5594
5595 ((void **)skb->cb)[0] = rdev;
5596 ((void **)skb->cb)[1] = hdr;
5597 ((void **)skb->cb)[2] = data;
5598
5599 return skb;
5600
5601 nla_put_failure:
5602 kfree_skb(skb);
5603 return NULL;
5604}
5605
5606struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
5607 int approxlen)
5608{
5609 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5610
5611 if (WARN_ON(!rdev->testmode_info))
5612 return NULL;
5613
5614 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
Eric W. Biederman15e47302012-09-07 20:12:54 +00005615 rdev->testmode_info->snd_portid,
Johannes Bergaff89a92009-07-01 21:26:51 +02005616 rdev->testmode_info->snd_seq,
5617 GFP_KERNEL);
5618}
5619EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
5620
5621int cfg80211_testmode_reply(struct sk_buff *skb)
5622{
5623 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
5624 void *hdr = ((void **)skb->cb)[1];
5625 struct nlattr *data = ((void **)skb->cb)[2];
5626
5627 if (WARN_ON(!rdev->testmode_info)) {
5628 kfree_skb(skb);
5629 return -EINVAL;
5630 }
5631
5632 nla_nest_end(skb, data);
5633 genlmsg_end(skb, hdr);
5634 return genlmsg_reply(skb, rdev->testmode_info);
5635}
5636EXPORT_SYMBOL(cfg80211_testmode_reply);
5637
5638struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
5639 int approxlen, gfp_t gfp)
5640{
5641 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
5642
5643 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
5644}
5645EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
5646
5647void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
5648{
5649 void *hdr = ((void **)skb->cb)[1];
5650 struct nlattr *data = ((void **)skb->cb)[2];
5651
5652 nla_nest_end(skb, data);
5653 genlmsg_end(skb, hdr);
5654 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
5655}
5656EXPORT_SYMBOL(cfg80211_testmode_event);
5657#endif
5658
Samuel Ortizb23aa672009-07-01 21:26:54 +02005659static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
5660{
Johannes Berg4c476992010-10-04 21:36:35 +02005661 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5662 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005663 struct cfg80211_connect_params connect;
5664 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02005665 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005666 int err;
5667
5668 memset(&connect, 0, sizeof(connect));
5669
5670 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
5671 return -EINVAL;
5672
5673 if (!info->attrs[NL80211_ATTR_SSID] ||
5674 !nla_len(info->attrs[NL80211_ATTR_SSID]))
5675 return -EINVAL;
5676
5677 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
5678 connect.auth_type =
5679 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Jouni Malinene39e5b52012-09-30 19:29:39 +03005680 if (!nl80211_valid_auth_type(rdev, connect.auth_type,
5681 NL80211_CMD_CONNECT))
Samuel Ortizb23aa672009-07-01 21:26:54 +02005682 return -EINVAL;
5683 } else
5684 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
5685
5686 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
5687
Johannes Bergc0692b82010-08-27 14:26:53 +03005688 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02005689 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005690 if (err)
5691 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005692
Johannes Berg074ac8d2010-09-16 14:58:22 +02005693 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005694 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5695 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005696
Johannes Berg79c97e92009-07-07 03:56:12 +02005697 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005698
Bala Shanmugam4486ea92012-03-07 17:27:12 +05305699 connect.bg_scan_period = -1;
5700 if (info->attrs[NL80211_ATTR_BG_SCAN_PERIOD] &&
5701 (wiphy->flags & WIPHY_FLAG_SUPPORTS_FW_ROAM)) {
5702 connect.bg_scan_period =
5703 nla_get_u16(info->attrs[NL80211_ATTR_BG_SCAN_PERIOD]);
5704 }
5705
Samuel Ortizb23aa672009-07-01 21:26:54 +02005706 if (info->attrs[NL80211_ATTR_MAC])
5707 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5708 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
5709 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
5710
5711 if (info->attrs[NL80211_ATTR_IE]) {
5712 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
5713 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
5714 }
5715
5716 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
5717 connect.channel =
5718 ieee80211_get_channel(wiphy,
5719 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
5720 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02005721 connect.channel->flags & IEEE80211_CHAN_DISABLED)
5722 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005723 }
5724
Johannes Bergfffd0932009-07-08 14:22:54 +02005725 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
5726 connkeys = nl80211_parse_connkeys(rdev,
Sujith Manoharande7044e2012-10-18 10:19:28 +05305727 info->attrs[NL80211_ATTR_KEYS], NULL);
Johannes Berg4c476992010-10-04 21:36:35 +02005728 if (IS_ERR(connkeys))
5729 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005730 }
5731
Ben Greear7e7c8922011-11-18 11:31:59 -08005732 if (nla_get_flag(info->attrs[NL80211_ATTR_DISABLE_HT]))
5733 connect.flags |= ASSOC_REQ_DISABLE_HT;
5734
5735 if (info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK])
5736 memcpy(&connect.ht_capa_mask,
5737 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]),
5738 sizeof(connect.ht_capa_mask));
5739
5740 if (info->attrs[NL80211_ATTR_HT_CAPABILITY]) {
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005741 if (!info->attrs[NL80211_ATTR_HT_CAPABILITY_MASK]) {
5742 kfree(connkeys);
Ben Greear7e7c8922011-11-18 11:31:59 -08005743 return -EINVAL;
Wei Yongjunb4e4f472012-09-02 21:41:04 +08005744 }
Ben Greear7e7c8922011-11-18 11:31:59 -08005745 memcpy(&connect.ht_capa,
5746 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]),
5747 sizeof(connect.ht_capa));
5748 }
5749
Johannes Bergfffd0932009-07-08 14:22:54 +02005750 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02005751 if (err)
5752 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005753 return err;
5754}
5755
5756static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
5757{
Johannes Berg4c476992010-10-04 21:36:35 +02005758 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5759 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02005760 u16 reason;
5761
5762 if (!info->attrs[NL80211_ATTR_REASON_CODE])
5763 reason = WLAN_REASON_DEAUTH_LEAVING;
5764 else
5765 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
5766
5767 if (reason == 0)
5768 return -EINVAL;
5769
Johannes Berg074ac8d2010-09-16 14:58:22 +02005770 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005771 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5772 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02005773
Johannes Berg4c476992010-10-04 21:36:35 +02005774 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005775}
5776
Johannes Berg463d0182009-07-14 00:33:35 +02005777static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
5778{
Johannes Berg4c476992010-10-04 21:36:35 +02005779 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02005780 struct net *net;
5781 int err;
5782 u32 pid;
5783
5784 if (!info->attrs[NL80211_ATTR_PID])
5785 return -EINVAL;
5786
5787 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
5788
Johannes Berg463d0182009-07-14 00:33:35 +02005789 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02005790 if (IS_ERR(net))
5791 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005792
5793 err = 0;
5794
5795 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02005796 if (!net_eq(wiphy_net(&rdev->wiphy), net))
5797 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02005798
Johannes Berg463d0182009-07-14 00:33:35 +02005799 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02005800 return err;
5801}
5802
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005803static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
5804{
Johannes Berg4c476992010-10-04 21:36:35 +02005805 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005806 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
5807 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02005808 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005809 struct cfg80211_pmksa pmksa;
5810
5811 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
5812
5813 if (!info->attrs[NL80211_ATTR_MAC])
5814 return -EINVAL;
5815
5816 if (!info->attrs[NL80211_ATTR_PMKID])
5817 return -EINVAL;
5818
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005819 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
5820 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
5821
Johannes Berg074ac8d2010-09-16 14:58:22 +02005822 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005823 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5824 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005825
5826 switch (info->genlhdr->cmd) {
5827 case NL80211_CMD_SET_PMKSA:
5828 rdev_ops = rdev->ops->set_pmksa;
5829 break;
5830 case NL80211_CMD_DEL_PMKSA:
5831 rdev_ops = rdev->ops->del_pmksa;
5832 break;
5833 default:
5834 WARN_ON(1);
5835 break;
5836 }
5837
Johannes Berg4c476992010-10-04 21:36:35 +02005838 if (!rdev_ops)
5839 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005840
Johannes Berg4c476992010-10-04 21:36:35 +02005841 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005842}
5843
5844static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
5845{
Johannes Berg4c476992010-10-04 21:36:35 +02005846 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5847 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005848
Johannes Berg074ac8d2010-09-16 14:58:22 +02005849 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005850 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
5851 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005852
Johannes Berg4c476992010-10-04 21:36:35 +02005853 if (!rdev->ops->flush_pmksa)
5854 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005855
Johannes Berg4c476992010-10-04 21:36:35 +02005856 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005857}
5858
Arik Nemtsov109086c2011-09-28 14:12:50 +03005859static int nl80211_tdls_mgmt(struct sk_buff *skb, struct genl_info *info)
5860{
5861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5862 struct net_device *dev = info->user_ptr[1];
5863 u8 action_code, dialog_token;
5864 u16 status_code;
5865 u8 *peer;
5866
5867 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5868 !rdev->ops->tdls_mgmt)
5869 return -EOPNOTSUPP;
5870
5871 if (!info->attrs[NL80211_ATTR_TDLS_ACTION] ||
5872 !info->attrs[NL80211_ATTR_STATUS_CODE] ||
5873 !info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN] ||
5874 !info->attrs[NL80211_ATTR_IE] ||
5875 !info->attrs[NL80211_ATTR_MAC])
5876 return -EINVAL;
5877
5878 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5879 action_code = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_ACTION]);
5880 status_code = nla_get_u16(info->attrs[NL80211_ATTR_STATUS_CODE]);
5881 dialog_token = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_DIALOG_TOKEN]);
5882
5883 return rdev->ops->tdls_mgmt(&rdev->wiphy, dev, peer, action_code,
5884 dialog_token, status_code,
5885 nla_data(info->attrs[NL80211_ATTR_IE]),
5886 nla_len(info->attrs[NL80211_ATTR_IE]));
5887}
5888
5889static int nl80211_tdls_oper(struct sk_buff *skb, struct genl_info *info)
5890{
5891 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5892 struct net_device *dev = info->user_ptr[1];
5893 enum nl80211_tdls_operation operation;
5894 u8 *peer;
5895
5896 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_TDLS) ||
5897 !rdev->ops->tdls_oper)
5898 return -EOPNOTSUPP;
5899
5900 if (!info->attrs[NL80211_ATTR_TDLS_OPERATION] ||
5901 !info->attrs[NL80211_ATTR_MAC])
5902 return -EINVAL;
5903
5904 operation = nla_get_u8(info->attrs[NL80211_ATTR_TDLS_OPERATION]);
5905 peer = nla_data(info->attrs[NL80211_ATTR_MAC]);
5906
5907 return rdev->ops->tdls_oper(&rdev->wiphy, dev, peer, operation);
5908}
5909
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005910static int nl80211_remain_on_channel(struct sk_buff *skb,
5911 struct genl_info *info)
5912{
Johannes Berg4c476992010-10-04 21:36:35 +02005913 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005914 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005915 struct ieee80211_channel *chan;
5916 struct sk_buff *msg;
5917 void *hdr;
5918 u64 cookie;
5919 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
5920 u32 freq, duration;
5921 int err;
5922
5923 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
5924 !info->attrs[NL80211_ATTR_DURATION])
5925 return -EINVAL;
5926
5927 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5928
Johannes Berg7c4ef712011-11-18 15:33:48 +01005929 if (!rdev->ops->remain_on_channel ||
5930 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL))
Johannes Berg4c476992010-10-04 21:36:35 +02005931 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005932
Johannes Bergebf348f2012-06-01 12:50:54 +02005933 /*
5934 * We should be on that channel for at least a minimum amount of
5935 * time (10ms) but no longer than the driver supports.
5936 */
5937 if (duration < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
5938 duration > rdev->wiphy.max_remain_on_channel_duration)
5939 return -EINVAL;
5940
Johannes Bergcd6c6592012-05-10 21:27:18 +02005941 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
5942 !nl80211_valid_channel_type(info, &channel_type))
5943 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005944
5945 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5946 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005947 if (chan == NULL)
5948 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005949
5950 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005951 if (!msg)
5952 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005953
Eric W. Biederman15e47302012-09-07 20:12:54 +00005954 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005955 NL80211_CMD_REMAIN_ON_CHANNEL);
5956
5957 if (IS_ERR(hdr)) {
5958 err = PTR_ERR(hdr);
5959 goto free_msg;
5960 }
5961
Johannes Berg71bbc992012-06-15 15:30:18 +02005962 err = rdev->ops->remain_on_channel(&rdev->wiphy, wdev, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005963 channel_type, duration, &cookie);
5964
5965 if (err)
5966 goto free_msg;
5967
David S. Miller9360ffd2012-03-29 04:41:26 -04005968 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
5969 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005970
5971 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005972
5973 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005974
5975 nla_put_failure:
5976 err = -ENOBUFS;
5977 free_msg:
5978 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005979 return err;
5980}
5981
5982static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
5983 struct genl_info *info)
5984{
Johannes Berg4c476992010-10-04 21:36:35 +02005985 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02005986 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005987 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005988
5989 if (!info->attrs[NL80211_ATTR_COOKIE])
5990 return -EINVAL;
5991
Johannes Berg4c476992010-10-04 21:36:35 +02005992 if (!rdev->ops->cancel_remain_on_channel)
5993 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005994
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005995 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5996
Johannes Berg71bbc992012-06-15 15:30:18 +02005997 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, wdev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005998}
5999
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006000static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
6001 u8 *rates, u8 rates_len)
6002{
6003 u8 i;
6004 u32 mask = 0;
6005
6006 for (i = 0; i < rates_len; i++) {
6007 int rate = (rates[i] & 0x7f) * 5;
6008 int ridx;
6009 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
6010 struct ieee80211_rate *srate =
6011 &sband->bitrates[ridx];
6012 if (rate == srate->bitrate) {
6013 mask |= 1 << ridx;
6014 break;
6015 }
6016 }
6017 if (ridx == sband->n_bitrates)
6018 return 0; /* rate not found */
6019 }
6020
6021 return mask;
6022}
6023
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006024static bool ht_rateset_to_mask(struct ieee80211_supported_band *sband,
6025 u8 *rates, u8 rates_len,
6026 u8 mcs[IEEE80211_HT_MCS_MASK_LEN])
6027{
6028 u8 i;
6029
6030 memset(mcs, 0, IEEE80211_HT_MCS_MASK_LEN);
6031
6032 for (i = 0; i < rates_len; i++) {
6033 int ridx, rbit;
6034
6035 ridx = rates[i] / 8;
6036 rbit = BIT(rates[i] % 8);
6037
6038 /* check validity */
Dan Carpenter910570b52012-02-01 10:42:11 +03006039 if ((ridx < 0) || (ridx >= IEEE80211_HT_MCS_MASK_LEN))
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006040 return false;
6041
6042 /* check availability */
6043 if (sband->ht_cap.mcs.rx_mask[ridx] & rbit)
6044 mcs[ridx] |= rbit;
6045 else
6046 return false;
6047 }
6048
6049 return true;
6050}
6051
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00006052static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006053 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
6054 .len = NL80211_MAX_SUPP_RATES },
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006055 [NL80211_TXRATE_MCS] = { .type = NLA_BINARY,
6056 .len = NL80211_MAX_SUPP_HT_RATES },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006057};
6058
6059static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
6060 struct genl_info *info)
6061{
6062 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02006063 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006064 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02006065 int rem, i;
6066 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006067 struct nlattr *tx_rates;
6068 struct ieee80211_supported_band *sband;
6069
6070 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
6071 return -EINVAL;
6072
Johannes Berg4c476992010-10-04 21:36:35 +02006073 if (!rdev->ops->set_bitrate_mask)
6074 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006075
6076 memset(&mask, 0, sizeof(mask));
6077 /* Default to all rates enabled */
6078 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
6079 sband = rdev->wiphy.bands[i];
6080 mask.control[i].legacy =
6081 sband ? (1 << sband->n_bitrates) - 1 : 0;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006082 if (sband)
6083 memcpy(mask.control[i].mcs,
6084 sband->ht_cap.mcs.rx_mask,
6085 sizeof(mask.control[i].mcs));
6086 else
6087 memset(mask.control[i].mcs, 0,
6088 sizeof(mask.control[i].mcs));
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006089 }
6090
6091 /*
6092 * The nested attribute uses enum nl80211_band as the index. This maps
6093 * directly to the enum ieee80211_band values used in cfg80211.
6094 */
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006095 BUILD_BUG_ON(NL80211_MAX_SUPP_HT_RATES > IEEE80211_HT_MCS_MASK_LEN * 8);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006096 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
6097 {
6098 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02006099 if (band < 0 || band >= IEEE80211_NUM_BANDS)
6100 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006101 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02006102 if (sband == NULL)
6103 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006104 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
6105 nla_len(tx_rates), nl80211_txattr_policy);
6106 if (tb[NL80211_TXRATE_LEGACY]) {
6107 mask.control[band].legacy = rateset_to_mask(
6108 sband,
6109 nla_data(tb[NL80211_TXRATE_LEGACY]),
6110 nla_len(tb[NL80211_TXRATE_LEGACY]));
Bala Shanmugam218d2e22012-04-20 19:12:58 +05306111 if ((mask.control[band].legacy == 0) &&
6112 nla_len(tb[NL80211_TXRATE_LEGACY]))
6113 return -EINVAL;
Simon Wunderlich24db78c2012-01-28 17:25:32 +01006114 }
6115 if (tb[NL80211_TXRATE_MCS]) {
6116 if (!ht_rateset_to_mask(
6117 sband,
6118 nla_data(tb[NL80211_TXRATE_MCS]),
6119 nla_len(tb[NL80211_TXRATE_MCS]),
6120 mask.control[band].mcs))
6121 return -EINVAL;
6122 }
6123
6124 if (mask.control[band].legacy == 0) {
6125 /* don't allow empty legacy rates if HT
6126 * is not even supported. */
6127 if (!rdev->wiphy.bands[band]->ht_cap.ht_supported)
6128 return -EINVAL;
6129
6130 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
6131 if (mask.control[band].mcs[i])
6132 break;
6133
6134 /* legacy and mcs rates may not be both empty */
6135 if (i == IEEE80211_HT_MCS_MASK_LEN)
Johannes Berg4c476992010-10-04 21:36:35 +02006136 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006137 }
6138 }
6139
Johannes Berg4c476992010-10-04 21:36:35 +02006140 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006141}
6142
Johannes Berg2e161f72010-08-12 15:38:38 +02006143static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006144{
Johannes Berg4c476992010-10-04 21:36:35 +02006145 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006146 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02006147 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02006148
6149 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
6150 return -EINVAL;
6151
Johannes Berg2e161f72010-08-12 15:38:38 +02006152 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
6153 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02006154
Johannes Berg71bbc992012-06-15 15:30:18 +02006155 switch (wdev->iftype) {
6156 case NL80211_IFTYPE_STATION:
6157 case NL80211_IFTYPE_ADHOC:
6158 case NL80211_IFTYPE_P2P_CLIENT:
6159 case NL80211_IFTYPE_AP:
6160 case NL80211_IFTYPE_AP_VLAN:
6161 case NL80211_IFTYPE_MESH_POINT:
6162 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006163 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006164 break;
6165 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006166 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006167 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006168
6169 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02006170 if (!rdev->ops->mgmt_tx)
6171 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006172
Eric W. Biederman15e47302012-09-07 20:12:54 +00006173 return cfg80211_mlme_register_mgmt(wdev, info->snd_portid, frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02006174 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
6175 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02006176}
6177
Johannes Berg2e161f72010-08-12 15:38:38 +02006178static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02006179{
Johannes Berg4c476992010-10-04 21:36:35 +02006180 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006181 struct wireless_dev *wdev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02006182 struct ieee80211_channel *chan;
6183 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02006184 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02006185 u32 freq;
6186 int err;
Johannes Bergd64d3732011-11-10 09:44:46 +01006187 void *hdr = NULL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006188 u64 cookie;
Johannes Berge247bd902011-11-04 11:18:21 +01006189 struct sk_buff *msg = NULL;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006190 unsigned int wait = 0;
Johannes Berge247bd902011-11-04 11:18:21 +01006191 bool offchan, no_cck, dont_wait_for_ack;
6192
6193 dont_wait_for_ack = info->attrs[NL80211_ATTR_DONT_WAIT_FOR_ACK];
Jouni Malinen026331c2010-02-15 12:53:10 +02006194
6195 if (!info->attrs[NL80211_ATTR_FRAME] ||
6196 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
6197 return -EINVAL;
6198
Johannes Berg4c476992010-10-04 21:36:35 +02006199 if (!rdev->ops->mgmt_tx)
6200 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02006201
Johannes Berg71bbc992012-06-15 15:30:18 +02006202 switch (wdev->iftype) {
6203 case NL80211_IFTYPE_STATION:
6204 case NL80211_IFTYPE_ADHOC:
6205 case NL80211_IFTYPE_P2P_CLIENT:
6206 case NL80211_IFTYPE_AP:
6207 case NL80211_IFTYPE_AP_VLAN:
6208 case NL80211_IFTYPE_MESH_POINT:
6209 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006210 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006211 break;
6212 default:
Johannes Berg4c476992010-10-04 21:36:35 +02006213 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006214 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006215
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006216 if (info->attrs[NL80211_ATTR_DURATION]) {
Johannes Berg7c4ef712011-11-18 15:33:48 +01006217 if (!(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006218 return -EINVAL;
6219 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
Johannes Bergebf348f2012-06-01 12:50:54 +02006220
6221 /*
6222 * We should wait on the channel for at least a minimum amount
6223 * of time (10ms) but no longer than the driver supports.
6224 */
6225 if (wait < NL80211_MIN_REMAIN_ON_CHANNEL_TIME ||
6226 wait > rdev->wiphy.max_remain_on_channel_duration)
6227 return -EINVAL;
6228
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006229 }
6230
Jouni Malinen026331c2010-02-15 12:53:10 +02006231 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
Johannes Bergcd6c6592012-05-10 21:27:18 +02006232 if (!nl80211_valid_channel_type(info, &channel_type))
Johannes Berg4c476992010-10-04 21:36:35 +02006233 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02006234 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02006235 }
6236
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006237 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
6238
Johannes Berg7c4ef712011-11-18 15:33:48 +01006239 if (offchan && !(rdev->wiphy.flags & WIPHY_FLAG_OFFCHAN_TX))
6240 return -EINVAL;
6241
Rajkumar Manoharane9f935e2011-09-25 14:53:30 +05306242 no_cck = nla_get_flag(info->attrs[NL80211_ATTR_TX_NO_CCK_RATE]);
6243
Jouni Malinen026331c2010-02-15 12:53:10 +02006244 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
6245 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02006246 if (chan == NULL)
6247 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02006248
Johannes Berge247bd902011-11-04 11:18:21 +01006249 if (!dont_wait_for_ack) {
6250 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6251 if (!msg)
6252 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02006253
Eric W. Biederman15e47302012-09-07 20:12:54 +00006254 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berge247bd902011-11-04 11:18:21 +01006255 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006256
Johannes Berge247bd902011-11-04 11:18:21 +01006257 if (IS_ERR(hdr)) {
6258 err = PTR_ERR(hdr);
6259 goto free_msg;
6260 }
Jouni Malinen026331c2010-02-15 12:53:10 +02006261 }
Johannes Berge247bd902011-11-04 11:18:21 +01006262
Johannes Berg71bbc992012-06-15 15:30:18 +02006263 err = cfg80211_mlme_mgmt_tx(rdev, wdev, chan, offchan, channel_type,
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006264 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02006265 nla_data(info->attrs[NL80211_ATTR_FRAME]),
6266 nla_len(info->attrs[NL80211_ATTR_FRAME]),
Johannes Berge247bd902011-11-04 11:18:21 +01006267 no_cck, dont_wait_for_ack, &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02006268 if (err)
6269 goto free_msg;
6270
Johannes Berge247bd902011-11-04 11:18:21 +01006271 if (msg) {
David S. Miller9360ffd2012-03-29 04:41:26 -04006272 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6273 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02006274
Johannes Berge247bd902011-11-04 11:18:21 +01006275 genlmsg_end(msg, hdr);
6276 return genlmsg_reply(msg, info);
6277 }
6278
6279 return 0;
Jouni Malinen026331c2010-02-15 12:53:10 +02006280
6281 nla_put_failure:
6282 err = -ENOBUFS;
6283 free_msg:
6284 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02006285 return err;
6286}
6287
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006288static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
6289{
6290 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg71bbc992012-06-15 15:30:18 +02006291 struct wireless_dev *wdev = info->user_ptr[1];
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006292 u64 cookie;
6293
6294 if (!info->attrs[NL80211_ATTR_COOKIE])
6295 return -EINVAL;
6296
6297 if (!rdev->ops->mgmt_tx_cancel_wait)
6298 return -EOPNOTSUPP;
6299
Johannes Berg71bbc992012-06-15 15:30:18 +02006300 switch (wdev->iftype) {
6301 case NL80211_IFTYPE_STATION:
6302 case NL80211_IFTYPE_ADHOC:
6303 case NL80211_IFTYPE_P2P_CLIENT:
6304 case NL80211_IFTYPE_AP:
6305 case NL80211_IFTYPE_AP_VLAN:
6306 case NL80211_IFTYPE_P2P_GO:
Johannes Berg98104fde2012-06-16 00:19:54 +02006307 case NL80211_IFTYPE_P2P_DEVICE:
Johannes Berg71bbc992012-06-15 15:30:18 +02006308 break;
6309 default:
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006310 return -EOPNOTSUPP;
Johannes Berg71bbc992012-06-15 15:30:18 +02006311 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006312
6313 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
6314
Johannes Berg71bbc992012-06-15 15:30:18 +02006315 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, wdev, cookie);
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006316}
6317
Kalle Valoffb9eb32010-02-17 17:58:10 +02006318static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
6319{
Johannes Berg4c476992010-10-04 21:36:35 +02006320 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006321 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006322 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006323 u8 ps_state;
6324 bool state;
6325 int err;
6326
Johannes Berg4c476992010-10-04 21:36:35 +02006327 if (!info->attrs[NL80211_ATTR_PS_STATE])
6328 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006329
6330 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
6331
Johannes Berg4c476992010-10-04 21:36:35 +02006332 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
6333 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006334
6335 wdev = dev->ieee80211_ptr;
6336
Johannes Berg4c476992010-10-04 21:36:35 +02006337 if (!rdev->ops->set_power_mgmt)
6338 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006339
6340 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
6341
6342 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02006343 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006344
Johannes Berg4c476992010-10-04 21:36:35 +02006345 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
6346 wdev->ps_timeout);
6347 if (!err)
6348 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006349 return err;
6350}
6351
6352static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
6353{
Johannes Berg4c476992010-10-04 21:36:35 +02006354 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006355 enum nl80211_ps_state ps_state;
6356 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006357 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02006358 struct sk_buff *msg;
6359 void *hdr;
6360 int err;
6361
Kalle Valoffb9eb32010-02-17 17:58:10 +02006362 wdev = dev->ieee80211_ptr;
6363
Johannes Berg4c476992010-10-04 21:36:35 +02006364 if (!rdev->ops->set_power_mgmt)
6365 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006366
6367 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02006368 if (!msg)
6369 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006370
Eric W. Biederman15e47302012-09-07 20:12:54 +00006371 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006372 NL80211_CMD_GET_POWER_SAVE);
6373 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02006374 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006375 goto free_msg;
6376 }
6377
6378 if (wdev->ps)
6379 ps_state = NL80211_PS_ENABLED;
6380 else
6381 ps_state = NL80211_PS_DISABLED;
6382
David S. Miller9360ffd2012-03-29 04:41:26 -04006383 if (nla_put_u32(msg, NL80211_ATTR_PS_STATE, ps_state))
6384 goto nla_put_failure;
Kalle Valoffb9eb32010-02-17 17:58:10 +02006385
6386 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02006387 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006388
Johannes Berg4c476992010-10-04 21:36:35 +02006389 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006390 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02006391 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02006392 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02006393 return err;
6394}
6395
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006396static struct nla_policy
6397nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
6398 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
6399 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
6400 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
Thomas Pedersen84f10702012-07-12 16:17:33 -07006401 [NL80211_ATTR_CQM_TXE_RATE] = { .type = NLA_U32 },
6402 [NL80211_ATTR_CQM_TXE_PKTS] = { .type = NLA_U32 },
6403 [NL80211_ATTR_CQM_TXE_INTVL] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006404};
6405
Thomas Pedersen84f10702012-07-12 16:17:33 -07006406static int nl80211_set_cqm_txe(struct genl_info *info,
6407 u32 rate, u32 pkts, u32 intvl)
6408{
6409 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6410 struct wireless_dev *wdev;
6411 struct net_device *dev = info->user_ptr[1];
6412
6413 if ((rate < 0 || rate > 100) ||
6414 (intvl < 0 || intvl > NL80211_CQM_TXE_MAX_INTVL))
6415 return -EINVAL;
6416
6417 wdev = dev->ieee80211_ptr;
6418
6419 if (!rdev->ops->set_cqm_txe_config)
6420 return -EOPNOTSUPP;
6421
6422 if (wdev->iftype != NL80211_IFTYPE_STATION &&
6423 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6424 return -EOPNOTSUPP;
6425
6426 return rdev->ops->set_cqm_txe_config(wdev->wiphy, dev,
6427 rate, pkts, intvl);
6428}
6429
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006430static int nl80211_set_cqm_rssi(struct genl_info *info,
6431 s32 threshold, u32 hysteresis)
6432{
Johannes Berg4c476992010-10-04 21:36:35 +02006433 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006434 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02006435 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006436
6437 if (threshold > 0)
6438 return -EINVAL;
6439
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006440 wdev = dev->ieee80211_ptr;
6441
Johannes Berg4c476992010-10-04 21:36:35 +02006442 if (!rdev->ops->set_cqm_rssi_config)
6443 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006444
Johannes Berg074ac8d2010-09-16 14:58:22 +02006445 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02006446 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
6447 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006448
Johannes Berg4c476992010-10-04 21:36:35 +02006449 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
6450 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006451}
6452
6453static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
6454{
6455 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
6456 struct nlattr *cqm;
6457 int err;
6458
6459 cqm = info->attrs[NL80211_ATTR_CQM];
6460 if (!cqm) {
6461 err = -EINVAL;
6462 goto out;
6463 }
6464
6465 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
6466 nl80211_attr_cqm_policy);
6467 if (err)
6468 goto out;
6469
6470 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
6471 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
6472 s32 threshold;
6473 u32 hysteresis;
6474 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
6475 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
6476 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
Thomas Pedersen84f10702012-07-12 16:17:33 -07006477 } else if (attrs[NL80211_ATTR_CQM_TXE_RATE] &&
6478 attrs[NL80211_ATTR_CQM_TXE_PKTS] &&
6479 attrs[NL80211_ATTR_CQM_TXE_INTVL]) {
6480 u32 rate, pkts, intvl;
6481 rate = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_RATE]);
6482 pkts = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_PKTS]);
6483 intvl = nla_get_u32(attrs[NL80211_ATTR_CQM_TXE_INTVL]);
6484 err = nl80211_set_cqm_txe(info, rate, pkts, intvl);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006485 } else
6486 err = -EINVAL;
6487
6488out:
6489 return err;
6490}
6491
Johannes Berg29cbe682010-12-03 09:20:44 +01006492static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
6493{
6494 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6495 struct net_device *dev = info->user_ptr[1];
6496 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08006497 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01006498 int err;
6499
6500 /* start with default */
6501 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08006502 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01006503
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006504 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01006505 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08006506 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01006507 if (err)
6508 return err;
6509 }
6510
6511 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
6512 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
6513 return -EINVAL;
6514
Javier Cardonac80d5452010-12-16 17:37:49 -08006515 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
6516 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
6517
Chun-Yeow Yeoh4bb62342011-11-24 17:15:20 -08006518 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
6519 !nl80211_parse_mcast_rate(rdev, setup.mcast_rate,
6520 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
6521 return -EINVAL;
6522
Javier Cardonac80d5452010-12-16 17:37:49 -08006523 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
6524 /* parse additional setup parameters if given */
6525 err = nl80211_parse_mesh_setup(info, &setup);
6526 if (err)
6527 return err;
6528 }
6529
Johannes Bergcc1d2802012-05-16 23:50:20 +02006530 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
6531 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
6532
6533 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE] &&
6534 !nl80211_valid_channel_type(info, &channel_type))
6535 return -EINVAL;
6536
6537 setup.channel = rdev_freq_to_chan(rdev,
6538 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]),
6539 channel_type);
6540 if (!setup.channel)
6541 return -EINVAL;
6542 setup.channel_type = channel_type;
6543 } else {
6544 /* cfg80211_join_mesh() will sort it out */
6545 setup.channel = NULL;
6546 }
6547
Javier Cardonac80d5452010-12-16 17:37:49 -08006548 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01006549}
6550
6551static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
6552{
6553 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6554 struct net_device *dev = info->user_ptr[1];
6555
6556 return cfg80211_leave_mesh(rdev, dev);
6557}
6558
Johannes Bergdfb89c52012-06-27 09:23:48 +02006559#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02006560static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
6561{
6562 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6563 struct sk_buff *msg;
6564 void *hdr;
6565
6566 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6567 return -EOPNOTSUPP;
6568
6569 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6570 if (!msg)
6571 return -ENOMEM;
6572
Eric W. Biederman15e47302012-09-07 20:12:54 +00006573 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Bergff1b6e62011-05-04 15:37:28 +02006574 NL80211_CMD_GET_WOWLAN);
6575 if (!hdr)
6576 goto nla_put_failure;
6577
6578 if (rdev->wowlan) {
6579 struct nlattr *nl_wowlan;
6580
6581 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
6582 if (!nl_wowlan)
6583 goto nla_put_failure;
6584
David S. Miller9360ffd2012-03-29 04:41:26 -04006585 if ((rdev->wowlan->any &&
6586 nla_put_flag(msg, NL80211_WOWLAN_TRIG_ANY)) ||
6587 (rdev->wowlan->disconnect &&
6588 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) ||
6589 (rdev->wowlan->magic_pkt &&
6590 nla_put_flag(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT)) ||
6591 (rdev->wowlan->gtk_rekey_failure &&
6592 nla_put_flag(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE)) ||
6593 (rdev->wowlan->eap_identity_req &&
6594 nla_put_flag(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST)) ||
6595 (rdev->wowlan->four_way_handshake &&
6596 nla_put_flag(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE)) ||
6597 (rdev->wowlan->rfkill_release &&
6598 nla_put_flag(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE)))
6599 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006600 if (rdev->wowlan->n_patterns) {
6601 struct nlattr *nl_pats, *nl_pat;
6602 int i, pat_len;
6603
6604 nl_pats = nla_nest_start(msg,
6605 NL80211_WOWLAN_TRIG_PKT_PATTERN);
6606 if (!nl_pats)
6607 goto nla_put_failure;
6608
6609 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
6610 nl_pat = nla_nest_start(msg, i + 1);
6611 if (!nl_pat)
6612 goto nla_put_failure;
6613 pat_len = rdev->wowlan->patterns[i].pattern_len;
David S. Miller9360ffd2012-03-29 04:41:26 -04006614 if (nla_put(msg, NL80211_WOWLAN_PKTPAT_MASK,
6615 DIV_ROUND_UP(pat_len, 8),
6616 rdev->wowlan->patterns[i].mask) ||
6617 nla_put(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
6618 pat_len,
6619 rdev->wowlan->patterns[i].pattern))
6620 goto nla_put_failure;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006621 nla_nest_end(msg, nl_pat);
6622 }
6623 nla_nest_end(msg, nl_pats);
6624 }
6625
6626 nla_nest_end(msg, nl_wowlan);
6627 }
6628
6629 genlmsg_end(msg, hdr);
6630 return genlmsg_reply(msg, info);
6631
6632nla_put_failure:
6633 nlmsg_free(msg);
6634 return -ENOBUFS;
6635}
6636
6637static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
6638{
6639 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6640 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
Johannes Bergff1b6e62011-05-04 15:37:28 +02006641 struct cfg80211_wowlan new_triggers = {};
Johannes Bergae33bd82012-07-12 16:25:02 +02006642 struct cfg80211_wowlan *ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006643 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
6644 int err, i;
Johannes Berg6d525632012-04-04 15:05:25 +02006645 bool prev_enabled = rdev->wowlan;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006646
6647 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
6648 return -EOPNOTSUPP;
6649
Johannes Bergae33bd82012-07-12 16:25:02 +02006650 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]) {
6651 cfg80211_rdev_free_wowlan(rdev);
6652 rdev->wowlan = NULL;
6653 goto set_wakeup;
6654 }
Johannes Bergff1b6e62011-05-04 15:37:28 +02006655
6656 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
6657 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6658 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
6659 nl80211_wowlan_policy);
6660 if (err)
6661 return err;
6662
6663 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
6664 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
6665 return -EINVAL;
6666 new_triggers.any = true;
6667 }
6668
6669 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
6670 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
6671 return -EINVAL;
6672 new_triggers.disconnect = true;
6673 }
6674
6675 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
6676 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
6677 return -EINVAL;
6678 new_triggers.magic_pkt = true;
6679 }
6680
Johannes Berg77dbbb12011-07-13 10:48:55 +02006681 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
6682 return -EINVAL;
6683
6684 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
6685 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
6686 return -EINVAL;
6687 new_triggers.gtk_rekey_failure = true;
6688 }
6689
6690 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
6691 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
6692 return -EINVAL;
6693 new_triggers.eap_identity_req = true;
6694 }
6695
6696 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
6697 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
6698 return -EINVAL;
6699 new_triggers.four_way_handshake = true;
6700 }
6701
6702 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
6703 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
6704 return -EINVAL;
6705 new_triggers.rfkill_release = true;
6706 }
6707
Johannes Bergff1b6e62011-05-04 15:37:28 +02006708 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
6709 struct nlattr *pat;
6710 int n_patterns = 0;
6711 int rem, pat_len, mask_len;
6712 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
6713
6714 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6715 rem)
6716 n_patterns++;
6717 if (n_patterns > wowlan->n_patterns)
6718 return -EINVAL;
6719
6720 new_triggers.patterns = kcalloc(n_patterns,
6721 sizeof(new_triggers.patterns[0]),
6722 GFP_KERNEL);
6723 if (!new_triggers.patterns)
6724 return -ENOMEM;
6725
6726 new_triggers.n_patterns = n_patterns;
6727 i = 0;
6728
6729 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
6730 rem) {
6731 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
6732 nla_data(pat), nla_len(pat), NULL);
6733 err = -EINVAL;
6734 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
6735 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
6736 goto error;
6737 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
6738 mask_len = DIV_ROUND_UP(pat_len, 8);
6739 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
6740 mask_len)
6741 goto error;
6742 if (pat_len > wowlan->pattern_max_len ||
6743 pat_len < wowlan->pattern_min_len)
6744 goto error;
6745
6746 new_triggers.patterns[i].mask =
6747 kmalloc(mask_len + pat_len, GFP_KERNEL);
6748 if (!new_triggers.patterns[i].mask) {
6749 err = -ENOMEM;
6750 goto error;
6751 }
6752 new_triggers.patterns[i].pattern =
6753 new_triggers.patterns[i].mask + mask_len;
6754 memcpy(new_triggers.patterns[i].mask,
6755 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
6756 mask_len);
6757 new_triggers.patterns[i].pattern_len = pat_len;
6758 memcpy(new_triggers.patterns[i].pattern,
6759 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
6760 pat_len);
6761 i++;
6762 }
6763 }
6764
Johannes Bergae33bd82012-07-12 16:25:02 +02006765 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
6766 if (!ntrig) {
6767 err = -ENOMEM;
6768 goto error;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006769 }
Johannes Bergae33bd82012-07-12 16:25:02 +02006770 cfg80211_rdev_free_wowlan(rdev);
6771 rdev->wowlan = ntrig;
Johannes Bergff1b6e62011-05-04 15:37:28 +02006772
Johannes Bergae33bd82012-07-12 16:25:02 +02006773 set_wakeup:
Johannes Berg6d525632012-04-04 15:05:25 +02006774 if (rdev->ops->set_wakeup && prev_enabled != !!rdev->wowlan)
6775 rdev->ops->set_wakeup(&rdev->wiphy, rdev->wowlan);
6776
Johannes Bergff1b6e62011-05-04 15:37:28 +02006777 return 0;
6778 error:
6779 for (i = 0; i < new_triggers.n_patterns; i++)
6780 kfree(new_triggers.patterns[i].mask);
6781 kfree(new_triggers.patterns);
6782 return err;
6783}
Johannes Bergdfb89c52012-06-27 09:23:48 +02006784#endif
Johannes Bergff1b6e62011-05-04 15:37:28 +02006785
Johannes Berge5497d72011-07-05 16:35:40 +02006786static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
6787{
6788 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6789 struct net_device *dev = info->user_ptr[1];
6790 struct wireless_dev *wdev = dev->ieee80211_ptr;
6791 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
6792 struct cfg80211_gtk_rekey_data rekey_data;
6793 int err;
6794
6795 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
6796 return -EINVAL;
6797
6798 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
6799 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
6800 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
6801 nl80211_rekey_policy);
6802 if (err)
6803 return err;
6804
6805 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
6806 return -ERANGE;
6807 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
6808 return -ERANGE;
6809 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
6810 return -ERANGE;
6811
6812 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
6813 NL80211_KEK_LEN);
6814 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
6815 NL80211_KCK_LEN);
6816 memcpy(rekey_data.replay_ctr,
6817 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
6818 NL80211_REPLAY_CTR_LEN);
6819
6820 wdev_lock(wdev);
6821 if (!wdev->current_bss) {
6822 err = -ENOTCONN;
6823 goto out;
6824 }
6825
6826 if (!rdev->ops->set_rekey_data) {
6827 err = -EOPNOTSUPP;
6828 goto out;
6829 }
6830
6831 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
6832 out:
6833 wdev_unlock(wdev);
6834 return err;
6835}
6836
Johannes Berg28946da2011-11-04 11:18:12 +01006837static int nl80211_register_unexpected_frame(struct sk_buff *skb,
6838 struct genl_info *info)
6839{
6840 struct net_device *dev = info->user_ptr[1];
6841 struct wireless_dev *wdev = dev->ieee80211_ptr;
6842
6843 if (wdev->iftype != NL80211_IFTYPE_AP &&
6844 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6845 return -EINVAL;
6846
Eric W. Biederman15e47302012-09-07 20:12:54 +00006847 if (wdev->ap_unexpected_nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01006848 return -EBUSY;
6849
Eric W. Biederman15e47302012-09-07 20:12:54 +00006850 wdev->ap_unexpected_nlportid = info->snd_portid;
Johannes Berg28946da2011-11-04 11:18:12 +01006851 return 0;
6852}
6853
Johannes Berg7f6cf312011-11-04 11:18:15 +01006854static int nl80211_probe_client(struct sk_buff *skb,
6855 struct genl_info *info)
6856{
6857 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6858 struct net_device *dev = info->user_ptr[1];
6859 struct wireless_dev *wdev = dev->ieee80211_ptr;
6860 struct sk_buff *msg;
6861 void *hdr;
6862 const u8 *addr;
6863 u64 cookie;
6864 int err;
6865
6866 if (wdev->iftype != NL80211_IFTYPE_AP &&
6867 wdev->iftype != NL80211_IFTYPE_P2P_GO)
6868 return -EOPNOTSUPP;
6869
6870 if (!info->attrs[NL80211_ATTR_MAC])
6871 return -EINVAL;
6872
6873 if (!rdev->ops->probe_client)
6874 return -EOPNOTSUPP;
6875
6876 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6877 if (!msg)
6878 return -ENOMEM;
6879
Eric W. Biederman15e47302012-09-07 20:12:54 +00006880 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
Johannes Berg7f6cf312011-11-04 11:18:15 +01006881 NL80211_CMD_PROBE_CLIENT);
6882
6883 if (IS_ERR(hdr)) {
6884 err = PTR_ERR(hdr);
6885 goto free_msg;
6886 }
6887
6888 addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
6889
6890 err = rdev->ops->probe_client(&rdev->wiphy, dev, addr, &cookie);
6891 if (err)
6892 goto free_msg;
6893
David S. Miller9360ffd2012-03-29 04:41:26 -04006894 if (nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
6895 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01006896
6897 genlmsg_end(msg, hdr);
6898
6899 return genlmsg_reply(msg, info);
6900
6901 nla_put_failure:
6902 err = -ENOBUFS;
6903 free_msg:
6904 nlmsg_free(msg);
6905 return err;
6906}
6907
Johannes Berg5e760232011-11-04 11:18:17 +01006908static int nl80211_register_beacons(struct sk_buff *skb, struct genl_info *info)
6909{
6910 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6911
6912 if (!(rdev->wiphy.flags & WIPHY_FLAG_REPORTS_OBSS))
6913 return -EOPNOTSUPP;
6914
Eric W. Biederman15e47302012-09-07 20:12:54 +00006915 if (rdev->ap_beacons_nlportid)
Johannes Berg5e760232011-11-04 11:18:17 +01006916 return -EBUSY;
6917
Eric W. Biederman15e47302012-09-07 20:12:54 +00006918 rdev->ap_beacons_nlportid = info->snd_portid;
Johannes Berg5e760232011-11-04 11:18:17 +01006919
6920 return 0;
6921}
6922
Johannes Berg98104fde2012-06-16 00:19:54 +02006923static int nl80211_start_p2p_device(struct sk_buff *skb, struct genl_info *info)
6924{
6925 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6926 struct wireless_dev *wdev = info->user_ptr[1];
6927 int err;
6928
6929 if (!rdev->ops->start_p2p_device)
6930 return -EOPNOTSUPP;
6931
6932 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
6933 return -EOPNOTSUPP;
6934
6935 if (wdev->p2p_started)
6936 return 0;
6937
6938 mutex_lock(&rdev->devlist_mtx);
6939 err = cfg80211_can_add_interface(rdev, wdev->iftype);
6940 mutex_unlock(&rdev->devlist_mtx);
6941 if (err)
6942 return err;
6943
6944 err = rdev->ops->start_p2p_device(&rdev->wiphy, wdev);
6945 if (err)
6946 return err;
6947
6948 wdev->p2p_started = true;
6949 mutex_lock(&rdev->devlist_mtx);
6950 rdev->opencount++;
6951 mutex_unlock(&rdev->devlist_mtx);
6952
6953 return 0;
6954}
6955
6956static int nl80211_stop_p2p_device(struct sk_buff *skb, struct genl_info *info)
6957{
6958 struct cfg80211_registered_device *rdev = info->user_ptr[0];
6959 struct wireless_dev *wdev = info->user_ptr[1];
6960
6961 if (wdev->iftype != NL80211_IFTYPE_P2P_DEVICE)
6962 return -EOPNOTSUPP;
6963
6964 if (!rdev->ops->stop_p2p_device)
6965 return -EOPNOTSUPP;
6966
6967 if (!wdev->p2p_started)
6968 return 0;
6969
6970 rdev->ops->stop_p2p_device(&rdev->wiphy, wdev);
6971 wdev->p2p_started = false;
6972
6973 mutex_lock(&rdev->devlist_mtx);
6974 rdev->opencount--;
6975 mutex_unlock(&rdev->devlist_mtx);
6976
6977 if (WARN_ON(rdev->scan_req && rdev->scan_req->wdev == wdev)) {
6978 rdev->scan_req->aborted = true;
6979 ___cfg80211_scan_done(rdev, true);
6980 }
6981
6982 return 0;
6983}
6984
Johannes Berg4c476992010-10-04 21:36:35 +02006985#define NL80211_FLAG_NEED_WIPHY 0x01
6986#define NL80211_FLAG_NEED_NETDEV 0x02
6987#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02006988#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
6989#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
6990 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg1bf614e2012-06-15 15:23:36 +02006991#define NL80211_FLAG_NEED_WDEV 0x10
Johannes Berg98104fde2012-06-16 00:19:54 +02006992/* If a netdev is associated, it must be UP, P2P must be started */
Johannes Berg1bf614e2012-06-15 15:23:36 +02006993#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
6994 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02006995
6996static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
6997 struct genl_info *info)
6998{
6999 struct cfg80211_registered_device *rdev;
Johannes Berg89a54e42012-06-15 14:33:17 +02007000 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007001 struct net_device *dev;
Johannes Berg4c476992010-10-04 21:36:35 +02007002 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
7003
7004 if (rtnl)
7005 rtnl_lock();
7006
7007 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
Johannes Berg4f7eff12012-06-15 14:14:22 +02007008 rdev = cfg80211_get_dev_from_info(genl_info_net(info), info);
Johannes Berg4c476992010-10-04 21:36:35 +02007009 if (IS_ERR(rdev)) {
7010 if (rtnl)
7011 rtnl_unlock();
7012 return PTR_ERR(rdev);
7013 }
7014 info->user_ptr[0] = rdev;
Johannes Berg1bf614e2012-06-15 15:23:36 +02007015 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV ||
7016 ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
Johannes Berg89a54e42012-06-15 14:33:17 +02007017 mutex_lock(&cfg80211_mutex);
7018 wdev = __cfg80211_wdev_from_attrs(genl_info_net(info),
7019 info->attrs);
7020 if (IS_ERR(wdev)) {
7021 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02007022 if (rtnl)
7023 rtnl_unlock();
Johannes Berg89a54e42012-06-15 14:33:17 +02007024 return PTR_ERR(wdev);
Johannes Berg4c476992010-10-04 21:36:35 +02007025 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007026
Johannes Berg89a54e42012-06-15 14:33:17 +02007027 dev = wdev->netdev;
7028 rdev = wiphy_to_dev(wdev->wiphy);
7029
Johannes Berg1bf614e2012-06-15 15:23:36 +02007030 if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
7031 if (!dev) {
7032 mutex_unlock(&cfg80211_mutex);
7033 if (rtnl)
7034 rtnl_unlock();
7035 return -EINVAL;
7036 }
7037
7038 info->user_ptr[1] = dev;
7039 } else {
7040 info->user_ptr[1] = wdev;
Johannes Berg41265712010-10-04 21:14:05 +02007041 }
Johannes Berg89a54e42012-06-15 14:33:17 +02007042
Johannes Berg1bf614e2012-06-15 15:23:36 +02007043 if (dev) {
7044 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
7045 !netif_running(dev)) {
7046 mutex_unlock(&cfg80211_mutex);
7047 if (rtnl)
7048 rtnl_unlock();
7049 return -ENETDOWN;
7050 }
7051
7052 dev_hold(dev);
Johannes Berg98104fde2012-06-16 00:19:54 +02007053 } else if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP) {
7054 if (!wdev->p2p_started) {
7055 mutex_unlock(&cfg80211_mutex);
7056 if (rtnl)
7057 rtnl_unlock();
7058 return -ENETDOWN;
7059 }
Johannes Berg1bf614e2012-06-15 15:23:36 +02007060 }
7061
Johannes Berg89a54e42012-06-15 14:33:17 +02007062 cfg80211_lock_rdev(rdev);
7063
7064 mutex_unlock(&cfg80211_mutex);
7065
Johannes Berg4c476992010-10-04 21:36:35 +02007066 info->user_ptr[0] = rdev;
Johannes Berg4c476992010-10-04 21:36:35 +02007067 }
7068
7069 return 0;
7070}
7071
7072static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
7073 struct genl_info *info)
7074{
7075 if (info->user_ptr[0])
7076 cfg80211_unlock_rdev(info->user_ptr[0]);
Johannes Berg1bf614e2012-06-15 15:23:36 +02007077 if (info->user_ptr[1]) {
7078 if (ops->internal_flags & NL80211_FLAG_NEED_WDEV) {
7079 struct wireless_dev *wdev = info->user_ptr[1];
7080
7081 if (wdev->netdev)
7082 dev_put(wdev->netdev);
7083 } else {
7084 dev_put(info->user_ptr[1]);
7085 }
7086 }
Johannes Berg4c476992010-10-04 21:36:35 +02007087 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
7088 rtnl_unlock();
7089}
7090
Johannes Berg55682962007-09-20 13:09:35 -04007091static struct genl_ops nl80211_ops[] = {
7092 {
7093 .cmd = NL80211_CMD_GET_WIPHY,
7094 .doit = nl80211_get_wiphy,
7095 .dumpit = nl80211_dump_wiphy,
7096 .policy = nl80211_policy,
7097 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007098 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04007099 },
7100 {
7101 .cmd = NL80211_CMD_SET_WIPHY,
7102 .doit = nl80211_set_wiphy,
7103 .policy = nl80211_policy,
7104 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007105 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007106 },
7107 {
7108 .cmd = NL80211_CMD_GET_INTERFACE,
7109 .doit = nl80211_get_interface,
7110 .dumpit = nl80211_dump_interface,
7111 .policy = nl80211_policy,
7112 /* can be retrieved by unprivileged users */
Johannes Berg72fb2ab2012-06-15 17:52:47 +02007113 .internal_flags = NL80211_FLAG_NEED_WDEV,
Johannes Berg55682962007-09-20 13:09:35 -04007114 },
7115 {
7116 .cmd = NL80211_CMD_SET_INTERFACE,
7117 .doit = nl80211_set_interface,
7118 .policy = nl80211_policy,
7119 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007120 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7121 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007122 },
7123 {
7124 .cmd = NL80211_CMD_NEW_INTERFACE,
7125 .doit = nl80211_new_interface,
7126 .policy = nl80211_policy,
7127 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007128 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7129 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007130 },
7131 {
7132 .cmd = NL80211_CMD_DEL_INTERFACE,
7133 .doit = nl80211_del_interface,
7134 .policy = nl80211_policy,
7135 .flags = GENL_ADMIN_PERM,
Johannes Berg84efbb82012-06-16 00:00:26 +02007136 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007137 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04007138 },
Johannes Berg41ade002007-12-19 02:03:29 +01007139 {
7140 .cmd = NL80211_CMD_GET_KEY,
7141 .doit = nl80211_get_key,
7142 .policy = nl80211_policy,
7143 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007144 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007145 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007146 },
7147 {
7148 .cmd = NL80211_CMD_SET_KEY,
7149 .doit = nl80211_set_key,
7150 .policy = nl80211_policy,
7151 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007152 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007153 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007154 },
7155 {
7156 .cmd = NL80211_CMD_NEW_KEY,
7157 .doit = nl80211_new_key,
7158 .policy = nl80211_policy,
7159 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007160 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007161 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007162 },
7163 {
7164 .cmd = NL80211_CMD_DEL_KEY,
7165 .doit = nl80211_del_key,
7166 .policy = nl80211_policy,
7167 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007168 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007169 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01007170 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01007171 {
7172 .cmd = NL80211_CMD_SET_BEACON,
7173 .policy = nl80211_policy,
7174 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007175 .doit = nl80211_set_beacon,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007176 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007177 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007178 },
7179 {
Johannes Berg88600202012-02-13 15:17:18 +01007180 .cmd = NL80211_CMD_START_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007181 .policy = nl80211_policy,
7182 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007183 .doit = nl80211_start_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007184 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007185 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007186 },
7187 {
Johannes Berg88600202012-02-13 15:17:18 +01007188 .cmd = NL80211_CMD_STOP_AP,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007189 .policy = nl80211_policy,
7190 .flags = GENL_ADMIN_PERM,
Johannes Berg88600202012-02-13 15:17:18 +01007191 .doit = nl80211_stop_ap,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007192 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007193 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01007194 },
Johannes Berg5727ef12007-12-19 02:03:34 +01007195 {
7196 .cmd = NL80211_CMD_GET_STATION,
7197 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007198 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01007199 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02007200 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7201 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007202 },
7203 {
7204 .cmd = NL80211_CMD_SET_STATION,
7205 .doit = nl80211_set_station,
7206 .policy = nl80211_policy,
7207 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007208 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007209 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007210 },
7211 {
7212 .cmd = NL80211_CMD_NEW_STATION,
7213 .doit = nl80211_new_station,
7214 .policy = nl80211_policy,
7215 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007216 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007217 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007218 },
7219 {
7220 .cmd = NL80211_CMD_DEL_STATION,
7221 .doit = nl80211_del_station,
7222 .policy = nl80211_policy,
7223 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007224 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007225 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01007226 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007227 {
7228 .cmd = NL80211_CMD_GET_MPATH,
7229 .doit = nl80211_get_mpath,
7230 .dumpit = nl80211_dump_mpath,
7231 .policy = nl80211_policy,
7232 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007233 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007234 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007235 },
7236 {
7237 .cmd = NL80211_CMD_SET_MPATH,
7238 .doit = nl80211_set_mpath,
7239 .policy = nl80211_policy,
7240 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007241 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007242 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007243 },
7244 {
7245 .cmd = NL80211_CMD_NEW_MPATH,
7246 .doit = nl80211_new_mpath,
7247 .policy = nl80211_policy,
7248 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007249 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007250 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007251 },
7252 {
7253 .cmd = NL80211_CMD_DEL_MPATH,
7254 .doit = nl80211_del_mpath,
7255 .policy = nl80211_policy,
7256 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007257 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007258 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01007259 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007260 {
7261 .cmd = NL80211_CMD_SET_BSS,
7262 .doit = nl80211_set_bss,
7263 .policy = nl80211_policy,
7264 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007265 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007266 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03007267 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007268 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08007269 .cmd = NL80211_CMD_GET_REG,
7270 .doit = nl80211_get_reg,
7271 .policy = nl80211_policy,
7272 /* can be retrieved by unprivileged users */
7273 },
7274 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07007275 .cmd = NL80211_CMD_SET_REG,
7276 .doit = nl80211_set_reg,
7277 .policy = nl80211_policy,
7278 .flags = GENL_ADMIN_PERM,
7279 },
7280 {
7281 .cmd = NL80211_CMD_REQ_SET_REG,
7282 .doit = nl80211_req_set_reg,
7283 .policy = nl80211_policy,
7284 .flags = GENL_ADMIN_PERM,
7285 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007286 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007287 .cmd = NL80211_CMD_GET_MESH_CONFIG,
7288 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007289 .policy = nl80211_policy,
7290 /* can be retrieved by unprivileged users */
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007291 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007292 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007293 },
7294 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08007295 .cmd = NL80211_CMD_SET_MESH_CONFIG,
7296 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007297 .policy = nl80211_policy,
7298 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01007299 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007300 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07007301 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02007302 {
Johannes Berg2a519312009-02-10 21:25:55 +01007303 .cmd = NL80211_CMD_TRIGGER_SCAN,
7304 .doit = nl80211_trigger_scan,
7305 .policy = nl80211_policy,
7306 .flags = GENL_ADMIN_PERM,
Johannes Bergfd014282012-06-18 19:17:03 +02007307 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007308 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01007309 },
7310 {
7311 .cmd = NL80211_CMD_GET_SCAN,
7312 .policy = nl80211_policy,
7313 .dumpit = nl80211_dump_scan,
7314 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02007315 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03007316 .cmd = NL80211_CMD_START_SCHED_SCAN,
7317 .doit = nl80211_start_sched_scan,
7318 .policy = nl80211_policy,
7319 .flags = GENL_ADMIN_PERM,
7320 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7321 NL80211_FLAG_NEED_RTNL,
7322 },
7323 {
7324 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
7325 .doit = nl80211_stop_sched_scan,
7326 .policy = nl80211_policy,
7327 .flags = GENL_ADMIN_PERM,
7328 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7329 NL80211_FLAG_NEED_RTNL,
7330 },
7331 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02007332 .cmd = NL80211_CMD_AUTHENTICATE,
7333 .doit = nl80211_authenticate,
7334 .policy = nl80211_policy,
7335 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007336 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007337 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007338 },
7339 {
7340 .cmd = NL80211_CMD_ASSOCIATE,
7341 .doit = nl80211_associate,
7342 .policy = nl80211_policy,
7343 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007344 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007345 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007346 },
7347 {
7348 .cmd = NL80211_CMD_DEAUTHENTICATE,
7349 .doit = nl80211_deauthenticate,
7350 .policy = nl80211_policy,
7351 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007352 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007353 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007354 },
7355 {
7356 .cmd = NL80211_CMD_DISASSOCIATE,
7357 .doit = nl80211_disassociate,
7358 .policy = nl80211_policy,
7359 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007360 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007361 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02007362 },
Johannes Berg04a773a2009-04-19 21:24:32 +02007363 {
7364 .cmd = NL80211_CMD_JOIN_IBSS,
7365 .doit = nl80211_join_ibss,
7366 .policy = nl80211_policy,
7367 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007368 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007369 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007370 },
7371 {
7372 .cmd = NL80211_CMD_LEAVE_IBSS,
7373 .doit = nl80211_leave_ibss,
7374 .policy = nl80211_policy,
7375 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007376 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007377 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02007378 },
Johannes Bergaff89a92009-07-01 21:26:51 +02007379#ifdef CONFIG_NL80211_TESTMODE
7380 {
7381 .cmd = NL80211_CMD_TESTMODE,
7382 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07007383 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02007384 .policy = nl80211_policy,
7385 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007386 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7387 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02007388 },
7389#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02007390 {
7391 .cmd = NL80211_CMD_CONNECT,
7392 .doit = nl80211_connect,
7393 .policy = nl80211_policy,
7394 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007395 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007396 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007397 },
7398 {
7399 .cmd = NL80211_CMD_DISCONNECT,
7400 .doit = nl80211_disconnect,
7401 .policy = nl80211_policy,
7402 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02007403 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007404 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02007405 },
Johannes Berg463d0182009-07-14 00:33:35 +02007406 {
7407 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
7408 .doit = nl80211_wiphy_netns,
7409 .policy = nl80211_policy,
7410 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007411 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7412 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02007413 },
Holger Schurig61fa7132009-11-11 12:25:40 +01007414 {
7415 .cmd = NL80211_CMD_GET_SURVEY,
7416 .policy = nl80211_policy,
7417 .dumpit = nl80211_dump_survey,
7418 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007419 {
7420 .cmd = NL80211_CMD_SET_PMKSA,
7421 .doit = nl80211_setdel_pmksa,
7422 .policy = nl80211_policy,
7423 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007424 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007425 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007426 },
7427 {
7428 .cmd = NL80211_CMD_DEL_PMKSA,
7429 .doit = nl80211_setdel_pmksa,
7430 .policy = nl80211_policy,
7431 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007432 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007433 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007434 },
7435 {
7436 .cmd = NL80211_CMD_FLUSH_PMKSA,
7437 .doit = nl80211_flush_pmksa,
7438 .policy = nl80211_policy,
7439 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007440 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007441 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01007442 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007443 {
7444 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
7445 .doit = nl80211_remain_on_channel,
7446 .policy = nl80211_policy,
7447 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007448 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007449 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007450 },
7451 {
7452 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
7453 .doit = nl80211_cancel_remain_on_channel,
7454 .policy = nl80211_policy,
7455 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007456 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007457 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007458 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007459 {
7460 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
7461 .doit = nl80211_set_tx_bitrate_mask,
7462 .policy = nl80211_policy,
7463 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007464 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7465 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02007466 },
Jouni Malinen026331c2010-02-15 12:53:10 +02007467 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007468 .cmd = NL80211_CMD_REGISTER_FRAME,
7469 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007470 .policy = nl80211_policy,
7471 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007472 .internal_flags = NL80211_FLAG_NEED_WDEV |
Johannes Berg4c476992010-10-04 21:36:35 +02007473 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007474 },
7475 {
Johannes Berg2e161f72010-08-12 15:38:38 +02007476 .cmd = NL80211_CMD_FRAME,
7477 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02007478 .policy = nl80211_policy,
7479 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007480 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02007481 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02007482 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02007483 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007484 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
7485 .doit = nl80211_tx_mgmt_cancel_wait,
7486 .policy = nl80211_policy,
7487 .flags = GENL_ADMIN_PERM,
Johannes Berg71bbc992012-06-15 15:30:18 +02007488 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
Johannes Bergf7ca38d2010-11-25 10:02:29 +01007489 NL80211_FLAG_NEED_RTNL,
7490 },
7491 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02007492 .cmd = NL80211_CMD_SET_POWER_SAVE,
7493 .doit = nl80211_set_power_save,
7494 .policy = nl80211_policy,
7495 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007496 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7497 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007498 },
7499 {
7500 .cmd = NL80211_CMD_GET_POWER_SAVE,
7501 .doit = nl80211_get_power_save,
7502 .policy = nl80211_policy,
7503 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02007504 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7505 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02007506 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007507 {
7508 .cmd = NL80211_CMD_SET_CQM,
7509 .doit = nl80211_set_cqm,
7510 .policy = nl80211_policy,
7511 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007512 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7513 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007514 },
Johannes Bergf444de02010-05-05 15:25:02 +02007515 {
7516 .cmd = NL80211_CMD_SET_CHANNEL,
7517 .doit = nl80211_set_channel,
7518 .policy = nl80211_policy,
7519 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02007520 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7521 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02007522 },
Bill Jordane8347eb2010-10-01 13:54:28 -04007523 {
7524 .cmd = NL80211_CMD_SET_WDS_PEER,
7525 .doit = nl80211_set_wds_peer,
7526 .policy = nl80211_policy,
7527 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02007528 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7529 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04007530 },
Johannes Berg29cbe682010-12-03 09:20:44 +01007531 {
7532 .cmd = NL80211_CMD_JOIN_MESH,
7533 .doit = nl80211_join_mesh,
7534 .policy = nl80211_policy,
7535 .flags = GENL_ADMIN_PERM,
7536 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7537 NL80211_FLAG_NEED_RTNL,
7538 },
7539 {
7540 .cmd = NL80211_CMD_LEAVE_MESH,
7541 .doit = nl80211_leave_mesh,
7542 .policy = nl80211_policy,
7543 .flags = GENL_ADMIN_PERM,
7544 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7545 NL80211_FLAG_NEED_RTNL,
7546 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007547#ifdef CONFIG_PM
Johannes Bergff1b6e62011-05-04 15:37:28 +02007548 {
7549 .cmd = NL80211_CMD_GET_WOWLAN,
7550 .doit = nl80211_get_wowlan,
7551 .policy = nl80211_policy,
7552 /* can be retrieved by unprivileged users */
7553 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7554 NL80211_FLAG_NEED_RTNL,
7555 },
7556 {
7557 .cmd = NL80211_CMD_SET_WOWLAN,
7558 .doit = nl80211_set_wowlan,
7559 .policy = nl80211_policy,
7560 .flags = GENL_ADMIN_PERM,
7561 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7562 NL80211_FLAG_NEED_RTNL,
7563 },
Johannes Bergdfb89c52012-06-27 09:23:48 +02007564#endif
Johannes Berge5497d72011-07-05 16:35:40 +02007565 {
7566 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
7567 .doit = nl80211_set_rekey_data,
7568 .policy = nl80211_policy,
7569 .flags = GENL_ADMIN_PERM,
7570 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7571 NL80211_FLAG_NEED_RTNL,
7572 },
Arik Nemtsov109086c2011-09-28 14:12:50 +03007573 {
7574 .cmd = NL80211_CMD_TDLS_MGMT,
7575 .doit = nl80211_tdls_mgmt,
7576 .policy = nl80211_policy,
7577 .flags = GENL_ADMIN_PERM,
7578 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7579 NL80211_FLAG_NEED_RTNL,
7580 },
7581 {
7582 .cmd = NL80211_CMD_TDLS_OPER,
7583 .doit = nl80211_tdls_oper,
7584 .policy = nl80211_policy,
7585 .flags = GENL_ADMIN_PERM,
7586 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
7587 NL80211_FLAG_NEED_RTNL,
7588 },
Johannes Berg28946da2011-11-04 11:18:12 +01007589 {
7590 .cmd = NL80211_CMD_UNEXPECTED_FRAME,
7591 .doit = nl80211_register_unexpected_frame,
7592 .policy = nl80211_policy,
7593 .flags = GENL_ADMIN_PERM,
7594 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7595 NL80211_FLAG_NEED_RTNL,
7596 },
Johannes Berg7f6cf312011-11-04 11:18:15 +01007597 {
7598 .cmd = NL80211_CMD_PROBE_CLIENT,
7599 .doit = nl80211_probe_client,
7600 .policy = nl80211_policy,
7601 .flags = GENL_ADMIN_PERM,
Johannes Berg2b5f8b02012-04-02 10:51:55 +02007602 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg7f6cf312011-11-04 11:18:15 +01007603 NL80211_FLAG_NEED_RTNL,
7604 },
Johannes Berg5e760232011-11-04 11:18:17 +01007605 {
7606 .cmd = NL80211_CMD_REGISTER_BEACONS,
7607 .doit = nl80211_register_beacons,
7608 .policy = nl80211_policy,
7609 .flags = GENL_ADMIN_PERM,
7610 .internal_flags = NL80211_FLAG_NEED_WIPHY |
7611 NL80211_FLAG_NEED_RTNL,
7612 },
Simon Wunderlich1d9d9212011-11-18 14:20:43 +01007613 {
7614 .cmd = NL80211_CMD_SET_NOACK_MAP,
7615 .doit = nl80211_set_noack_map,
7616 .policy = nl80211_policy,
7617 .flags = GENL_ADMIN_PERM,
7618 .internal_flags = NL80211_FLAG_NEED_NETDEV |
7619 NL80211_FLAG_NEED_RTNL,
7620 },
Johannes Berg98104fde2012-06-16 00:19:54 +02007621 {
7622 .cmd = NL80211_CMD_START_P2P_DEVICE,
7623 .doit = nl80211_start_p2p_device,
7624 .policy = nl80211_policy,
7625 .flags = GENL_ADMIN_PERM,
7626 .internal_flags = NL80211_FLAG_NEED_WDEV |
7627 NL80211_FLAG_NEED_RTNL,
7628 },
7629 {
7630 .cmd = NL80211_CMD_STOP_P2P_DEVICE,
7631 .doit = nl80211_stop_p2p_device,
7632 .policy = nl80211_policy,
7633 .flags = GENL_ADMIN_PERM,
7634 .internal_flags = NL80211_FLAG_NEED_WDEV_UP |
7635 NL80211_FLAG_NEED_RTNL,
7636 },
Johannes Berg55682962007-09-20 13:09:35 -04007637};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01007638
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007639static struct genl_multicast_group nl80211_mlme_mcgrp = {
7640 .name = "mlme",
7641};
Johannes Berg55682962007-09-20 13:09:35 -04007642
7643/* multicast groups */
7644static struct genl_multicast_group nl80211_config_mcgrp = {
7645 .name = "config",
7646};
Johannes Berg2a519312009-02-10 21:25:55 +01007647static struct genl_multicast_group nl80211_scan_mcgrp = {
7648 .name = "scan",
7649};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007650static struct genl_multicast_group nl80211_regulatory_mcgrp = {
7651 .name = "regulatory",
7652};
Johannes Berg55682962007-09-20 13:09:35 -04007653
7654/* notification functions */
7655
7656void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
7657{
7658 struct sk_buff *msg;
7659
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007660 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007661 if (!msg)
7662 return;
7663
7664 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
7665 nlmsg_free(msg);
7666 return;
7667 }
7668
Johannes Berg463d0182009-07-14 00:33:35 +02007669 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7670 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04007671}
7672
Johannes Berg362a4152009-05-24 16:43:15 +02007673static int nl80211_add_scan_req(struct sk_buff *msg,
7674 struct cfg80211_registered_device *rdev)
7675{
7676 struct cfg80211_scan_request *req = rdev->scan_req;
7677 struct nlattr *nest;
7678 int i;
7679
Johannes Berg667503d2009-07-07 03:56:11 +02007680 ASSERT_RDEV_LOCK(rdev);
7681
Johannes Berg362a4152009-05-24 16:43:15 +02007682 if (WARN_ON(!req))
7683 return 0;
7684
7685 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
7686 if (!nest)
7687 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007688 for (i = 0; i < req->n_ssids; i++) {
7689 if (nla_put(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
7690 goto nla_put_failure;
7691 }
Johannes Berg362a4152009-05-24 16:43:15 +02007692 nla_nest_end(msg, nest);
7693
7694 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
7695 if (!nest)
7696 goto nla_put_failure;
David S. Miller9360ffd2012-03-29 04:41:26 -04007697 for (i = 0; i < req->n_channels; i++) {
7698 if (nla_put_u32(msg, i, req->channels[i]->center_freq))
7699 goto nla_put_failure;
7700 }
Johannes Berg362a4152009-05-24 16:43:15 +02007701 nla_nest_end(msg, nest);
7702
David S. Miller9360ffd2012-03-29 04:41:26 -04007703 if (req->ie &&
7704 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
7705 goto nla_put_failure;
Johannes Berg362a4152009-05-24 16:43:15 +02007706
Sam Lefflered4737712012-10-11 21:03:31 -07007707 if (req->flags)
7708 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags);
7709
Johannes Berg362a4152009-05-24 16:43:15 +02007710 return 0;
7711 nla_put_failure:
7712 return -ENOBUFS;
7713}
7714
Johannes Berga538e2d2009-06-16 19:56:42 +02007715static int nl80211_send_scan_msg(struct sk_buff *msg,
7716 struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007717 struct wireless_dev *wdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007718 u32 portid, u32 seq, int flags,
Johannes Berga538e2d2009-06-16 19:56:42 +02007719 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01007720{
7721 void *hdr;
7722
Eric W. Biederman15e47302012-09-07 20:12:54 +00007723 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Johannes Berg2a519312009-02-10 21:25:55 +01007724 if (!hdr)
7725 return -1;
7726
David S. Miller9360ffd2012-03-29 04:41:26 -04007727 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Bergfd014282012-06-18 19:17:03 +02007728 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
7729 wdev->netdev->ifindex)) ||
7730 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)))
David S. Miller9360ffd2012-03-29 04:41:26 -04007731 goto nla_put_failure;
Johannes Berg2a519312009-02-10 21:25:55 +01007732
Johannes Berg362a4152009-05-24 16:43:15 +02007733 /* ignore errors and send incomplete event anyway */
7734 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01007735
7736 return genlmsg_end(msg, hdr);
7737
7738 nla_put_failure:
7739 genlmsg_cancel(msg, hdr);
7740 return -EMSGSIZE;
7741}
7742
Luciano Coelho807f8a82011-05-11 17:09:35 +03007743static int
7744nl80211_send_sched_scan_msg(struct sk_buff *msg,
7745 struct cfg80211_registered_device *rdev,
7746 struct net_device *netdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00007747 u32 portid, u32 seq, int flags, u32 cmd)
Luciano Coelho807f8a82011-05-11 17:09:35 +03007748{
7749 void *hdr;
7750
Eric W. Biederman15e47302012-09-07 20:12:54 +00007751 hdr = nl80211hdr_put(msg, portid, seq, flags, cmd);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007752 if (!hdr)
7753 return -1;
7754
David S. Miller9360ffd2012-03-29 04:41:26 -04007755 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7756 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
7757 goto nla_put_failure;
Luciano Coelho807f8a82011-05-11 17:09:35 +03007758
7759 return genlmsg_end(msg, hdr);
7760
7761 nla_put_failure:
7762 genlmsg_cancel(msg, hdr);
7763 return -EMSGSIZE;
7764}
7765
Johannes Berga538e2d2009-06-16 19:56:42 +02007766void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007767 struct wireless_dev *wdev)
Johannes Berga538e2d2009-06-16 19:56:42 +02007768{
7769 struct sk_buff *msg;
7770
Thomas Graf58050fc2012-06-28 03:57:45 +00007771 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007772 if (!msg)
7773 return;
7774
Johannes Bergfd014282012-06-18 19:17:03 +02007775 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007776 NL80211_CMD_TRIGGER_SCAN) < 0) {
7777 nlmsg_free(msg);
7778 return;
7779 }
7780
Johannes Berg463d0182009-07-14 00:33:35 +02007781 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7782 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02007783}
7784
Johannes Berg2a519312009-02-10 21:25:55 +01007785void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007786 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007787{
7788 struct sk_buff *msg;
7789
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007790 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007791 if (!msg)
7792 return;
7793
Johannes Bergfd014282012-06-18 19:17:03 +02007794 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007795 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007796 nlmsg_free(msg);
7797 return;
7798 }
7799
Johannes Berg463d0182009-07-14 00:33:35 +02007800 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7801 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007802}
7803
7804void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
Johannes Bergfd014282012-06-18 19:17:03 +02007805 struct wireless_dev *wdev)
Johannes Berg2a519312009-02-10 21:25:55 +01007806{
7807 struct sk_buff *msg;
7808
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007809 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007810 if (!msg)
7811 return;
7812
Johannes Bergfd014282012-06-18 19:17:03 +02007813 if (nl80211_send_scan_msg(msg, rdev, wdev, 0, 0, 0,
Johannes Berga538e2d2009-06-16 19:56:42 +02007814 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01007815 nlmsg_free(msg);
7816 return;
7817 }
7818
Johannes Berg463d0182009-07-14 00:33:35 +02007819 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7820 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01007821}
7822
Luciano Coelho807f8a82011-05-11 17:09:35 +03007823void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
7824 struct net_device *netdev)
7825{
7826 struct sk_buff *msg;
7827
7828 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
7829 if (!msg)
7830 return;
7831
7832 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
7833 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
7834 nlmsg_free(msg);
7835 return;
7836 }
7837
7838 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7839 nl80211_scan_mcgrp.id, GFP_KERNEL);
7840}
7841
7842void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
7843 struct net_device *netdev, u32 cmd)
7844{
7845 struct sk_buff *msg;
7846
Thomas Graf58050fc2012-06-28 03:57:45 +00007847 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luciano Coelho807f8a82011-05-11 17:09:35 +03007848 if (!msg)
7849 return;
7850
7851 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
7852 nlmsg_free(msg);
7853 return;
7854 }
7855
7856 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7857 nl80211_scan_mcgrp.id, GFP_KERNEL);
7858}
7859
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007860/*
7861 * This can happen on global regulatory changes or device specific settings
7862 * based on custom world regulatory domains.
7863 */
7864void nl80211_send_reg_change_event(struct regulatory_request *request)
7865{
7866 struct sk_buff *msg;
7867 void *hdr;
7868
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07007869 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007870 if (!msg)
7871 return;
7872
7873 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
7874 if (!hdr) {
7875 nlmsg_free(msg);
7876 return;
7877 }
7878
7879 /* Userspace can always count this one always being set */
David S. Miller9360ffd2012-03-29 04:41:26 -04007880 if (nla_put_u8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator))
7881 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007882
David S. Miller9360ffd2012-03-29 04:41:26 -04007883 if (request->alpha2[0] == '0' && request->alpha2[1] == '0') {
7884 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7885 NL80211_REGDOM_TYPE_WORLD))
7886 goto nla_put_failure;
7887 } else if (request->alpha2[0] == '9' && request->alpha2[1] == '9') {
7888 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7889 NL80211_REGDOM_TYPE_CUSTOM_WORLD))
7890 goto nla_put_failure;
7891 } else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
7892 request->intersect) {
7893 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7894 NL80211_REGDOM_TYPE_INTERSECTION))
7895 goto nla_put_failure;
7896 } else {
7897 if (nla_put_u8(msg, NL80211_ATTR_REG_TYPE,
7898 NL80211_REGDOM_TYPE_COUNTRY) ||
7899 nla_put_string(msg, NL80211_ATTR_REG_ALPHA2,
7900 request->alpha2))
7901 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007902 }
7903
David S. Miller9360ffd2012-03-29 04:41:26 -04007904 if (wiphy_idx_valid(request->wiphy_idx) &&
7905 nla_put_u32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx))
7906 goto nla_put_failure;
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007907
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007908 genlmsg_end(msg, hdr);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007909
Johannes Bergbc43b282009-07-25 10:54:13 +02007910 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02007911 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02007912 GFP_ATOMIC);
7913 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007914
7915 return;
7916
7917nla_put_failure:
7918 genlmsg_cancel(msg, hdr);
7919 nlmsg_free(msg);
7920}
7921
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007922static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
7923 struct net_device *netdev,
7924 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007925 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007926{
7927 struct sk_buff *msg;
7928 void *hdr;
7929
Johannes Berge6d6e342009-07-01 21:26:47 +02007930 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007931 if (!msg)
7932 return;
7933
7934 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
7935 if (!hdr) {
7936 nlmsg_free(msg);
7937 return;
7938 }
7939
David S. Miller9360ffd2012-03-29 04:41:26 -04007940 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
7941 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
7942 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
7943 goto nla_put_failure;
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007944
Johannes Berg3b7b72e2011-10-22 19:05:51 +02007945 genlmsg_end(msg, hdr);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007946
Johannes Berg463d0182009-07-14 00:33:35 +02007947 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7948 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007949 return;
7950
7951 nla_put_failure:
7952 genlmsg_cancel(msg, hdr);
7953 nlmsg_free(msg);
7954}
7955
7956void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007957 struct net_device *netdev, const u8 *buf,
7958 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007959{
7960 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007961 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007962}
7963
7964void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
7965 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007966 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007967{
Johannes Berge6d6e342009-07-01 21:26:47 +02007968 nl80211_send_mlme_event(rdev, netdev, buf, len,
7969 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007970}
7971
Jouni Malinen53b46b82009-03-27 20:53:56 +02007972void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02007973 struct net_device *netdev, const u8 *buf,
7974 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007975{
7976 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007977 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007978}
7979
Jouni Malinen53b46b82009-03-27 20:53:56 +02007980void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
7981 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02007982 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007983{
7984 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02007985 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007986}
7987
Jouni Malinencf4e5942010-12-16 00:52:40 +02007988void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
7989 struct net_device *netdev, const u8 *buf,
7990 size_t len, gfp_t gfp)
7991{
7992 nl80211_send_mlme_event(rdev, netdev, buf, len,
7993 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
7994}
7995
7996void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
7997 struct net_device *netdev, const u8 *buf,
7998 size_t len, gfp_t gfp)
7999{
8000 nl80211_send_mlme_event(rdev, netdev, buf, len,
8001 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
8002}
8003
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04008004static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
8005 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02008006 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008007{
8008 struct sk_buff *msg;
8009 void *hdr;
8010
Johannes Berge6d6e342009-07-01 21:26:47 +02008011 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008012 if (!msg)
8013 return;
8014
8015 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8016 if (!hdr) {
8017 nlmsg_free(msg);
8018 return;
8019 }
8020
David S. Miller9360ffd2012-03-29 04:41:26 -04008021 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8022 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8023 nla_put_flag(msg, NL80211_ATTR_TIMED_OUT) ||
8024 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8025 goto nla_put_failure;
Jouni Malinen1965c852009-04-22 21:38:25 +03008026
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008027 genlmsg_end(msg, hdr);
Jouni Malinen1965c852009-04-22 21:38:25 +03008028
Johannes Berg463d0182009-07-14 00:33:35 +02008029 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8030 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008031 return;
8032
8033 nla_put_failure:
8034 genlmsg_cancel(msg, hdr);
8035 nlmsg_free(msg);
8036}
8037
8038void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008039 struct net_device *netdev, const u8 *addr,
8040 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008041{
8042 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02008043 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008044}
8045
8046void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02008047 struct net_device *netdev, const u8 *addr,
8048 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03008049{
Johannes Berge6d6e342009-07-01 21:26:47 +02008050 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
8051 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03008052}
8053
Samuel Ortizb23aa672009-07-01 21:26:54 +02008054void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
8055 struct net_device *netdev, const u8 *bssid,
8056 const u8 *req_ie, size_t req_ie_len,
8057 const u8 *resp_ie, size_t resp_ie_len,
8058 u16 status, gfp_t gfp)
8059{
8060 struct sk_buff *msg;
8061 void *hdr;
8062
Thomas Graf58050fc2012-06-28 03:57:45 +00008063 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008064 if (!msg)
8065 return;
8066
8067 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
8068 if (!hdr) {
8069 nlmsg_free(msg);
8070 return;
8071 }
8072
David S. Miller9360ffd2012-03-29 04:41:26 -04008073 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8074 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8075 (bssid && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid)) ||
8076 nla_put_u16(msg, NL80211_ATTR_STATUS_CODE, status) ||
8077 (req_ie &&
8078 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8079 (resp_ie &&
8080 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8081 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008082
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008083 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008084
Johannes Berg463d0182009-07-14 00:33:35 +02008085 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8086 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008087 return;
8088
8089 nla_put_failure:
8090 genlmsg_cancel(msg, hdr);
8091 nlmsg_free(msg);
8092
8093}
8094
8095void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
8096 struct net_device *netdev, const u8 *bssid,
8097 const u8 *req_ie, size_t req_ie_len,
8098 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
8099{
8100 struct sk_buff *msg;
8101 void *hdr;
8102
Thomas Graf58050fc2012-06-28 03:57:45 +00008103 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008104 if (!msg)
8105 return;
8106
8107 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
8108 if (!hdr) {
8109 nlmsg_free(msg);
8110 return;
8111 }
8112
David S. Miller9360ffd2012-03-29 04:41:26 -04008113 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8114 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8115 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid) ||
8116 (req_ie &&
8117 nla_put(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie)) ||
8118 (resp_ie &&
8119 nla_put(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie)))
8120 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008121
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008122 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008123
Johannes Berg463d0182009-07-14 00:33:35 +02008124 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8125 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008126 return;
8127
8128 nla_put_failure:
8129 genlmsg_cancel(msg, hdr);
8130 nlmsg_free(msg);
8131
8132}
8133
8134void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
8135 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02008136 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02008137{
8138 struct sk_buff *msg;
8139 void *hdr;
8140
Thomas Graf58050fc2012-06-28 03:57:45 +00008141 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008142 if (!msg)
8143 return;
8144
8145 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
8146 if (!hdr) {
8147 nlmsg_free(msg);
8148 return;
8149 }
8150
David S. Miller9360ffd2012-03-29 04:41:26 -04008151 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8152 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8153 (from_ap && reason &&
8154 nla_put_u16(msg, NL80211_ATTR_REASON_CODE, reason)) ||
8155 (from_ap &&
8156 nla_put_flag(msg, NL80211_ATTR_DISCONNECTED_BY_AP)) ||
8157 (ie && nla_put(msg, NL80211_ATTR_IE, ie_len, ie)))
8158 goto nla_put_failure;
Samuel Ortizb23aa672009-07-01 21:26:54 +02008159
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008160 genlmsg_end(msg, hdr);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008161
Johannes Berg463d0182009-07-14 00:33:35 +02008162 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8163 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02008164 return;
8165
8166 nla_put_failure:
8167 genlmsg_cancel(msg, hdr);
8168 nlmsg_free(msg);
8169
8170}
8171
Johannes Berg04a773a2009-04-19 21:24:32 +02008172void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
8173 struct net_device *netdev, const u8 *bssid,
8174 gfp_t gfp)
8175{
8176 struct sk_buff *msg;
8177 void *hdr;
8178
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008179 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008180 if (!msg)
8181 return;
8182
8183 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
8184 if (!hdr) {
8185 nlmsg_free(msg);
8186 return;
8187 }
8188
David S. Miller9360ffd2012-03-29 04:41:26 -04008189 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8190 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8191 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8192 goto nla_put_failure;
Johannes Berg04a773a2009-04-19 21:24:32 +02008193
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008194 genlmsg_end(msg, hdr);
Johannes Berg04a773a2009-04-19 21:24:32 +02008195
Johannes Berg463d0182009-07-14 00:33:35 +02008196 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8197 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02008198 return;
8199
8200 nla_put_failure:
8201 genlmsg_cancel(msg, hdr);
8202 nlmsg_free(msg);
8203}
8204
Javier Cardonac93b5e72011-04-07 15:08:34 -07008205void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
8206 struct net_device *netdev,
8207 const u8 *macaddr, const u8* ie, u8 ie_len,
8208 gfp_t gfp)
8209{
8210 struct sk_buff *msg;
8211 void *hdr;
8212
8213 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8214 if (!msg)
8215 return;
8216
8217 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
8218 if (!hdr) {
8219 nlmsg_free(msg);
8220 return;
8221 }
8222
David S. Miller9360ffd2012-03-29 04:41:26 -04008223 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8224 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8225 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr) ||
8226 (ie_len && ie &&
8227 nla_put(msg, NL80211_ATTR_IE, ie_len , ie)))
8228 goto nla_put_failure;
Javier Cardonac93b5e72011-04-07 15:08:34 -07008229
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008230 genlmsg_end(msg, hdr);
Javier Cardonac93b5e72011-04-07 15:08:34 -07008231
8232 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8233 nl80211_mlme_mcgrp.id, gfp);
8234 return;
8235
8236 nla_put_failure:
8237 genlmsg_cancel(msg, hdr);
8238 nlmsg_free(msg);
8239}
8240
Jouni Malinena3b8b052009-03-27 21:59:49 +02008241void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
8242 struct net_device *netdev, const u8 *addr,
8243 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02008244 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02008245{
8246 struct sk_buff *msg;
8247 void *hdr;
8248
Johannes Berge6d6e342009-07-01 21:26:47 +02008249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008250 if (!msg)
8251 return;
8252
8253 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
8254 if (!hdr) {
8255 nlmsg_free(msg);
8256 return;
8257 }
8258
David S. Miller9360ffd2012-03-29 04:41:26 -04008259 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8260 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8261 (addr && nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr)) ||
8262 nla_put_u32(msg, NL80211_ATTR_KEY_TYPE, key_type) ||
8263 (key_id != -1 &&
8264 nla_put_u8(msg, NL80211_ATTR_KEY_IDX, key_id)) ||
8265 (tsc && nla_put(msg, NL80211_ATTR_KEY_SEQ, 6, tsc)))
8266 goto nla_put_failure;
Jouni Malinena3b8b052009-03-27 21:59:49 +02008267
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008268 genlmsg_end(msg, hdr);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008269
Johannes Berg463d0182009-07-14 00:33:35 +02008270 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8271 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02008272 return;
8273
8274 nla_put_failure:
8275 genlmsg_cancel(msg, hdr);
8276 nlmsg_free(msg);
8277}
8278
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008279void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
8280 struct ieee80211_channel *channel_before,
8281 struct ieee80211_channel *channel_after)
8282{
8283 struct sk_buff *msg;
8284 void *hdr;
8285 struct nlattr *nl_freq;
8286
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07008287 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008288 if (!msg)
8289 return;
8290
8291 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
8292 if (!hdr) {
8293 nlmsg_free(msg);
8294 return;
8295 }
8296
8297 /*
8298 * Since we are applying the beacon hint to a wiphy we know its
8299 * wiphy_idx is valid
8300 */
David S. Miller9360ffd2012-03-29 04:41:26 -04008301 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy)))
8302 goto nla_put_failure;
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008303
8304 /* Before */
8305 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
8306 if (!nl_freq)
8307 goto nla_put_failure;
8308 if (nl80211_msg_put_channel(msg, channel_before))
8309 goto nla_put_failure;
8310 nla_nest_end(msg, nl_freq);
8311
8312 /* After */
8313 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
8314 if (!nl_freq)
8315 goto nla_put_failure;
8316 if (nl80211_msg_put_channel(msg, channel_after))
8317 goto nla_put_failure;
8318 nla_nest_end(msg, nl_freq);
8319
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008320 genlmsg_end(msg, hdr);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008321
Johannes Berg463d0182009-07-14 00:33:35 +02008322 rcu_read_lock();
8323 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
8324 GFP_ATOMIC);
8325 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04008326
8327 return;
8328
8329nla_put_failure:
8330 genlmsg_cancel(msg, hdr);
8331 nlmsg_free(msg);
8332}
8333
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008334static void nl80211_send_remain_on_chan_event(
8335 int cmd, struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008336 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008337 struct ieee80211_channel *chan,
8338 enum nl80211_channel_type channel_type,
8339 unsigned int duration, gfp_t gfp)
8340{
8341 struct sk_buff *msg;
8342 void *hdr;
8343
8344 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8345 if (!msg)
8346 return;
8347
8348 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
8349 if (!hdr) {
8350 nlmsg_free(msg);
8351 return;
8352 }
8353
David S. Miller9360ffd2012-03-29 04:41:26 -04008354 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008355 (wdev->netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8356 wdev->netdev->ifindex)) ||
Johannes Berg00f53352012-07-17 11:53:12 +02008357 nla_put_u64(msg, NL80211_ATTR_WDEV, wdev_id(wdev)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008358 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq) ||
8359 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type) ||
8360 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie))
8361 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008362
David S. Miller9360ffd2012-03-29 04:41:26 -04008363 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL &&
8364 nla_put_u32(msg, NL80211_ATTR_DURATION, duration))
8365 goto nla_put_failure;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008366
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008367 genlmsg_end(msg, hdr);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008368
8369 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8370 nl80211_mlme_mcgrp.id, gfp);
8371 return;
8372
8373 nla_put_failure:
8374 genlmsg_cancel(msg, hdr);
8375 nlmsg_free(msg);
8376}
8377
8378void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008379 struct wireless_dev *wdev, u64 cookie,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008380 struct ieee80211_channel *chan,
8381 enum nl80211_channel_type channel_type,
8382 unsigned int duration, gfp_t gfp)
8383{
8384 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008385 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008386 channel_type, duration, gfp);
8387}
8388
8389void nl80211_send_remain_on_channel_cancel(
Johannes Berg71bbc992012-06-15 15:30:18 +02008390 struct cfg80211_registered_device *rdev,
8391 struct wireless_dev *wdev,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008392 u64 cookie, struct ieee80211_channel *chan,
8393 enum nl80211_channel_type channel_type, gfp_t gfp)
8394{
8395 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
Johannes Berg71bbc992012-06-15 15:30:18 +02008396 rdev, wdev, cookie, chan,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01008397 channel_type, 0, gfp);
8398}
8399
Johannes Berg98b62182009-12-23 13:15:44 +01008400void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
8401 struct net_device *dev, const u8 *mac_addr,
8402 struct station_info *sinfo, gfp_t gfp)
8403{
8404 struct sk_buff *msg;
8405
Thomas Graf58050fc2012-06-28 03:57:45 +00008406 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg98b62182009-12-23 13:15:44 +01008407 if (!msg)
8408 return;
8409
John W. Linville66266b32012-03-15 13:25:41 -04008410 if (nl80211_send_station(msg, 0, 0, 0,
8411 rdev, dev, mac_addr, sinfo) < 0) {
Johannes Berg98b62182009-12-23 13:15:44 +01008412 nlmsg_free(msg);
8413 return;
8414 }
8415
8416 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8417 nl80211_mlme_mcgrp.id, gfp);
8418}
8419
Jouni Malinenec15e682011-03-23 15:29:52 +02008420void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
8421 struct net_device *dev, const u8 *mac_addr,
8422 gfp_t gfp)
8423{
8424 struct sk_buff *msg;
8425 void *hdr;
8426
Thomas Graf58050fc2012-06-28 03:57:45 +00008427 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenec15e682011-03-23 15:29:52 +02008428 if (!msg)
8429 return;
8430
8431 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
8432 if (!hdr) {
8433 nlmsg_free(msg);
8434 return;
8435 }
8436
David S. Miller9360ffd2012-03-29 04:41:26 -04008437 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8438 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr))
8439 goto nla_put_failure;
Jouni Malinenec15e682011-03-23 15:29:52 +02008440
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008441 genlmsg_end(msg, hdr);
Jouni Malinenec15e682011-03-23 15:29:52 +02008442
8443 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8444 nl80211_mlme_mcgrp.id, gfp);
8445 return;
8446
8447 nla_put_failure:
8448 genlmsg_cancel(msg, hdr);
8449 nlmsg_free(msg);
8450}
8451
Pandiyarajan Pitchaimuthued44a952012-09-18 16:50:49 +05308452void nl80211_send_conn_failed_event(struct cfg80211_registered_device *rdev,
8453 struct net_device *dev, const u8 *mac_addr,
8454 enum nl80211_connect_failed_reason reason,
8455 gfp_t gfp)
8456{
8457 struct sk_buff *msg;
8458 void *hdr;
8459
8460 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8461 if (!msg)
8462 return;
8463
8464 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONN_FAILED);
8465 if (!hdr) {
8466 nlmsg_free(msg);
8467 return;
8468 }
8469
8470 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8471 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr) ||
8472 nla_put_u32(msg, NL80211_ATTR_CONN_FAILED_REASON, reason))
8473 goto nla_put_failure;
8474
8475 genlmsg_end(msg, hdr);
8476
8477 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8478 nl80211_mlme_mcgrp.id, gfp);
8479 return;
8480
8481 nla_put_failure:
8482 genlmsg_cancel(msg, hdr);
8483 nlmsg_free(msg);
8484}
8485
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008486static bool __nl80211_unexpected_frame(struct net_device *dev, u8 cmd,
8487 const u8 *addr, gfp_t gfp)
Johannes Berg28946da2011-11-04 11:18:12 +01008488{
8489 struct wireless_dev *wdev = dev->ieee80211_ptr;
8490 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8491 struct sk_buff *msg;
8492 void *hdr;
8493 int err;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008494 u32 nlportid = ACCESS_ONCE(wdev->ap_unexpected_nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008495
Eric W. Biederman15e47302012-09-07 20:12:54 +00008496 if (!nlportid)
Johannes Berg28946da2011-11-04 11:18:12 +01008497 return false;
8498
8499 msg = nlmsg_new(100, gfp);
8500 if (!msg)
8501 return true;
8502
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008503 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
Johannes Berg28946da2011-11-04 11:18:12 +01008504 if (!hdr) {
8505 nlmsg_free(msg);
8506 return true;
8507 }
8508
David S. Miller9360ffd2012-03-29 04:41:26 -04008509 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8510 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8511 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr))
8512 goto nla_put_failure;
Johannes Berg28946da2011-11-04 11:18:12 +01008513
8514 err = genlmsg_end(msg, hdr);
8515 if (err < 0) {
8516 nlmsg_free(msg);
8517 return true;
8518 }
8519
Eric W. Biederman15e47302012-09-07 20:12:54 +00008520 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg28946da2011-11-04 11:18:12 +01008521 return true;
8522
8523 nla_put_failure:
8524 genlmsg_cancel(msg, hdr);
8525 nlmsg_free(msg);
8526 return true;
8527}
8528
Johannes Bergb92ab5d2011-11-04 11:18:19 +01008529bool nl80211_unexpected_frame(struct net_device *dev, const u8 *addr, gfp_t gfp)
8530{
8531 return __nl80211_unexpected_frame(dev, NL80211_CMD_UNEXPECTED_FRAME,
8532 addr, gfp);
8533}
8534
8535bool nl80211_unexpected_4addr_frame(struct net_device *dev,
8536 const u8 *addr, gfp_t gfp)
8537{
8538 return __nl80211_unexpected_frame(dev,
8539 NL80211_CMD_UNEXPECTED_4ADDR_FRAME,
8540 addr, gfp);
8541}
8542
Johannes Berg2e161f72010-08-12 15:38:38 +02008543int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
Eric W. Biederman15e47302012-09-07 20:12:54 +00008544 struct wireless_dev *wdev, u32 nlportid,
Johannes Berg804483e2012-03-05 22:18:41 +01008545 int freq, int sig_dbm,
8546 const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008547{
Johannes Berg71bbc992012-06-15 15:30:18 +02008548 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008549 struct sk_buff *msg;
8550 void *hdr;
Jouni Malinen026331c2010-02-15 12:53:10 +02008551
8552 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8553 if (!msg)
8554 return -ENOMEM;
8555
Johannes Berg2e161f72010-08-12 15:38:38 +02008556 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02008557 if (!hdr) {
8558 nlmsg_free(msg);
8559 return -ENOMEM;
8560 }
8561
David S. Miller9360ffd2012-03-29 04:41:26 -04008562 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008563 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8564 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008565 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8566 (sig_dbm &&
8567 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8568 nla_put(msg, NL80211_ATTR_FRAME, len, buf))
8569 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008570
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008571 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008572
Eric W. Biederman15e47302012-09-07 20:12:54 +00008573 return genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Jouni Malinen026331c2010-02-15 12:53:10 +02008574
8575 nla_put_failure:
8576 genlmsg_cancel(msg, hdr);
8577 nlmsg_free(msg);
8578 return -ENOBUFS;
8579}
8580
Johannes Berg2e161f72010-08-12 15:38:38 +02008581void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
Johannes Berg71bbc992012-06-15 15:30:18 +02008582 struct wireless_dev *wdev, u64 cookie,
Johannes Berg2e161f72010-08-12 15:38:38 +02008583 const u8 *buf, size_t len, bool ack,
8584 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02008585{
Johannes Berg71bbc992012-06-15 15:30:18 +02008586 struct net_device *netdev = wdev->netdev;
Jouni Malinen026331c2010-02-15 12:53:10 +02008587 struct sk_buff *msg;
8588 void *hdr;
8589
8590 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
8591 if (!msg)
8592 return;
8593
Johannes Berg2e161f72010-08-12 15:38:38 +02008594 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02008595 if (!hdr) {
8596 nlmsg_free(msg);
8597 return;
8598 }
8599
David S. Miller9360ffd2012-03-29 04:41:26 -04008600 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
Johannes Berg71bbc992012-06-15 15:30:18 +02008601 (netdev && nla_put_u32(msg, NL80211_ATTR_IFINDEX,
8602 netdev->ifindex)) ||
David S. Miller9360ffd2012-03-29 04:41:26 -04008603 nla_put(msg, NL80211_ATTR_FRAME, len, buf) ||
8604 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8605 (ack && nla_put_flag(msg, NL80211_ATTR_ACK)))
8606 goto nla_put_failure;
Jouni Malinen026331c2010-02-15 12:53:10 +02008607
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008608 genlmsg_end(msg, hdr);
Jouni Malinen026331c2010-02-15 12:53:10 +02008609
8610 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
8611 return;
8612
8613 nla_put_failure:
8614 genlmsg_cancel(msg, hdr);
8615 nlmsg_free(msg);
8616}
8617
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008618void
8619nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
8620 struct net_device *netdev,
8621 enum nl80211_cqm_rssi_threshold_event rssi_event,
8622 gfp_t gfp)
8623{
8624 struct sk_buff *msg;
8625 struct nlattr *pinfoattr;
8626 void *hdr;
8627
Thomas Graf58050fc2012-06-28 03:57:45 +00008628 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008629 if (!msg)
8630 return;
8631
8632 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8633 if (!hdr) {
8634 nlmsg_free(msg);
8635 return;
8636 }
8637
David S. Miller9360ffd2012-03-29 04:41:26 -04008638 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8639 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8640 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008641
8642 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8643 if (!pinfoattr)
8644 goto nla_put_failure;
8645
David S. Miller9360ffd2012-03-29 04:41:26 -04008646 if (nla_put_u32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
8647 rssi_event))
8648 goto nla_put_failure;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008649
8650 nla_nest_end(msg, pinfoattr);
8651
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008652 genlmsg_end(msg, hdr);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02008653
8654 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8655 nl80211_mlme_mcgrp.id, gfp);
8656 return;
8657
8658 nla_put_failure:
8659 genlmsg_cancel(msg, hdr);
8660 nlmsg_free(msg);
8661}
8662
Johannes Berge5497d72011-07-05 16:35:40 +02008663void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
8664 struct net_device *netdev, const u8 *bssid,
8665 const u8 *replay_ctr, gfp_t gfp)
8666{
8667 struct sk_buff *msg;
8668 struct nlattr *rekey_attr;
8669 void *hdr;
8670
Thomas Graf58050fc2012-06-28 03:57:45 +00008671 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berge5497d72011-07-05 16:35:40 +02008672 if (!msg)
8673 return;
8674
8675 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
8676 if (!hdr) {
8677 nlmsg_free(msg);
8678 return;
8679 }
8680
David S. Miller9360ffd2012-03-29 04:41:26 -04008681 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8682 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8683 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid))
8684 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008685
8686 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
8687 if (!rekey_attr)
8688 goto nla_put_failure;
8689
David S. Miller9360ffd2012-03-29 04:41:26 -04008690 if (nla_put(msg, NL80211_REKEY_DATA_REPLAY_CTR,
8691 NL80211_REPLAY_CTR_LEN, replay_ctr))
8692 goto nla_put_failure;
Johannes Berge5497d72011-07-05 16:35:40 +02008693
8694 nla_nest_end(msg, rekey_attr);
8695
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008696 genlmsg_end(msg, hdr);
Johannes Berge5497d72011-07-05 16:35:40 +02008697
8698 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8699 nl80211_mlme_mcgrp.id, gfp);
8700 return;
8701
8702 nla_put_failure:
8703 genlmsg_cancel(msg, hdr);
8704 nlmsg_free(msg);
8705}
8706
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008707void nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
8708 struct net_device *netdev, int index,
8709 const u8 *bssid, bool preauth, gfp_t gfp)
8710{
8711 struct sk_buff *msg;
8712 struct nlattr *attr;
8713 void *hdr;
8714
Thomas Graf58050fc2012-06-28 03:57:45 +00008715 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008716 if (!msg)
8717 return;
8718
8719 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PMKSA_CANDIDATE);
8720 if (!hdr) {
8721 nlmsg_free(msg);
8722 return;
8723 }
8724
David S. Miller9360ffd2012-03-29 04:41:26 -04008725 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8726 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex))
8727 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008728
8729 attr = nla_nest_start(msg, NL80211_ATTR_PMKSA_CANDIDATE);
8730 if (!attr)
8731 goto nla_put_failure;
8732
David S. Miller9360ffd2012-03-29 04:41:26 -04008733 if (nla_put_u32(msg, NL80211_PMKSA_CANDIDATE_INDEX, index) ||
8734 nla_put(msg, NL80211_PMKSA_CANDIDATE_BSSID, ETH_ALEN, bssid) ||
8735 (preauth &&
8736 nla_put_flag(msg, NL80211_PMKSA_CANDIDATE_PREAUTH)))
8737 goto nla_put_failure;
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008738
8739 nla_nest_end(msg, attr);
8740
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008741 genlmsg_end(msg, hdr);
Jouni Malinenc9df56b2011-09-16 18:56:23 +03008742
8743 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8744 nl80211_mlme_mcgrp.id, gfp);
8745 return;
8746
8747 nla_put_failure:
8748 genlmsg_cancel(msg, hdr);
8749 nlmsg_free(msg);
8750}
8751
Thomas Pedersen53145262012-04-06 13:35:47 -07008752void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
8753 struct net_device *netdev, int freq,
8754 enum nl80211_channel_type type, gfp_t gfp)
8755{
8756 struct sk_buff *msg;
8757 void *hdr;
8758
Thomas Graf58050fc2012-06-28 03:57:45 +00008759 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Thomas Pedersen53145262012-04-06 13:35:47 -07008760 if (!msg)
8761 return;
8762
8763 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CH_SWITCH_NOTIFY);
8764 if (!hdr) {
8765 nlmsg_free(msg);
8766 return;
8767 }
8768
John W. Linville7eab0f62012-04-12 14:25:14 -04008769 if (nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8770 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq) ||
8771 nla_put_u32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, type))
8772 goto nla_put_failure;
Thomas Pedersen53145262012-04-06 13:35:47 -07008773
8774 genlmsg_end(msg, hdr);
8775
8776 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8777 nl80211_mlme_mcgrp.id, gfp);
8778 return;
8779
8780 nla_put_failure:
8781 genlmsg_cancel(msg, hdr);
8782 nlmsg_free(msg);
8783}
8784
Johannes Bergc063dbf2010-11-24 08:10:05 +01008785void
Thomas Pedersen84f10702012-07-12 16:17:33 -07008786nl80211_send_cqm_txe_notify(struct cfg80211_registered_device *rdev,
8787 struct net_device *netdev, const u8 *peer,
8788 u32 num_packets, u32 rate, u32 intvl, gfp_t gfp)
8789{
8790 struct sk_buff *msg;
8791 struct nlattr *pinfoattr;
8792 void *hdr;
8793
8794 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
8795 if (!msg)
8796 return;
8797
8798 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8799 if (!hdr) {
8800 nlmsg_free(msg);
8801 return;
8802 }
8803
8804 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8805 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8806 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8807 goto nla_put_failure;
8808
8809 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8810 if (!pinfoattr)
8811 goto nla_put_failure;
8812
8813 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_PKTS, num_packets))
8814 goto nla_put_failure;
8815
8816 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_RATE, rate))
8817 goto nla_put_failure;
8818
8819 if (nla_put_u32(msg, NL80211_ATTR_CQM_TXE_INTVL, intvl))
8820 goto nla_put_failure;
8821
8822 nla_nest_end(msg, pinfoattr);
8823
8824 genlmsg_end(msg, hdr);
8825
8826 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8827 nl80211_mlme_mcgrp.id, gfp);
8828 return;
8829
8830 nla_put_failure:
8831 genlmsg_cancel(msg, hdr);
8832 nlmsg_free(msg);
8833}
8834
8835void
Johannes Bergc063dbf2010-11-24 08:10:05 +01008836nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
8837 struct net_device *netdev, const u8 *peer,
8838 u32 num_packets, gfp_t gfp)
8839{
8840 struct sk_buff *msg;
8841 struct nlattr *pinfoattr;
8842 void *hdr;
8843
Thomas Graf58050fc2012-06-28 03:57:45 +00008844 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008845 if (!msg)
8846 return;
8847
8848 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
8849 if (!hdr) {
8850 nlmsg_free(msg);
8851 return;
8852 }
8853
David S. Miller9360ffd2012-03-29 04:41:26 -04008854 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8855 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
8856 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, peer))
8857 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008858
8859 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
8860 if (!pinfoattr)
8861 goto nla_put_failure;
8862
David S. Miller9360ffd2012-03-29 04:41:26 -04008863 if (nla_put_u32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets))
8864 goto nla_put_failure;
Johannes Bergc063dbf2010-11-24 08:10:05 +01008865
8866 nla_nest_end(msg, pinfoattr);
8867
Johannes Berg3b7b72e2011-10-22 19:05:51 +02008868 genlmsg_end(msg, hdr);
Johannes Bergc063dbf2010-11-24 08:10:05 +01008869
8870 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8871 nl80211_mlme_mcgrp.id, gfp);
8872 return;
8873
8874 nla_put_failure:
8875 genlmsg_cancel(msg, hdr);
8876 nlmsg_free(msg);
8877}
8878
Johannes Berg7f6cf312011-11-04 11:18:15 +01008879void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
8880 u64 cookie, bool acked, gfp_t gfp)
8881{
8882 struct wireless_dev *wdev = dev->ieee80211_ptr;
8883 struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
8884 struct sk_buff *msg;
8885 void *hdr;
8886 int err;
8887
Thomas Graf58050fc2012-06-28 03:57:45 +00008888 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg7f6cf312011-11-04 11:18:15 +01008889 if (!msg)
8890 return;
8891
8892 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_PROBE_CLIENT);
8893 if (!hdr) {
8894 nlmsg_free(msg);
8895 return;
8896 }
8897
David S. Miller9360ffd2012-03-29 04:41:26 -04008898 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8899 nla_put_u32(msg, NL80211_ATTR_IFINDEX, dev->ifindex) ||
8900 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, addr) ||
8901 nla_put_u64(msg, NL80211_ATTR_COOKIE, cookie) ||
8902 (acked && nla_put_flag(msg, NL80211_ATTR_ACK)))
8903 goto nla_put_failure;
Johannes Berg7f6cf312011-11-04 11:18:15 +01008904
8905 err = genlmsg_end(msg, hdr);
8906 if (err < 0) {
8907 nlmsg_free(msg);
8908 return;
8909 }
8910
8911 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
8912 nl80211_mlme_mcgrp.id, gfp);
8913 return;
8914
8915 nla_put_failure:
8916 genlmsg_cancel(msg, hdr);
8917 nlmsg_free(msg);
8918}
8919EXPORT_SYMBOL(cfg80211_probe_status);
8920
Johannes Berg5e760232011-11-04 11:18:17 +01008921void cfg80211_report_obss_beacon(struct wiphy *wiphy,
8922 const u8 *frame, size_t len,
Johannes Berg804483e2012-03-05 22:18:41 +01008923 int freq, int sig_dbm, gfp_t gfp)
Johannes Berg5e760232011-11-04 11:18:17 +01008924{
8925 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
8926 struct sk_buff *msg;
8927 void *hdr;
Eric W. Biederman15e47302012-09-07 20:12:54 +00008928 u32 nlportid = ACCESS_ONCE(rdev->ap_beacons_nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01008929
Eric W. Biederman15e47302012-09-07 20:12:54 +00008930 if (!nlportid)
Johannes Berg5e760232011-11-04 11:18:17 +01008931 return;
8932
8933 msg = nlmsg_new(len + 100, gfp);
8934 if (!msg)
8935 return;
8936
8937 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
8938 if (!hdr) {
8939 nlmsg_free(msg);
8940 return;
8941 }
8942
David S. Miller9360ffd2012-03-29 04:41:26 -04008943 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
8944 (freq &&
8945 nla_put_u32(msg, NL80211_ATTR_WIPHY_FREQ, freq)) ||
8946 (sig_dbm &&
8947 nla_put_u32(msg, NL80211_ATTR_RX_SIGNAL_DBM, sig_dbm)) ||
8948 nla_put(msg, NL80211_ATTR_FRAME, len, frame))
8949 goto nla_put_failure;
Johannes Berg5e760232011-11-04 11:18:17 +01008950
8951 genlmsg_end(msg, hdr);
8952
Eric W. Biederman15e47302012-09-07 20:12:54 +00008953 genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlportid);
Johannes Berg5e760232011-11-04 11:18:17 +01008954 return;
8955
8956 nla_put_failure:
8957 genlmsg_cancel(msg, hdr);
8958 nlmsg_free(msg);
8959}
8960EXPORT_SYMBOL(cfg80211_report_obss_beacon);
8961
Jouni Malinen026331c2010-02-15 12:53:10 +02008962static int nl80211_netlink_notify(struct notifier_block * nb,
8963 unsigned long state,
8964 void *_notify)
8965{
8966 struct netlink_notify *notify = _notify;
8967 struct cfg80211_registered_device *rdev;
8968 struct wireless_dev *wdev;
8969
8970 if (state != NETLINK_URELEASE)
8971 return NOTIFY_DONE;
8972
8973 rcu_read_lock();
8974
Johannes Berg5e760232011-11-04 11:18:17 +01008975 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list) {
Johannes Berg89a54e42012-06-15 14:33:17 +02008976 list_for_each_entry_rcu(wdev, &rdev->wdev_list, list)
Eric W. Biederman15e47302012-09-07 20:12:54 +00008977 cfg80211_mlme_unregister_socket(wdev, notify->portid);
8978 if (rdev->ap_beacons_nlportid == notify->portid)
8979 rdev->ap_beacons_nlportid = 0;
Johannes Berg5e760232011-11-04 11:18:17 +01008980 }
Jouni Malinen026331c2010-02-15 12:53:10 +02008981
8982 rcu_read_unlock();
8983
8984 return NOTIFY_DONE;
8985}
8986
8987static struct notifier_block nl80211_netlink_notifier = {
8988 .notifier_call = nl80211_netlink_notify,
8989};
8990
Johannes Berg55682962007-09-20 13:09:35 -04008991/* initialisation/exit functions */
8992
8993int nl80211_init(void)
8994{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008995 int err;
Johannes Berg55682962007-09-20 13:09:35 -04008996
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00008997 err = genl_register_family_with_ops(&nl80211_fam,
8998 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04008999 if (err)
9000 return err;
9001
Johannes Berg55682962007-09-20 13:09:35 -04009002 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
9003 if (err)
9004 goto err_out;
9005
Johannes Berg2a519312009-02-10 21:25:55 +01009006 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
9007 if (err)
9008 goto err_out;
9009
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04009010 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
9011 if (err)
9012 goto err_out;
9013
Jouni Malinen6039f6d2009-03-19 13:39:21 +02009014 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
9015 if (err)
9016 goto err_out;
9017
Johannes Bergaff89a92009-07-01 21:26:51 +02009018#ifdef CONFIG_NL80211_TESTMODE
9019 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
9020 if (err)
9021 goto err_out;
9022#endif
9023
Jouni Malinen026331c2010-02-15 12:53:10 +02009024 err = netlink_register_notifier(&nl80211_netlink_notifier);
9025 if (err)
9026 goto err_out;
9027
Johannes Berg55682962007-09-20 13:09:35 -04009028 return 0;
9029 err_out:
9030 genl_unregister_family(&nl80211_fam);
9031 return err;
9032}
9033
9034void nl80211_exit(void)
9035{
Jouni Malinen026331c2010-02-15 12:53:10 +02009036 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04009037 genl_unregister_family(&nl80211_fam);
9038}