blob: fc3c95d925464f0bf58a95247545458c1c8916a3 [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
Johannes Berg4c476992010-10-04 21:36:35 +020026static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
27 struct genl_info *info);
28static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
29 struct genl_info *info);
30
Johannes Berg55682962007-09-20 13:09:35 -040031/* the netlink family */
32static struct genl_family nl80211_fam = {
33 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
34 .name = "nl80211", /* have users key off the name instead */
35 .hdrsize = 0, /* no private header */
36 .version = 1, /* no particular meaning now */
37 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020038 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020039 .pre_doit = nl80211_pre_doit,
40 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040041};
42
Johannes Berg79c97e92009-07-07 03:56:12 +020043/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020044static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020045 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040046 struct net_device **dev)
47{
Johannes Berg463d0182009-07-14 00:33:35 +020048 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040049 int ifindex;
50
Johannes Bergbba95fe2008-07-29 13:22:51 +020051 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040052 return -EINVAL;
53
Johannes Bergbba95fe2008-07-29 13:22:51 +020054 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020055 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040056 if (!*dev)
57 return -ENODEV;
58
Johannes Berg463d0182009-07-14 00:33:35 +020059 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020060 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040061 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020062 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040063 }
64
65 return 0;
66}
67
68/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000069static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040070 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070072 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020073 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020074 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053075 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020076 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
77 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
78 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
79 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010080 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040081
82 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
83 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
84 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010085
86 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020087 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010088
Johannes Bergb9454e82009-07-08 13:29:08 +020089 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010090 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
91 .len = WLAN_MAX_KEY_LEN },
92 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
93 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
94 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030095 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berge31b8212010-10-05 19:39:30 +020096 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010097
98 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
99 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
100 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
101 .len = IEEE80211_MAX_DATA_LEN },
102 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
103 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100104 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
105 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
106 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
107 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
108 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100109 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100110 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200111 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100112 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
113 .len = IEEE80211_MAX_MESH_ID_LEN },
114 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300115
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700116 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
117 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
118
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300119 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
120 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
121 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200122 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
123 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100124 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300125
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800126 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700127 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700128
Jouni Malinen36aedc92008-08-25 11:58:58 +0300129 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
130 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200131
132 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
133 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
134 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100135 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
136 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200137
138 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
139 .len = IEEE80211_MAX_SSID_LEN },
140 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
141 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200142 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300143 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300144 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300145 [NL80211_ATTR_STA_FLAGS2] = {
146 .len = sizeof(struct nl80211_sta_flag_update),
147 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300148 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300149 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
150 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200151 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
152 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
153 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200154 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100155 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100156 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
157 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100158 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
159 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200160 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200161 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
162 .len = IEEE80211_MAX_DATA_LEN },
163 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200164 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200165 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300166 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200167 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300168 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
169 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200170 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900171 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
172 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100173 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100174 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100175 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200176 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700177 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -0400178};
179
Johannes Berge31b8212010-10-05 19:39:30 +0200180/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000181static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200182 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200183 [NL80211_KEY_IDX] = { .type = NLA_U8 },
184 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
185 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
186 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
187 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200188 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100189 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
190};
191
192/* policy for the key default flags */
193static const struct nla_policy
194nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
195 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
196 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200197};
198
Johannes Bergff1b6e62011-05-04 15:37:28 +0200199/* policy for WoWLAN attributes */
200static const struct nla_policy
201nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
202 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
203 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
204 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
205 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
206};
207
Holger Schuriga0438972009-11-11 11:30:02 +0100208/* ifidx get helper */
209static int nl80211_get_ifidx(struct netlink_callback *cb)
210{
211 int res;
212
213 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
214 nl80211_fam.attrbuf, nl80211_fam.maxattr,
215 nl80211_policy);
216 if (res)
217 return res;
218
219 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
220 return -EINVAL;
221
222 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
223 if (!res)
224 return -EINVAL;
225 return res;
226}
227
Johannes Berg67748892010-10-04 21:14:06 +0200228static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
229 struct netlink_callback *cb,
230 struct cfg80211_registered_device **rdev,
231 struct net_device **dev)
232{
233 int ifidx = cb->args[0];
234 int err;
235
236 if (!ifidx)
237 ifidx = nl80211_get_ifidx(cb);
238 if (ifidx < 0)
239 return ifidx;
240
241 cb->args[0] = ifidx;
242
243 rtnl_lock();
244
245 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
246 if (!*dev) {
247 err = -ENODEV;
248 goto out_rtnl;
249 }
250
251 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100252 if (IS_ERR(*rdev)) {
253 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200254 goto out_rtnl;
255 }
256
257 return 0;
258 out_rtnl:
259 rtnl_unlock();
260 return err;
261}
262
263static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
264{
265 cfg80211_unlock_rdev(rdev);
266 rtnl_unlock();
267}
268
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100269/* IE validation */
270static bool is_valid_ie_attr(const struct nlattr *attr)
271{
272 const u8 *pos;
273 int len;
274
275 if (!attr)
276 return true;
277
278 pos = nla_data(attr);
279 len = nla_len(attr);
280
281 while (len) {
282 u8 elemlen;
283
284 if (len < 2)
285 return false;
286 len -= 2;
287
288 elemlen = pos[1];
289 if (elemlen > len)
290 return false;
291
292 len -= elemlen;
293 pos += 2 + elemlen;
294 }
295
296 return true;
297}
298
Johannes Berg55682962007-09-20 13:09:35 -0400299/* message building helper */
300static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
301 int flags, u8 cmd)
302{
303 /* since there is no private header just add the generic one */
304 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
305}
306
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400307static int nl80211_msg_put_channel(struct sk_buff *msg,
308 struct ieee80211_channel *chan)
309{
310 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
311 chan->center_freq);
312
313 if (chan->flags & IEEE80211_CHAN_DISABLED)
314 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
315 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
316 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
317 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
318 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
319 if (chan->flags & IEEE80211_CHAN_RADAR)
320 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
321
322 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
323 DBM_TO_MBM(chan->max_power));
324
325 return 0;
326
327 nla_put_failure:
328 return -ENOBUFS;
329}
330
Johannes Berg55682962007-09-20 13:09:35 -0400331/* netlink command implementations */
332
Johannes Bergb9454e82009-07-08 13:29:08 +0200333struct key_parse {
334 struct key_params p;
335 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200336 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200337 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100338 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200339};
340
341static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
342{
343 struct nlattr *tb[NL80211_KEY_MAX + 1];
344 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
345 nl80211_key_policy);
346 if (err)
347 return err;
348
349 k->def = !!tb[NL80211_KEY_DEFAULT];
350 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
351
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100352 if (k->def) {
353 k->def_uni = true;
354 k->def_multi = true;
355 }
356 if (k->defmgmt)
357 k->def_multi = true;
358
Johannes Bergb9454e82009-07-08 13:29:08 +0200359 if (tb[NL80211_KEY_IDX])
360 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
361
362 if (tb[NL80211_KEY_DATA]) {
363 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
364 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
365 }
366
367 if (tb[NL80211_KEY_SEQ]) {
368 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
369 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
370 }
371
372 if (tb[NL80211_KEY_CIPHER])
373 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
374
Johannes Berge31b8212010-10-05 19:39:30 +0200375 if (tb[NL80211_KEY_TYPE]) {
376 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
377 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
378 return -EINVAL;
379 }
380
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100381 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
382 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
383 int err = nla_parse_nested(kdt,
384 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
385 tb[NL80211_KEY_DEFAULT_TYPES],
386 nl80211_key_default_policy);
387 if (err)
388 return err;
389
390 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
391 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
392 }
393
Johannes Bergb9454e82009-07-08 13:29:08 +0200394 return 0;
395}
396
397static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
398{
399 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
400 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
401 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
402 }
403
404 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
405 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
406 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
407 }
408
409 if (info->attrs[NL80211_ATTR_KEY_IDX])
410 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
411
412 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
413 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
414
415 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
416 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
417
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100418 if (k->def) {
419 k->def_uni = true;
420 k->def_multi = true;
421 }
422 if (k->defmgmt)
423 k->def_multi = true;
424
Johannes Berge31b8212010-10-05 19:39:30 +0200425 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
426 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
427 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
428 return -EINVAL;
429 }
430
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100431 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
432 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
433 int err = nla_parse_nested(
434 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
435 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
436 nl80211_key_default_policy);
437 if (err)
438 return err;
439
440 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
441 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
442 }
443
Johannes Bergb9454e82009-07-08 13:29:08 +0200444 return 0;
445}
446
447static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
448{
449 int err;
450
451 memset(k, 0, sizeof(*k));
452 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200453 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200454
455 if (info->attrs[NL80211_ATTR_KEY])
456 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
457 else
458 err = nl80211_parse_key_old(info, k);
459
460 if (err)
461 return err;
462
463 if (k->def && k->defmgmt)
464 return -EINVAL;
465
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100466 if (k->defmgmt) {
467 if (k->def_uni || !k->def_multi)
468 return -EINVAL;
469 }
470
Johannes Bergb9454e82009-07-08 13:29:08 +0200471 if (k->idx != -1) {
472 if (k->defmgmt) {
473 if (k->idx < 4 || k->idx > 5)
474 return -EINVAL;
475 } else if (k->def) {
476 if (k->idx < 0 || k->idx > 3)
477 return -EINVAL;
478 } else {
479 if (k->idx < 0 || k->idx > 5)
480 return -EINVAL;
481 }
482 }
483
484 return 0;
485}
486
Johannes Bergfffd0932009-07-08 14:22:54 +0200487static struct cfg80211_cached_keys *
488nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
489 struct nlattr *keys)
490{
491 struct key_parse parse;
492 struct nlattr *key;
493 struct cfg80211_cached_keys *result;
494 int rem, err, def = 0;
495
496 result = kzalloc(sizeof(*result), GFP_KERNEL);
497 if (!result)
498 return ERR_PTR(-ENOMEM);
499
500 result->def = -1;
501 result->defmgmt = -1;
502
503 nla_for_each_nested(key, keys, rem) {
504 memset(&parse, 0, sizeof(parse));
505 parse.idx = -1;
506
507 err = nl80211_parse_key_new(key, &parse);
508 if (err)
509 goto error;
510 err = -EINVAL;
511 if (!parse.p.key)
512 goto error;
513 if (parse.idx < 0 || parse.idx > 4)
514 goto error;
515 if (parse.def) {
516 if (def)
517 goto error;
518 def = 1;
519 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100520 if (!parse.def_uni || !parse.def_multi)
521 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200522 } else if (parse.defmgmt)
523 goto error;
524 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200525 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200526 if (err)
527 goto error;
528 result->params[parse.idx].cipher = parse.p.cipher;
529 result->params[parse.idx].key_len = parse.p.key_len;
530 result->params[parse.idx].key = result->data[parse.idx];
531 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
532 }
533
534 return result;
535 error:
536 kfree(result);
537 return ERR_PTR(err);
538}
539
540static int nl80211_key_allowed(struct wireless_dev *wdev)
541{
542 ASSERT_WDEV_LOCK(wdev);
543
Johannes Bergfffd0932009-07-08 14:22:54 +0200544 switch (wdev->iftype) {
545 case NL80211_IFTYPE_AP:
546 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200547 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700548 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200549 break;
550 case NL80211_IFTYPE_ADHOC:
551 if (!wdev->current_bss)
552 return -ENOLINK;
553 break;
554 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200555 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200556 if (wdev->sme_state != CFG80211_SME_CONNECTED)
557 return -ENOLINK;
558 break;
559 default:
560 return -EINVAL;
561 }
562
563 return 0;
564}
565
Johannes Berg55682962007-09-20 13:09:35 -0400566static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
567 struct cfg80211_registered_device *dev)
568{
569 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100570 struct nlattr *nl_bands, *nl_band;
571 struct nlattr *nl_freqs, *nl_freq;
572 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700573 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100574 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100575 enum ieee80211_band band;
576 struct ieee80211_channel *chan;
577 struct ieee80211_rate *rate;
578 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700579 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg2e161f72010-08-12 15:38:38 +0200580 const struct ieee80211_txrx_stypes *mgmt_stypes =
581 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400582
583 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
584 if (!hdr)
585 return -1;
586
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500587 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400588 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200589
Johannes Bergf5ea9122009-08-07 16:17:38 +0200590 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
591 cfg80211_rdev_list_generation);
592
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200593 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
594 dev->wiphy.retry_short);
595 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
596 dev->wiphy.retry_long);
597 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
598 dev->wiphy.frag_threshold);
599 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
600 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100601 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
602 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100603 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
604 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200605 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
606 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100607
Johannes Berge31b8212010-10-05 19:39:30 +0200608 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
609 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700610 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
611 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Johannes Berge31b8212010-10-05 19:39:30 +0200612
Johannes Berg25e47c12009-04-02 20:14:06 +0200613 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
614 sizeof(u32) * dev->wiphy.n_cipher_suites,
615 dev->wiphy.cipher_suites);
616
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100617 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
618 dev->wiphy.max_num_pmkids);
619
Johannes Bergc0692b82010-08-27 14:26:53 +0300620 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
621 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
622
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900623 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
624 dev->wiphy.available_antennas_tx);
625 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
626 dev->wiphy.available_antennas_rx);
627
Bruno Randolf7f531e02010-12-16 11:30:22 +0900628 if ((dev->wiphy.available_antennas_tx ||
629 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900630 u32 tx_ant = 0, rx_ant = 0;
631 int res;
632 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
633 if (!res) {
634 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
635 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
636 }
637 }
638
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700639 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
640 if (!nl_modes)
641 goto nla_put_failure;
642
643 i = 0;
644 while (ifmodes) {
645 if (ifmodes & 1)
646 NLA_PUT_FLAG(msg, i);
647 ifmodes >>= 1;
648 i++;
649 }
650
651 nla_nest_end(msg, nl_modes);
652
Johannes Bergee688b002008-01-24 19:38:39 +0100653 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
654 if (!nl_bands)
655 goto nla_put_failure;
656
657 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
658 if (!dev->wiphy.bands[band])
659 continue;
660
661 nl_band = nla_nest_start(msg, band);
662 if (!nl_band)
663 goto nla_put_failure;
664
Johannes Bergd51626d2008-10-09 12:20:13 +0200665 /* add HT info */
666 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
667 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
668 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
669 &dev->wiphy.bands[band]->ht_cap.mcs);
670 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
671 dev->wiphy.bands[band]->ht_cap.cap);
672 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
673 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
674 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
675 dev->wiphy.bands[band]->ht_cap.ampdu_density);
676 }
677
Johannes Bergee688b002008-01-24 19:38:39 +0100678 /* add frequencies */
679 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
680 if (!nl_freqs)
681 goto nla_put_failure;
682
683 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
684 nl_freq = nla_nest_start(msg, i);
685 if (!nl_freq)
686 goto nla_put_failure;
687
688 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100689
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400690 if (nl80211_msg_put_channel(msg, chan))
691 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200692
Johannes Bergee688b002008-01-24 19:38:39 +0100693 nla_nest_end(msg, nl_freq);
694 }
695
696 nla_nest_end(msg, nl_freqs);
697
698 /* add bitrates */
699 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
700 if (!nl_rates)
701 goto nla_put_failure;
702
703 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
704 nl_rate = nla_nest_start(msg, i);
705 if (!nl_rate)
706 goto nla_put_failure;
707
708 rate = &dev->wiphy.bands[band]->bitrates[i];
709 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
710 rate->bitrate);
711 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
712 NLA_PUT_FLAG(msg,
713 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
714
715 nla_nest_end(msg, nl_rate);
716 }
717
718 nla_nest_end(msg, nl_rates);
719
720 nla_nest_end(msg, nl_band);
721 }
722 nla_nest_end(msg, nl_bands);
723
Johannes Berg8fdc6212009-03-14 09:34:01 +0100724 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
725 if (!nl_cmds)
726 goto nla_put_failure;
727
728 i = 0;
729#define CMD(op, n) \
730 do { \
731 if (dev->ops->op) { \
732 i++; \
733 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
734 } \
735 } while (0)
736
737 CMD(add_virtual_intf, NEW_INTERFACE);
738 CMD(change_virtual_intf, SET_INTERFACE);
739 CMD(add_key, NEW_KEY);
740 CMD(add_beacon, NEW_BEACON);
741 CMD(add_station, NEW_STATION);
742 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800743 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100744 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200745 CMD(auth, AUTHENTICATE);
746 CMD(assoc, ASSOCIATE);
747 CMD(deauth, DEAUTHENTICATE);
748 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200749 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100750 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100751 CMD(set_pmksa, SET_PMKSA);
752 CMD(del_pmksa, DEL_PMKSA);
753 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100754 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200755 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200756 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100757 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100758 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200759 i++;
760 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
761 }
Johannes Bergf444de02010-05-05 15:25:02 +0200762 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400763 CMD(set_wds_peer, SET_WDS_PEER);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100764
765#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200766
Johannes Berg6829c872009-07-02 09:13:27 +0200767 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200768 i++;
769 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
770 }
771
Johannes Berg6829c872009-07-02 09:13:27 +0200772 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200773 i++;
774 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
775 }
776
Johannes Berg8fdc6212009-03-14 09:34:01 +0100777 nla_nest_end(msg, nl_cmds);
778
Johannes Berga2939112010-12-14 17:54:28 +0100779 if (dev->ops->remain_on_channel)
780 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
781 dev->wiphy.max_remain_on_channel_duration);
782
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100783 /* for now at least assume all drivers have it */
784 if (dev->ops->mgmt_tx)
785 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
786
Johannes Berg2e161f72010-08-12 15:38:38 +0200787 if (mgmt_stypes) {
788 u16 stypes;
789 struct nlattr *nl_ftypes, *nl_ifs;
790 enum nl80211_iftype ift;
791
792 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
793 if (!nl_ifs)
794 goto nla_put_failure;
795
796 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
797 nl_ftypes = nla_nest_start(msg, ift);
798 if (!nl_ftypes)
799 goto nla_put_failure;
800 i = 0;
801 stypes = mgmt_stypes[ift].tx;
802 while (stypes) {
803 if (stypes & 1)
804 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
805 (i << 4) | IEEE80211_FTYPE_MGMT);
806 stypes >>= 1;
807 i++;
808 }
809 nla_nest_end(msg, nl_ftypes);
810 }
811
Johannes Berg74b70a42010-08-24 12:15:53 +0200812 nla_nest_end(msg, nl_ifs);
813
Johannes Berg2e161f72010-08-12 15:38:38 +0200814 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
815 if (!nl_ifs)
816 goto nla_put_failure;
817
818 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
819 nl_ftypes = nla_nest_start(msg, ift);
820 if (!nl_ftypes)
821 goto nla_put_failure;
822 i = 0;
823 stypes = mgmt_stypes[ift].rx;
824 while (stypes) {
825 if (stypes & 1)
826 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
827 (i << 4) | IEEE80211_FTYPE_MGMT);
828 stypes >>= 1;
829 i++;
830 }
831 nla_nest_end(msg, nl_ftypes);
832 }
833 nla_nest_end(msg, nl_ifs);
834 }
835
Johannes Bergff1b6e62011-05-04 15:37:28 +0200836 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
837 struct nlattr *nl_wowlan;
838
839 nl_wowlan = nla_nest_start(msg,
840 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
841 if (!nl_wowlan)
842 goto nla_put_failure;
843
844 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
845 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
846 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
847 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
848 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
849 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
850 if (dev->wiphy.wowlan.n_patterns) {
851 struct nl80211_wowlan_pattern_support pat = {
852 .max_patterns = dev->wiphy.wowlan.n_patterns,
853 .min_pattern_len =
854 dev->wiphy.wowlan.pattern_min_len,
855 .max_pattern_len =
856 dev->wiphy.wowlan.pattern_max_len,
857 };
858 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
859 sizeof(pat), &pat);
860 }
861
862 nla_nest_end(msg, nl_wowlan);
863 }
864
Johannes Berg55682962007-09-20 13:09:35 -0400865 return genlmsg_end(msg, hdr);
866
867 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700868 genlmsg_cancel(msg, hdr);
869 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400870}
871
872static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
873{
874 int idx = 0;
875 int start = cb->args[0];
876 struct cfg80211_registered_device *dev;
877
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500878 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200879 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200880 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
881 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200882 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400883 continue;
884 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
885 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200886 dev) < 0) {
887 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400888 break;
Julius Volzb4637272008-07-08 14:02:19 +0200889 }
Johannes Berg55682962007-09-20 13:09:35 -0400890 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500891 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400892
893 cb->args[0] = idx;
894
895 return skb->len;
896}
897
898static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
899{
900 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200901 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400902
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700903 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400904 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200905 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400906
Johannes Berg4c476992010-10-04 21:36:35 +0200907 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
908 nlmsg_free(msg);
909 return -ENOBUFS;
910 }
Johannes Berg55682962007-09-20 13:09:35 -0400911
Johannes Berg134e6372009-07-10 09:51:34 +0000912 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400913}
914
Jouni Malinen31888482008-10-30 16:59:24 +0200915static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
916 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
917 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
918 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
919 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
920 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
921};
922
923static int parse_txq_params(struct nlattr *tb[],
924 struct ieee80211_txq_params *txq_params)
925{
926 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
927 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
928 !tb[NL80211_TXQ_ATTR_AIFS])
929 return -EINVAL;
930
931 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
932 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
933 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
934 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
935 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
936
937 return 0;
938}
939
Johannes Bergf444de02010-05-05 15:25:02 +0200940static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
941{
942 /*
943 * You can only set the channel explicitly for AP, mesh
944 * and WDS type interfaces; all others have their channel
945 * managed via their respective "establish a connection"
946 * command (connect, join, ...)
947 *
948 * Monitors are special as they are normally slaved to
949 * whatever else is going on, so they behave as though
950 * you tried setting the wiphy channel itself.
951 */
952 return !wdev ||
953 wdev->iftype == NL80211_IFTYPE_AP ||
954 wdev->iftype == NL80211_IFTYPE_WDS ||
955 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200956 wdev->iftype == NL80211_IFTYPE_MONITOR ||
957 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200958}
959
960static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
961 struct wireless_dev *wdev,
962 struct genl_info *info)
963{
964 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
965 u32 freq;
966 int result;
967
968 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
969 return -EINVAL;
970
971 if (!nl80211_can_set_dev_channel(wdev))
972 return -EOPNOTSUPP;
973
974 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
975 channel_type = nla_get_u32(info->attrs[
976 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
977 if (channel_type != NL80211_CHAN_NO_HT &&
978 channel_type != NL80211_CHAN_HT20 &&
979 channel_type != NL80211_CHAN_HT40PLUS &&
980 channel_type != NL80211_CHAN_HT40MINUS)
981 return -EINVAL;
982 }
983
984 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
985
986 mutex_lock(&rdev->devlist_mtx);
987 if (wdev) {
988 wdev_lock(wdev);
989 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
990 wdev_unlock(wdev);
991 } else {
992 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
993 }
994 mutex_unlock(&rdev->devlist_mtx);
995
996 return result;
997}
998
999static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1000{
Johannes Berg4c476992010-10-04 21:36:35 +02001001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1002 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001003
Johannes Berg4c476992010-10-04 21:36:35 +02001004 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001005}
1006
Bill Jordane8347eb2010-10-01 13:54:28 -04001007static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1008{
Johannes Berg43b19952010-10-07 13:10:30 +02001009 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1010 struct net_device *dev = info->user_ptr[1];
1011 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001012 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001013
1014 if (!info->attrs[NL80211_ATTR_MAC])
1015 return -EINVAL;
1016
Johannes Berg43b19952010-10-07 13:10:30 +02001017 if (netif_running(dev))
1018 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001019
Johannes Berg43b19952010-10-07 13:10:30 +02001020 if (!rdev->ops->set_wds_peer)
1021 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001022
Johannes Berg43b19952010-10-07 13:10:30 +02001023 if (wdev->iftype != NL80211_IFTYPE_WDS)
1024 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001025
1026 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001027 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001028}
1029
1030
Johannes Berg55682962007-09-20 13:09:35 -04001031static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1032{
1033 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001034 struct net_device *netdev = NULL;
1035 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001036 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001037 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001038 u32 changed;
1039 u8 retry_short = 0, retry_long = 0;
1040 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001041 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001042
Johannes Bergf444de02010-05-05 15:25:02 +02001043 /*
1044 * Try to find the wiphy and netdev. Normally this
1045 * function shouldn't need the netdev, but this is
1046 * done for backward compatibility -- previously
1047 * setting the channel was done per wiphy, but now
1048 * it is per netdev. Previous userland like hostapd
1049 * also passed a netdev to set_wiphy, so that it is
1050 * possible to let that go to the right netdev!
1051 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001052 mutex_lock(&cfg80211_mutex);
1053
Johannes Bergf444de02010-05-05 15:25:02 +02001054 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1055 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1056
1057 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1058 if (netdev && netdev->ieee80211_ptr) {
1059 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1060 mutex_lock(&rdev->mtx);
1061 } else
1062 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001063 }
1064
Johannes Bergf444de02010-05-05 15:25:02 +02001065 if (!netdev) {
1066 rdev = __cfg80211_rdev_from_info(info);
1067 if (IS_ERR(rdev)) {
1068 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001069 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001070 }
1071 wdev = NULL;
1072 netdev = NULL;
1073 result = 0;
1074
1075 mutex_lock(&rdev->mtx);
1076 } else if (netif_running(netdev) &&
1077 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1078 wdev = netdev->ieee80211_ptr;
1079 else
1080 wdev = NULL;
1081
1082 /*
1083 * end workaround code, by now the rdev is available
1084 * and locked, and wdev may or may not be NULL.
1085 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001086
1087 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001088 result = cfg80211_dev_rename(
1089 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001090
1091 mutex_unlock(&cfg80211_mutex);
1092
1093 if (result)
1094 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001095
Jouni Malinen31888482008-10-30 16:59:24 +02001096 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1097 struct ieee80211_txq_params txq_params;
1098 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1099
1100 if (!rdev->ops->set_txq_params) {
1101 result = -EOPNOTSUPP;
1102 goto bad_res;
1103 }
1104
1105 nla_for_each_nested(nl_txq_params,
1106 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1107 rem_txq_params) {
1108 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1109 nla_data(nl_txq_params),
1110 nla_len(nl_txq_params),
1111 txq_params_policy);
1112 result = parse_txq_params(tb, &txq_params);
1113 if (result)
1114 goto bad_res;
1115
1116 result = rdev->ops->set_txq_params(&rdev->wiphy,
1117 &txq_params);
1118 if (result)
1119 goto bad_res;
1120 }
1121 }
1122
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001123 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001124 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001125 if (result)
1126 goto bad_res;
1127 }
1128
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001129 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1130 enum nl80211_tx_power_setting type;
1131 int idx, mbm = 0;
1132
1133 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001134 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001135 goto bad_res;
1136 }
1137
1138 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1139 type = nla_get_u32(info->attrs[idx]);
1140
1141 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1142 (type != NL80211_TX_POWER_AUTOMATIC)) {
1143 result = -EINVAL;
1144 goto bad_res;
1145 }
1146
1147 if (type != NL80211_TX_POWER_AUTOMATIC) {
1148 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1149 mbm = nla_get_u32(info->attrs[idx]);
1150 }
1151
1152 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1153 if (result)
1154 goto bad_res;
1155 }
1156
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001157 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1158 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1159 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001160 if ((!rdev->wiphy.available_antennas_tx &&
1161 !rdev->wiphy.available_antennas_rx) ||
1162 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001163 result = -EOPNOTSUPP;
1164 goto bad_res;
1165 }
1166
1167 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1168 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1169
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001170 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001171 * available antenna masks, except for the "all" mask */
1172 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1173 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001174 result = -EINVAL;
1175 goto bad_res;
1176 }
1177
Bruno Randolf7f531e02010-12-16 11:30:22 +09001178 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1179 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001180
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001181 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1182 if (result)
1183 goto bad_res;
1184 }
1185
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001186 changed = 0;
1187
1188 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1189 retry_short = nla_get_u8(
1190 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1191 if (retry_short == 0) {
1192 result = -EINVAL;
1193 goto bad_res;
1194 }
1195 changed |= WIPHY_PARAM_RETRY_SHORT;
1196 }
1197
1198 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1199 retry_long = nla_get_u8(
1200 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1201 if (retry_long == 0) {
1202 result = -EINVAL;
1203 goto bad_res;
1204 }
1205 changed |= WIPHY_PARAM_RETRY_LONG;
1206 }
1207
1208 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1209 frag_threshold = nla_get_u32(
1210 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1211 if (frag_threshold < 256) {
1212 result = -EINVAL;
1213 goto bad_res;
1214 }
1215 if (frag_threshold != (u32) -1) {
1216 /*
1217 * Fragments (apart from the last one) are required to
1218 * have even length. Make the fragmentation code
1219 * simpler by stripping LSB should someone try to use
1220 * odd threshold value.
1221 */
1222 frag_threshold &= ~0x1;
1223 }
1224 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1225 }
1226
1227 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1228 rts_threshold = nla_get_u32(
1229 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1230 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1231 }
1232
Lukáš Turek81077e82009-12-21 22:50:47 +01001233 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1234 coverage_class = nla_get_u8(
1235 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1236 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1237 }
1238
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001239 if (changed) {
1240 u8 old_retry_short, old_retry_long;
1241 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001242 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001243
1244 if (!rdev->ops->set_wiphy_params) {
1245 result = -EOPNOTSUPP;
1246 goto bad_res;
1247 }
1248
1249 old_retry_short = rdev->wiphy.retry_short;
1250 old_retry_long = rdev->wiphy.retry_long;
1251 old_frag_threshold = rdev->wiphy.frag_threshold;
1252 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001253 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001254
1255 if (changed & WIPHY_PARAM_RETRY_SHORT)
1256 rdev->wiphy.retry_short = retry_short;
1257 if (changed & WIPHY_PARAM_RETRY_LONG)
1258 rdev->wiphy.retry_long = retry_long;
1259 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1260 rdev->wiphy.frag_threshold = frag_threshold;
1261 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1262 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001263 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1264 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001265
1266 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1267 if (result) {
1268 rdev->wiphy.retry_short = old_retry_short;
1269 rdev->wiphy.retry_long = old_retry_long;
1270 rdev->wiphy.frag_threshold = old_frag_threshold;
1271 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001272 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001273 }
1274 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001275
Johannes Berg306d6112008-12-08 12:39:04 +01001276 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001277 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001278 if (netdev)
1279 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001280 return result;
1281}
1282
1283
1284static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001285 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001286 struct net_device *dev)
1287{
1288 void *hdr;
1289
1290 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1291 if (!hdr)
1292 return -1;
1293
1294 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001295 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001296 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001297 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001298
1299 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1300 rdev->devlist_generation ^
1301 (cfg80211_rdev_list_generation << 2));
1302
Johannes Berg55682962007-09-20 13:09:35 -04001303 return genlmsg_end(msg, hdr);
1304
1305 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001306 genlmsg_cancel(msg, hdr);
1307 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001308}
1309
1310static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1311{
1312 int wp_idx = 0;
1313 int if_idx = 0;
1314 int wp_start = cb->args[0];
1315 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001316 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001317 struct wireless_dev *wdev;
1318
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001319 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001320 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1321 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001322 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001323 if (wp_idx < wp_start) {
1324 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001325 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001326 }
Johannes Berg55682962007-09-20 13:09:35 -04001327 if_idx = 0;
1328
Johannes Bergf5ea9122009-08-07 16:17:38 +02001329 mutex_lock(&rdev->devlist_mtx);
1330 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001331 if (if_idx < if_start) {
1332 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001333 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001334 }
Johannes Berg55682962007-09-20 13:09:35 -04001335 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1336 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001337 rdev, wdev->netdev) < 0) {
1338 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001339 goto out;
1340 }
1341 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001342 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001343 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001344
1345 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001346 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001347 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001348 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001349
1350 cb->args[0] = wp_idx;
1351 cb->args[1] = if_idx;
1352
1353 return skb->len;
1354}
1355
1356static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1357{
1358 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001359 struct cfg80211_registered_device *dev = info->user_ptr[0];
1360 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001361
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001362 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001363 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001364 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001365
Johannes Bergd7264052009-04-19 16:23:20 +02001366 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001367 dev, netdev) < 0) {
1368 nlmsg_free(msg);
1369 return -ENOBUFS;
1370 }
Johannes Berg55682962007-09-20 13:09:35 -04001371
Johannes Berg134e6372009-07-10 09:51:34 +00001372 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001373}
1374
Michael Wu66f7ac52008-01-31 19:48:22 +01001375static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1376 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1377 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1378 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1379 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1380 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1381};
1382
1383static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1384{
1385 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1386 int flag;
1387
1388 *mntrflags = 0;
1389
1390 if (!nla)
1391 return -EINVAL;
1392
1393 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1394 nla, mntr_flags_policy))
1395 return -EINVAL;
1396
1397 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1398 if (flags[flag])
1399 *mntrflags |= (1<<flag);
1400
1401 return 0;
1402}
1403
Johannes Berg9bc383d2009-11-19 11:55:19 +01001404static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001405 struct net_device *netdev, u8 use_4addr,
1406 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001407{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001408 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001409 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001410 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001411 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001412 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001413
1414 switch (iftype) {
1415 case NL80211_IFTYPE_AP_VLAN:
1416 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1417 return 0;
1418 break;
1419 case NL80211_IFTYPE_STATION:
1420 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1421 return 0;
1422 break;
1423 default:
1424 break;
1425 }
1426
1427 return -EOPNOTSUPP;
1428}
1429
Johannes Berg55682962007-09-20 13:09:35 -04001430static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1431{
Johannes Berg4c476992010-10-04 21:36:35 +02001432 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001433 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001434 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001435 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001436 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001437 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001438 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001439
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001440 memset(&params, 0, sizeof(params));
1441
Johannes Berg04a773a2009-04-19 21:24:32 +02001442 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001443
Johannes Berg723b0382008-09-16 20:22:09 +02001444 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001445 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001446 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001447 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001448 if (ntype > NL80211_IFTYPE_MAX)
1449 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001450 }
1451
Johannes Berg92ffe052008-09-16 20:39:36 +02001452 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001453 struct wireless_dev *wdev = dev->ieee80211_ptr;
1454
Johannes Berg4c476992010-10-04 21:36:35 +02001455 if (ntype != NL80211_IFTYPE_MESH_POINT)
1456 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001457 if (netif_running(dev))
1458 return -EBUSY;
1459
1460 wdev_lock(wdev);
1461 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1462 IEEE80211_MAX_MESH_ID_LEN);
1463 wdev->mesh_id_up_len =
1464 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1465 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1466 wdev->mesh_id_up_len);
1467 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001468 }
1469
Felix Fietkau8b787642009-11-10 18:53:10 +01001470 if (info->attrs[NL80211_ATTR_4ADDR]) {
1471 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1472 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001473 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001474 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001475 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001476 } else {
1477 params.use_4addr = -1;
1478 }
1479
Johannes Berg92ffe052008-09-16 20:39:36 +02001480 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001481 if (ntype != NL80211_IFTYPE_MONITOR)
1482 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001483 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1484 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001485 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001486 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001487
1488 flags = &_flags;
1489 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001490 }
Johannes Berg3b858752009-03-12 09:55:09 +01001491
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001492 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001493 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001494 else
1495 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001496
Johannes Berg9bc383d2009-11-19 11:55:19 +01001497 if (!err && params.use_4addr != -1)
1498 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1499
Johannes Berg55682962007-09-20 13:09:35 -04001500 return err;
1501}
1502
1503static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1504{
Johannes Berg4c476992010-10-04 21:36:35 +02001505 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001506 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001507 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001508 int err;
1509 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001510 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001511
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001512 memset(&params, 0, sizeof(params));
1513
Johannes Berg55682962007-09-20 13:09:35 -04001514 if (!info->attrs[NL80211_ATTR_IFNAME])
1515 return -EINVAL;
1516
1517 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1518 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1519 if (type > NL80211_IFTYPE_MAX)
1520 return -EINVAL;
1521 }
1522
Johannes Berg79c97e92009-07-07 03:56:12 +02001523 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001524 !(rdev->wiphy.interface_modes & (1 << type)))
1525 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001526
Johannes Berg9bc383d2009-11-19 11:55:19 +01001527 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001528 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001529 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001530 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001531 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001532 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001533
Michael Wu66f7ac52008-01-31 19:48:22 +01001534 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1535 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1536 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001537 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001538 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001539 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001540 if (IS_ERR(dev))
1541 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001542
Johannes Berg29cbe682010-12-03 09:20:44 +01001543 if (type == NL80211_IFTYPE_MESH_POINT &&
1544 info->attrs[NL80211_ATTR_MESH_ID]) {
1545 struct wireless_dev *wdev = dev->ieee80211_ptr;
1546
1547 wdev_lock(wdev);
1548 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1549 IEEE80211_MAX_MESH_ID_LEN);
1550 wdev->mesh_id_up_len =
1551 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1552 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1553 wdev->mesh_id_up_len);
1554 wdev_unlock(wdev);
1555 }
1556
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001557 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001558}
1559
1560static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1561{
Johannes Berg4c476992010-10-04 21:36:35 +02001562 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1563 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001564
Johannes Berg4c476992010-10-04 21:36:35 +02001565 if (!rdev->ops->del_virtual_intf)
1566 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001567
Johannes Berg4c476992010-10-04 21:36:35 +02001568 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001569}
1570
Johannes Berg41ade002007-12-19 02:03:29 +01001571struct get_key_cookie {
1572 struct sk_buff *msg;
1573 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001574 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001575};
1576
1577static void get_key_callback(void *c, struct key_params *params)
1578{
Johannes Bergb9454e82009-07-08 13:29:08 +02001579 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001580 struct get_key_cookie *cookie = c;
1581
1582 if (params->key)
1583 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1584 params->key_len, params->key);
1585
1586 if (params->seq)
1587 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1588 params->seq_len, params->seq);
1589
1590 if (params->cipher)
1591 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1592 params->cipher);
1593
Johannes Bergb9454e82009-07-08 13:29:08 +02001594 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1595 if (!key)
1596 goto nla_put_failure;
1597
1598 if (params->key)
1599 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1600 params->key_len, params->key);
1601
1602 if (params->seq)
1603 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1604 params->seq_len, params->seq);
1605
1606 if (params->cipher)
1607 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1608 params->cipher);
1609
1610 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1611
1612 nla_nest_end(cookie->msg, key);
1613
Johannes Berg41ade002007-12-19 02:03:29 +01001614 return;
1615 nla_put_failure:
1616 cookie->error = 1;
1617}
1618
1619static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1620{
Johannes Berg4c476992010-10-04 21:36:35 +02001621 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001622 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001623 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001624 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001625 const u8 *mac_addr = NULL;
1626 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001627 struct get_key_cookie cookie = {
1628 .error = 0,
1629 };
1630 void *hdr;
1631 struct sk_buff *msg;
1632
1633 if (info->attrs[NL80211_ATTR_KEY_IDX])
1634 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1635
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001636 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001637 return -EINVAL;
1638
1639 if (info->attrs[NL80211_ATTR_MAC])
1640 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1641
Johannes Berge31b8212010-10-05 19:39:30 +02001642 pairwise = !!mac_addr;
1643 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1644 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1645 if (kt >= NUM_NL80211_KEYTYPES)
1646 return -EINVAL;
1647 if (kt != NL80211_KEYTYPE_GROUP &&
1648 kt != NL80211_KEYTYPE_PAIRWISE)
1649 return -EINVAL;
1650 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1651 }
1652
Johannes Berg4c476992010-10-04 21:36:35 +02001653 if (!rdev->ops->get_key)
1654 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001655
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001656 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001657 if (!msg)
1658 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001659
1660 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1661 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001662 if (IS_ERR(hdr))
1663 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001664
1665 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001666 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001667
1668 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1669 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1670 if (mac_addr)
1671 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1672
Johannes Berge31b8212010-10-05 19:39:30 +02001673 if (pairwise && mac_addr &&
1674 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1675 return -ENOENT;
1676
1677 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1678 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001679
1680 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001681 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001682
1683 if (cookie.error)
1684 goto nla_put_failure;
1685
1686 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001687 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001688
1689 nla_put_failure:
1690 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001691 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001692 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001693 return err;
1694}
1695
1696static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1697{
Johannes Berg4c476992010-10-04 21:36:35 +02001698 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001699 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001700 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001701 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001702
Johannes Bergb9454e82009-07-08 13:29:08 +02001703 err = nl80211_parse_key(info, &key);
1704 if (err)
1705 return err;
1706
1707 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001708 return -EINVAL;
1709
Johannes Bergb9454e82009-07-08 13:29:08 +02001710 /* only support setting default key */
1711 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001712 return -EINVAL;
1713
Johannes Bergfffd0932009-07-08 14:22:54 +02001714 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001715
1716 if (key.def) {
1717 if (!rdev->ops->set_default_key) {
1718 err = -EOPNOTSUPP;
1719 goto out;
1720 }
1721
1722 err = nl80211_key_allowed(dev->ieee80211_ptr);
1723 if (err)
1724 goto out;
1725
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001726 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1727 key.def_uni, key.def_multi);
1728
1729 if (err)
1730 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001731
Johannes Berg3d23e342009-09-29 23:27:28 +02001732#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001733 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001734#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001735 } else {
1736 if (key.def_uni || !key.def_multi) {
1737 err = -EINVAL;
1738 goto out;
1739 }
1740
1741 if (!rdev->ops->set_default_mgmt_key) {
1742 err = -EOPNOTSUPP;
1743 goto out;
1744 }
1745
1746 err = nl80211_key_allowed(dev->ieee80211_ptr);
1747 if (err)
1748 goto out;
1749
1750 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1751 dev, key.idx);
1752 if (err)
1753 goto out;
1754
1755#ifdef CONFIG_CFG80211_WEXT
1756 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1757#endif
1758 }
1759
1760 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001761 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001762
Johannes Berg41ade002007-12-19 02:03:29 +01001763 return err;
1764}
1765
1766static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1767{
Johannes Berg4c476992010-10-04 21:36:35 +02001768 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001769 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001770 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001771 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001772 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001773
Johannes Bergb9454e82009-07-08 13:29:08 +02001774 err = nl80211_parse_key(info, &key);
1775 if (err)
1776 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001777
Johannes Bergb9454e82009-07-08 13:29:08 +02001778 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001779 return -EINVAL;
1780
Johannes Berg41ade002007-12-19 02:03:29 +01001781 if (info->attrs[NL80211_ATTR_MAC])
1782 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1783
Johannes Berge31b8212010-10-05 19:39:30 +02001784 if (key.type == -1) {
1785 if (mac_addr)
1786 key.type = NL80211_KEYTYPE_PAIRWISE;
1787 else
1788 key.type = NL80211_KEYTYPE_GROUP;
1789 }
1790
1791 /* for now */
1792 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1793 key.type != NL80211_KEYTYPE_GROUP)
1794 return -EINVAL;
1795
Johannes Berg4c476992010-10-04 21:36:35 +02001796 if (!rdev->ops->add_key)
1797 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001798
Johannes Berge31b8212010-10-05 19:39:30 +02001799 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1800 key.type == NL80211_KEYTYPE_PAIRWISE,
1801 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001802 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001803
1804 wdev_lock(dev->ieee80211_ptr);
1805 err = nl80211_key_allowed(dev->ieee80211_ptr);
1806 if (!err)
1807 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001808 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001809 mac_addr, &key.p);
1810 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001811
Johannes Berg41ade002007-12-19 02:03:29 +01001812 return err;
1813}
1814
1815static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1816{
Johannes Berg4c476992010-10-04 21:36:35 +02001817 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001818 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001819 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001820 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001821 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001822
Johannes Bergb9454e82009-07-08 13:29:08 +02001823 err = nl80211_parse_key(info, &key);
1824 if (err)
1825 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001826
1827 if (info->attrs[NL80211_ATTR_MAC])
1828 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1829
Johannes Berge31b8212010-10-05 19:39:30 +02001830 if (key.type == -1) {
1831 if (mac_addr)
1832 key.type = NL80211_KEYTYPE_PAIRWISE;
1833 else
1834 key.type = NL80211_KEYTYPE_GROUP;
1835 }
1836
1837 /* for now */
1838 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1839 key.type != NL80211_KEYTYPE_GROUP)
1840 return -EINVAL;
1841
Johannes Berg4c476992010-10-04 21:36:35 +02001842 if (!rdev->ops->del_key)
1843 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001844
Johannes Bergfffd0932009-07-08 14:22:54 +02001845 wdev_lock(dev->ieee80211_ptr);
1846 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001847
1848 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1849 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1850 err = -ENOENT;
1851
Johannes Bergfffd0932009-07-08 14:22:54 +02001852 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001853 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1854 key.type == NL80211_KEYTYPE_PAIRWISE,
1855 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001856
Johannes Berg3d23e342009-09-29 23:27:28 +02001857#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001858 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001859 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001860 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001861 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001862 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1863 }
1864#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001865 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001866
Johannes Berg41ade002007-12-19 02:03:29 +01001867 return err;
1868}
1869
Johannes Berged1b6cc2007-12-19 02:03:32 +01001870static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1871{
1872 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1873 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001874 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1875 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001876 struct beacon_parameters params;
1877 int haveinfo = 0;
1878
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001879 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1880 return -EINVAL;
1881
Johannes Berg074ac8d2010-09-16 14:58:22 +02001882 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001883 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1884 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001885
Johannes Berged1b6cc2007-12-19 02:03:32 +01001886 switch (info->genlhdr->cmd) {
1887 case NL80211_CMD_NEW_BEACON:
1888 /* these are required for NEW_BEACON */
1889 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1890 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001891 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1892 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001893
Johannes Berg79c97e92009-07-07 03:56:12 +02001894 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001895 break;
1896 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001897 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001898 break;
1899 default:
1900 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001901 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001902 }
1903
Johannes Berg4c476992010-10-04 21:36:35 +02001904 if (!call)
1905 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001906
1907 memset(&params, 0, sizeof(params));
1908
1909 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1910 params.interval =
1911 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1912 haveinfo = 1;
1913 }
1914
1915 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1916 params.dtim_period =
1917 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1918 haveinfo = 1;
1919 }
1920
1921 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1922 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1923 params.head_len =
1924 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1925 haveinfo = 1;
1926 }
1927
1928 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1929 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1930 params.tail_len =
1931 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1932 haveinfo = 1;
1933 }
1934
Johannes Berg4c476992010-10-04 21:36:35 +02001935 if (!haveinfo)
1936 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001937
Johannes Berg4c476992010-10-04 21:36:35 +02001938 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001939}
1940
1941static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1942{
Johannes Berg4c476992010-10-04 21:36:35 +02001943 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1944 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001945
Johannes Berg4c476992010-10-04 21:36:35 +02001946 if (!rdev->ops->del_beacon)
1947 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001948
Johannes Berg074ac8d2010-09-16 14:58:22 +02001949 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001950 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1951 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001952
Johannes Berg4c476992010-10-04 21:36:35 +02001953 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001954}
1955
Johannes Berg5727ef12007-12-19 02:03:34 +01001956static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1957 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1958 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1959 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001960 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07001961 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001962};
1963
Johannes Bergeccb8e82009-05-11 21:57:56 +03001964static int parse_station_flags(struct genl_info *info,
1965 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001966{
1967 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001968 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001969 int flag;
1970
Johannes Bergeccb8e82009-05-11 21:57:56 +03001971 /*
1972 * Try parsing the new attribute first so userspace
1973 * can specify both for older kernels.
1974 */
1975 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1976 if (nla) {
1977 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001978
Johannes Bergeccb8e82009-05-11 21:57:56 +03001979 sta_flags = nla_data(nla);
1980 params->sta_flags_mask = sta_flags->mask;
1981 params->sta_flags_set = sta_flags->set;
1982 if ((params->sta_flags_mask |
1983 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1984 return -EINVAL;
1985 return 0;
1986 }
1987
1988 /* if present, parse the old attribute */
1989
1990 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001991 if (!nla)
1992 return 0;
1993
1994 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1995 nla, sta_flags_policy))
1996 return -EINVAL;
1997
Johannes Bergeccb8e82009-05-11 21:57:56 +03001998 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1999 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002000
2001 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2002 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002003 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002004
2005 return 0;
2006}
2007
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002008static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2009 int attr)
2010{
2011 struct nlattr *rate;
2012 u16 bitrate;
2013
2014 rate = nla_nest_start(msg, attr);
2015 if (!rate)
2016 goto nla_put_failure;
2017
2018 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2019 bitrate = cfg80211_calculate_bitrate(info);
2020 if (bitrate > 0)
2021 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2022
2023 if (info->flags & RATE_INFO_FLAGS_MCS)
2024 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2025 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2026 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2027 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2028 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2029
2030 nla_nest_end(msg, rate);
2031 return true;
2032
2033nla_put_failure:
2034 return false;
2035}
2036
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002037static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2038 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002039 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002040{
2041 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002042 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002043
2044 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2045 if (!hdr)
2046 return -1;
2047
2048 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2049 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2050
Johannes Bergf5ea9122009-08-07 16:17:38 +02002051 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2052
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002053 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2054 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002055 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302056 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2057 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2058 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002059 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2060 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2061 sinfo->inactive_time);
2062 if (sinfo->filled & STATION_INFO_RX_BYTES)
2063 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2064 sinfo->rx_bytes);
2065 if (sinfo->filled & STATION_INFO_TX_BYTES)
2066 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2067 sinfo->tx_bytes);
2068 if (sinfo->filled & STATION_INFO_LLID)
2069 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2070 sinfo->llid);
2071 if (sinfo->filled & STATION_INFO_PLID)
2072 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2073 sinfo->plid);
2074 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2075 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2076 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002077 if (sinfo->filled & STATION_INFO_SIGNAL)
2078 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2079 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002080 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2081 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2082 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002083 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002084 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2085 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002086 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002087 }
2088 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2089 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2090 NL80211_STA_INFO_RX_BITRATE))
2091 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002092 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002093 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2094 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2095 sinfo->rx_packets);
2096 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2097 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2098 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002099 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2100 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2101 sinfo->tx_retries);
2102 if (sinfo->filled & STATION_INFO_TX_FAILED)
2103 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2104 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002105 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2106 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2107 if (!bss_param)
2108 goto nla_put_failure;
2109
2110 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2111 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2112 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2113 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2114 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2115 NLA_PUT_FLAG(msg,
2116 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2117 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2118 sinfo->bss_param.dtim_period);
2119 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2120 sinfo->bss_param.beacon_interval);
2121
2122 nla_nest_end(msg, bss_param);
2123 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002124 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002125
2126 return genlmsg_end(msg, hdr);
2127
2128 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002129 genlmsg_cancel(msg, hdr);
2130 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002131}
2132
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002133static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002134 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002135{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002136 struct station_info sinfo;
2137 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002138 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002139 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002140 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002141 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002142
Johannes Berg67748892010-10-04 21:14:06 +02002143 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2144 if (err)
2145 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002146
2147 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002148 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002149 goto out_err;
2150 }
2151
Johannes Bergbba95fe2008-07-29 13:22:51 +02002152 while (1) {
2153 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2154 mac_addr, &sinfo);
2155 if (err == -ENOENT)
2156 break;
2157 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002158 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002159
2160 if (nl80211_send_station(skb,
2161 NETLINK_CB(cb->skb).pid,
2162 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2163 netdev, mac_addr,
2164 &sinfo) < 0)
2165 goto out;
2166
2167 sta_idx++;
2168 }
2169
2170
2171 out:
2172 cb->args[1] = sta_idx;
2173 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002174 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002175 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002176
2177 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002178}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002179
Johannes Berg5727ef12007-12-19 02:03:34 +01002180static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2181{
Johannes Berg4c476992010-10-04 21:36:35 +02002182 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2183 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002184 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002185 struct sk_buff *msg;
2186 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002187 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002188
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002189 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002190
2191 if (!info->attrs[NL80211_ATTR_MAC])
2192 return -EINVAL;
2193
2194 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2195
Johannes Berg4c476992010-10-04 21:36:35 +02002196 if (!rdev->ops->get_station)
2197 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002198
Johannes Berg79c97e92009-07-07 03:56:12 +02002199 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002200 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002201 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002202
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002203 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002204 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002205 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002206
2207 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002208 dev, mac_addr, &sinfo) < 0) {
2209 nlmsg_free(msg);
2210 return -ENOBUFS;
2211 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002212
Johannes Berg4c476992010-10-04 21:36:35 +02002213 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002214}
2215
2216/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002217 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002218 */
Johannes Berg463d0182009-07-14 00:33:35 +02002219static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002220 struct cfg80211_registered_device *rdev,
2221 struct net_device **vlan)
2222{
Johannes Berg463d0182009-07-14 00:33:35 +02002223 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002224 *vlan = NULL;
2225
2226 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002227 *vlan = dev_get_by_index(genl_info_net(info),
2228 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002229 if (!*vlan)
2230 return -ENODEV;
2231 if (!(*vlan)->ieee80211_ptr)
2232 return -EINVAL;
2233 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2234 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002235 if (!netif_running(*vlan))
2236 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002237 }
2238 return 0;
2239}
2240
2241static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2242{
Johannes Berg4c476992010-10-04 21:36:35 +02002243 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002244 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002245 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002246 struct station_parameters params;
2247 u8 *mac_addr = NULL;
2248
2249 memset(&params, 0, sizeof(params));
2250
2251 params.listen_interval = -1;
Javier Cardona9c3990a2011-05-03 16:57:11 -07002252 params.plink_state = PLINK_INVALID;
Johannes Berg5727ef12007-12-19 02:03:34 +01002253
2254 if (info->attrs[NL80211_ATTR_STA_AID])
2255 return -EINVAL;
2256
2257 if (!info->attrs[NL80211_ATTR_MAC])
2258 return -EINVAL;
2259
2260 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2261
2262 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2263 params.supported_rates =
2264 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2265 params.supported_rates_len =
2266 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2267 }
2268
2269 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2270 params.listen_interval =
2271 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2272
Jouni Malinen36aedc92008-08-25 11:58:58 +03002273 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2274 params.ht_capa =
2275 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2276
Johannes Bergeccb8e82009-05-11 21:57:56 +03002277 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002278 return -EINVAL;
2279
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002280 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2281 params.plink_action =
2282 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2283
Javier Cardona9c3990a2011-05-03 16:57:11 -07002284 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2285 params.plink_state =
2286 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2287
Johannes Berg463d0182009-07-14 00:33:35 +02002288 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002289 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002290 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002291
2292 /* validate settings */
2293 err = 0;
2294
2295 switch (dev->ieee80211_ptr->iftype) {
2296 case NL80211_IFTYPE_AP:
2297 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002298 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002299 /* disallow mesh-specific things */
2300 if (params.plink_action)
2301 err = -EINVAL;
2302 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002303 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002304 case NL80211_IFTYPE_STATION:
2305 /* disallow everything but AUTHORIZED flag */
2306 if (params.plink_action)
2307 err = -EINVAL;
2308 if (params.vlan)
2309 err = -EINVAL;
2310 if (params.supported_rates)
2311 err = -EINVAL;
2312 if (params.ht_capa)
2313 err = -EINVAL;
2314 if (params.listen_interval >= 0)
2315 err = -EINVAL;
2316 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2317 err = -EINVAL;
2318 break;
2319 case NL80211_IFTYPE_MESH_POINT:
2320 /* disallow things mesh doesn't support */
2321 if (params.vlan)
2322 err = -EINVAL;
2323 if (params.ht_capa)
2324 err = -EINVAL;
2325 if (params.listen_interval >= 0)
2326 err = -EINVAL;
2327 if (params.supported_rates)
2328 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002329 if (params.sta_flags_mask &
2330 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002331 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002332 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002333 err = -EINVAL;
2334 break;
2335 default:
2336 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002337 }
2338
Johannes Berg5727ef12007-12-19 02:03:34 +01002339 if (err)
2340 goto out;
2341
Johannes Berg79c97e92009-07-07 03:56:12 +02002342 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002343 err = -EOPNOTSUPP;
2344 goto out;
2345 }
2346
Johannes Berg79c97e92009-07-07 03:56:12 +02002347 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002348
2349 out:
2350 if (params.vlan)
2351 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002352
Johannes Berg5727ef12007-12-19 02:03:34 +01002353 return err;
2354}
2355
2356static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2357{
Johannes Berg4c476992010-10-04 21:36:35 +02002358 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002359 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002360 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002361 struct station_parameters params;
2362 u8 *mac_addr = NULL;
2363
2364 memset(&params, 0, sizeof(params));
2365
2366 if (!info->attrs[NL80211_ATTR_MAC])
2367 return -EINVAL;
2368
Johannes Berg5727ef12007-12-19 02:03:34 +01002369 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2370 return -EINVAL;
2371
2372 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2373 return -EINVAL;
2374
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002375 if (!info->attrs[NL80211_ATTR_STA_AID])
2376 return -EINVAL;
2377
Johannes Berg5727ef12007-12-19 02:03:34 +01002378 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2379 params.supported_rates =
2380 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2381 params.supported_rates_len =
2382 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2383 params.listen_interval =
2384 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002385
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002386 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2387 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2388 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002389
Jouni Malinen36aedc92008-08-25 11:58:58 +03002390 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2391 params.ht_capa =
2392 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002393
Javier Cardona96b78df2011-04-07 15:08:33 -07002394 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2395 params.plink_action =
2396 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2397
Johannes Bergeccb8e82009-05-11 21:57:56 +03002398 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002399 return -EINVAL;
2400
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002401 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002402 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002403 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002404 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2405 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002406
Johannes Berg463d0182009-07-14 00:33:35 +02002407 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002408 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002409 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002410
2411 /* validate settings */
2412 err = 0;
2413
Johannes Berg79c97e92009-07-07 03:56:12 +02002414 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002415 err = -EOPNOTSUPP;
2416 goto out;
2417 }
2418
Johannes Berg79c97e92009-07-07 03:56:12 +02002419 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002420
2421 out:
2422 if (params.vlan)
2423 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002424 return err;
2425}
2426
2427static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2428{
Johannes Berg4c476992010-10-04 21:36:35 +02002429 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2430 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002431 u8 *mac_addr = NULL;
2432
2433 if (info->attrs[NL80211_ATTR_MAC])
2434 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2435
Johannes Berge80cf852009-05-11 14:43:13 +02002436 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002437 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002438 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002439 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2440 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002441
Johannes Berg4c476992010-10-04 21:36:35 +02002442 if (!rdev->ops->del_station)
2443 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002444
Johannes Berg4c476992010-10-04 21:36:35 +02002445 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002446}
2447
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002448static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2449 int flags, struct net_device *dev,
2450 u8 *dst, u8 *next_hop,
2451 struct mpath_info *pinfo)
2452{
2453 void *hdr;
2454 struct nlattr *pinfoattr;
2455
2456 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2457 if (!hdr)
2458 return -1;
2459
2460 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2461 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2462 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2463
Johannes Bergf5ea9122009-08-07 16:17:38 +02002464 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2465
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002466 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2467 if (!pinfoattr)
2468 goto nla_put_failure;
2469 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2470 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2471 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002472 if (pinfo->filled & MPATH_INFO_SN)
2473 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2474 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002475 if (pinfo->filled & MPATH_INFO_METRIC)
2476 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2477 pinfo->metric);
2478 if (pinfo->filled & MPATH_INFO_EXPTIME)
2479 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2480 pinfo->exptime);
2481 if (pinfo->filled & MPATH_INFO_FLAGS)
2482 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2483 pinfo->flags);
2484 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2485 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2486 pinfo->discovery_timeout);
2487 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2488 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2489 pinfo->discovery_retries);
2490
2491 nla_nest_end(msg, pinfoattr);
2492
2493 return genlmsg_end(msg, hdr);
2494
2495 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002496 genlmsg_cancel(msg, hdr);
2497 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002498}
2499
2500static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002501 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002502{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002503 struct mpath_info pinfo;
2504 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002505 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002506 u8 dst[ETH_ALEN];
2507 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002508 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002509 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002510
Johannes Berg67748892010-10-04 21:14:06 +02002511 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2512 if (err)
2513 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002514
2515 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002516 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002517 goto out_err;
2518 }
2519
Jouni Malineneec60b02009-03-20 21:21:19 +02002520 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2521 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002522 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002523 }
2524
Johannes Bergbba95fe2008-07-29 13:22:51 +02002525 while (1) {
2526 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2527 dst, next_hop, &pinfo);
2528 if (err == -ENOENT)
2529 break;
2530 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002531 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002532
2533 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2534 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2535 netdev, dst, next_hop,
2536 &pinfo) < 0)
2537 goto out;
2538
2539 path_idx++;
2540 }
2541
2542
2543 out:
2544 cb->args[1] = path_idx;
2545 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002546 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002547 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002548 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002549}
2550
2551static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2552{
Johannes Berg4c476992010-10-04 21:36:35 +02002553 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002554 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002555 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002556 struct mpath_info pinfo;
2557 struct sk_buff *msg;
2558 u8 *dst = NULL;
2559 u8 next_hop[ETH_ALEN];
2560
2561 memset(&pinfo, 0, sizeof(pinfo));
2562
2563 if (!info->attrs[NL80211_ATTR_MAC])
2564 return -EINVAL;
2565
2566 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2567
Johannes Berg4c476992010-10-04 21:36:35 +02002568 if (!rdev->ops->get_mpath)
2569 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002570
Johannes Berg4c476992010-10-04 21:36:35 +02002571 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2572 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002573
Johannes Berg79c97e92009-07-07 03:56:12 +02002574 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002575 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002576 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002577
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002578 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002579 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002580 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002581
2582 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002583 dev, dst, next_hop, &pinfo) < 0) {
2584 nlmsg_free(msg);
2585 return -ENOBUFS;
2586 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002587
Johannes Berg4c476992010-10-04 21:36:35 +02002588 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002589}
2590
2591static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2592{
Johannes Berg4c476992010-10-04 21:36:35 +02002593 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2594 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002595 u8 *dst = NULL;
2596 u8 *next_hop = NULL;
2597
2598 if (!info->attrs[NL80211_ATTR_MAC])
2599 return -EINVAL;
2600
2601 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2602 return -EINVAL;
2603
2604 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2605 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2606
Johannes Berg4c476992010-10-04 21:36:35 +02002607 if (!rdev->ops->change_mpath)
2608 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002609
Johannes Berg4c476992010-10-04 21:36:35 +02002610 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2611 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002612
Johannes Berg4c476992010-10-04 21:36:35 +02002613 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002614}
Johannes Berg4c476992010-10-04 21:36:35 +02002615
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002616static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2617{
Johannes Berg4c476992010-10-04 21:36:35 +02002618 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2619 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002620 u8 *dst = NULL;
2621 u8 *next_hop = NULL;
2622
2623 if (!info->attrs[NL80211_ATTR_MAC])
2624 return -EINVAL;
2625
2626 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2627 return -EINVAL;
2628
2629 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2630 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2631
Johannes Berg4c476992010-10-04 21:36:35 +02002632 if (!rdev->ops->add_mpath)
2633 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002634
Johannes Berg4c476992010-10-04 21:36:35 +02002635 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2636 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002637
Johannes Berg4c476992010-10-04 21:36:35 +02002638 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002639}
2640
2641static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2642{
Johannes Berg4c476992010-10-04 21:36:35 +02002643 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2644 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002645 u8 *dst = NULL;
2646
2647 if (info->attrs[NL80211_ATTR_MAC])
2648 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2649
Johannes Berg4c476992010-10-04 21:36:35 +02002650 if (!rdev->ops->del_mpath)
2651 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002652
Johannes Berg4c476992010-10-04 21:36:35 +02002653 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002654}
2655
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002656static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2657{
Johannes Berg4c476992010-10-04 21:36:35 +02002658 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2659 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002660 struct bss_parameters params;
2661
2662 memset(&params, 0, sizeof(params));
2663 /* default to not changing parameters */
2664 params.use_cts_prot = -1;
2665 params.use_short_preamble = -1;
2666 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002667 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002668 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002669
2670 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2671 params.use_cts_prot =
2672 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2673 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2674 params.use_short_preamble =
2675 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2676 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2677 params.use_short_slot_time =
2678 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002679 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2680 params.basic_rates =
2681 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2682 params.basic_rates_len =
2683 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2684 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002685 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2686 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002687 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2688 params.ht_opmode =
2689 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002690
Johannes Berg4c476992010-10-04 21:36:35 +02002691 if (!rdev->ops->change_bss)
2692 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002693
Johannes Berg074ac8d2010-09-16 14:58:22 +02002694 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002695 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2696 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002697
Johannes Berg4c476992010-10-04 21:36:35 +02002698 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002699}
2700
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002701static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002702 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2703 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2704 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2705 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2706 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2707 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2708};
2709
2710static int parse_reg_rule(struct nlattr *tb[],
2711 struct ieee80211_reg_rule *reg_rule)
2712{
2713 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2714 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2715
2716 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2717 return -EINVAL;
2718 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2719 return -EINVAL;
2720 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2721 return -EINVAL;
2722 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2723 return -EINVAL;
2724 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2725 return -EINVAL;
2726
2727 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2728
2729 freq_range->start_freq_khz =
2730 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2731 freq_range->end_freq_khz =
2732 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2733 freq_range->max_bandwidth_khz =
2734 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2735
2736 power_rule->max_eirp =
2737 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2738
2739 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2740 power_rule->max_antenna_gain =
2741 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2742
2743 return 0;
2744}
2745
2746static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2747{
2748 int r;
2749 char *data = NULL;
2750
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002751 /*
2752 * You should only get this when cfg80211 hasn't yet initialized
2753 * completely when built-in to the kernel right between the time
2754 * window between nl80211_init() and regulatory_init(), if that is
2755 * even possible.
2756 */
2757 mutex_lock(&cfg80211_mutex);
2758 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002759 mutex_unlock(&cfg80211_mutex);
2760 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002761 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002762 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002763
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002764 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2765 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002766
2767 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2768
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002769 r = regulatory_hint_user(data);
2770
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002771 return r;
2772}
2773
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002774static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002775 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002776{
Johannes Berg4c476992010-10-04 21:36:35 +02002777 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02002778 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002779 struct wireless_dev *wdev = dev->ieee80211_ptr;
2780 struct mesh_config cur_params;
2781 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002782 void *hdr;
2783 struct nlattr *pinfoattr;
2784 struct sk_buff *msg;
2785
Johannes Berg29cbe682010-12-03 09:20:44 +01002786 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2787 return -EOPNOTSUPP;
2788
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002789 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02002790 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002791
Johannes Berg29cbe682010-12-03 09:20:44 +01002792 wdev_lock(wdev);
2793 /* If not connected, get default parameters */
2794 if (!wdev->mesh_id_len)
2795 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
2796 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002797 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01002798 &cur_params);
2799 wdev_unlock(wdev);
2800
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002801 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002802 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002803
2804 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002805 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002806 if (!msg)
2807 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002808 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002809 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002810 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01002811 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002812 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002813 if (!pinfoattr)
2814 goto nla_put_failure;
2815 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2816 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2817 cur_params.dot11MeshRetryTimeout);
2818 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2819 cur_params.dot11MeshConfirmTimeout);
2820 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2821 cur_params.dot11MeshHoldingTimeout);
2822 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2823 cur_params.dot11MeshMaxPeerLinks);
2824 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2825 cur_params.dot11MeshMaxRetries);
2826 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2827 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01002828 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
2829 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002830 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2831 cur_params.auto_open_plinks);
2832 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2833 cur_params.dot11MeshHWMPmaxPREQretries);
2834 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2835 cur_params.path_refresh_time);
2836 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2837 cur_params.min_discovery_timeout);
2838 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2839 cur_params.dot11MeshHWMPactivePathTimeout);
2840 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2841 cur_params.dot11MeshHWMPpreqMinInterval);
2842 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2843 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002844 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2845 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002846 nla_nest_end(msg, pinfoattr);
2847 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002848 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002849
Johannes Berg3b858752009-03-12 09:55:09 +01002850 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002851 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01002852 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04002853 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002854 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002855}
2856
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002857static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002858 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2859 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2860 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2861 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2862 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2863 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01002864 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002865 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2866
2867 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2868 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2869 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2870 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2871 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2872 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2873};
2874
Javier Cardonac80d5452010-12-16 17:37:49 -08002875static const struct nla_policy
2876 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
2877 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
2878 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07002879 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07002880 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08002881 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07002882 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08002883};
2884
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002885static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002886 struct mesh_config *cfg,
2887 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002888{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002889 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002890 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002891
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002892#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2893do {\
2894 if (table[attr_num]) {\
2895 cfg->param = nla_fn(table[attr_num]); \
2896 mask |= (1 << (attr_num - 1)); \
2897 } \
2898} while (0);\
2899
2900
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002901 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002902 return -EINVAL;
2903 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002904 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002905 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002906 return -EINVAL;
2907
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002908 /* This makes sure that there aren't more than 32 mesh config
2909 * parameters (otherwise our bitfield scheme would not work.) */
2910 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2911
2912 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002913 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2914 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2915 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2916 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2917 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2918 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2919 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2920 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2921 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2922 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2923 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2924 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01002925 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
2926 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002927 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2928 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2929 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2930 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2931 nla_get_u8);
2932 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2933 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2934 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2935 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2936 nla_get_u16);
2937 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2938 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2939 nla_get_u32);
2940 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2941 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2942 nla_get_u16);
2943 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2944 dot11MeshHWMPnetDiameterTraversalTime,
2945 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2946 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002947 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2948 dot11MeshHWMPRootMode, mask,
2949 NL80211_MESHCONF_HWMP_ROOTMODE,
2950 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002951 if (mask_out)
2952 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08002953
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002954 return 0;
2955
2956#undef FILL_IN_MESH_PARAM_IF_SET
2957}
2958
Javier Cardonac80d5452010-12-16 17:37:49 -08002959static int nl80211_parse_mesh_setup(struct genl_info *info,
2960 struct mesh_setup *setup)
2961{
2962 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
2963
2964 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
2965 return -EINVAL;
2966 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
2967 info->attrs[NL80211_ATTR_MESH_SETUP],
2968 nl80211_mesh_setup_params_policy))
2969 return -EINVAL;
2970
2971 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
2972 setup->path_sel_proto =
2973 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
2974 IEEE80211_PATH_PROTOCOL_VENDOR :
2975 IEEE80211_PATH_PROTOCOL_HWMP;
2976
2977 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
2978 setup->path_metric =
2979 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
2980 IEEE80211_PATH_METRIC_VENDOR :
2981 IEEE80211_PATH_METRIC_AIRTIME;
2982
Javier Cardona581a8b02011-04-07 15:08:27 -07002983
2984 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08002985 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07002986 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08002987 if (!is_valid_ie_attr(ieattr))
2988 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07002989 setup->ie = nla_data(ieattr);
2990 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08002991 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07002992 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
2993 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08002994
2995 return 0;
2996}
2997
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002998static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002999 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003000{
3001 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3002 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003003 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003004 struct mesh_config cfg;
3005 u32 mask;
3006 int err;
3007
Johannes Berg29cbe682010-12-03 09:20:44 +01003008 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3009 return -EOPNOTSUPP;
3010
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003011 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003012 return -EOPNOTSUPP;
3013
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003014 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003015 if (err)
3016 return err;
3017
Johannes Berg29cbe682010-12-03 09:20:44 +01003018 wdev_lock(wdev);
3019 if (!wdev->mesh_id_len)
3020 err = -ENOLINK;
3021
3022 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003023 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003024 mask, &cfg);
3025
3026 wdev_unlock(wdev);
3027
3028 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003029}
3030
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003031static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3032{
3033 struct sk_buff *msg;
3034 void *hdr = NULL;
3035 struct nlattr *nl_reg_rules;
3036 unsigned int i;
3037 int err = -EINVAL;
3038
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003039 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003040
3041 if (!cfg80211_regdomain)
3042 goto out;
3043
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003044 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003045 if (!msg) {
3046 err = -ENOBUFS;
3047 goto out;
3048 }
3049
3050 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3051 NL80211_CMD_GET_REG);
3052 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003053 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003054
3055 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3056 cfg80211_regdomain->alpha2);
3057
3058 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3059 if (!nl_reg_rules)
3060 goto nla_put_failure;
3061
3062 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3063 struct nlattr *nl_reg_rule;
3064 const struct ieee80211_reg_rule *reg_rule;
3065 const struct ieee80211_freq_range *freq_range;
3066 const struct ieee80211_power_rule *power_rule;
3067
3068 reg_rule = &cfg80211_regdomain->reg_rules[i];
3069 freq_range = &reg_rule->freq_range;
3070 power_rule = &reg_rule->power_rule;
3071
3072 nl_reg_rule = nla_nest_start(msg, i);
3073 if (!nl_reg_rule)
3074 goto nla_put_failure;
3075
3076 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3077 reg_rule->flags);
3078 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3079 freq_range->start_freq_khz);
3080 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3081 freq_range->end_freq_khz);
3082 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3083 freq_range->max_bandwidth_khz);
3084 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3085 power_rule->max_antenna_gain);
3086 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3087 power_rule->max_eirp);
3088
3089 nla_nest_end(msg, nl_reg_rule);
3090 }
3091
3092 nla_nest_end(msg, nl_reg_rules);
3093
3094 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003095 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003096 goto out;
3097
3098nla_put_failure:
3099 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003100put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003101 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003102 err = -EMSGSIZE;
3103out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003104 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003105 return err;
3106}
3107
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003108static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3109{
3110 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3111 struct nlattr *nl_reg_rule;
3112 char *alpha2 = NULL;
3113 int rem_reg_rules = 0, r = 0;
3114 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3115 struct ieee80211_regdomain *rd = NULL;
3116
3117 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3118 return -EINVAL;
3119
3120 if (!info->attrs[NL80211_ATTR_REG_RULES])
3121 return -EINVAL;
3122
3123 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3124
3125 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3126 rem_reg_rules) {
3127 num_rules++;
3128 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003129 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003130 }
3131
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003132 mutex_lock(&cfg80211_mutex);
3133
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003134 if (!reg_is_valid_request(alpha2)) {
3135 r = -EINVAL;
3136 goto bad_reg;
3137 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003138
3139 size_of_regd = sizeof(struct ieee80211_regdomain) +
3140 (num_rules * sizeof(struct ieee80211_reg_rule));
3141
3142 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003143 if (!rd) {
3144 r = -ENOMEM;
3145 goto bad_reg;
3146 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003147
3148 rd->n_reg_rules = num_rules;
3149 rd->alpha2[0] = alpha2[0];
3150 rd->alpha2[1] = alpha2[1];
3151
3152 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3153 rem_reg_rules) {
3154 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3155 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3156 reg_rule_policy);
3157 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3158 if (r)
3159 goto bad_reg;
3160
3161 rule_idx++;
3162
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003163 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3164 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003165 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003166 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003167 }
3168
3169 BUG_ON(rule_idx != num_rules);
3170
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003171 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003172
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003173 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003174
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003175 return r;
3176
Johannes Bergd2372b32008-10-24 20:32:20 +02003177 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003178 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003179 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003180 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003181}
3182
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003183static int validate_scan_freqs(struct nlattr *freqs)
3184{
3185 struct nlattr *attr1, *attr2;
3186 int n_channels = 0, tmp1, tmp2;
3187
3188 nla_for_each_nested(attr1, freqs, tmp1) {
3189 n_channels++;
3190 /*
3191 * Some hardware has a limited channel list for
3192 * scanning, and it is pretty much nonsensical
3193 * to scan for a channel twice, so disallow that
3194 * and don't require drivers to check that the
3195 * channel list they get isn't longer than what
3196 * they can scan, as long as they can scan all
3197 * the channels they registered at once.
3198 */
3199 nla_for_each_nested(attr2, freqs, tmp2)
3200 if (attr1 != attr2 &&
3201 nla_get_u32(attr1) == nla_get_u32(attr2))
3202 return 0;
3203 }
3204
3205 return n_channels;
3206}
3207
Johannes Berg2a519312009-02-10 21:25:55 +01003208static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3209{
Johannes Berg4c476992010-10-04 21:36:35 +02003210 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3211 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003212 struct cfg80211_scan_request *request;
3213 struct cfg80211_ssid *ssid;
3214 struct ieee80211_channel *channel;
3215 struct nlattr *attr;
3216 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003217 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01003218 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003219 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003220
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003221 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3222 return -EINVAL;
3223
Johannes Berg79c97e92009-07-07 03:56:12 +02003224 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003225
Johannes Berg4c476992010-10-04 21:36:35 +02003226 if (!rdev->ops->scan)
3227 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003228
Johannes Berg4c476992010-10-04 21:36:35 +02003229 if (rdev->scan_req)
3230 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003231
3232 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003233 n_channels = validate_scan_freqs(
3234 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003235 if (!n_channels)
3236 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003237 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003238 n_channels = 0;
3239
Johannes Berg2a519312009-02-10 21:25:55 +01003240 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3241 if (wiphy->bands[band])
3242 n_channels += wiphy->bands[band]->n_channels;
3243 }
3244
3245 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3246 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3247 n_ssids++;
3248
Johannes Berg4c476992010-10-04 21:36:35 +02003249 if (n_ssids > wiphy->max_scan_ssids)
3250 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003251
Jouni Malinen70692ad2009-02-16 19:39:13 +02003252 if (info->attrs[NL80211_ATTR_IE])
3253 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3254 else
3255 ie_len = 0;
3256
Johannes Berg4c476992010-10-04 21:36:35 +02003257 if (ie_len > wiphy->max_scan_ie_len)
3258 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003259
Johannes Berg2a519312009-02-10 21:25:55 +01003260 request = kzalloc(sizeof(*request)
3261 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003262 + sizeof(channel) * n_channels
3263 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003264 if (!request)
3265 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003266
Johannes Berg2a519312009-02-10 21:25:55 +01003267 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003268 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003269 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003270 if (ie_len) {
3271 if (request->ssids)
3272 request->ie = (void *)(request->ssids + n_ssids);
3273 else
3274 request->ie = (void *)(request->channels + n_channels);
3275 }
Johannes Berg2a519312009-02-10 21:25:55 +01003276
Johannes Berg584991d2009-11-02 13:32:03 +01003277 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003278 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3279 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003280 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003281 struct ieee80211_channel *chan;
3282
3283 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3284
3285 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003286 err = -EINVAL;
3287 goto out_free;
3288 }
Johannes Berg584991d2009-11-02 13:32:03 +01003289
3290 /* ignore disabled channels */
3291 if (chan->flags & IEEE80211_CHAN_DISABLED)
3292 continue;
3293
3294 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003295 i++;
3296 }
3297 } else {
3298 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003299 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3300 int j;
3301 if (!wiphy->bands[band])
3302 continue;
3303 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003304 struct ieee80211_channel *chan;
3305
3306 chan = &wiphy->bands[band]->channels[j];
3307
3308 if (chan->flags & IEEE80211_CHAN_DISABLED)
3309 continue;
3310
3311 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003312 i++;
3313 }
3314 }
3315 }
3316
Johannes Berg584991d2009-11-02 13:32:03 +01003317 if (!i) {
3318 err = -EINVAL;
3319 goto out_free;
3320 }
3321
3322 request->n_channels = i;
3323
Johannes Berg2a519312009-02-10 21:25:55 +01003324 i = 0;
3325 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3326 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3327 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3328 err = -EINVAL;
3329 goto out_free;
3330 }
3331 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3332 request->ssids[i].ssid_len = nla_len(attr);
3333 i++;
3334 }
3335 }
3336
Jouni Malinen70692ad2009-02-16 19:39:13 +02003337 if (info->attrs[NL80211_ATTR_IE]) {
3338 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003339 memcpy((void *)request->ie,
3340 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003341 request->ie_len);
3342 }
3343
Johannes Berg463d0182009-07-14 00:33:35 +02003344 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003345 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003346
Johannes Berg79c97e92009-07-07 03:56:12 +02003347 rdev->scan_req = request;
3348 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003349
Johannes Berg463d0182009-07-14 00:33:35 +02003350 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003351 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003352 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003353 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003354 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003355 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003356 kfree(request);
3357 }
Johannes Berg3b858752009-03-12 09:55:09 +01003358
Johannes Berg2a519312009-02-10 21:25:55 +01003359 return err;
3360}
3361
3362static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3363 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003364 struct wireless_dev *wdev,
3365 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003366{
Johannes Berg48ab9052009-07-10 18:42:31 +02003367 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003368 void *hdr;
3369 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003370 int i;
3371
3372 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003373
3374 hdr = nl80211hdr_put(msg, pid, seq, flags,
3375 NL80211_CMD_NEW_SCAN_RESULTS);
3376 if (!hdr)
3377 return -1;
3378
Johannes Bergf5ea9122009-08-07 16:17:38 +02003379 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003380 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003381
3382 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3383 if (!bss)
3384 goto nla_put_failure;
3385 if (!is_zero_ether_addr(res->bssid))
3386 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3387 if (res->information_elements && res->len_information_elements)
3388 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3389 res->len_information_elements,
3390 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003391 if (res->beacon_ies && res->len_beacon_ies &&
3392 res->beacon_ies != res->information_elements)
3393 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3394 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003395 if (res->tsf)
3396 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3397 if (res->beacon_interval)
3398 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3399 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3400 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003401 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3402 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003403
Johannes Berg77965c92009-02-18 18:45:06 +01003404 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003405 case CFG80211_SIGNAL_TYPE_MBM:
3406 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3407 break;
3408 case CFG80211_SIGNAL_TYPE_UNSPEC:
3409 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3410 break;
3411 default:
3412 break;
3413 }
3414
Johannes Berg48ab9052009-07-10 18:42:31 +02003415 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003416 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003417 case NL80211_IFTYPE_STATION:
3418 if (intbss == wdev->current_bss)
3419 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3420 NL80211_BSS_STATUS_ASSOCIATED);
3421 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3422 if (intbss != wdev->auth_bsses[i])
3423 continue;
3424 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3425 NL80211_BSS_STATUS_AUTHENTICATED);
3426 break;
3427 }
3428 break;
3429 case NL80211_IFTYPE_ADHOC:
3430 if (intbss == wdev->current_bss)
3431 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3432 NL80211_BSS_STATUS_IBSS_JOINED);
3433 break;
3434 default:
3435 break;
3436 }
3437
Johannes Berg2a519312009-02-10 21:25:55 +01003438 nla_nest_end(msg, bss);
3439
3440 return genlmsg_end(msg, hdr);
3441
3442 nla_put_failure:
3443 genlmsg_cancel(msg, hdr);
3444 return -EMSGSIZE;
3445}
3446
3447static int nl80211_dump_scan(struct sk_buff *skb,
3448 struct netlink_callback *cb)
3449{
Johannes Berg48ab9052009-07-10 18:42:31 +02003450 struct cfg80211_registered_device *rdev;
3451 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003452 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003453 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003454 int start = cb->args[1], idx = 0;
3455 int err;
3456
Johannes Berg67748892010-10-04 21:14:06 +02003457 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3458 if (err)
3459 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003460
Johannes Berg48ab9052009-07-10 18:42:31 +02003461 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003462
Johannes Berg48ab9052009-07-10 18:42:31 +02003463 wdev_lock(wdev);
3464 spin_lock_bh(&rdev->bss_lock);
3465 cfg80211_bss_expire(rdev);
3466
3467 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003468 if (++idx <= start)
3469 continue;
3470 if (nl80211_send_bss(skb,
3471 NETLINK_CB(cb->skb).pid,
3472 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003473 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003474 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003475 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003476 }
3477 }
3478
Johannes Berg48ab9052009-07-10 18:42:31 +02003479 spin_unlock_bh(&rdev->bss_lock);
3480 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003481
3482 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003483 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003484
Johannes Berg67748892010-10-04 21:14:06 +02003485 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003486}
3487
Holger Schurig61fa7132009-11-11 12:25:40 +01003488static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3489 int flags, struct net_device *dev,
3490 struct survey_info *survey)
3491{
3492 void *hdr;
3493 struct nlattr *infoattr;
3494
3495 /* Survey without a channel doesn't make sense */
3496 if (!survey->channel)
3497 return -EINVAL;
3498
3499 hdr = nl80211hdr_put(msg, pid, seq, flags,
3500 NL80211_CMD_NEW_SURVEY_RESULTS);
3501 if (!hdr)
3502 return -ENOMEM;
3503
3504 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3505
3506 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3507 if (!infoattr)
3508 goto nla_put_failure;
3509
3510 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3511 survey->channel->center_freq);
3512 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3513 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3514 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003515 if (survey->filled & SURVEY_INFO_IN_USE)
3516 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02003517 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3518 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3519 survey->channel_time);
3520 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3521 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3522 survey->channel_time_busy);
3523 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3524 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3525 survey->channel_time_ext_busy);
3526 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3527 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3528 survey->channel_time_rx);
3529 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3530 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3531 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003532
3533 nla_nest_end(msg, infoattr);
3534
3535 return genlmsg_end(msg, hdr);
3536
3537 nla_put_failure:
3538 genlmsg_cancel(msg, hdr);
3539 return -EMSGSIZE;
3540}
3541
3542static int nl80211_dump_survey(struct sk_buff *skb,
3543 struct netlink_callback *cb)
3544{
3545 struct survey_info survey;
3546 struct cfg80211_registered_device *dev;
3547 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003548 int survey_idx = cb->args[1];
3549 int res;
3550
Johannes Berg67748892010-10-04 21:14:06 +02003551 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3552 if (res)
3553 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003554
3555 if (!dev->ops->dump_survey) {
3556 res = -EOPNOTSUPP;
3557 goto out_err;
3558 }
3559
3560 while (1) {
3561 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3562 &survey);
3563 if (res == -ENOENT)
3564 break;
3565 if (res)
3566 goto out_err;
3567
3568 if (nl80211_send_survey(skb,
3569 NETLINK_CB(cb->skb).pid,
3570 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3571 netdev,
3572 &survey) < 0)
3573 goto out;
3574 survey_idx++;
3575 }
3576
3577 out:
3578 cb->args[1] = survey_idx;
3579 res = skb->len;
3580 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003581 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003582 return res;
3583}
3584
Jouni Malinen255e7372009-03-20 21:21:17 +02003585static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3586{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003587 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003588}
3589
Samuel Ortizb23aa672009-07-01 21:26:54 +02003590static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3591{
3592 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3593 NL80211_WPA_VERSION_2));
3594}
3595
3596static bool nl80211_valid_akm_suite(u32 akm)
3597{
3598 return akm == WLAN_AKM_SUITE_8021X ||
3599 akm == WLAN_AKM_SUITE_PSK;
3600}
3601
3602static bool nl80211_valid_cipher_suite(u32 cipher)
3603{
3604 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3605 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3606 cipher == WLAN_CIPHER_SUITE_TKIP ||
3607 cipher == WLAN_CIPHER_SUITE_CCMP ||
3608 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3609}
3610
3611
Jouni Malinen636a5d32009-03-19 13:39:22 +02003612static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3613{
Johannes Berg4c476992010-10-04 21:36:35 +02003614 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3615 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003616 struct ieee80211_channel *chan;
3617 const u8 *bssid, *ssid, *ie = NULL;
3618 int err, ssid_len, ie_len = 0;
3619 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003620 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003621 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003622
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003623 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3624 return -EINVAL;
3625
3626 if (!info->attrs[NL80211_ATTR_MAC])
3627 return -EINVAL;
3628
Jouni Malinen17780922009-03-27 20:52:47 +02003629 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3630 return -EINVAL;
3631
Johannes Berg19957bb2009-07-02 17:20:43 +02003632 if (!info->attrs[NL80211_ATTR_SSID])
3633 return -EINVAL;
3634
3635 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3636 return -EINVAL;
3637
Johannes Bergfffd0932009-07-08 14:22:54 +02003638 err = nl80211_parse_key(info, &key);
3639 if (err)
3640 return err;
3641
3642 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003643 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3644 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003645 if (!key.p.key || !key.p.key_len)
3646 return -EINVAL;
3647 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3648 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3649 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3650 key.p.key_len != WLAN_KEY_LEN_WEP104))
3651 return -EINVAL;
3652 if (key.idx > 4)
3653 return -EINVAL;
3654 } else {
3655 key.p.key_len = 0;
3656 key.p.key = NULL;
3657 }
3658
Johannes Bergafea0b72010-08-10 09:46:42 +02003659 if (key.idx >= 0) {
3660 int i;
3661 bool ok = false;
3662 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3663 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3664 ok = true;
3665 break;
3666 }
3667 }
Johannes Berg4c476992010-10-04 21:36:35 +02003668 if (!ok)
3669 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003670 }
3671
Johannes Berg4c476992010-10-04 21:36:35 +02003672 if (!rdev->ops->auth)
3673 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003674
Johannes Berg074ac8d2010-09-16 14:58:22 +02003675 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003676 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3677 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003678
Johannes Berg19957bb2009-07-02 17:20:43 +02003679 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003680 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003681 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003682 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3683 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003684
Johannes Berg19957bb2009-07-02 17:20:43 +02003685 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3686 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3687
3688 if (info->attrs[NL80211_ATTR_IE]) {
3689 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3690 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3691 }
3692
3693 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003694 if (!nl80211_valid_auth_type(auth_type))
3695 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003696
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003697 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3698
Johannes Berg4c476992010-10-04 21:36:35 +02003699 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3700 ssid, ssid_len, ie, ie_len,
3701 key.p.key, key.p.key_len, key.idx,
3702 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003703}
3704
Johannes Bergc0692b82010-08-27 14:26:53 +03003705static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3706 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003707 struct cfg80211_crypto_settings *settings,
3708 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003709{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003710 memset(settings, 0, sizeof(*settings));
3711
Samuel Ortizb23aa672009-07-01 21:26:54 +02003712 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3713
Johannes Bergc0692b82010-08-27 14:26:53 +03003714 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3715 u16 proto;
3716 proto = nla_get_u16(
3717 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3718 settings->control_port_ethertype = cpu_to_be16(proto);
3719 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3720 proto != ETH_P_PAE)
3721 return -EINVAL;
3722 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3723 settings->control_port_no_encrypt = true;
3724 } else
3725 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3726
Samuel Ortizb23aa672009-07-01 21:26:54 +02003727 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3728 void *data;
3729 int len, i;
3730
3731 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3732 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3733 settings->n_ciphers_pairwise = len / sizeof(u32);
3734
3735 if (len % sizeof(u32))
3736 return -EINVAL;
3737
Johannes Berg3dc27d22009-07-02 21:36:37 +02003738 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003739 return -EINVAL;
3740
3741 memcpy(settings->ciphers_pairwise, data, len);
3742
3743 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3744 if (!nl80211_valid_cipher_suite(
3745 settings->ciphers_pairwise[i]))
3746 return -EINVAL;
3747 }
3748
3749 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3750 settings->cipher_group =
3751 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3752 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3753 return -EINVAL;
3754 }
3755
3756 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3757 settings->wpa_versions =
3758 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3759 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3760 return -EINVAL;
3761 }
3762
3763 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3764 void *data;
3765 int len, i;
3766
3767 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3768 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3769 settings->n_akm_suites = len / sizeof(u32);
3770
3771 if (len % sizeof(u32))
3772 return -EINVAL;
3773
3774 memcpy(settings->akm_suites, data, len);
3775
3776 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3777 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3778 return -EINVAL;
3779 }
3780
3781 return 0;
3782}
3783
Jouni Malinen636a5d32009-03-19 13:39:22 +02003784static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3785{
Johannes Berg4c476992010-10-04 21:36:35 +02003786 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3787 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003788 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003789 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003790 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003791 int err, ssid_len, ie_len = 0;
3792 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003793
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003794 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3795 return -EINVAL;
3796
3797 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003798 !info->attrs[NL80211_ATTR_SSID] ||
3799 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003800 return -EINVAL;
3801
Johannes Berg4c476992010-10-04 21:36:35 +02003802 if (!rdev->ops->assoc)
3803 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003804
Johannes Berg074ac8d2010-09-16 14:58:22 +02003805 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003806 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3807 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003808
Johannes Berg19957bb2009-07-02 17:20:43 +02003809 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003810
Johannes Berg19957bb2009-07-02 17:20:43 +02003811 chan = ieee80211_get_channel(&rdev->wiphy,
3812 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003813 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3814 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003815
Johannes Berg19957bb2009-07-02 17:20:43 +02003816 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3817 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003818
3819 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003820 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3821 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003822 }
3823
Jouni Malinendc6382c2009-05-06 22:09:37 +03003824 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003825 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03003826 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003827 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003828 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02003829 else if (mfp != NL80211_MFP_NO)
3830 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03003831 }
3832
Johannes Berg3e5d7642009-07-07 14:37:26 +02003833 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3834 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3835
Johannes Bergc0692b82010-08-27 14:26:53 +03003836 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003837 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003838 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3839 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003840 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003841
Jouni Malinen636a5d32009-03-19 13:39:22 +02003842 return err;
3843}
3844
3845static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3846{
Johannes Berg4c476992010-10-04 21:36:35 +02003847 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3848 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003849 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003850 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003851 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003852 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003853
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003854 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3855 return -EINVAL;
3856
3857 if (!info->attrs[NL80211_ATTR_MAC])
3858 return -EINVAL;
3859
3860 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3861 return -EINVAL;
3862
Johannes Berg4c476992010-10-04 21:36:35 +02003863 if (!rdev->ops->deauth)
3864 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003865
Johannes Berg074ac8d2010-09-16 14:58:22 +02003866 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003867 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3868 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003869
Johannes Berg19957bb2009-07-02 17:20:43 +02003870 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003871
Johannes Berg19957bb2009-07-02 17:20:43 +02003872 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3873 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003874 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003875 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003876 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003877
3878 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003879 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3880 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003881 }
3882
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003883 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3884
Johannes Berg4c476992010-10-04 21:36:35 +02003885 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3886 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003887}
3888
3889static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3890{
Johannes Berg4c476992010-10-04 21:36:35 +02003891 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3892 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003893 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003894 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003895 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003896 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003897
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003898 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3899 return -EINVAL;
3900
3901 if (!info->attrs[NL80211_ATTR_MAC])
3902 return -EINVAL;
3903
3904 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3905 return -EINVAL;
3906
Johannes Berg4c476992010-10-04 21:36:35 +02003907 if (!rdev->ops->disassoc)
3908 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003909
Johannes Berg074ac8d2010-09-16 14:58:22 +02003910 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003911 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3912 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003913
Johannes Berg19957bb2009-07-02 17:20:43 +02003914 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003915
Johannes Berg19957bb2009-07-02 17:20:43 +02003916 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3917 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003918 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003919 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003920 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003921
3922 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003923 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3924 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003925 }
3926
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003927 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3928
Johannes Berg4c476992010-10-04 21:36:35 +02003929 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3930 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003931}
3932
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003933static bool
3934nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
3935 int mcast_rate[IEEE80211_NUM_BANDS],
3936 int rateval)
3937{
3938 struct wiphy *wiphy = &rdev->wiphy;
3939 bool found = false;
3940 int band, i;
3941
3942 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3943 struct ieee80211_supported_band *sband;
3944
3945 sband = wiphy->bands[band];
3946 if (!sband)
3947 continue;
3948
3949 for (i = 0; i < sband->n_bitrates; i++) {
3950 if (sband->bitrates[i].bitrate == rateval) {
3951 mcast_rate[band] = i + 1;
3952 found = true;
3953 break;
3954 }
3955 }
3956 }
3957
3958 return found;
3959}
3960
Johannes Berg04a773a2009-04-19 21:24:32 +02003961static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3962{
Johannes Berg4c476992010-10-04 21:36:35 +02003963 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3964 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003965 struct cfg80211_ibss_params ibss;
3966 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003967 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003968 int err;
3969
Johannes Berg8e30bc52009-04-22 17:45:38 +02003970 memset(&ibss, 0, sizeof(ibss));
3971
Johannes Berg04a773a2009-04-19 21:24:32 +02003972 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3973 return -EINVAL;
3974
3975 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3976 !info->attrs[NL80211_ATTR_SSID] ||
3977 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3978 return -EINVAL;
3979
Johannes Berg8e30bc52009-04-22 17:45:38 +02003980 ibss.beacon_interval = 100;
3981
3982 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3983 ibss.beacon_interval =
3984 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3985 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3986 return -EINVAL;
3987 }
3988
Johannes Berg4c476992010-10-04 21:36:35 +02003989 if (!rdev->ops->join_ibss)
3990 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003991
Johannes Berg4c476992010-10-04 21:36:35 +02003992 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3993 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003994
Johannes Berg79c97e92009-07-07 03:56:12 +02003995 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003996
3997 if (info->attrs[NL80211_ATTR_MAC])
3998 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3999 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4000 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4001
4002 if (info->attrs[NL80211_ATTR_IE]) {
4003 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4004 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4005 }
4006
4007 ibss.channel = ieee80211_get_channel(wiphy,
4008 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4009 if (!ibss.channel ||
4010 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004011 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4012 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004013
4014 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004015 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004016
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004017 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4018 u8 *rates =
4019 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4020 int n_rates =
4021 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4022 struct ieee80211_supported_band *sband =
4023 wiphy->bands[ibss.channel->band];
4024 int i, j;
4025
Johannes Berg4c476992010-10-04 21:36:35 +02004026 if (n_rates == 0)
4027 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004028
4029 for (i = 0; i < n_rates; i++) {
4030 int rate = (rates[i] & 0x7f) * 5;
4031 bool found = false;
4032
4033 for (j = 0; j < sband->n_bitrates; j++) {
4034 if (sband->bitrates[j].bitrate == rate) {
4035 found = true;
4036 ibss.basic_rates |= BIT(j);
4037 break;
4038 }
4039 }
Johannes Berg4c476992010-10-04 21:36:35 +02004040 if (!found)
4041 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004042 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004043 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004044
4045 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4046 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4047 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4048 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004049
Johannes Berg4c476992010-10-04 21:36:35 +02004050 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4051 connkeys = nl80211_parse_connkeys(rdev,
4052 info->attrs[NL80211_ATTR_KEYS]);
4053 if (IS_ERR(connkeys))
4054 return PTR_ERR(connkeys);
4055 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004056
Johannes Berg4c476992010-10-04 21:36:35 +02004057 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004058 if (err)
4059 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004060 return err;
4061}
4062
4063static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4064{
Johannes Berg4c476992010-10-04 21:36:35 +02004065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4066 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004067
Johannes Berg4c476992010-10-04 21:36:35 +02004068 if (!rdev->ops->leave_ibss)
4069 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004070
Johannes Berg4c476992010-10-04 21:36:35 +02004071 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4072 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004073
Johannes Berg4c476992010-10-04 21:36:35 +02004074 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004075}
4076
Johannes Bergaff89a92009-07-01 21:26:51 +02004077#ifdef CONFIG_NL80211_TESTMODE
4078static struct genl_multicast_group nl80211_testmode_mcgrp = {
4079 .name = "testmode",
4080};
4081
4082static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4083{
Johannes Berg4c476992010-10-04 21:36:35 +02004084 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004085 int err;
4086
4087 if (!info->attrs[NL80211_ATTR_TESTDATA])
4088 return -EINVAL;
4089
Johannes Bergaff89a92009-07-01 21:26:51 +02004090 err = -EOPNOTSUPP;
4091 if (rdev->ops->testmode_cmd) {
4092 rdev->testmode_info = info;
4093 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4094 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4095 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4096 rdev->testmode_info = NULL;
4097 }
4098
Johannes Bergaff89a92009-07-01 21:26:51 +02004099 return err;
4100}
4101
4102static struct sk_buff *
4103__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4104 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4105{
4106 struct sk_buff *skb;
4107 void *hdr;
4108 struct nlattr *data;
4109
4110 skb = nlmsg_new(approxlen + 100, gfp);
4111 if (!skb)
4112 return NULL;
4113
4114 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4115 if (!hdr) {
4116 kfree_skb(skb);
4117 return NULL;
4118 }
4119
4120 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4121 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4122
4123 ((void **)skb->cb)[0] = rdev;
4124 ((void **)skb->cb)[1] = hdr;
4125 ((void **)skb->cb)[2] = data;
4126
4127 return skb;
4128
4129 nla_put_failure:
4130 kfree_skb(skb);
4131 return NULL;
4132}
4133
4134struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4135 int approxlen)
4136{
4137 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4138
4139 if (WARN_ON(!rdev->testmode_info))
4140 return NULL;
4141
4142 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4143 rdev->testmode_info->snd_pid,
4144 rdev->testmode_info->snd_seq,
4145 GFP_KERNEL);
4146}
4147EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4148
4149int cfg80211_testmode_reply(struct sk_buff *skb)
4150{
4151 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4152 void *hdr = ((void **)skb->cb)[1];
4153 struct nlattr *data = ((void **)skb->cb)[2];
4154
4155 if (WARN_ON(!rdev->testmode_info)) {
4156 kfree_skb(skb);
4157 return -EINVAL;
4158 }
4159
4160 nla_nest_end(skb, data);
4161 genlmsg_end(skb, hdr);
4162 return genlmsg_reply(skb, rdev->testmode_info);
4163}
4164EXPORT_SYMBOL(cfg80211_testmode_reply);
4165
4166struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4167 int approxlen, gfp_t gfp)
4168{
4169 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4170
4171 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4172}
4173EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4174
4175void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4176{
4177 void *hdr = ((void **)skb->cb)[1];
4178 struct nlattr *data = ((void **)skb->cb)[2];
4179
4180 nla_nest_end(skb, data);
4181 genlmsg_end(skb, hdr);
4182 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4183}
4184EXPORT_SYMBOL(cfg80211_testmode_event);
4185#endif
4186
Samuel Ortizb23aa672009-07-01 21:26:54 +02004187static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4188{
Johannes Berg4c476992010-10-04 21:36:35 +02004189 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4190 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004191 struct cfg80211_connect_params connect;
4192 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004193 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004194 int err;
4195
4196 memset(&connect, 0, sizeof(connect));
4197
4198 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4199 return -EINVAL;
4200
4201 if (!info->attrs[NL80211_ATTR_SSID] ||
4202 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4203 return -EINVAL;
4204
4205 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4206 connect.auth_type =
4207 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4208 if (!nl80211_valid_auth_type(connect.auth_type))
4209 return -EINVAL;
4210 } else
4211 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4212
4213 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4214
Johannes Bergc0692b82010-08-27 14:26:53 +03004215 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004216 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004217 if (err)
4218 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004219
Johannes Berg074ac8d2010-09-16 14:58:22 +02004220 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004221 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4222 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004223
Johannes Berg79c97e92009-07-07 03:56:12 +02004224 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004225
Samuel Ortizb23aa672009-07-01 21:26:54 +02004226 if (info->attrs[NL80211_ATTR_MAC])
4227 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4228 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4229 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4230
4231 if (info->attrs[NL80211_ATTR_IE]) {
4232 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4233 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4234 }
4235
4236 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4237 connect.channel =
4238 ieee80211_get_channel(wiphy,
4239 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4240 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004241 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4242 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004243 }
4244
Johannes Bergfffd0932009-07-08 14:22:54 +02004245 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4246 connkeys = nl80211_parse_connkeys(rdev,
4247 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004248 if (IS_ERR(connkeys))
4249 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004250 }
4251
4252 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004253 if (err)
4254 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004255 return err;
4256}
4257
4258static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4259{
Johannes Berg4c476992010-10-04 21:36:35 +02004260 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4261 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004262 u16 reason;
4263
4264 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4265 reason = WLAN_REASON_DEAUTH_LEAVING;
4266 else
4267 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4268
4269 if (reason == 0)
4270 return -EINVAL;
4271
Johannes Berg074ac8d2010-09-16 14:58:22 +02004272 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004273 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4274 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004275
Johannes Berg4c476992010-10-04 21:36:35 +02004276 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004277}
4278
Johannes Berg463d0182009-07-14 00:33:35 +02004279static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4280{
Johannes Berg4c476992010-10-04 21:36:35 +02004281 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004282 struct net *net;
4283 int err;
4284 u32 pid;
4285
4286 if (!info->attrs[NL80211_ATTR_PID])
4287 return -EINVAL;
4288
4289 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4290
Johannes Berg463d0182009-07-14 00:33:35 +02004291 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004292 if (IS_ERR(net))
4293 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004294
4295 err = 0;
4296
4297 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004298 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4299 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004300
Johannes Berg463d0182009-07-14 00:33:35 +02004301 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004302 return err;
4303}
4304
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004305static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4306{
Johannes Berg4c476992010-10-04 21:36:35 +02004307 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004308 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4309 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004310 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004311 struct cfg80211_pmksa pmksa;
4312
4313 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4314
4315 if (!info->attrs[NL80211_ATTR_MAC])
4316 return -EINVAL;
4317
4318 if (!info->attrs[NL80211_ATTR_PMKID])
4319 return -EINVAL;
4320
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004321 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4322 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4323
Johannes Berg074ac8d2010-09-16 14:58:22 +02004324 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004325 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4326 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004327
4328 switch (info->genlhdr->cmd) {
4329 case NL80211_CMD_SET_PMKSA:
4330 rdev_ops = rdev->ops->set_pmksa;
4331 break;
4332 case NL80211_CMD_DEL_PMKSA:
4333 rdev_ops = rdev->ops->del_pmksa;
4334 break;
4335 default:
4336 WARN_ON(1);
4337 break;
4338 }
4339
Johannes Berg4c476992010-10-04 21:36:35 +02004340 if (!rdev_ops)
4341 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004342
Johannes Berg4c476992010-10-04 21:36:35 +02004343 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004344}
4345
4346static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4347{
Johannes Berg4c476992010-10-04 21:36:35 +02004348 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4349 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004350
Johannes Berg074ac8d2010-09-16 14:58:22 +02004351 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004352 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4353 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004354
Johannes Berg4c476992010-10-04 21:36:35 +02004355 if (!rdev->ops->flush_pmksa)
4356 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004357
Johannes Berg4c476992010-10-04 21:36:35 +02004358 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004359}
4360
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004361static int nl80211_remain_on_channel(struct sk_buff *skb,
4362 struct genl_info *info)
4363{
Johannes Berg4c476992010-10-04 21:36:35 +02004364 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4365 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004366 struct ieee80211_channel *chan;
4367 struct sk_buff *msg;
4368 void *hdr;
4369 u64 cookie;
4370 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4371 u32 freq, duration;
4372 int err;
4373
4374 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4375 !info->attrs[NL80211_ATTR_DURATION])
4376 return -EINVAL;
4377
4378 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4379
4380 /*
4381 * We should be on that channel for at least one jiffie,
4382 * and more than 5 seconds seems excessive.
4383 */
Johannes Berga2939112010-12-14 17:54:28 +01004384 if (!duration || !msecs_to_jiffies(duration) ||
4385 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004386 return -EINVAL;
4387
Johannes Berg4c476992010-10-04 21:36:35 +02004388 if (!rdev->ops->remain_on_channel)
4389 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004390
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004391 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4392 channel_type = nla_get_u32(
4393 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4394 if (channel_type != NL80211_CHAN_NO_HT &&
4395 channel_type != NL80211_CHAN_HT20 &&
4396 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004397 channel_type != NL80211_CHAN_HT40MINUS)
4398 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004399 }
4400
4401 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4402 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004403 if (chan == NULL)
4404 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004405
4406 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004407 if (!msg)
4408 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004409
4410 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4411 NL80211_CMD_REMAIN_ON_CHANNEL);
4412
4413 if (IS_ERR(hdr)) {
4414 err = PTR_ERR(hdr);
4415 goto free_msg;
4416 }
4417
4418 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4419 channel_type, duration, &cookie);
4420
4421 if (err)
4422 goto free_msg;
4423
4424 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4425
4426 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004427
4428 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004429
4430 nla_put_failure:
4431 err = -ENOBUFS;
4432 free_msg:
4433 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004434 return err;
4435}
4436
4437static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4438 struct genl_info *info)
4439{
Johannes Berg4c476992010-10-04 21:36:35 +02004440 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4441 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004442 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004443
4444 if (!info->attrs[NL80211_ATTR_COOKIE])
4445 return -EINVAL;
4446
Johannes Berg4c476992010-10-04 21:36:35 +02004447 if (!rdev->ops->cancel_remain_on_channel)
4448 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004449
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004450 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4451
Johannes Berg4c476992010-10-04 21:36:35 +02004452 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004453}
4454
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004455static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4456 u8 *rates, u8 rates_len)
4457{
4458 u8 i;
4459 u32 mask = 0;
4460
4461 for (i = 0; i < rates_len; i++) {
4462 int rate = (rates[i] & 0x7f) * 5;
4463 int ridx;
4464 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4465 struct ieee80211_rate *srate =
4466 &sband->bitrates[ridx];
4467 if (rate == srate->bitrate) {
4468 mask |= 1 << ridx;
4469 break;
4470 }
4471 }
4472 if (ridx == sband->n_bitrates)
4473 return 0; /* rate not found */
4474 }
4475
4476 return mask;
4477}
4478
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004479static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004480 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4481 .len = NL80211_MAX_SUPP_RATES },
4482};
4483
4484static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4485 struct genl_info *info)
4486{
4487 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004488 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004489 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004490 int rem, i;
4491 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004492 struct nlattr *tx_rates;
4493 struct ieee80211_supported_band *sband;
4494
4495 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4496 return -EINVAL;
4497
Johannes Berg4c476992010-10-04 21:36:35 +02004498 if (!rdev->ops->set_bitrate_mask)
4499 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004500
4501 memset(&mask, 0, sizeof(mask));
4502 /* Default to all rates enabled */
4503 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4504 sband = rdev->wiphy.bands[i];
4505 mask.control[i].legacy =
4506 sband ? (1 << sband->n_bitrates) - 1 : 0;
4507 }
4508
4509 /*
4510 * The nested attribute uses enum nl80211_band as the index. This maps
4511 * directly to the enum ieee80211_band values used in cfg80211.
4512 */
4513 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4514 {
4515 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004516 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4517 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004518 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004519 if (sband == NULL)
4520 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004521 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4522 nla_len(tx_rates), nl80211_txattr_policy);
4523 if (tb[NL80211_TXRATE_LEGACY]) {
4524 mask.control[band].legacy = rateset_to_mask(
4525 sband,
4526 nla_data(tb[NL80211_TXRATE_LEGACY]),
4527 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004528 if (mask.control[band].legacy == 0)
4529 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004530 }
4531 }
4532
Johannes Berg4c476992010-10-04 21:36:35 +02004533 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004534}
4535
Johannes Berg2e161f72010-08-12 15:38:38 +02004536static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004537{
Johannes Berg4c476992010-10-04 21:36:35 +02004538 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4539 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004540 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004541
4542 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4543 return -EINVAL;
4544
Johannes Berg2e161f72010-08-12 15:38:38 +02004545 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4546 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004547
Johannes Berg9d38d852010-06-09 17:20:33 +02004548 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004549 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004550 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4551 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4552 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08004553 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004554 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4555 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004556
4557 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004558 if (!rdev->ops->mgmt_tx)
4559 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004560
Johannes Berg4c476992010-10-04 21:36:35 +02004561 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004562 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004563 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4564 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004565}
4566
Johannes Berg2e161f72010-08-12 15:38:38 +02004567static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004568{
Johannes Berg4c476992010-10-04 21:36:35 +02004569 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4570 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004571 struct ieee80211_channel *chan;
4572 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004573 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004574 u32 freq;
4575 int err;
4576 void *hdr;
4577 u64 cookie;
4578 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004579 unsigned int wait = 0;
4580 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02004581
4582 if (!info->attrs[NL80211_ATTR_FRAME] ||
4583 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4584 return -EINVAL;
4585
Johannes Berg4c476992010-10-04 21:36:35 +02004586 if (!rdev->ops->mgmt_tx)
4587 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004588
Johannes Berg9d38d852010-06-09 17:20:33 +02004589 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004590 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004591 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4592 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4593 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08004594 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004595 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4596 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004597
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004598 if (info->attrs[NL80211_ATTR_DURATION]) {
4599 if (!rdev->ops->mgmt_tx_cancel_wait)
4600 return -EINVAL;
4601 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4602 }
4603
Jouni Malinen026331c2010-02-15 12:53:10 +02004604 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4605 channel_type = nla_get_u32(
4606 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4607 if (channel_type != NL80211_CHAN_NO_HT &&
4608 channel_type != NL80211_CHAN_HT20 &&
4609 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004610 channel_type != NL80211_CHAN_HT40MINUS)
4611 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004612 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004613 }
4614
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004615 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
4616
Jouni Malinen026331c2010-02-15 12:53:10 +02004617 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4618 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004619 if (chan == NULL)
4620 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004621
4622 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004623 if (!msg)
4624 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004625
4626 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004627 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004628
4629 if (IS_ERR(hdr)) {
4630 err = PTR_ERR(hdr);
4631 goto free_msg;
4632 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004633 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
4634 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02004635 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4636 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4637 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004638 if (err)
4639 goto free_msg;
4640
4641 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4642
4643 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004644 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004645
4646 nla_put_failure:
4647 err = -ENOBUFS;
4648 free_msg:
4649 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004650 return err;
4651}
4652
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004653static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
4654{
4655 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4656 struct net_device *dev = info->user_ptr[1];
4657 u64 cookie;
4658
4659 if (!info->attrs[NL80211_ATTR_COOKIE])
4660 return -EINVAL;
4661
4662 if (!rdev->ops->mgmt_tx_cancel_wait)
4663 return -EOPNOTSUPP;
4664
4665 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4666 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4667 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4668 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4669 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4670 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4671 return -EOPNOTSUPP;
4672
4673 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4674
4675 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
4676}
4677
Kalle Valoffb9eb32010-02-17 17:58:10 +02004678static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4679{
Johannes Berg4c476992010-10-04 21:36:35 +02004680 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004681 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004682 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004683 u8 ps_state;
4684 bool state;
4685 int err;
4686
Johannes Berg4c476992010-10-04 21:36:35 +02004687 if (!info->attrs[NL80211_ATTR_PS_STATE])
4688 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004689
4690 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4691
Johannes Berg4c476992010-10-04 21:36:35 +02004692 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4693 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004694
4695 wdev = dev->ieee80211_ptr;
4696
Johannes Berg4c476992010-10-04 21:36:35 +02004697 if (!rdev->ops->set_power_mgmt)
4698 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004699
4700 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4701
4702 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004703 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004704
Johannes Berg4c476992010-10-04 21:36:35 +02004705 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4706 wdev->ps_timeout);
4707 if (!err)
4708 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004709 return err;
4710}
4711
4712static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4713{
Johannes Berg4c476992010-10-04 21:36:35 +02004714 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004715 enum nl80211_ps_state ps_state;
4716 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004717 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004718 struct sk_buff *msg;
4719 void *hdr;
4720 int err;
4721
Kalle Valoffb9eb32010-02-17 17:58:10 +02004722 wdev = dev->ieee80211_ptr;
4723
Johannes Berg4c476992010-10-04 21:36:35 +02004724 if (!rdev->ops->set_power_mgmt)
4725 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004726
4727 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004728 if (!msg)
4729 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004730
4731 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4732 NL80211_CMD_GET_POWER_SAVE);
4733 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004734 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004735 goto free_msg;
4736 }
4737
4738 if (wdev->ps)
4739 ps_state = NL80211_PS_ENABLED;
4740 else
4741 ps_state = NL80211_PS_DISABLED;
4742
4743 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4744
4745 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004746 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004747
Johannes Berg4c476992010-10-04 21:36:35 +02004748 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004749 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004750 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004751 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004752 return err;
4753}
4754
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004755static struct nla_policy
4756nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4757 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4758 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4759 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4760};
4761
4762static int nl80211_set_cqm_rssi(struct genl_info *info,
4763 s32 threshold, u32 hysteresis)
4764{
Johannes Berg4c476992010-10-04 21:36:35 +02004765 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004766 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004767 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004768
4769 if (threshold > 0)
4770 return -EINVAL;
4771
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004772 wdev = dev->ieee80211_ptr;
4773
Johannes Berg4c476992010-10-04 21:36:35 +02004774 if (!rdev->ops->set_cqm_rssi_config)
4775 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004776
Johannes Berg074ac8d2010-09-16 14:58:22 +02004777 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004778 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4779 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004780
Johannes Berg4c476992010-10-04 21:36:35 +02004781 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4782 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004783}
4784
4785static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4786{
4787 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4788 struct nlattr *cqm;
4789 int err;
4790
4791 cqm = info->attrs[NL80211_ATTR_CQM];
4792 if (!cqm) {
4793 err = -EINVAL;
4794 goto out;
4795 }
4796
4797 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4798 nl80211_attr_cqm_policy);
4799 if (err)
4800 goto out;
4801
4802 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4803 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4804 s32 threshold;
4805 u32 hysteresis;
4806 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4807 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4808 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4809 } else
4810 err = -EINVAL;
4811
4812out:
4813 return err;
4814}
4815
Johannes Berg29cbe682010-12-03 09:20:44 +01004816static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
4817{
4818 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4819 struct net_device *dev = info->user_ptr[1];
4820 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08004821 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01004822 int err;
4823
4824 /* start with default */
4825 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08004826 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01004827
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004828 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01004829 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004830 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01004831 if (err)
4832 return err;
4833 }
4834
4835 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
4836 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
4837 return -EINVAL;
4838
Javier Cardonac80d5452010-12-16 17:37:49 -08004839 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
4840 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4841
4842 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
4843 /* parse additional setup parameters if given */
4844 err = nl80211_parse_mesh_setup(info, &setup);
4845 if (err)
4846 return err;
4847 }
4848
4849 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004850}
4851
4852static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
4853{
4854 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4855 struct net_device *dev = info->user_ptr[1];
4856
4857 return cfg80211_leave_mesh(rdev, dev);
4858}
4859
Johannes Bergff1b6e62011-05-04 15:37:28 +02004860static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
4861{
4862 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4863 struct sk_buff *msg;
4864 void *hdr;
4865
4866 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
4867 return -EOPNOTSUPP;
4868
4869 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
4870 if (!msg)
4871 return -ENOMEM;
4872
4873 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4874 NL80211_CMD_GET_WOWLAN);
4875 if (!hdr)
4876 goto nla_put_failure;
4877
4878 if (rdev->wowlan) {
4879 struct nlattr *nl_wowlan;
4880
4881 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
4882 if (!nl_wowlan)
4883 goto nla_put_failure;
4884
4885 if (rdev->wowlan->any)
4886 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
4887 if (rdev->wowlan->disconnect)
4888 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
4889 if (rdev->wowlan->magic_pkt)
4890 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
4891 if (rdev->wowlan->n_patterns) {
4892 struct nlattr *nl_pats, *nl_pat;
4893 int i, pat_len;
4894
4895 nl_pats = nla_nest_start(msg,
4896 NL80211_WOWLAN_TRIG_PKT_PATTERN);
4897 if (!nl_pats)
4898 goto nla_put_failure;
4899
4900 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
4901 nl_pat = nla_nest_start(msg, i + 1);
4902 if (!nl_pat)
4903 goto nla_put_failure;
4904 pat_len = rdev->wowlan->patterns[i].pattern_len;
4905 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
4906 DIV_ROUND_UP(pat_len, 8),
4907 rdev->wowlan->patterns[i].mask);
4908 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
4909 pat_len,
4910 rdev->wowlan->patterns[i].pattern);
4911 nla_nest_end(msg, nl_pat);
4912 }
4913 nla_nest_end(msg, nl_pats);
4914 }
4915
4916 nla_nest_end(msg, nl_wowlan);
4917 }
4918
4919 genlmsg_end(msg, hdr);
4920 return genlmsg_reply(msg, info);
4921
4922nla_put_failure:
4923 nlmsg_free(msg);
4924 return -ENOBUFS;
4925}
4926
4927static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
4928{
4929 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4930 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
4931 struct cfg80211_wowlan no_triggers = {};
4932 struct cfg80211_wowlan new_triggers = {};
4933 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
4934 int err, i;
4935
4936 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
4937 return -EOPNOTSUPP;
4938
4939 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
4940 goto no_triggers;
4941
4942 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
4943 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
4944 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
4945 nl80211_wowlan_policy);
4946 if (err)
4947 return err;
4948
4949 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
4950 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
4951 return -EINVAL;
4952 new_triggers.any = true;
4953 }
4954
4955 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
4956 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
4957 return -EINVAL;
4958 new_triggers.disconnect = true;
4959 }
4960
4961 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
4962 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
4963 return -EINVAL;
4964 new_triggers.magic_pkt = true;
4965 }
4966
4967 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
4968 struct nlattr *pat;
4969 int n_patterns = 0;
4970 int rem, pat_len, mask_len;
4971 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
4972
4973 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
4974 rem)
4975 n_patterns++;
4976 if (n_patterns > wowlan->n_patterns)
4977 return -EINVAL;
4978
4979 new_triggers.patterns = kcalloc(n_patterns,
4980 sizeof(new_triggers.patterns[0]),
4981 GFP_KERNEL);
4982 if (!new_triggers.patterns)
4983 return -ENOMEM;
4984
4985 new_triggers.n_patterns = n_patterns;
4986 i = 0;
4987
4988 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
4989 rem) {
4990 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
4991 nla_data(pat), nla_len(pat), NULL);
4992 err = -EINVAL;
4993 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
4994 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
4995 goto error;
4996 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
4997 mask_len = DIV_ROUND_UP(pat_len, 8);
4998 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
4999 mask_len)
5000 goto error;
5001 if (pat_len > wowlan->pattern_max_len ||
5002 pat_len < wowlan->pattern_min_len)
5003 goto error;
5004
5005 new_triggers.patterns[i].mask =
5006 kmalloc(mask_len + pat_len, GFP_KERNEL);
5007 if (!new_triggers.patterns[i].mask) {
5008 err = -ENOMEM;
5009 goto error;
5010 }
5011 new_triggers.patterns[i].pattern =
5012 new_triggers.patterns[i].mask + mask_len;
5013 memcpy(new_triggers.patterns[i].mask,
5014 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5015 mask_len);
5016 new_triggers.patterns[i].pattern_len = pat_len;
5017 memcpy(new_triggers.patterns[i].pattern,
5018 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5019 pat_len);
5020 i++;
5021 }
5022 }
5023
5024 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5025 struct cfg80211_wowlan *ntrig;
5026 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5027 GFP_KERNEL);
5028 if (!ntrig) {
5029 err = -ENOMEM;
5030 goto error;
5031 }
5032 cfg80211_rdev_free_wowlan(rdev);
5033 rdev->wowlan = ntrig;
5034 } else {
5035 no_triggers:
5036 cfg80211_rdev_free_wowlan(rdev);
5037 rdev->wowlan = NULL;
5038 }
5039
5040 return 0;
5041 error:
5042 for (i = 0; i < new_triggers.n_patterns; i++)
5043 kfree(new_triggers.patterns[i].mask);
5044 kfree(new_triggers.patterns);
5045 return err;
5046}
5047
Johannes Berg4c476992010-10-04 21:36:35 +02005048#define NL80211_FLAG_NEED_WIPHY 0x01
5049#define NL80211_FLAG_NEED_NETDEV 0x02
5050#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005051#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5052#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5053 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005054
5055static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5056 struct genl_info *info)
5057{
5058 struct cfg80211_registered_device *rdev;
5059 struct net_device *dev;
5060 int err;
5061 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5062
5063 if (rtnl)
5064 rtnl_lock();
5065
5066 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5067 rdev = cfg80211_get_dev_from_info(info);
5068 if (IS_ERR(rdev)) {
5069 if (rtnl)
5070 rtnl_unlock();
5071 return PTR_ERR(rdev);
5072 }
5073 info->user_ptr[0] = rdev;
5074 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5075 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5076 if (err) {
5077 if (rtnl)
5078 rtnl_unlock();
5079 return err;
5080 }
Johannes Berg41265712010-10-04 21:14:05 +02005081 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5082 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005083 cfg80211_unlock_rdev(rdev);
5084 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005085 if (rtnl)
5086 rtnl_unlock();
5087 return -ENETDOWN;
5088 }
Johannes Berg4c476992010-10-04 21:36:35 +02005089 info->user_ptr[0] = rdev;
5090 info->user_ptr[1] = dev;
5091 }
5092
5093 return 0;
5094}
5095
5096static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5097 struct genl_info *info)
5098{
5099 if (info->user_ptr[0])
5100 cfg80211_unlock_rdev(info->user_ptr[0]);
5101 if (info->user_ptr[1])
5102 dev_put(info->user_ptr[1]);
5103 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5104 rtnl_unlock();
5105}
5106
Johannes Berg55682962007-09-20 13:09:35 -04005107static struct genl_ops nl80211_ops[] = {
5108 {
5109 .cmd = NL80211_CMD_GET_WIPHY,
5110 .doit = nl80211_get_wiphy,
5111 .dumpit = nl80211_dump_wiphy,
5112 .policy = nl80211_policy,
5113 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005114 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005115 },
5116 {
5117 .cmd = NL80211_CMD_SET_WIPHY,
5118 .doit = nl80211_set_wiphy,
5119 .policy = nl80211_policy,
5120 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005121 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005122 },
5123 {
5124 .cmd = NL80211_CMD_GET_INTERFACE,
5125 .doit = nl80211_get_interface,
5126 .dumpit = nl80211_dump_interface,
5127 .policy = nl80211_policy,
5128 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005129 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04005130 },
5131 {
5132 .cmd = NL80211_CMD_SET_INTERFACE,
5133 .doit = nl80211_set_interface,
5134 .policy = nl80211_policy,
5135 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005136 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5137 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005138 },
5139 {
5140 .cmd = NL80211_CMD_NEW_INTERFACE,
5141 .doit = nl80211_new_interface,
5142 .policy = nl80211_policy,
5143 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005144 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5145 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005146 },
5147 {
5148 .cmd = NL80211_CMD_DEL_INTERFACE,
5149 .doit = nl80211_del_interface,
5150 .policy = nl80211_policy,
5151 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005152 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5153 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005154 },
Johannes Berg41ade002007-12-19 02:03:29 +01005155 {
5156 .cmd = NL80211_CMD_GET_KEY,
5157 .doit = nl80211_get_key,
5158 .policy = nl80211_policy,
5159 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005160 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5161 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005162 },
5163 {
5164 .cmd = NL80211_CMD_SET_KEY,
5165 .doit = nl80211_set_key,
5166 .policy = nl80211_policy,
5167 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005168 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005169 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005170 },
5171 {
5172 .cmd = NL80211_CMD_NEW_KEY,
5173 .doit = nl80211_new_key,
5174 .policy = nl80211_policy,
5175 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005176 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005177 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005178 },
5179 {
5180 .cmd = NL80211_CMD_DEL_KEY,
5181 .doit = nl80211_del_key,
5182 .policy = nl80211_policy,
5183 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005184 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005185 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005186 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005187 {
5188 .cmd = NL80211_CMD_SET_BEACON,
5189 .policy = nl80211_policy,
5190 .flags = GENL_ADMIN_PERM,
5191 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005192 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5193 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005194 },
5195 {
5196 .cmd = NL80211_CMD_NEW_BEACON,
5197 .policy = nl80211_policy,
5198 .flags = GENL_ADMIN_PERM,
5199 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005200 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5201 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005202 },
5203 {
5204 .cmd = NL80211_CMD_DEL_BEACON,
5205 .policy = nl80211_policy,
5206 .flags = GENL_ADMIN_PERM,
5207 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005208 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5209 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005210 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005211 {
5212 .cmd = NL80211_CMD_GET_STATION,
5213 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005214 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005215 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02005216 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5217 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005218 },
5219 {
5220 .cmd = NL80211_CMD_SET_STATION,
5221 .doit = nl80211_set_station,
5222 .policy = nl80211_policy,
5223 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005224 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5225 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005226 },
5227 {
5228 .cmd = NL80211_CMD_NEW_STATION,
5229 .doit = nl80211_new_station,
5230 .policy = nl80211_policy,
5231 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005232 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005233 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005234 },
5235 {
5236 .cmd = NL80211_CMD_DEL_STATION,
5237 .doit = nl80211_del_station,
5238 .policy = nl80211_policy,
5239 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005240 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5241 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005242 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005243 {
5244 .cmd = NL80211_CMD_GET_MPATH,
5245 .doit = nl80211_get_mpath,
5246 .dumpit = nl80211_dump_mpath,
5247 .policy = nl80211_policy,
5248 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005249 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005250 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005251 },
5252 {
5253 .cmd = NL80211_CMD_SET_MPATH,
5254 .doit = nl80211_set_mpath,
5255 .policy = nl80211_policy,
5256 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005257 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005258 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005259 },
5260 {
5261 .cmd = NL80211_CMD_NEW_MPATH,
5262 .doit = nl80211_new_mpath,
5263 .policy = nl80211_policy,
5264 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005265 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005266 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005267 },
5268 {
5269 .cmd = NL80211_CMD_DEL_MPATH,
5270 .doit = nl80211_del_mpath,
5271 .policy = nl80211_policy,
5272 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005273 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5274 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005275 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005276 {
5277 .cmd = NL80211_CMD_SET_BSS,
5278 .doit = nl80211_set_bss,
5279 .policy = nl80211_policy,
5280 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005281 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5282 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005283 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005284 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005285 .cmd = NL80211_CMD_GET_REG,
5286 .doit = nl80211_get_reg,
5287 .policy = nl80211_policy,
5288 /* can be retrieved by unprivileged users */
5289 },
5290 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005291 .cmd = NL80211_CMD_SET_REG,
5292 .doit = nl80211_set_reg,
5293 .policy = nl80211_policy,
5294 .flags = GENL_ADMIN_PERM,
5295 },
5296 {
5297 .cmd = NL80211_CMD_REQ_SET_REG,
5298 .doit = nl80211_req_set_reg,
5299 .policy = nl80211_policy,
5300 .flags = GENL_ADMIN_PERM,
5301 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005302 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005303 .cmd = NL80211_CMD_GET_MESH_CONFIG,
5304 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005305 .policy = nl80211_policy,
5306 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005307 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5308 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005309 },
5310 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005311 .cmd = NL80211_CMD_SET_MESH_CONFIG,
5312 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005313 .policy = nl80211_policy,
5314 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01005315 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005316 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005317 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005318 {
Johannes Berg2a519312009-02-10 21:25:55 +01005319 .cmd = NL80211_CMD_TRIGGER_SCAN,
5320 .doit = nl80211_trigger_scan,
5321 .policy = nl80211_policy,
5322 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005323 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005324 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01005325 },
5326 {
5327 .cmd = NL80211_CMD_GET_SCAN,
5328 .policy = nl80211_policy,
5329 .dumpit = nl80211_dump_scan,
5330 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005331 {
5332 .cmd = NL80211_CMD_AUTHENTICATE,
5333 .doit = nl80211_authenticate,
5334 .policy = nl80211_policy,
5335 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005336 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005337 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005338 },
5339 {
5340 .cmd = NL80211_CMD_ASSOCIATE,
5341 .doit = nl80211_associate,
5342 .policy = nl80211_policy,
5343 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005344 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005345 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005346 },
5347 {
5348 .cmd = NL80211_CMD_DEAUTHENTICATE,
5349 .doit = nl80211_deauthenticate,
5350 .policy = nl80211_policy,
5351 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005352 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005353 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005354 },
5355 {
5356 .cmd = NL80211_CMD_DISASSOCIATE,
5357 .doit = nl80211_disassociate,
5358 .policy = nl80211_policy,
5359 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005360 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005361 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005362 },
Johannes Berg04a773a2009-04-19 21:24:32 +02005363 {
5364 .cmd = NL80211_CMD_JOIN_IBSS,
5365 .doit = nl80211_join_ibss,
5366 .policy = nl80211_policy,
5367 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005368 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005369 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005370 },
5371 {
5372 .cmd = NL80211_CMD_LEAVE_IBSS,
5373 .doit = nl80211_leave_ibss,
5374 .policy = nl80211_policy,
5375 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005376 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005377 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005378 },
Johannes Bergaff89a92009-07-01 21:26:51 +02005379#ifdef CONFIG_NL80211_TESTMODE
5380 {
5381 .cmd = NL80211_CMD_TESTMODE,
5382 .doit = nl80211_testmode_do,
5383 .policy = nl80211_policy,
5384 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005385 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5386 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02005387 },
5388#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02005389 {
5390 .cmd = NL80211_CMD_CONNECT,
5391 .doit = nl80211_connect,
5392 .policy = nl80211_policy,
5393 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005394 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005395 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005396 },
5397 {
5398 .cmd = NL80211_CMD_DISCONNECT,
5399 .doit = nl80211_disconnect,
5400 .policy = nl80211_policy,
5401 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005402 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005403 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005404 },
Johannes Berg463d0182009-07-14 00:33:35 +02005405 {
5406 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5407 .doit = nl80211_wiphy_netns,
5408 .policy = nl80211_policy,
5409 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005410 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5411 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02005412 },
Holger Schurig61fa7132009-11-11 12:25:40 +01005413 {
5414 .cmd = NL80211_CMD_GET_SURVEY,
5415 .policy = nl80211_policy,
5416 .dumpit = nl80211_dump_survey,
5417 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005418 {
5419 .cmd = NL80211_CMD_SET_PMKSA,
5420 .doit = nl80211_setdel_pmksa,
5421 .policy = nl80211_policy,
5422 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005423 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5424 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005425 },
5426 {
5427 .cmd = NL80211_CMD_DEL_PMKSA,
5428 .doit = nl80211_setdel_pmksa,
5429 .policy = nl80211_policy,
5430 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005431 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5432 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005433 },
5434 {
5435 .cmd = NL80211_CMD_FLUSH_PMKSA,
5436 .doit = nl80211_flush_pmksa,
5437 .policy = nl80211_policy,
5438 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005439 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5440 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005441 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005442 {
5443 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5444 .doit = nl80211_remain_on_channel,
5445 .policy = nl80211_policy,
5446 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005447 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005448 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005449 },
5450 {
5451 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5452 .doit = nl80211_cancel_remain_on_channel,
5453 .policy = nl80211_policy,
5454 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005455 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005456 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005457 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005458 {
5459 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5460 .doit = nl80211_set_tx_bitrate_mask,
5461 .policy = nl80211_policy,
5462 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005463 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5464 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005465 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005466 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005467 .cmd = NL80211_CMD_REGISTER_FRAME,
5468 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005469 .policy = nl80211_policy,
5470 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005471 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5472 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005473 },
5474 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005475 .cmd = NL80211_CMD_FRAME,
5476 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005477 .policy = nl80211_policy,
5478 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005479 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005480 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005481 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005482 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005483 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
5484 .doit = nl80211_tx_mgmt_cancel_wait,
5485 .policy = nl80211_policy,
5486 .flags = GENL_ADMIN_PERM,
5487 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5488 NL80211_FLAG_NEED_RTNL,
5489 },
5490 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02005491 .cmd = NL80211_CMD_SET_POWER_SAVE,
5492 .doit = nl80211_set_power_save,
5493 .policy = nl80211_policy,
5494 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005495 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5496 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005497 },
5498 {
5499 .cmd = NL80211_CMD_GET_POWER_SAVE,
5500 .doit = nl80211_get_power_save,
5501 .policy = nl80211_policy,
5502 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005503 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5504 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005505 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005506 {
5507 .cmd = NL80211_CMD_SET_CQM,
5508 .doit = nl80211_set_cqm,
5509 .policy = nl80211_policy,
5510 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005511 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5512 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005513 },
Johannes Bergf444de02010-05-05 15:25:02 +02005514 {
5515 .cmd = NL80211_CMD_SET_CHANNEL,
5516 .doit = nl80211_set_channel,
5517 .policy = nl80211_policy,
5518 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005519 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5520 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02005521 },
Bill Jordane8347eb2010-10-01 13:54:28 -04005522 {
5523 .cmd = NL80211_CMD_SET_WDS_PEER,
5524 .doit = nl80211_set_wds_peer,
5525 .policy = nl80211_policy,
5526 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02005527 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5528 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04005529 },
Johannes Berg29cbe682010-12-03 09:20:44 +01005530 {
5531 .cmd = NL80211_CMD_JOIN_MESH,
5532 .doit = nl80211_join_mesh,
5533 .policy = nl80211_policy,
5534 .flags = GENL_ADMIN_PERM,
5535 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5536 NL80211_FLAG_NEED_RTNL,
5537 },
5538 {
5539 .cmd = NL80211_CMD_LEAVE_MESH,
5540 .doit = nl80211_leave_mesh,
5541 .policy = nl80211_policy,
5542 .flags = GENL_ADMIN_PERM,
5543 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5544 NL80211_FLAG_NEED_RTNL,
5545 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02005546 {
5547 .cmd = NL80211_CMD_GET_WOWLAN,
5548 .doit = nl80211_get_wowlan,
5549 .policy = nl80211_policy,
5550 /* can be retrieved by unprivileged users */
5551 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5552 NL80211_FLAG_NEED_RTNL,
5553 },
5554 {
5555 .cmd = NL80211_CMD_SET_WOWLAN,
5556 .doit = nl80211_set_wowlan,
5557 .policy = nl80211_policy,
5558 .flags = GENL_ADMIN_PERM,
5559 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5560 NL80211_FLAG_NEED_RTNL,
5561 },
Johannes Berg55682962007-09-20 13:09:35 -04005562};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005563
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005564static struct genl_multicast_group nl80211_mlme_mcgrp = {
5565 .name = "mlme",
5566};
Johannes Berg55682962007-09-20 13:09:35 -04005567
5568/* multicast groups */
5569static struct genl_multicast_group nl80211_config_mcgrp = {
5570 .name = "config",
5571};
Johannes Berg2a519312009-02-10 21:25:55 +01005572static struct genl_multicast_group nl80211_scan_mcgrp = {
5573 .name = "scan",
5574};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005575static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5576 .name = "regulatory",
5577};
Johannes Berg55682962007-09-20 13:09:35 -04005578
5579/* notification functions */
5580
5581void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5582{
5583 struct sk_buff *msg;
5584
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005585 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005586 if (!msg)
5587 return;
5588
5589 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5590 nlmsg_free(msg);
5591 return;
5592 }
5593
Johannes Berg463d0182009-07-14 00:33:35 +02005594 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5595 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005596}
5597
Johannes Berg362a4152009-05-24 16:43:15 +02005598static int nl80211_add_scan_req(struct sk_buff *msg,
5599 struct cfg80211_registered_device *rdev)
5600{
5601 struct cfg80211_scan_request *req = rdev->scan_req;
5602 struct nlattr *nest;
5603 int i;
5604
Johannes Berg667503dd2009-07-07 03:56:11 +02005605 ASSERT_RDEV_LOCK(rdev);
5606
Johannes Berg362a4152009-05-24 16:43:15 +02005607 if (WARN_ON(!req))
5608 return 0;
5609
5610 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5611 if (!nest)
5612 goto nla_put_failure;
5613 for (i = 0; i < req->n_ssids; i++)
5614 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5615 nla_nest_end(msg, nest);
5616
5617 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5618 if (!nest)
5619 goto nla_put_failure;
5620 for (i = 0; i < req->n_channels; i++)
5621 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5622 nla_nest_end(msg, nest);
5623
5624 if (req->ie)
5625 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5626
5627 return 0;
5628 nla_put_failure:
5629 return -ENOBUFS;
5630}
5631
Johannes Berga538e2d2009-06-16 19:56:42 +02005632static int nl80211_send_scan_msg(struct sk_buff *msg,
5633 struct cfg80211_registered_device *rdev,
5634 struct net_device *netdev,
5635 u32 pid, u32 seq, int flags,
5636 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005637{
5638 void *hdr;
5639
5640 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5641 if (!hdr)
5642 return -1;
5643
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005644 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005645 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5646
Johannes Berg362a4152009-05-24 16:43:15 +02005647 /* ignore errors and send incomplete event anyway */
5648 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005649
5650 return genlmsg_end(msg, hdr);
5651
5652 nla_put_failure:
5653 genlmsg_cancel(msg, hdr);
5654 return -EMSGSIZE;
5655}
5656
Johannes Berga538e2d2009-06-16 19:56:42 +02005657void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5658 struct net_device *netdev)
5659{
5660 struct sk_buff *msg;
5661
5662 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5663 if (!msg)
5664 return;
5665
5666 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5667 NL80211_CMD_TRIGGER_SCAN) < 0) {
5668 nlmsg_free(msg);
5669 return;
5670 }
5671
Johannes Berg463d0182009-07-14 00:33:35 +02005672 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5673 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005674}
5675
Johannes Berg2a519312009-02-10 21:25:55 +01005676void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5677 struct net_device *netdev)
5678{
5679 struct sk_buff *msg;
5680
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005681 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005682 if (!msg)
5683 return;
5684
Johannes Berga538e2d2009-06-16 19:56:42 +02005685 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5686 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005687 nlmsg_free(msg);
5688 return;
5689 }
5690
Johannes Berg463d0182009-07-14 00:33:35 +02005691 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5692 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005693}
5694
5695void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5696 struct net_device *netdev)
5697{
5698 struct sk_buff *msg;
5699
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005700 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005701 if (!msg)
5702 return;
5703
Johannes Berga538e2d2009-06-16 19:56:42 +02005704 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5705 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005706 nlmsg_free(msg);
5707 return;
5708 }
5709
Johannes Berg463d0182009-07-14 00:33:35 +02005710 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5711 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005712}
5713
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005714/*
5715 * This can happen on global regulatory changes or device specific settings
5716 * based on custom world regulatory domains.
5717 */
5718void nl80211_send_reg_change_event(struct regulatory_request *request)
5719{
5720 struct sk_buff *msg;
5721 void *hdr;
5722
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005723 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005724 if (!msg)
5725 return;
5726
5727 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5728 if (!hdr) {
5729 nlmsg_free(msg);
5730 return;
5731 }
5732
5733 /* Userspace can always count this one always being set */
5734 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5735
5736 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5737 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5738 NL80211_REGDOM_TYPE_WORLD);
5739 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5740 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5741 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5742 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5743 request->intersect)
5744 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5745 NL80211_REGDOM_TYPE_INTERSECTION);
5746 else {
5747 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5748 NL80211_REGDOM_TYPE_COUNTRY);
5749 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5750 }
5751
5752 if (wiphy_idx_valid(request->wiphy_idx))
5753 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5754
5755 if (genlmsg_end(msg, hdr) < 0) {
5756 nlmsg_free(msg);
5757 return;
5758 }
5759
Johannes Bergbc43b282009-07-25 10:54:13 +02005760 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005761 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005762 GFP_ATOMIC);
5763 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005764
5765 return;
5766
5767nla_put_failure:
5768 genlmsg_cancel(msg, hdr);
5769 nlmsg_free(msg);
5770}
5771
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005772static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5773 struct net_device *netdev,
5774 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005775 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005776{
5777 struct sk_buff *msg;
5778 void *hdr;
5779
Johannes Berge6d6e342009-07-01 21:26:47 +02005780 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005781 if (!msg)
5782 return;
5783
5784 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5785 if (!hdr) {
5786 nlmsg_free(msg);
5787 return;
5788 }
5789
5790 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5791 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5792 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5793
5794 if (genlmsg_end(msg, hdr) < 0) {
5795 nlmsg_free(msg);
5796 return;
5797 }
5798
Johannes Berg463d0182009-07-14 00:33:35 +02005799 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5800 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005801 return;
5802
5803 nla_put_failure:
5804 genlmsg_cancel(msg, hdr);
5805 nlmsg_free(msg);
5806}
5807
5808void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005809 struct net_device *netdev, const u8 *buf,
5810 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005811{
5812 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005813 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005814}
5815
5816void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5817 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005818 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005819{
Johannes Berge6d6e342009-07-01 21:26:47 +02005820 nl80211_send_mlme_event(rdev, netdev, buf, len,
5821 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005822}
5823
Jouni Malinen53b46b82009-03-27 20:53:56 +02005824void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005825 struct net_device *netdev, const u8 *buf,
5826 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005827{
5828 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005829 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005830}
5831
Jouni Malinen53b46b82009-03-27 20:53:56 +02005832void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5833 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005834 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005835{
5836 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005837 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005838}
5839
Jouni Malinencf4e5942010-12-16 00:52:40 +02005840void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
5841 struct net_device *netdev, const u8 *buf,
5842 size_t len, gfp_t gfp)
5843{
5844 nl80211_send_mlme_event(rdev, netdev, buf, len,
5845 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
5846}
5847
5848void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
5849 struct net_device *netdev, const u8 *buf,
5850 size_t len, gfp_t gfp)
5851{
5852 nl80211_send_mlme_event(rdev, netdev, buf, len,
5853 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
5854}
5855
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005856static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5857 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005858 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005859{
5860 struct sk_buff *msg;
5861 void *hdr;
5862
Johannes Berge6d6e342009-07-01 21:26:47 +02005863 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005864 if (!msg)
5865 return;
5866
5867 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5868 if (!hdr) {
5869 nlmsg_free(msg);
5870 return;
5871 }
5872
5873 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5874 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5875 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5876 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5877
5878 if (genlmsg_end(msg, hdr) < 0) {
5879 nlmsg_free(msg);
5880 return;
5881 }
5882
Johannes Berg463d0182009-07-14 00:33:35 +02005883 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5884 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005885 return;
5886
5887 nla_put_failure:
5888 genlmsg_cancel(msg, hdr);
5889 nlmsg_free(msg);
5890}
5891
5892void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005893 struct net_device *netdev, const u8 *addr,
5894 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005895{
5896 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005897 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005898}
5899
5900void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005901 struct net_device *netdev, const u8 *addr,
5902 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005903{
Johannes Berge6d6e342009-07-01 21:26:47 +02005904 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5905 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005906}
5907
Samuel Ortizb23aa672009-07-01 21:26:54 +02005908void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5909 struct net_device *netdev, const u8 *bssid,
5910 const u8 *req_ie, size_t req_ie_len,
5911 const u8 *resp_ie, size_t resp_ie_len,
5912 u16 status, gfp_t gfp)
5913{
5914 struct sk_buff *msg;
5915 void *hdr;
5916
5917 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5918 if (!msg)
5919 return;
5920
5921 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5922 if (!hdr) {
5923 nlmsg_free(msg);
5924 return;
5925 }
5926
5927 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5928 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5929 if (bssid)
5930 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5931 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5932 if (req_ie)
5933 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5934 if (resp_ie)
5935 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5936
5937 if (genlmsg_end(msg, hdr) < 0) {
5938 nlmsg_free(msg);
5939 return;
5940 }
5941
Johannes Berg463d0182009-07-14 00:33:35 +02005942 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5943 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005944 return;
5945
5946 nla_put_failure:
5947 genlmsg_cancel(msg, hdr);
5948 nlmsg_free(msg);
5949
5950}
5951
5952void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5953 struct net_device *netdev, const u8 *bssid,
5954 const u8 *req_ie, size_t req_ie_len,
5955 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5956{
5957 struct sk_buff *msg;
5958 void *hdr;
5959
5960 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5961 if (!msg)
5962 return;
5963
5964 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5965 if (!hdr) {
5966 nlmsg_free(msg);
5967 return;
5968 }
5969
5970 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5971 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5972 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5973 if (req_ie)
5974 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5975 if (resp_ie)
5976 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5977
5978 if (genlmsg_end(msg, hdr) < 0) {
5979 nlmsg_free(msg);
5980 return;
5981 }
5982
Johannes Berg463d0182009-07-14 00:33:35 +02005983 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5984 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005985 return;
5986
5987 nla_put_failure:
5988 genlmsg_cancel(msg, hdr);
5989 nlmsg_free(msg);
5990
5991}
5992
5993void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5994 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02005995 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005996{
5997 struct sk_buff *msg;
5998 void *hdr;
5999
Johannes Berg667503dd2009-07-07 03:56:11 +02006000 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006001 if (!msg)
6002 return;
6003
6004 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6005 if (!hdr) {
6006 nlmsg_free(msg);
6007 return;
6008 }
6009
6010 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6011 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6012 if (from_ap && reason)
6013 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6014 if (from_ap)
6015 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6016 if (ie)
6017 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6018
6019 if (genlmsg_end(msg, hdr) < 0) {
6020 nlmsg_free(msg);
6021 return;
6022 }
6023
Johannes Berg463d0182009-07-14 00:33:35 +02006024 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6025 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006026 return;
6027
6028 nla_put_failure:
6029 genlmsg_cancel(msg, hdr);
6030 nlmsg_free(msg);
6031
6032}
6033
Johannes Berg04a773a2009-04-19 21:24:32 +02006034void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6035 struct net_device *netdev, const u8 *bssid,
6036 gfp_t gfp)
6037{
6038 struct sk_buff *msg;
6039 void *hdr;
6040
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006041 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006042 if (!msg)
6043 return;
6044
6045 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6046 if (!hdr) {
6047 nlmsg_free(msg);
6048 return;
6049 }
6050
6051 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6052 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6053 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6054
6055 if (genlmsg_end(msg, hdr) < 0) {
6056 nlmsg_free(msg);
6057 return;
6058 }
6059
Johannes Berg463d0182009-07-14 00:33:35 +02006060 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6061 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006062 return;
6063
6064 nla_put_failure:
6065 genlmsg_cancel(msg, hdr);
6066 nlmsg_free(msg);
6067}
6068
Javier Cardonac93b5e72011-04-07 15:08:34 -07006069void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6070 struct net_device *netdev,
6071 const u8 *macaddr, const u8* ie, u8 ie_len,
6072 gfp_t gfp)
6073{
6074 struct sk_buff *msg;
6075 void *hdr;
6076
6077 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6078 if (!msg)
6079 return;
6080
6081 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6082 if (!hdr) {
6083 nlmsg_free(msg);
6084 return;
6085 }
6086
6087 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6088 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6089 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6090 if (ie_len && ie)
6091 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6092
6093 if (genlmsg_end(msg, hdr) < 0) {
6094 nlmsg_free(msg);
6095 return;
6096 }
6097
6098 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6099 nl80211_mlme_mcgrp.id, gfp);
6100 return;
6101
6102 nla_put_failure:
6103 genlmsg_cancel(msg, hdr);
6104 nlmsg_free(msg);
6105}
6106
Jouni Malinena3b8b052009-03-27 21:59:49 +02006107void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6108 struct net_device *netdev, const u8 *addr,
6109 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02006110 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02006111{
6112 struct sk_buff *msg;
6113 void *hdr;
6114
Johannes Berge6d6e342009-07-01 21:26:47 +02006115 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006116 if (!msg)
6117 return;
6118
6119 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
6120 if (!hdr) {
6121 nlmsg_free(msg);
6122 return;
6123 }
6124
6125 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6126 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6127 if (addr)
6128 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6129 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
6130 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
6131 if (tsc)
6132 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
6133
6134 if (genlmsg_end(msg, hdr) < 0) {
6135 nlmsg_free(msg);
6136 return;
6137 }
6138
Johannes Berg463d0182009-07-14 00:33:35 +02006139 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6140 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006141 return;
6142
6143 nla_put_failure:
6144 genlmsg_cancel(msg, hdr);
6145 nlmsg_free(msg);
6146}
6147
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006148void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
6149 struct ieee80211_channel *channel_before,
6150 struct ieee80211_channel *channel_after)
6151{
6152 struct sk_buff *msg;
6153 void *hdr;
6154 struct nlattr *nl_freq;
6155
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006156 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006157 if (!msg)
6158 return;
6159
6160 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
6161 if (!hdr) {
6162 nlmsg_free(msg);
6163 return;
6164 }
6165
6166 /*
6167 * Since we are applying the beacon hint to a wiphy we know its
6168 * wiphy_idx is valid
6169 */
6170 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
6171
6172 /* Before */
6173 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
6174 if (!nl_freq)
6175 goto nla_put_failure;
6176 if (nl80211_msg_put_channel(msg, channel_before))
6177 goto nla_put_failure;
6178 nla_nest_end(msg, nl_freq);
6179
6180 /* After */
6181 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
6182 if (!nl_freq)
6183 goto nla_put_failure;
6184 if (nl80211_msg_put_channel(msg, channel_after))
6185 goto nla_put_failure;
6186 nla_nest_end(msg, nl_freq);
6187
6188 if (genlmsg_end(msg, hdr) < 0) {
6189 nlmsg_free(msg);
6190 return;
6191 }
6192
Johannes Berg463d0182009-07-14 00:33:35 +02006193 rcu_read_lock();
6194 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6195 GFP_ATOMIC);
6196 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006197
6198 return;
6199
6200nla_put_failure:
6201 genlmsg_cancel(msg, hdr);
6202 nlmsg_free(msg);
6203}
6204
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006205static void nl80211_send_remain_on_chan_event(
6206 int cmd, struct cfg80211_registered_device *rdev,
6207 struct net_device *netdev, u64 cookie,
6208 struct ieee80211_channel *chan,
6209 enum nl80211_channel_type channel_type,
6210 unsigned int duration, gfp_t gfp)
6211{
6212 struct sk_buff *msg;
6213 void *hdr;
6214
6215 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6216 if (!msg)
6217 return;
6218
6219 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6220 if (!hdr) {
6221 nlmsg_free(msg);
6222 return;
6223 }
6224
6225 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6226 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6227 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
6228 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
6229 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6230
6231 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
6232 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
6233
6234 if (genlmsg_end(msg, hdr) < 0) {
6235 nlmsg_free(msg);
6236 return;
6237 }
6238
6239 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6240 nl80211_mlme_mcgrp.id, gfp);
6241 return;
6242
6243 nla_put_failure:
6244 genlmsg_cancel(msg, hdr);
6245 nlmsg_free(msg);
6246}
6247
6248void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
6249 struct net_device *netdev, u64 cookie,
6250 struct ieee80211_channel *chan,
6251 enum nl80211_channel_type channel_type,
6252 unsigned int duration, gfp_t gfp)
6253{
6254 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
6255 rdev, netdev, cookie, chan,
6256 channel_type, duration, gfp);
6257}
6258
6259void nl80211_send_remain_on_channel_cancel(
6260 struct cfg80211_registered_device *rdev, struct net_device *netdev,
6261 u64 cookie, struct ieee80211_channel *chan,
6262 enum nl80211_channel_type channel_type, gfp_t gfp)
6263{
6264 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6265 rdev, netdev, cookie, chan,
6266 channel_type, 0, gfp);
6267}
6268
Johannes Berg98b62182009-12-23 13:15:44 +01006269void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
6270 struct net_device *dev, const u8 *mac_addr,
6271 struct station_info *sinfo, gfp_t gfp)
6272{
6273 struct sk_buff *msg;
6274
6275 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6276 if (!msg)
6277 return;
6278
6279 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
6280 nlmsg_free(msg);
6281 return;
6282 }
6283
6284 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6285 nl80211_mlme_mcgrp.id, gfp);
6286}
6287
Jouni Malinenec15e682011-03-23 15:29:52 +02006288void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
6289 struct net_device *dev, const u8 *mac_addr,
6290 gfp_t gfp)
6291{
6292 struct sk_buff *msg;
6293 void *hdr;
6294
6295 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6296 if (!msg)
6297 return;
6298
6299 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
6300 if (!hdr) {
6301 nlmsg_free(msg);
6302 return;
6303 }
6304
6305 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
6306 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
6307
6308 if (genlmsg_end(msg, hdr) < 0) {
6309 nlmsg_free(msg);
6310 return;
6311 }
6312
6313 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6314 nl80211_mlme_mcgrp.id, gfp);
6315 return;
6316
6317 nla_put_failure:
6318 genlmsg_cancel(msg, hdr);
6319 nlmsg_free(msg);
6320}
6321
Johannes Berg2e161f72010-08-12 15:38:38 +02006322int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
6323 struct net_device *netdev, u32 nlpid,
6324 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006325{
6326 struct sk_buff *msg;
6327 void *hdr;
6328 int err;
6329
6330 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6331 if (!msg)
6332 return -ENOMEM;
6333
Johannes Berg2e161f72010-08-12 15:38:38 +02006334 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006335 if (!hdr) {
6336 nlmsg_free(msg);
6337 return -ENOMEM;
6338 }
6339
6340 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6341 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6342 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6343 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6344
6345 err = genlmsg_end(msg, hdr);
6346 if (err < 0) {
6347 nlmsg_free(msg);
6348 return err;
6349 }
6350
6351 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
6352 if (err < 0)
6353 return err;
6354 return 0;
6355
6356 nla_put_failure:
6357 genlmsg_cancel(msg, hdr);
6358 nlmsg_free(msg);
6359 return -ENOBUFS;
6360}
6361
Johannes Berg2e161f72010-08-12 15:38:38 +02006362void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
6363 struct net_device *netdev, u64 cookie,
6364 const u8 *buf, size_t len, bool ack,
6365 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006366{
6367 struct sk_buff *msg;
6368 void *hdr;
6369
6370 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6371 if (!msg)
6372 return;
6373
Johannes Berg2e161f72010-08-12 15:38:38 +02006374 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02006375 if (!hdr) {
6376 nlmsg_free(msg);
6377 return;
6378 }
6379
6380 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6381 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6382 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6383 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6384 if (ack)
6385 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
6386
6387 if (genlmsg_end(msg, hdr) < 0) {
6388 nlmsg_free(msg);
6389 return;
6390 }
6391
6392 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
6393 return;
6394
6395 nla_put_failure:
6396 genlmsg_cancel(msg, hdr);
6397 nlmsg_free(msg);
6398}
6399
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006400void
6401nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
6402 struct net_device *netdev,
6403 enum nl80211_cqm_rssi_threshold_event rssi_event,
6404 gfp_t gfp)
6405{
6406 struct sk_buff *msg;
6407 struct nlattr *pinfoattr;
6408 void *hdr;
6409
6410 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6411 if (!msg)
6412 return;
6413
6414 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6415 if (!hdr) {
6416 nlmsg_free(msg);
6417 return;
6418 }
6419
6420 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6421 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6422
6423 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6424 if (!pinfoattr)
6425 goto nla_put_failure;
6426
6427 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
6428 rssi_event);
6429
6430 nla_nest_end(msg, pinfoattr);
6431
6432 if (genlmsg_end(msg, hdr) < 0) {
6433 nlmsg_free(msg);
6434 return;
6435 }
6436
6437 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6438 nl80211_mlme_mcgrp.id, gfp);
6439 return;
6440
6441 nla_put_failure:
6442 genlmsg_cancel(msg, hdr);
6443 nlmsg_free(msg);
6444}
6445
Johannes Bergc063dbf2010-11-24 08:10:05 +01006446void
6447nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
6448 struct net_device *netdev, const u8 *peer,
6449 u32 num_packets, gfp_t gfp)
6450{
6451 struct sk_buff *msg;
6452 struct nlattr *pinfoattr;
6453 void *hdr;
6454
6455 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6456 if (!msg)
6457 return;
6458
6459 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6460 if (!hdr) {
6461 nlmsg_free(msg);
6462 return;
6463 }
6464
6465 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6466 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6467 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
6468
6469 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6470 if (!pinfoattr)
6471 goto nla_put_failure;
6472
6473 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
6474
6475 nla_nest_end(msg, pinfoattr);
6476
6477 if (genlmsg_end(msg, hdr) < 0) {
6478 nlmsg_free(msg);
6479 return;
6480 }
6481
6482 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6483 nl80211_mlme_mcgrp.id, gfp);
6484 return;
6485
6486 nla_put_failure:
6487 genlmsg_cancel(msg, hdr);
6488 nlmsg_free(msg);
6489}
6490
Jouni Malinen026331c2010-02-15 12:53:10 +02006491static int nl80211_netlink_notify(struct notifier_block * nb,
6492 unsigned long state,
6493 void *_notify)
6494{
6495 struct netlink_notify *notify = _notify;
6496 struct cfg80211_registered_device *rdev;
6497 struct wireless_dev *wdev;
6498
6499 if (state != NETLINK_URELEASE)
6500 return NOTIFY_DONE;
6501
6502 rcu_read_lock();
6503
6504 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
6505 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02006506 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02006507
6508 rcu_read_unlock();
6509
6510 return NOTIFY_DONE;
6511}
6512
6513static struct notifier_block nl80211_netlink_notifier = {
6514 .notifier_call = nl80211_netlink_notify,
6515};
6516
Johannes Berg55682962007-09-20 13:09:35 -04006517/* initialisation/exit functions */
6518
6519int nl80211_init(void)
6520{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006521 int err;
Johannes Berg55682962007-09-20 13:09:35 -04006522
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006523 err = genl_register_family_with_ops(&nl80211_fam,
6524 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04006525 if (err)
6526 return err;
6527
Johannes Berg55682962007-09-20 13:09:35 -04006528 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6529 if (err)
6530 goto err_out;
6531
Johannes Berg2a519312009-02-10 21:25:55 +01006532 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6533 if (err)
6534 goto err_out;
6535
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006536 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6537 if (err)
6538 goto err_out;
6539
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006540 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6541 if (err)
6542 goto err_out;
6543
Johannes Bergaff89a92009-07-01 21:26:51 +02006544#ifdef CONFIG_NL80211_TESTMODE
6545 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6546 if (err)
6547 goto err_out;
6548#endif
6549
Jouni Malinen026331c2010-02-15 12:53:10 +02006550 err = netlink_register_notifier(&nl80211_netlink_notifier);
6551 if (err)
6552 goto err_out;
6553
Johannes Berg55682962007-09-20 13:09:35 -04006554 return 0;
6555 err_out:
6556 genl_unregister_family(&nl80211_fam);
6557 return err;
6558}
6559
6560void nl80211_exit(void)
6561{
Jouni Malinen026331c2010-02-15 12:53:10 +02006562 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006563 genl_unregister_family(&nl80211_fam);
6564}