blob: 297d7ce4117b99aec8cc358086ac0a956a2bb8ad [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 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700127
Jouni Malinen36aedc92008-08-25 11:58:58 +0300128 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
129 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200130
131 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
132 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
133 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100134 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
135 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200136
137 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_SSID_LEN },
139 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
140 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200141 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300142 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300143 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300144 [NL80211_ATTR_STA_FLAGS2] = {
145 .len = sizeof(struct nl80211_sta_flag_update),
146 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300147 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300148 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
149 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200150 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
151 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
152 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200153 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100154 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100155 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
156 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100157 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
158 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200159 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200160 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
161 .len = IEEE80211_MAX_DATA_LEN },
162 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200163 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200164 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300165 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200166 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300167 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
168 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200169 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900170 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
171 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100172 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100173 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100174 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Berg55682962007-09-20 13:09:35 -0400175};
176
Johannes Berge31b8212010-10-05 19:39:30 +0200177/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000178static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200179 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200180 [NL80211_KEY_IDX] = { .type = NLA_U8 },
181 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
182 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
183 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
184 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200185 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100186 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
187};
188
189/* policy for the key default flags */
190static const struct nla_policy
191nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
192 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
193 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200194};
195
Holger Schuriga0438972009-11-11 11:30:02 +0100196/* ifidx get helper */
197static int nl80211_get_ifidx(struct netlink_callback *cb)
198{
199 int res;
200
201 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
202 nl80211_fam.attrbuf, nl80211_fam.maxattr,
203 nl80211_policy);
204 if (res)
205 return res;
206
207 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
208 return -EINVAL;
209
210 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
211 if (!res)
212 return -EINVAL;
213 return res;
214}
215
Johannes Berg67748892010-10-04 21:14:06 +0200216static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
217 struct netlink_callback *cb,
218 struct cfg80211_registered_device **rdev,
219 struct net_device **dev)
220{
221 int ifidx = cb->args[0];
222 int err;
223
224 if (!ifidx)
225 ifidx = nl80211_get_ifidx(cb);
226 if (ifidx < 0)
227 return ifidx;
228
229 cb->args[0] = ifidx;
230
231 rtnl_lock();
232
233 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
234 if (!*dev) {
235 err = -ENODEV;
236 goto out_rtnl;
237 }
238
239 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100240 if (IS_ERR(*rdev)) {
241 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200242 goto out_rtnl;
243 }
244
245 return 0;
246 out_rtnl:
247 rtnl_unlock();
248 return err;
249}
250
251static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
252{
253 cfg80211_unlock_rdev(rdev);
254 rtnl_unlock();
255}
256
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100257/* IE validation */
258static bool is_valid_ie_attr(const struct nlattr *attr)
259{
260 const u8 *pos;
261 int len;
262
263 if (!attr)
264 return true;
265
266 pos = nla_data(attr);
267 len = nla_len(attr);
268
269 while (len) {
270 u8 elemlen;
271
272 if (len < 2)
273 return false;
274 len -= 2;
275
276 elemlen = pos[1];
277 if (elemlen > len)
278 return false;
279
280 len -= elemlen;
281 pos += 2 + elemlen;
282 }
283
284 return true;
285}
286
Johannes Berg55682962007-09-20 13:09:35 -0400287/* message building helper */
288static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
289 int flags, u8 cmd)
290{
291 /* since there is no private header just add the generic one */
292 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
293}
294
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400295static int nl80211_msg_put_channel(struct sk_buff *msg,
296 struct ieee80211_channel *chan)
297{
298 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
299 chan->center_freq);
300
301 if (chan->flags & IEEE80211_CHAN_DISABLED)
302 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
303 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
304 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
305 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
306 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
307 if (chan->flags & IEEE80211_CHAN_RADAR)
308 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
309
310 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
311 DBM_TO_MBM(chan->max_power));
312
313 return 0;
314
315 nla_put_failure:
316 return -ENOBUFS;
317}
318
Johannes Berg55682962007-09-20 13:09:35 -0400319/* netlink command implementations */
320
Johannes Bergb9454e82009-07-08 13:29:08 +0200321struct key_parse {
322 struct key_params p;
323 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200324 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200325 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100326 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200327};
328
329static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
330{
331 struct nlattr *tb[NL80211_KEY_MAX + 1];
332 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
333 nl80211_key_policy);
334 if (err)
335 return err;
336
337 k->def = !!tb[NL80211_KEY_DEFAULT];
338 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
339
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100340 if (k->def) {
341 k->def_uni = true;
342 k->def_multi = true;
343 }
344 if (k->defmgmt)
345 k->def_multi = true;
346
Johannes Bergb9454e82009-07-08 13:29:08 +0200347 if (tb[NL80211_KEY_IDX])
348 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
349
350 if (tb[NL80211_KEY_DATA]) {
351 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
352 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
353 }
354
355 if (tb[NL80211_KEY_SEQ]) {
356 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
357 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
358 }
359
360 if (tb[NL80211_KEY_CIPHER])
361 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
362
Johannes Berge31b8212010-10-05 19:39:30 +0200363 if (tb[NL80211_KEY_TYPE]) {
364 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
365 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
366 return -EINVAL;
367 }
368
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100369 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
370 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
371 int err = nla_parse_nested(kdt,
372 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
373 tb[NL80211_KEY_DEFAULT_TYPES],
374 nl80211_key_default_policy);
375 if (err)
376 return err;
377
378 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
379 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
380 }
381
Johannes Bergb9454e82009-07-08 13:29:08 +0200382 return 0;
383}
384
385static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
386{
387 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
388 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
389 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
390 }
391
392 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
393 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
394 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
395 }
396
397 if (info->attrs[NL80211_ATTR_KEY_IDX])
398 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
399
400 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
401 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
402
403 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
404 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
405
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100406 if (k->def) {
407 k->def_uni = true;
408 k->def_multi = true;
409 }
410 if (k->defmgmt)
411 k->def_multi = true;
412
Johannes Berge31b8212010-10-05 19:39:30 +0200413 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
414 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
415 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
416 return -EINVAL;
417 }
418
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100419 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
420 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
421 int err = nla_parse_nested(
422 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
423 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
424 nl80211_key_default_policy);
425 if (err)
426 return err;
427
428 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
429 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
430 }
431
Johannes Bergb9454e82009-07-08 13:29:08 +0200432 return 0;
433}
434
435static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
436{
437 int err;
438
439 memset(k, 0, sizeof(*k));
440 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200441 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200442
443 if (info->attrs[NL80211_ATTR_KEY])
444 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
445 else
446 err = nl80211_parse_key_old(info, k);
447
448 if (err)
449 return err;
450
451 if (k->def && k->defmgmt)
452 return -EINVAL;
453
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100454 if (k->defmgmt) {
455 if (k->def_uni || !k->def_multi)
456 return -EINVAL;
457 }
458
Johannes Bergb9454e82009-07-08 13:29:08 +0200459 if (k->idx != -1) {
460 if (k->defmgmt) {
461 if (k->idx < 4 || k->idx > 5)
462 return -EINVAL;
463 } else if (k->def) {
464 if (k->idx < 0 || k->idx > 3)
465 return -EINVAL;
466 } else {
467 if (k->idx < 0 || k->idx > 5)
468 return -EINVAL;
469 }
470 }
471
472 return 0;
473}
474
Johannes Bergfffd0932009-07-08 14:22:54 +0200475static struct cfg80211_cached_keys *
476nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
477 struct nlattr *keys)
478{
479 struct key_parse parse;
480 struct nlattr *key;
481 struct cfg80211_cached_keys *result;
482 int rem, err, def = 0;
483
484 result = kzalloc(sizeof(*result), GFP_KERNEL);
485 if (!result)
486 return ERR_PTR(-ENOMEM);
487
488 result->def = -1;
489 result->defmgmt = -1;
490
491 nla_for_each_nested(key, keys, rem) {
492 memset(&parse, 0, sizeof(parse));
493 parse.idx = -1;
494
495 err = nl80211_parse_key_new(key, &parse);
496 if (err)
497 goto error;
498 err = -EINVAL;
499 if (!parse.p.key)
500 goto error;
501 if (parse.idx < 0 || parse.idx > 4)
502 goto error;
503 if (parse.def) {
504 if (def)
505 goto error;
506 def = 1;
507 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100508 if (!parse.def_uni || !parse.def_multi)
509 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200510 } else if (parse.defmgmt)
511 goto error;
512 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200513 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200514 if (err)
515 goto error;
516 result->params[parse.idx].cipher = parse.p.cipher;
517 result->params[parse.idx].key_len = parse.p.key_len;
518 result->params[parse.idx].key = result->data[parse.idx];
519 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
520 }
521
522 return result;
523 error:
524 kfree(result);
525 return ERR_PTR(err);
526}
527
528static int nl80211_key_allowed(struct wireless_dev *wdev)
529{
530 ASSERT_WDEV_LOCK(wdev);
531
Johannes Bergfffd0932009-07-08 14:22:54 +0200532 switch (wdev->iftype) {
533 case NL80211_IFTYPE_AP:
534 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200535 case NL80211_IFTYPE_P2P_GO:
Johannes Bergfffd0932009-07-08 14:22:54 +0200536 break;
537 case NL80211_IFTYPE_ADHOC:
538 if (!wdev->current_bss)
539 return -ENOLINK;
540 break;
541 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200542 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200543 if (wdev->sme_state != CFG80211_SME_CONNECTED)
544 return -ENOLINK;
545 break;
546 default:
547 return -EINVAL;
548 }
549
550 return 0;
551}
552
Johannes Berg55682962007-09-20 13:09:35 -0400553static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
554 struct cfg80211_registered_device *dev)
555{
556 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100557 struct nlattr *nl_bands, *nl_band;
558 struct nlattr *nl_freqs, *nl_freq;
559 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700560 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100561 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100562 enum ieee80211_band band;
563 struct ieee80211_channel *chan;
564 struct ieee80211_rate *rate;
565 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700566 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg2e161f72010-08-12 15:38:38 +0200567 const struct ieee80211_txrx_stypes *mgmt_stypes =
568 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400569
570 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
571 if (!hdr)
572 return -1;
573
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500574 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400575 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200576
Johannes Bergf5ea9122009-08-07 16:17:38 +0200577 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
578 cfg80211_rdev_list_generation);
579
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200580 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
581 dev->wiphy.retry_short);
582 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
583 dev->wiphy.retry_long);
584 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
585 dev->wiphy.frag_threshold);
586 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
587 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100588 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
589 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100590 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
591 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200592 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
593 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100594
Johannes Berge31b8212010-10-05 19:39:30 +0200595 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
596 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
597
Johannes Berg25e47c182009-04-02 20:14:06 +0200598 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
599 sizeof(u32) * dev->wiphy.n_cipher_suites,
600 dev->wiphy.cipher_suites);
601
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100602 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
603 dev->wiphy.max_num_pmkids);
604
Johannes Bergc0692b82010-08-27 14:26:53 +0300605 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
606 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
607
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900608 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
609 dev->wiphy.available_antennas_tx);
610 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
611 dev->wiphy.available_antennas_rx);
612
Bruno Randolf7f531e02010-12-16 11:30:22 +0900613 if ((dev->wiphy.available_antennas_tx ||
614 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900615 u32 tx_ant = 0, rx_ant = 0;
616 int res;
617 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
618 if (!res) {
619 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
620 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
621 }
622 }
623
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700624 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
625 if (!nl_modes)
626 goto nla_put_failure;
627
628 i = 0;
629 while (ifmodes) {
630 if (ifmodes & 1)
631 NLA_PUT_FLAG(msg, i);
632 ifmodes >>= 1;
633 i++;
634 }
635
636 nla_nest_end(msg, nl_modes);
637
Johannes Bergee688b002008-01-24 19:38:39 +0100638 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
639 if (!nl_bands)
640 goto nla_put_failure;
641
642 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
643 if (!dev->wiphy.bands[band])
644 continue;
645
646 nl_band = nla_nest_start(msg, band);
647 if (!nl_band)
648 goto nla_put_failure;
649
Johannes Bergd51626d2008-10-09 12:20:13 +0200650 /* add HT info */
651 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
652 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
653 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
654 &dev->wiphy.bands[band]->ht_cap.mcs);
655 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
656 dev->wiphy.bands[band]->ht_cap.cap);
657 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
658 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
659 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
660 dev->wiphy.bands[band]->ht_cap.ampdu_density);
661 }
662
Johannes Bergee688b002008-01-24 19:38:39 +0100663 /* add frequencies */
664 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
665 if (!nl_freqs)
666 goto nla_put_failure;
667
668 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
669 nl_freq = nla_nest_start(msg, i);
670 if (!nl_freq)
671 goto nla_put_failure;
672
673 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100674
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400675 if (nl80211_msg_put_channel(msg, chan))
676 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200677
Johannes Bergee688b002008-01-24 19:38:39 +0100678 nla_nest_end(msg, nl_freq);
679 }
680
681 nla_nest_end(msg, nl_freqs);
682
683 /* add bitrates */
684 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
685 if (!nl_rates)
686 goto nla_put_failure;
687
688 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
689 nl_rate = nla_nest_start(msg, i);
690 if (!nl_rate)
691 goto nla_put_failure;
692
693 rate = &dev->wiphy.bands[band]->bitrates[i];
694 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
695 rate->bitrate);
696 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
697 NLA_PUT_FLAG(msg,
698 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
699
700 nla_nest_end(msg, nl_rate);
701 }
702
703 nla_nest_end(msg, nl_rates);
704
705 nla_nest_end(msg, nl_band);
706 }
707 nla_nest_end(msg, nl_bands);
708
Johannes Berg8fdc6212009-03-14 09:34:01 +0100709 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
710 if (!nl_cmds)
711 goto nla_put_failure;
712
713 i = 0;
714#define CMD(op, n) \
715 do { \
716 if (dev->ops->op) { \
717 i++; \
718 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
719 } \
720 } while (0)
721
722 CMD(add_virtual_intf, NEW_INTERFACE);
723 CMD(change_virtual_intf, SET_INTERFACE);
724 CMD(add_key, NEW_KEY);
725 CMD(add_beacon, NEW_BEACON);
726 CMD(add_station, NEW_STATION);
727 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800728 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100729 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200730 CMD(auth, AUTHENTICATE);
731 CMD(assoc, ASSOCIATE);
732 CMD(deauth, DEAUTHENTICATE);
733 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200734 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100735 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100736 CMD(set_pmksa, SET_PMKSA);
737 CMD(del_pmksa, DEL_PMKSA);
738 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100739 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200740 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200741 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100742 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100743 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200744 i++;
745 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
746 }
Johannes Bergf444de02010-05-05 15:25:02 +0200747 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400748 CMD(set_wds_peer, SET_WDS_PEER);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100749
750#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200751
Johannes Berg6829c8782009-07-02 09:13:27 +0200752 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200753 i++;
754 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
755 }
756
Johannes Berg6829c8782009-07-02 09:13:27 +0200757 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200758 i++;
759 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
760 }
761
Johannes Berg8fdc6212009-03-14 09:34:01 +0100762 nla_nest_end(msg, nl_cmds);
763
Johannes Berga2939112010-12-14 17:54:28 +0100764 if (dev->ops->remain_on_channel)
765 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
766 dev->wiphy.max_remain_on_channel_duration);
767
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100768 /* for now at least assume all drivers have it */
769 if (dev->ops->mgmt_tx)
770 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
771
Johannes Berg2e161f72010-08-12 15:38:38 +0200772 if (mgmt_stypes) {
773 u16 stypes;
774 struct nlattr *nl_ftypes, *nl_ifs;
775 enum nl80211_iftype ift;
776
777 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
778 if (!nl_ifs)
779 goto nla_put_failure;
780
781 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
782 nl_ftypes = nla_nest_start(msg, ift);
783 if (!nl_ftypes)
784 goto nla_put_failure;
785 i = 0;
786 stypes = mgmt_stypes[ift].tx;
787 while (stypes) {
788 if (stypes & 1)
789 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
790 (i << 4) | IEEE80211_FTYPE_MGMT);
791 stypes >>= 1;
792 i++;
793 }
794 nla_nest_end(msg, nl_ftypes);
795 }
796
Johannes Berg74b70a42010-08-24 12:15:53 +0200797 nla_nest_end(msg, nl_ifs);
798
Johannes Berg2e161f72010-08-12 15:38:38 +0200799 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
800 if (!nl_ifs)
801 goto nla_put_failure;
802
803 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
804 nl_ftypes = nla_nest_start(msg, ift);
805 if (!nl_ftypes)
806 goto nla_put_failure;
807 i = 0;
808 stypes = mgmt_stypes[ift].rx;
809 while (stypes) {
810 if (stypes & 1)
811 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
812 (i << 4) | IEEE80211_FTYPE_MGMT);
813 stypes >>= 1;
814 i++;
815 }
816 nla_nest_end(msg, nl_ftypes);
817 }
818 nla_nest_end(msg, nl_ifs);
819 }
820
Johannes Berg55682962007-09-20 13:09:35 -0400821 return genlmsg_end(msg, hdr);
822
823 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700824 genlmsg_cancel(msg, hdr);
825 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400826}
827
828static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
829{
830 int idx = 0;
831 int start = cb->args[0];
832 struct cfg80211_registered_device *dev;
833
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500834 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200835 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200836 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
837 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200838 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400839 continue;
840 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
841 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200842 dev) < 0) {
843 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400844 break;
Julius Volzb4637272008-07-08 14:02:19 +0200845 }
Johannes Berg55682962007-09-20 13:09:35 -0400846 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500847 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400848
849 cb->args[0] = idx;
850
851 return skb->len;
852}
853
854static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
855{
856 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200857 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400858
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700859 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400860 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200861 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400862
Johannes Berg4c476992010-10-04 21:36:35 +0200863 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
864 nlmsg_free(msg);
865 return -ENOBUFS;
866 }
Johannes Berg55682962007-09-20 13:09:35 -0400867
Johannes Berg134e6372009-07-10 09:51:34 +0000868 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400869}
870
Jouni Malinen31888482008-10-30 16:59:24 +0200871static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
872 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
873 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
874 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
875 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
876 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
877};
878
879static int parse_txq_params(struct nlattr *tb[],
880 struct ieee80211_txq_params *txq_params)
881{
882 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
883 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
884 !tb[NL80211_TXQ_ATTR_AIFS])
885 return -EINVAL;
886
887 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
888 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
889 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
890 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
891 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
892
893 return 0;
894}
895
Johannes Bergf444de02010-05-05 15:25:02 +0200896static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
897{
898 /*
899 * You can only set the channel explicitly for AP, mesh
900 * and WDS type interfaces; all others have their channel
901 * managed via their respective "establish a connection"
902 * command (connect, join, ...)
903 *
904 * Monitors are special as they are normally slaved to
905 * whatever else is going on, so they behave as though
906 * you tried setting the wiphy channel itself.
907 */
908 return !wdev ||
909 wdev->iftype == NL80211_IFTYPE_AP ||
910 wdev->iftype == NL80211_IFTYPE_WDS ||
911 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200912 wdev->iftype == NL80211_IFTYPE_MONITOR ||
913 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200914}
915
916static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
917 struct wireless_dev *wdev,
918 struct genl_info *info)
919{
920 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
921 u32 freq;
922 int result;
923
924 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
925 return -EINVAL;
926
927 if (!nl80211_can_set_dev_channel(wdev))
928 return -EOPNOTSUPP;
929
930 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
931 channel_type = nla_get_u32(info->attrs[
932 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
933 if (channel_type != NL80211_CHAN_NO_HT &&
934 channel_type != NL80211_CHAN_HT20 &&
935 channel_type != NL80211_CHAN_HT40PLUS &&
936 channel_type != NL80211_CHAN_HT40MINUS)
937 return -EINVAL;
938 }
939
940 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
941
942 mutex_lock(&rdev->devlist_mtx);
943 if (wdev) {
944 wdev_lock(wdev);
945 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
946 wdev_unlock(wdev);
947 } else {
948 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
949 }
950 mutex_unlock(&rdev->devlist_mtx);
951
952 return result;
953}
954
955static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
956{
Johannes Berg4c476992010-10-04 21:36:35 +0200957 struct cfg80211_registered_device *rdev = info->user_ptr[0];
958 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +0200959
Johannes Berg4c476992010-10-04 21:36:35 +0200960 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +0200961}
962
Bill Jordane8347eb2010-10-01 13:54:28 -0400963static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
964{
Johannes Berg43b19952010-10-07 13:10:30 +0200965 struct cfg80211_registered_device *rdev = info->user_ptr[0];
966 struct net_device *dev = info->user_ptr[1];
967 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +0200968 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -0400969
970 if (!info->attrs[NL80211_ATTR_MAC])
971 return -EINVAL;
972
Johannes Berg43b19952010-10-07 13:10:30 +0200973 if (netif_running(dev))
974 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -0400975
Johannes Berg43b19952010-10-07 13:10:30 +0200976 if (!rdev->ops->set_wds_peer)
977 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400978
Johannes Berg43b19952010-10-07 13:10:30 +0200979 if (wdev->iftype != NL80211_IFTYPE_WDS)
980 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400981
982 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +0200983 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -0400984}
985
986
Johannes Berg55682962007-09-20 13:09:35 -0400987static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
988{
989 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200990 struct net_device *netdev = NULL;
991 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -0400992 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200993 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200994 u32 changed;
995 u8 retry_short = 0, retry_long = 0;
996 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100997 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400998
Johannes Bergf444de02010-05-05 15:25:02 +0200999 /*
1000 * Try to find the wiphy and netdev. Normally this
1001 * function shouldn't need the netdev, but this is
1002 * done for backward compatibility -- previously
1003 * setting the channel was done per wiphy, but now
1004 * it is per netdev. Previous userland like hostapd
1005 * also passed a netdev to set_wiphy, so that it is
1006 * possible to let that go to the right netdev!
1007 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001008 mutex_lock(&cfg80211_mutex);
1009
Johannes Bergf444de02010-05-05 15:25:02 +02001010 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1011 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1012
1013 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1014 if (netdev && netdev->ieee80211_ptr) {
1015 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1016 mutex_lock(&rdev->mtx);
1017 } else
1018 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001019 }
1020
Johannes Bergf444de02010-05-05 15:25:02 +02001021 if (!netdev) {
1022 rdev = __cfg80211_rdev_from_info(info);
1023 if (IS_ERR(rdev)) {
1024 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001025 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001026 }
1027 wdev = NULL;
1028 netdev = NULL;
1029 result = 0;
1030
1031 mutex_lock(&rdev->mtx);
1032 } else if (netif_running(netdev) &&
1033 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1034 wdev = netdev->ieee80211_ptr;
1035 else
1036 wdev = NULL;
1037
1038 /*
1039 * end workaround code, by now the rdev is available
1040 * and locked, and wdev may or may not be NULL.
1041 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001042
1043 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001044 result = cfg80211_dev_rename(
1045 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001046
1047 mutex_unlock(&cfg80211_mutex);
1048
1049 if (result)
1050 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001051
Jouni Malinen31888482008-10-30 16:59:24 +02001052 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1053 struct ieee80211_txq_params txq_params;
1054 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1055
1056 if (!rdev->ops->set_txq_params) {
1057 result = -EOPNOTSUPP;
1058 goto bad_res;
1059 }
1060
1061 nla_for_each_nested(nl_txq_params,
1062 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1063 rem_txq_params) {
1064 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1065 nla_data(nl_txq_params),
1066 nla_len(nl_txq_params),
1067 txq_params_policy);
1068 result = parse_txq_params(tb, &txq_params);
1069 if (result)
1070 goto bad_res;
1071
1072 result = rdev->ops->set_txq_params(&rdev->wiphy,
1073 &txq_params);
1074 if (result)
1075 goto bad_res;
1076 }
1077 }
1078
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001079 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001080 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001081 if (result)
1082 goto bad_res;
1083 }
1084
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001085 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1086 enum nl80211_tx_power_setting type;
1087 int idx, mbm = 0;
1088
1089 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001090 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001091 goto bad_res;
1092 }
1093
1094 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1095 type = nla_get_u32(info->attrs[idx]);
1096
1097 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1098 (type != NL80211_TX_POWER_AUTOMATIC)) {
1099 result = -EINVAL;
1100 goto bad_res;
1101 }
1102
1103 if (type != NL80211_TX_POWER_AUTOMATIC) {
1104 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1105 mbm = nla_get_u32(info->attrs[idx]);
1106 }
1107
1108 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1109 if (result)
1110 goto bad_res;
1111 }
1112
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001113 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1114 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1115 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001116 if ((!rdev->wiphy.available_antennas_tx &&
1117 !rdev->wiphy.available_antennas_rx) ||
1118 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001119 result = -EOPNOTSUPP;
1120 goto bad_res;
1121 }
1122
1123 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1124 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1125
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001126 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001127 * available antenna masks, except for the "all" mask */
1128 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1129 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001130 result = -EINVAL;
1131 goto bad_res;
1132 }
1133
Bruno Randolf7f531e02010-12-16 11:30:22 +09001134 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1135 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001136
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001137 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1138 if (result)
1139 goto bad_res;
1140 }
1141
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001142 changed = 0;
1143
1144 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1145 retry_short = nla_get_u8(
1146 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1147 if (retry_short == 0) {
1148 result = -EINVAL;
1149 goto bad_res;
1150 }
1151 changed |= WIPHY_PARAM_RETRY_SHORT;
1152 }
1153
1154 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1155 retry_long = nla_get_u8(
1156 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1157 if (retry_long == 0) {
1158 result = -EINVAL;
1159 goto bad_res;
1160 }
1161 changed |= WIPHY_PARAM_RETRY_LONG;
1162 }
1163
1164 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1165 frag_threshold = nla_get_u32(
1166 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1167 if (frag_threshold < 256) {
1168 result = -EINVAL;
1169 goto bad_res;
1170 }
1171 if (frag_threshold != (u32) -1) {
1172 /*
1173 * Fragments (apart from the last one) are required to
1174 * have even length. Make the fragmentation code
1175 * simpler by stripping LSB should someone try to use
1176 * odd threshold value.
1177 */
1178 frag_threshold &= ~0x1;
1179 }
1180 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1181 }
1182
1183 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1184 rts_threshold = nla_get_u32(
1185 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1186 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1187 }
1188
Lukáš Turek81077e82009-12-21 22:50:47 +01001189 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1190 coverage_class = nla_get_u8(
1191 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1192 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1193 }
1194
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001195 if (changed) {
1196 u8 old_retry_short, old_retry_long;
1197 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001198 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001199
1200 if (!rdev->ops->set_wiphy_params) {
1201 result = -EOPNOTSUPP;
1202 goto bad_res;
1203 }
1204
1205 old_retry_short = rdev->wiphy.retry_short;
1206 old_retry_long = rdev->wiphy.retry_long;
1207 old_frag_threshold = rdev->wiphy.frag_threshold;
1208 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001209 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001210
1211 if (changed & WIPHY_PARAM_RETRY_SHORT)
1212 rdev->wiphy.retry_short = retry_short;
1213 if (changed & WIPHY_PARAM_RETRY_LONG)
1214 rdev->wiphy.retry_long = retry_long;
1215 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1216 rdev->wiphy.frag_threshold = frag_threshold;
1217 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1218 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001219 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1220 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001221
1222 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1223 if (result) {
1224 rdev->wiphy.retry_short = old_retry_short;
1225 rdev->wiphy.retry_long = old_retry_long;
1226 rdev->wiphy.frag_threshold = old_frag_threshold;
1227 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001228 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001229 }
1230 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001231
Johannes Berg306d6112008-12-08 12:39:04 +01001232 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001233 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001234 if (netdev)
1235 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001236 return result;
1237}
1238
1239
1240static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001241 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001242 struct net_device *dev)
1243{
1244 void *hdr;
1245
1246 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1247 if (!hdr)
1248 return -1;
1249
1250 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001251 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001252 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001253 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001254
1255 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1256 rdev->devlist_generation ^
1257 (cfg80211_rdev_list_generation << 2));
1258
Johannes Berg55682962007-09-20 13:09:35 -04001259 return genlmsg_end(msg, hdr);
1260
1261 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001262 genlmsg_cancel(msg, hdr);
1263 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001264}
1265
1266static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1267{
1268 int wp_idx = 0;
1269 int if_idx = 0;
1270 int wp_start = cb->args[0];
1271 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001272 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001273 struct wireless_dev *wdev;
1274
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001275 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001276 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1277 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001278 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001279 if (wp_idx < wp_start) {
1280 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001281 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001282 }
Johannes Berg55682962007-09-20 13:09:35 -04001283 if_idx = 0;
1284
Johannes Bergf5ea9122009-08-07 16:17:38 +02001285 mutex_lock(&rdev->devlist_mtx);
1286 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001287 if (if_idx < if_start) {
1288 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001289 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001290 }
Johannes Berg55682962007-09-20 13:09:35 -04001291 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1292 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001293 rdev, wdev->netdev) < 0) {
1294 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001295 goto out;
1296 }
1297 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001298 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001299 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001300
1301 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001302 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001303 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001304 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001305
1306 cb->args[0] = wp_idx;
1307 cb->args[1] = if_idx;
1308
1309 return skb->len;
1310}
1311
1312static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1313{
1314 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001315 struct cfg80211_registered_device *dev = info->user_ptr[0];
1316 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001317
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001318 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001319 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001320 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001321
Johannes Bergd7264052009-04-19 16:23:20 +02001322 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001323 dev, netdev) < 0) {
1324 nlmsg_free(msg);
1325 return -ENOBUFS;
1326 }
Johannes Berg55682962007-09-20 13:09:35 -04001327
Johannes Berg134e6372009-07-10 09:51:34 +00001328 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001329}
1330
Michael Wu66f7ac52008-01-31 19:48:22 +01001331static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1332 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1333 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1334 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1335 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1336 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1337};
1338
1339static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1340{
1341 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1342 int flag;
1343
1344 *mntrflags = 0;
1345
1346 if (!nla)
1347 return -EINVAL;
1348
1349 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1350 nla, mntr_flags_policy))
1351 return -EINVAL;
1352
1353 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1354 if (flags[flag])
1355 *mntrflags |= (1<<flag);
1356
1357 return 0;
1358}
1359
Johannes Berg9bc383d2009-11-19 11:55:19 +01001360static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001361 struct net_device *netdev, u8 use_4addr,
1362 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001363{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001364 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001365 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001366 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001367 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001368 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001369
1370 switch (iftype) {
1371 case NL80211_IFTYPE_AP_VLAN:
1372 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1373 return 0;
1374 break;
1375 case NL80211_IFTYPE_STATION:
1376 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1377 return 0;
1378 break;
1379 default:
1380 break;
1381 }
1382
1383 return -EOPNOTSUPP;
1384}
1385
Johannes Berg55682962007-09-20 13:09:35 -04001386static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1387{
Johannes Berg4c476992010-10-04 21:36:35 +02001388 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001389 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001390 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001391 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001392 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001393 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001394 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001395
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001396 memset(&params, 0, sizeof(params));
1397
Johannes Berg04a773a2009-04-19 21:24:32 +02001398 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001399
Johannes Berg723b0382008-09-16 20:22:09 +02001400 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001401 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001402 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001403 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001404 if (ntype > NL80211_IFTYPE_MAX)
1405 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001406 }
1407
Johannes Berg92ffe052008-09-16 20:39:36 +02001408 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001409 struct wireless_dev *wdev = dev->ieee80211_ptr;
1410
Johannes Berg4c476992010-10-04 21:36:35 +02001411 if (ntype != NL80211_IFTYPE_MESH_POINT)
1412 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001413 if (netif_running(dev))
1414 return -EBUSY;
1415
1416 wdev_lock(wdev);
1417 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1418 IEEE80211_MAX_MESH_ID_LEN);
1419 wdev->mesh_id_up_len =
1420 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1421 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1422 wdev->mesh_id_up_len);
1423 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001424 }
1425
Felix Fietkau8b787642009-11-10 18:53:10 +01001426 if (info->attrs[NL80211_ATTR_4ADDR]) {
1427 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1428 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001429 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001430 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001431 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001432 } else {
1433 params.use_4addr = -1;
1434 }
1435
Johannes Berg92ffe052008-09-16 20:39:36 +02001436 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001437 if (ntype != NL80211_IFTYPE_MONITOR)
1438 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001439 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1440 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001441 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001442 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001443
1444 flags = &_flags;
1445 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001446 }
Johannes Berg3b858752009-03-12 09:55:09 +01001447
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001448 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001449 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001450 else
1451 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001452
Johannes Berg9bc383d2009-11-19 11:55:19 +01001453 if (!err && params.use_4addr != -1)
1454 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1455
Johannes Berg55682962007-09-20 13:09:35 -04001456 return err;
1457}
1458
1459static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1460{
Johannes Berg4c476992010-10-04 21:36:35 +02001461 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001462 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001463 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001464 int err;
1465 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001466 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001467
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001468 memset(&params, 0, sizeof(params));
1469
Johannes Berg55682962007-09-20 13:09:35 -04001470 if (!info->attrs[NL80211_ATTR_IFNAME])
1471 return -EINVAL;
1472
1473 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1474 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1475 if (type > NL80211_IFTYPE_MAX)
1476 return -EINVAL;
1477 }
1478
Johannes Berg79c97e92009-07-07 03:56:12 +02001479 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001480 !(rdev->wiphy.interface_modes & (1 << type)))
1481 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001482
Johannes Berg9bc383d2009-11-19 11:55:19 +01001483 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001484 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001485 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001486 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001487 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001488 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001489
Michael Wu66f7ac52008-01-31 19:48:22 +01001490 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1491 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1492 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001493 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001494 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001495 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001496 if (IS_ERR(dev))
1497 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001498
Johannes Berg29cbe682010-12-03 09:20:44 +01001499 if (type == NL80211_IFTYPE_MESH_POINT &&
1500 info->attrs[NL80211_ATTR_MESH_ID]) {
1501 struct wireless_dev *wdev = dev->ieee80211_ptr;
1502
1503 wdev_lock(wdev);
1504 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1505 IEEE80211_MAX_MESH_ID_LEN);
1506 wdev->mesh_id_up_len =
1507 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1508 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1509 wdev->mesh_id_up_len);
1510 wdev_unlock(wdev);
1511 }
1512
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001513 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001514}
1515
1516static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1517{
Johannes Berg4c476992010-10-04 21:36:35 +02001518 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1519 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001520
Johannes Berg4c476992010-10-04 21:36:35 +02001521 if (!rdev->ops->del_virtual_intf)
1522 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001523
Johannes Berg4c476992010-10-04 21:36:35 +02001524 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001525}
1526
Johannes Berg41ade002007-12-19 02:03:29 +01001527struct get_key_cookie {
1528 struct sk_buff *msg;
1529 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001530 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001531};
1532
1533static void get_key_callback(void *c, struct key_params *params)
1534{
Johannes Bergb9454e82009-07-08 13:29:08 +02001535 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001536 struct get_key_cookie *cookie = c;
1537
1538 if (params->key)
1539 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1540 params->key_len, params->key);
1541
1542 if (params->seq)
1543 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1544 params->seq_len, params->seq);
1545
1546 if (params->cipher)
1547 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1548 params->cipher);
1549
Johannes Bergb9454e82009-07-08 13:29:08 +02001550 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1551 if (!key)
1552 goto nla_put_failure;
1553
1554 if (params->key)
1555 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1556 params->key_len, params->key);
1557
1558 if (params->seq)
1559 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1560 params->seq_len, params->seq);
1561
1562 if (params->cipher)
1563 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1564 params->cipher);
1565
1566 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1567
1568 nla_nest_end(cookie->msg, key);
1569
Johannes Berg41ade002007-12-19 02:03:29 +01001570 return;
1571 nla_put_failure:
1572 cookie->error = 1;
1573}
1574
1575static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1576{
Johannes Berg4c476992010-10-04 21:36:35 +02001577 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001578 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001579 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001580 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001581 const u8 *mac_addr = NULL;
1582 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001583 struct get_key_cookie cookie = {
1584 .error = 0,
1585 };
1586 void *hdr;
1587 struct sk_buff *msg;
1588
1589 if (info->attrs[NL80211_ATTR_KEY_IDX])
1590 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1591
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001592 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001593 return -EINVAL;
1594
1595 if (info->attrs[NL80211_ATTR_MAC])
1596 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1597
Johannes Berge31b8212010-10-05 19:39:30 +02001598 pairwise = !!mac_addr;
1599 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1600 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1601 if (kt >= NUM_NL80211_KEYTYPES)
1602 return -EINVAL;
1603 if (kt != NL80211_KEYTYPE_GROUP &&
1604 kt != NL80211_KEYTYPE_PAIRWISE)
1605 return -EINVAL;
1606 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1607 }
1608
Johannes Berg4c476992010-10-04 21:36:35 +02001609 if (!rdev->ops->get_key)
1610 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001611
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001612 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001613 if (!msg)
1614 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001615
1616 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1617 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001618 if (IS_ERR(hdr))
1619 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001620
1621 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001622 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001623
1624 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1625 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1626 if (mac_addr)
1627 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1628
Johannes Berge31b8212010-10-05 19:39:30 +02001629 if (pairwise && mac_addr &&
1630 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1631 return -ENOENT;
1632
1633 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1634 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001635
1636 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001637 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001638
1639 if (cookie.error)
1640 goto nla_put_failure;
1641
1642 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001643 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001644
1645 nla_put_failure:
1646 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001647 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001648 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001649 return err;
1650}
1651
1652static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1653{
Johannes Berg4c476992010-10-04 21:36:35 +02001654 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001655 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001656 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001657 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001658
Johannes Bergb9454e82009-07-08 13:29:08 +02001659 err = nl80211_parse_key(info, &key);
1660 if (err)
1661 return err;
1662
1663 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001664 return -EINVAL;
1665
Johannes Bergb9454e82009-07-08 13:29:08 +02001666 /* only support setting default key */
1667 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001668 return -EINVAL;
1669
Johannes Bergfffd0932009-07-08 14:22:54 +02001670 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001671
1672 if (key.def) {
1673 if (!rdev->ops->set_default_key) {
1674 err = -EOPNOTSUPP;
1675 goto out;
1676 }
1677
1678 err = nl80211_key_allowed(dev->ieee80211_ptr);
1679 if (err)
1680 goto out;
1681
1682 if (!(rdev->wiphy.flags &
1683 WIPHY_FLAG_SUPPORTS_SEPARATE_DEFAULT_KEYS)) {
1684 if (!key.def_uni || !key.def_multi) {
1685 err = -EOPNOTSUPP;
1686 goto out;
1687 }
1688 }
1689
1690 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1691 key.def_uni, key.def_multi);
1692
1693 if (err)
1694 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001695
Johannes Berg3d23e342009-09-29 23:27:28 +02001696#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001697 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001698#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001699 } else {
1700 if (key.def_uni || !key.def_multi) {
1701 err = -EINVAL;
1702 goto out;
1703 }
1704
1705 if (!rdev->ops->set_default_mgmt_key) {
1706 err = -EOPNOTSUPP;
1707 goto out;
1708 }
1709
1710 err = nl80211_key_allowed(dev->ieee80211_ptr);
1711 if (err)
1712 goto out;
1713
1714 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1715 dev, key.idx);
1716 if (err)
1717 goto out;
1718
1719#ifdef CONFIG_CFG80211_WEXT
1720 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1721#endif
1722 }
1723
1724 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001725 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001726
Johannes Berg41ade002007-12-19 02:03:29 +01001727 return err;
1728}
1729
1730static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1731{
Johannes Berg4c476992010-10-04 21:36:35 +02001732 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001733 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001734 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001735 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001736 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001737
Johannes Bergb9454e82009-07-08 13:29:08 +02001738 err = nl80211_parse_key(info, &key);
1739 if (err)
1740 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001741
Johannes Bergb9454e82009-07-08 13:29:08 +02001742 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001743 return -EINVAL;
1744
Johannes Berg41ade002007-12-19 02:03:29 +01001745 if (info->attrs[NL80211_ATTR_MAC])
1746 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1747
Johannes Berge31b8212010-10-05 19:39:30 +02001748 if (key.type == -1) {
1749 if (mac_addr)
1750 key.type = NL80211_KEYTYPE_PAIRWISE;
1751 else
1752 key.type = NL80211_KEYTYPE_GROUP;
1753 }
1754
1755 /* for now */
1756 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1757 key.type != NL80211_KEYTYPE_GROUP)
1758 return -EINVAL;
1759
Johannes Berg4c476992010-10-04 21:36:35 +02001760 if (!rdev->ops->add_key)
1761 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001762
Johannes Berge31b8212010-10-05 19:39:30 +02001763 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1764 key.type == NL80211_KEYTYPE_PAIRWISE,
1765 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001766 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001767
1768 wdev_lock(dev->ieee80211_ptr);
1769 err = nl80211_key_allowed(dev->ieee80211_ptr);
1770 if (!err)
1771 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001772 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001773 mac_addr, &key.p);
1774 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001775
Johannes Berg41ade002007-12-19 02:03:29 +01001776 return err;
1777}
1778
1779static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1780{
Johannes Berg4c476992010-10-04 21:36:35 +02001781 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001782 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001783 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001784 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001785 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001786
Johannes Bergb9454e82009-07-08 13:29:08 +02001787 err = nl80211_parse_key(info, &key);
1788 if (err)
1789 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001790
1791 if (info->attrs[NL80211_ATTR_MAC])
1792 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1793
Johannes Berge31b8212010-10-05 19:39:30 +02001794 if (key.type == -1) {
1795 if (mac_addr)
1796 key.type = NL80211_KEYTYPE_PAIRWISE;
1797 else
1798 key.type = NL80211_KEYTYPE_GROUP;
1799 }
1800
1801 /* for now */
1802 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1803 key.type != NL80211_KEYTYPE_GROUP)
1804 return -EINVAL;
1805
Johannes Berg4c476992010-10-04 21:36:35 +02001806 if (!rdev->ops->del_key)
1807 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001808
Johannes Bergfffd0932009-07-08 14:22:54 +02001809 wdev_lock(dev->ieee80211_ptr);
1810 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001811
1812 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1813 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1814 err = -ENOENT;
1815
Johannes Bergfffd0932009-07-08 14:22:54 +02001816 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001817 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1818 key.type == NL80211_KEYTYPE_PAIRWISE,
1819 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001820
Johannes Berg3d23e342009-09-29 23:27:28 +02001821#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001822 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001823 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001824 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001825 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001826 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1827 }
1828#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001829 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001830
Johannes Berg41ade002007-12-19 02:03:29 +01001831 return err;
1832}
1833
Johannes Berged1b6cc2007-12-19 02:03:32 +01001834static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1835{
1836 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1837 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001838 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1839 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001840 struct beacon_parameters params;
1841 int haveinfo = 0;
1842
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001843 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1844 return -EINVAL;
1845
Johannes Berg074ac8d2010-09-16 14:58:22 +02001846 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001847 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1848 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001849
Johannes Berged1b6cc2007-12-19 02:03:32 +01001850 switch (info->genlhdr->cmd) {
1851 case NL80211_CMD_NEW_BEACON:
1852 /* these are required for NEW_BEACON */
1853 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1854 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001855 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1856 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001857
Johannes Berg79c97e92009-07-07 03:56:12 +02001858 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001859 break;
1860 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001861 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001862 break;
1863 default:
1864 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001865 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001866 }
1867
Johannes Berg4c476992010-10-04 21:36:35 +02001868 if (!call)
1869 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001870
1871 memset(&params, 0, sizeof(params));
1872
1873 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1874 params.interval =
1875 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1876 haveinfo = 1;
1877 }
1878
1879 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1880 params.dtim_period =
1881 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1882 haveinfo = 1;
1883 }
1884
1885 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1886 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1887 params.head_len =
1888 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1889 haveinfo = 1;
1890 }
1891
1892 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1893 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1894 params.tail_len =
1895 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1896 haveinfo = 1;
1897 }
1898
Johannes Berg4c476992010-10-04 21:36:35 +02001899 if (!haveinfo)
1900 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001901
Johannes Berg4c476992010-10-04 21:36:35 +02001902 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001903}
1904
1905static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1906{
Johannes Berg4c476992010-10-04 21:36:35 +02001907 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1908 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001909
Johannes Berg4c476992010-10-04 21:36:35 +02001910 if (!rdev->ops->del_beacon)
1911 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001912
Johannes Berg074ac8d2010-09-16 14:58:22 +02001913 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001914 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1915 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001916
Johannes Berg4c476992010-10-04 21:36:35 +02001917 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001918}
1919
Johannes Berg5727ef12007-12-19 02:03:34 +01001920static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1921 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1922 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1923 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001924 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001925};
1926
Johannes Bergeccb8e82009-05-11 21:57:56 +03001927static int parse_station_flags(struct genl_info *info,
1928 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001929{
1930 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001931 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001932 int flag;
1933
Johannes Bergeccb8e82009-05-11 21:57:56 +03001934 /*
1935 * Try parsing the new attribute first so userspace
1936 * can specify both for older kernels.
1937 */
1938 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1939 if (nla) {
1940 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001941
Johannes Bergeccb8e82009-05-11 21:57:56 +03001942 sta_flags = nla_data(nla);
1943 params->sta_flags_mask = sta_flags->mask;
1944 params->sta_flags_set = sta_flags->set;
1945 if ((params->sta_flags_mask |
1946 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1947 return -EINVAL;
1948 return 0;
1949 }
1950
1951 /* if present, parse the old attribute */
1952
1953 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001954 if (!nla)
1955 return 0;
1956
1957 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1958 nla, sta_flags_policy))
1959 return -EINVAL;
1960
Johannes Bergeccb8e82009-05-11 21:57:56 +03001961 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1962 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001963
1964 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1965 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001966 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001967
1968 return 0;
1969}
1970
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01001971static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
1972 int attr)
1973{
1974 struct nlattr *rate;
1975 u16 bitrate;
1976
1977 rate = nla_nest_start(msg, attr);
1978 if (!rate)
1979 goto nla_put_failure;
1980
1981 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1982 bitrate = cfg80211_calculate_bitrate(info);
1983 if (bitrate > 0)
1984 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1985
1986 if (info->flags & RATE_INFO_FLAGS_MCS)
1987 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
1988 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1989 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1990 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
1991 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1992
1993 nla_nest_end(msg, rate);
1994 return true;
1995
1996nla_put_failure:
1997 return false;
1998}
1999
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002000static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2001 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002002 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002003{
2004 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002005 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002006
2007 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2008 if (!hdr)
2009 return -1;
2010
2011 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2012 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2013
Johannes Bergf5ea9122009-08-07 16:17:38 +02002014 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2015
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002016 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2017 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002018 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002019 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2020 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2021 sinfo->inactive_time);
2022 if (sinfo->filled & STATION_INFO_RX_BYTES)
2023 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2024 sinfo->rx_bytes);
2025 if (sinfo->filled & STATION_INFO_TX_BYTES)
2026 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2027 sinfo->tx_bytes);
2028 if (sinfo->filled & STATION_INFO_LLID)
2029 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2030 sinfo->llid);
2031 if (sinfo->filled & STATION_INFO_PLID)
2032 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2033 sinfo->plid);
2034 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2035 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2036 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002037 if (sinfo->filled & STATION_INFO_SIGNAL)
2038 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2039 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002040 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2041 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2042 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002043 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002044 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2045 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002046 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002047 }
2048 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2049 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2050 NL80211_STA_INFO_RX_BITRATE))
2051 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002052 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002053 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2054 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2055 sinfo->rx_packets);
2056 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2057 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2058 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002059 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2060 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2061 sinfo->tx_retries);
2062 if (sinfo->filled & STATION_INFO_TX_FAILED)
2063 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2064 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002065 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2066 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2067 if (!bss_param)
2068 goto nla_put_failure;
2069
2070 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2071 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2072 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2073 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2074 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2075 NLA_PUT_FLAG(msg,
2076 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2077 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2078 sinfo->bss_param.dtim_period);
2079 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2080 sinfo->bss_param.beacon_interval);
2081
2082 nla_nest_end(msg, bss_param);
2083 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002084 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002085
2086 return genlmsg_end(msg, hdr);
2087
2088 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002089 genlmsg_cancel(msg, hdr);
2090 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002091}
2092
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002093static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002094 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002095{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002096 struct station_info sinfo;
2097 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002098 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002099 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002100 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002101 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002102
Johannes Berg67748892010-10-04 21:14:06 +02002103 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2104 if (err)
2105 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002106
2107 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002108 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002109 goto out_err;
2110 }
2111
Johannes Bergbba95fe2008-07-29 13:22:51 +02002112 while (1) {
2113 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2114 mac_addr, &sinfo);
2115 if (err == -ENOENT)
2116 break;
2117 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002118 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002119
2120 if (nl80211_send_station(skb,
2121 NETLINK_CB(cb->skb).pid,
2122 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2123 netdev, mac_addr,
2124 &sinfo) < 0)
2125 goto out;
2126
2127 sta_idx++;
2128 }
2129
2130
2131 out:
2132 cb->args[1] = sta_idx;
2133 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002134 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002135 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002136
2137 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002138}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002139
Johannes Berg5727ef12007-12-19 02:03:34 +01002140static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2141{
Johannes Berg4c476992010-10-04 21:36:35 +02002142 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2143 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002144 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002145 struct sk_buff *msg;
2146 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002147 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002148
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002149 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002150
2151 if (!info->attrs[NL80211_ATTR_MAC])
2152 return -EINVAL;
2153
2154 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2155
Johannes Berg4c476992010-10-04 21:36:35 +02002156 if (!rdev->ops->get_station)
2157 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002158
Johannes Berg79c97e92009-07-07 03:56:12 +02002159 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002160 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002161 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002162
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002163 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002164 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002165 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002166
2167 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002168 dev, mac_addr, &sinfo) < 0) {
2169 nlmsg_free(msg);
2170 return -ENOBUFS;
2171 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002172
Johannes Berg4c476992010-10-04 21:36:35 +02002173 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002174}
2175
2176/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002177 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002178 */
Johannes Berg463d0182009-07-14 00:33:35 +02002179static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002180 struct cfg80211_registered_device *rdev,
2181 struct net_device **vlan)
2182{
Johannes Berg463d0182009-07-14 00:33:35 +02002183 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002184 *vlan = NULL;
2185
2186 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002187 *vlan = dev_get_by_index(genl_info_net(info),
2188 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002189 if (!*vlan)
2190 return -ENODEV;
2191 if (!(*vlan)->ieee80211_ptr)
2192 return -EINVAL;
2193 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2194 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002195 if (!netif_running(*vlan))
2196 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002197 }
2198 return 0;
2199}
2200
2201static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2202{
Johannes Berg4c476992010-10-04 21:36:35 +02002203 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002204 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002205 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002206 struct station_parameters params;
2207 u8 *mac_addr = NULL;
2208
2209 memset(&params, 0, sizeof(params));
2210
2211 params.listen_interval = -1;
2212
2213 if (info->attrs[NL80211_ATTR_STA_AID])
2214 return -EINVAL;
2215
2216 if (!info->attrs[NL80211_ATTR_MAC])
2217 return -EINVAL;
2218
2219 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2220
2221 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2222 params.supported_rates =
2223 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2224 params.supported_rates_len =
2225 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2226 }
2227
2228 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2229 params.listen_interval =
2230 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2231
Jouni Malinen36aedc92008-08-25 11:58:58 +03002232 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2233 params.ht_capa =
2234 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2235
Johannes Bergeccb8e82009-05-11 21:57:56 +03002236 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002237 return -EINVAL;
2238
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002239 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2240 params.plink_action =
2241 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2242
Johannes Berg463d0182009-07-14 00:33:35 +02002243 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002244 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002245 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002246
2247 /* validate settings */
2248 err = 0;
2249
2250 switch (dev->ieee80211_ptr->iftype) {
2251 case NL80211_IFTYPE_AP:
2252 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002253 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002254 /* disallow mesh-specific things */
2255 if (params.plink_action)
2256 err = -EINVAL;
2257 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002258 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002259 case NL80211_IFTYPE_STATION:
2260 /* disallow everything but AUTHORIZED flag */
2261 if (params.plink_action)
2262 err = -EINVAL;
2263 if (params.vlan)
2264 err = -EINVAL;
2265 if (params.supported_rates)
2266 err = -EINVAL;
2267 if (params.ht_capa)
2268 err = -EINVAL;
2269 if (params.listen_interval >= 0)
2270 err = -EINVAL;
2271 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2272 err = -EINVAL;
2273 break;
2274 case NL80211_IFTYPE_MESH_POINT:
2275 /* disallow things mesh doesn't support */
2276 if (params.vlan)
2277 err = -EINVAL;
2278 if (params.ht_capa)
2279 err = -EINVAL;
2280 if (params.listen_interval >= 0)
2281 err = -EINVAL;
2282 if (params.supported_rates)
2283 err = -EINVAL;
2284 if (params.sta_flags_mask)
2285 err = -EINVAL;
2286 break;
2287 default:
2288 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002289 }
2290
Johannes Berg5727ef12007-12-19 02:03:34 +01002291 if (err)
2292 goto out;
2293
Johannes Berg79c97e92009-07-07 03:56:12 +02002294 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002295 err = -EOPNOTSUPP;
2296 goto out;
2297 }
2298
Johannes Berg79c97e92009-07-07 03:56:12 +02002299 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002300
2301 out:
2302 if (params.vlan)
2303 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002304
Johannes Berg5727ef12007-12-19 02:03:34 +01002305 return err;
2306}
2307
2308static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2309{
Johannes Berg4c476992010-10-04 21:36:35 +02002310 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002311 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002312 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002313 struct station_parameters params;
2314 u8 *mac_addr = NULL;
2315
2316 memset(&params, 0, sizeof(params));
2317
2318 if (!info->attrs[NL80211_ATTR_MAC])
2319 return -EINVAL;
2320
Johannes Berg5727ef12007-12-19 02:03:34 +01002321 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2322 return -EINVAL;
2323
2324 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2325 return -EINVAL;
2326
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002327 if (!info->attrs[NL80211_ATTR_STA_AID])
2328 return -EINVAL;
2329
Johannes Berg5727ef12007-12-19 02:03:34 +01002330 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2331 params.supported_rates =
2332 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2333 params.supported_rates_len =
2334 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2335 params.listen_interval =
2336 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002337
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002338 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2339 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2340 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002341
Jouni Malinen36aedc92008-08-25 11:58:58 +03002342 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2343 params.ht_capa =
2344 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002345
Johannes Bergeccb8e82009-05-11 21:57:56 +03002346 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002347 return -EINVAL;
2348
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002349 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002350 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02002351 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2352 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002353
Johannes Berg463d0182009-07-14 00:33:35 +02002354 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002355 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002356 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002357
2358 /* validate settings */
2359 err = 0;
2360
Johannes Berg79c97e92009-07-07 03:56:12 +02002361 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002362 err = -EOPNOTSUPP;
2363 goto out;
2364 }
2365
Johannes Berg79c97e92009-07-07 03:56:12 +02002366 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002367
2368 out:
2369 if (params.vlan)
2370 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002371 return err;
2372}
2373
2374static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2375{
Johannes Berg4c476992010-10-04 21:36:35 +02002376 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2377 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002378 u8 *mac_addr = NULL;
2379
2380 if (info->attrs[NL80211_ATTR_MAC])
2381 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2382
Johannes Berge80cf852009-05-11 14:43:13 +02002383 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002384 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002385 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002386 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2387 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002388
Johannes Berg4c476992010-10-04 21:36:35 +02002389 if (!rdev->ops->del_station)
2390 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002391
Johannes Berg4c476992010-10-04 21:36:35 +02002392 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002393}
2394
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002395static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2396 int flags, struct net_device *dev,
2397 u8 *dst, u8 *next_hop,
2398 struct mpath_info *pinfo)
2399{
2400 void *hdr;
2401 struct nlattr *pinfoattr;
2402
2403 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2404 if (!hdr)
2405 return -1;
2406
2407 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2408 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2409 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2410
Johannes Bergf5ea9122009-08-07 16:17:38 +02002411 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2412
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002413 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2414 if (!pinfoattr)
2415 goto nla_put_failure;
2416 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2417 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2418 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002419 if (pinfo->filled & MPATH_INFO_SN)
2420 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2421 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002422 if (pinfo->filled & MPATH_INFO_METRIC)
2423 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2424 pinfo->metric);
2425 if (pinfo->filled & MPATH_INFO_EXPTIME)
2426 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2427 pinfo->exptime);
2428 if (pinfo->filled & MPATH_INFO_FLAGS)
2429 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2430 pinfo->flags);
2431 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2432 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2433 pinfo->discovery_timeout);
2434 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2435 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2436 pinfo->discovery_retries);
2437
2438 nla_nest_end(msg, pinfoattr);
2439
2440 return genlmsg_end(msg, hdr);
2441
2442 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002443 genlmsg_cancel(msg, hdr);
2444 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002445}
2446
2447static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002448 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002449{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002450 struct mpath_info pinfo;
2451 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002452 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002453 u8 dst[ETH_ALEN];
2454 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002455 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002456 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002457
Johannes Berg67748892010-10-04 21:14:06 +02002458 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2459 if (err)
2460 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002461
2462 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002463 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002464 goto out_err;
2465 }
2466
Jouni Malineneec60b02009-03-20 21:21:19 +02002467 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2468 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002469 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002470 }
2471
Johannes Bergbba95fe2008-07-29 13:22:51 +02002472 while (1) {
2473 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2474 dst, next_hop, &pinfo);
2475 if (err == -ENOENT)
2476 break;
2477 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002478 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002479
2480 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2481 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2482 netdev, dst, next_hop,
2483 &pinfo) < 0)
2484 goto out;
2485
2486 path_idx++;
2487 }
2488
2489
2490 out:
2491 cb->args[1] = path_idx;
2492 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002493 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002494 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002495 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002496}
2497
2498static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2499{
Johannes Berg4c476992010-10-04 21:36:35 +02002500 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002501 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002502 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002503 struct mpath_info pinfo;
2504 struct sk_buff *msg;
2505 u8 *dst = NULL;
2506 u8 next_hop[ETH_ALEN];
2507
2508 memset(&pinfo, 0, sizeof(pinfo));
2509
2510 if (!info->attrs[NL80211_ATTR_MAC])
2511 return -EINVAL;
2512
2513 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2514
Johannes Berg4c476992010-10-04 21:36:35 +02002515 if (!rdev->ops->get_mpath)
2516 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002517
Johannes Berg4c476992010-10-04 21:36:35 +02002518 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2519 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002520
Johannes Berg79c97e92009-07-07 03:56:12 +02002521 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002522 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002523 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002524
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002525 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002526 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002527 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002528
2529 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002530 dev, dst, next_hop, &pinfo) < 0) {
2531 nlmsg_free(msg);
2532 return -ENOBUFS;
2533 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002534
Johannes Berg4c476992010-10-04 21:36:35 +02002535 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002536}
2537
2538static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2539{
Johannes Berg4c476992010-10-04 21:36:35 +02002540 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2541 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002542 u8 *dst = NULL;
2543 u8 *next_hop = NULL;
2544
2545 if (!info->attrs[NL80211_ATTR_MAC])
2546 return -EINVAL;
2547
2548 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2549 return -EINVAL;
2550
2551 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2552 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2553
Johannes Berg4c476992010-10-04 21:36:35 +02002554 if (!rdev->ops->change_mpath)
2555 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002556
Johannes Berg4c476992010-10-04 21:36:35 +02002557 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2558 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002559
Johannes Berg4c476992010-10-04 21:36:35 +02002560 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002561}
Johannes Berg4c476992010-10-04 21:36:35 +02002562
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002563static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2564{
Johannes Berg4c476992010-10-04 21:36:35 +02002565 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2566 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002567 u8 *dst = NULL;
2568 u8 *next_hop = NULL;
2569
2570 if (!info->attrs[NL80211_ATTR_MAC])
2571 return -EINVAL;
2572
2573 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2574 return -EINVAL;
2575
2576 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2577 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2578
Johannes Berg4c476992010-10-04 21:36:35 +02002579 if (!rdev->ops->add_mpath)
2580 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002581
Johannes Berg4c476992010-10-04 21:36:35 +02002582 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2583 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002584
Johannes Berg4c476992010-10-04 21:36:35 +02002585 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002586}
2587
2588static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2589{
Johannes Berg4c476992010-10-04 21:36:35 +02002590 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2591 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002592 u8 *dst = NULL;
2593
2594 if (info->attrs[NL80211_ATTR_MAC])
2595 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2596
Johannes Berg4c476992010-10-04 21:36:35 +02002597 if (!rdev->ops->del_mpath)
2598 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002599
Johannes Berg4c476992010-10-04 21:36:35 +02002600 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002601}
2602
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002603static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2604{
Johannes Berg4c476992010-10-04 21:36:35 +02002605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2606 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002607 struct bss_parameters params;
2608
2609 memset(&params, 0, sizeof(params));
2610 /* default to not changing parameters */
2611 params.use_cts_prot = -1;
2612 params.use_short_preamble = -1;
2613 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002614 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002615 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002616
2617 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2618 params.use_cts_prot =
2619 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2620 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2621 params.use_short_preamble =
2622 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2623 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2624 params.use_short_slot_time =
2625 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002626 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2627 params.basic_rates =
2628 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2629 params.basic_rates_len =
2630 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2631 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002632 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2633 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002634 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2635 params.ht_opmode =
2636 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002637
Johannes Berg4c476992010-10-04 21:36:35 +02002638 if (!rdev->ops->change_bss)
2639 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002640
Johannes Berg074ac8d2010-09-16 14:58:22 +02002641 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002642 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2643 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002644
Johannes Berg4c476992010-10-04 21:36:35 +02002645 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002646}
2647
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002648static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002649 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2650 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2651 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2652 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2653 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2654 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2655};
2656
2657static int parse_reg_rule(struct nlattr *tb[],
2658 struct ieee80211_reg_rule *reg_rule)
2659{
2660 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2661 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2662
2663 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2664 return -EINVAL;
2665 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2666 return -EINVAL;
2667 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2668 return -EINVAL;
2669 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2670 return -EINVAL;
2671 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2672 return -EINVAL;
2673
2674 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2675
2676 freq_range->start_freq_khz =
2677 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2678 freq_range->end_freq_khz =
2679 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2680 freq_range->max_bandwidth_khz =
2681 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2682
2683 power_rule->max_eirp =
2684 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2685
2686 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2687 power_rule->max_antenna_gain =
2688 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2689
2690 return 0;
2691}
2692
2693static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2694{
2695 int r;
2696 char *data = NULL;
2697
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002698 /*
2699 * You should only get this when cfg80211 hasn't yet initialized
2700 * completely when built-in to the kernel right between the time
2701 * window between nl80211_init() and regulatory_init(), if that is
2702 * even possible.
2703 */
2704 mutex_lock(&cfg80211_mutex);
2705 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002706 mutex_unlock(&cfg80211_mutex);
2707 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002708 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002709 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002710
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002711 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2712 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002713
2714 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2715
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002716 r = regulatory_hint_user(data);
2717
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002718 return r;
2719}
2720
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002721static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002722 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002723{
Johannes Berg4c476992010-10-04 21:36:35 +02002724 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02002725 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002726 struct wireless_dev *wdev = dev->ieee80211_ptr;
2727 struct mesh_config cur_params;
2728 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002729 void *hdr;
2730 struct nlattr *pinfoattr;
2731 struct sk_buff *msg;
2732
Johannes Berg29cbe682010-12-03 09:20:44 +01002733 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2734 return -EOPNOTSUPP;
2735
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002736 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02002737 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002738
Johannes Berg29cbe682010-12-03 09:20:44 +01002739 wdev_lock(wdev);
2740 /* If not connected, get default parameters */
2741 if (!wdev->mesh_id_len)
2742 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
2743 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002744 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01002745 &cur_params);
2746 wdev_unlock(wdev);
2747
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002748 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002749 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002750
2751 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002752 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002753 if (!msg)
2754 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002755 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002756 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002757 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01002758 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002759 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002760 if (!pinfoattr)
2761 goto nla_put_failure;
2762 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2763 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2764 cur_params.dot11MeshRetryTimeout);
2765 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2766 cur_params.dot11MeshConfirmTimeout);
2767 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2768 cur_params.dot11MeshHoldingTimeout);
2769 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2770 cur_params.dot11MeshMaxPeerLinks);
2771 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2772 cur_params.dot11MeshMaxRetries);
2773 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2774 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01002775 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
2776 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002777 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2778 cur_params.auto_open_plinks);
2779 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2780 cur_params.dot11MeshHWMPmaxPREQretries);
2781 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2782 cur_params.path_refresh_time);
2783 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2784 cur_params.min_discovery_timeout);
2785 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2786 cur_params.dot11MeshHWMPactivePathTimeout);
2787 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2788 cur_params.dot11MeshHWMPpreqMinInterval);
2789 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2790 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002791 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2792 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002793 nla_nest_end(msg, pinfoattr);
2794 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002795 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002796
Johannes Berg3b858752009-03-12 09:55:09 +01002797 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002798 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01002799 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04002800 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002801 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002802}
2803
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002804static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002805 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2806 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2807 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2808 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2809 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2810 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01002811 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002812 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2813
2814 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2815 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2816 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2817 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2818 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2819 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2820};
2821
Javier Cardonac80d5452010-12-16 17:37:49 -08002822static const struct nla_policy
2823 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
2824 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
2825 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
2826 [NL80211_MESH_SETUP_VENDOR_PATH_SEL_IE] = { .type = NLA_BINARY,
2827 .len = IEEE80211_MAX_DATA_LEN },
2828};
2829
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002830static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002831 struct mesh_config *cfg,
2832 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002833{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002834 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002835 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002836
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002837#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2838do {\
2839 if (table[attr_num]) {\
2840 cfg->param = nla_fn(table[attr_num]); \
2841 mask |= (1 << (attr_num - 1)); \
2842 } \
2843} while (0);\
2844
2845
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002846 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002847 return -EINVAL;
2848 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002849 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002850 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002851 return -EINVAL;
2852
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002853 /* This makes sure that there aren't more than 32 mesh config
2854 * parameters (otherwise our bitfield scheme would not work.) */
2855 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2856
2857 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002858 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2859 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2860 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2861 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2862 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2863 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2864 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2865 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2866 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2867 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2868 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2869 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01002870 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
2871 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002872 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2873 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2874 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2875 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2876 nla_get_u8);
2877 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2878 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2879 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2880 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2881 nla_get_u16);
2882 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2883 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2884 nla_get_u32);
2885 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2886 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2887 nla_get_u16);
2888 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2889 dot11MeshHWMPnetDiameterTraversalTime,
2890 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2891 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002892 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2893 dot11MeshHWMPRootMode, mask,
2894 NL80211_MESHCONF_HWMP_ROOTMODE,
2895 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002896 if (mask_out)
2897 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08002898
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002899 return 0;
2900
2901#undef FILL_IN_MESH_PARAM_IF_SET
2902}
2903
Javier Cardonac80d5452010-12-16 17:37:49 -08002904static int nl80211_parse_mesh_setup(struct genl_info *info,
2905 struct mesh_setup *setup)
2906{
2907 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
2908
2909 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
2910 return -EINVAL;
2911 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
2912 info->attrs[NL80211_ATTR_MESH_SETUP],
2913 nl80211_mesh_setup_params_policy))
2914 return -EINVAL;
2915
2916 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
2917 setup->path_sel_proto =
2918 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
2919 IEEE80211_PATH_PROTOCOL_VENDOR :
2920 IEEE80211_PATH_PROTOCOL_HWMP;
2921
2922 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
2923 setup->path_metric =
2924 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
2925 IEEE80211_PATH_METRIC_VENDOR :
2926 IEEE80211_PATH_METRIC_AIRTIME;
2927
2928 if (tb[NL80211_MESH_SETUP_VENDOR_PATH_SEL_IE]) {
2929 struct nlattr *ieattr =
2930 tb[NL80211_MESH_SETUP_VENDOR_PATH_SEL_IE];
2931 if (!is_valid_ie_attr(ieattr))
2932 return -EINVAL;
2933 setup->vendor_ie = nla_data(ieattr);
2934 setup->vendor_ie_len = nla_len(ieattr);
2935 }
2936
2937 return 0;
2938}
2939
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002940static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002941 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002942{
2943 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2944 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002945 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002946 struct mesh_config cfg;
2947 u32 mask;
2948 int err;
2949
Johannes Berg29cbe682010-12-03 09:20:44 +01002950 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2951 return -EOPNOTSUPP;
2952
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002953 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002954 return -EOPNOTSUPP;
2955
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002956 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002957 if (err)
2958 return err;
2959
Johannes Berg29cbe682010-12-03 09:20:44 +01002960 wdev_lock(wdev);
2961 if (!wdev->mesh_id_len)
2962 err = -ENOLINK;
2963
2964 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002965 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01002966 mask, &cfg);
2967
2968 wdev_unlock(wdev);
2969
2970 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002971}
2972
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002973static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2974{
2975 struct sk_buff *msg;
2976 void *hdr = NULL;
2977 struct nlattr *nl_reg_rules;
2978 unsigned int i;
2979 int err = -EINVAL;
2980
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002981 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002982
2983 if (!cfg80211_regdomain)
2984 goto out;
2985
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002986 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002987 if (!msg) {
2988 err = -ENOBUFS;
2989 goto out;
2990 }
2991
2992 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2993 NL80211_CMD_GET_REG);
2994 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01002995 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002996
2997 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2998 cfg80211_regdomain->alpha2);
2999
3000 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3001 if (!nl_reg_rules)
3002 goto nla_put_failure;
3003
3004 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3005 struct nlattr *nl_reg_rule;
3006 const struct ieee80211_reg_rule *reg_rule;
3007 const struct ieee80211_freq_range *freq_range;
3008 const struct ieee80211_power_rule *power_rule;
3009
3010 reg_rule = &cfg80211_regdomain->reg_rules[i];
3011 freq_range = &reg_rule->freq_range;
3012 power_rule = &reg_rule->power_rule;
3013
3014 nl_reg_rule = nla_nest_start(msg, i);
3015 if (!nl_reg_rule)
3016 goto nla_put_failure;
3017
3018 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3019 reg_rule->flags);
3020 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3021 freq_range->start_freq_khz);
3022 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3023 freq_range->end_freq_khz);
3024 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3025 freq_range->max_bandwidth_khz);
3026 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3027 power_rule->max_antenna_gain);
3028 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3029 power_rule->max_eirp);
3030
3031 nla_nest_end(msg, nl_reg_rule);
3032 }
3033
3034 nla_nest_end(msg, nl_reg_rules);
3035
3036 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003037 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003038 goto out;
3039
3040nla_put_failure:
3041 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003042put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003043 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003044 err = -EMSGSIZE;
3045out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003046 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003047 return err;
3048}
3049
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003050static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3051{
3052 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3053 struct nlattr *nl_reg_rule;
3054 char *alpha2 = NULL;
3055 int rem_reg_rules = 0, r = 0;
3056 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3057 struct ieee80211_regdomain *rd = NULL;
3058
3059 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3060 return -EINVAL;
3061
3062 if (!info->attrs[NL80211_ATTR_REG_RULES])
3063 return -EINVAL;
3064
3065 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3066
3067 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3068 rem_reg_rules) {
3069 num_rules++;
3070 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003071 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003072 }
3073
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003074 mutex_lock(&cfg80211_mutex);
3075
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003076 if (!reg_is_valid_request(alpha2)) {
3077 r = -EINVAL;
3078 goto bad_reg;
3079 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003080
3081 size_of_regd = sizeof(struct ieee80211_regdomain) +
3082 (num_rules * sizeof(struct ieee80211_reg_rule));
3083
3084 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003085 if (!rd) {
3086 r = -ENOMEM;
3087 goto bad_reg;
3088 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003089
3090 rd->n_reg_rules = num_rules;
3091 rd->alpha2[0] = alpha2[0];
3092 rd->alpha2[1] = alpha2[1];
3093
3094 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3095 rem_reg_rules) {
3096 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3097 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3098 reg_rule_policy);
3099 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3100 if (r)
3101 goto bad_reg;
3102
3103 rule_idx++;
3104
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003105 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3106 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003107 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003108 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003109 }
3110
3111 BUG_ON(rule_idx != num_rules);
3112
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003113 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003114
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003115 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003116
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003117 return r;
3118
Johannes Bergd2372b32008-10-24 20:32:20 +02003119 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003120 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003121 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003122 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003123}
3124
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003125static int validate_scan_freqs(struct nlattr *freqs)
3126{
3127 struct nlattr *attr1, *attr2;
3128 int n_channels = 0, tmp1, tmp2;
3129
3130 nla_for_each_nested(attr1, freqs, tmp1) {
3131 n_channels++;
3132 /*
3133 * Some hardware has a limited channel list for
3134 * scanning, and it is pretty much nonsensical
3135 * to scan for a channel twice, so disallow that
3136 * and don't require drivers to check that the
3137 * channel list they get isn't longer than what
3138 * they can scan, as long as they can scan all
3139 * the channels they registered at once.
3140 */
3141 nla_for_each_nested(attr2, freqs, tmp2)
3142 if (attr1 != attr2 &&
3143 nla_get_u32(attr1) == nla_get_u32(attr2))
3144 return 0;
3145 }
3146
3147 return n_channels;
3148}
3149
Johannes Berg2a519312009-02-10 21:25:55 +01003150static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3151{
Johannes Berg4c476992010-10-04 21:36:35 +02003152 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3153 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003154 struct cfg80211_scan_request *request;
3155 struct cfg80211_ssid *ssid;
3156 struct ieee80211_channel *channel;
3157 struct nlattr *attr;
3158 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003159 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01003160 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003161 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003162
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003163 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3164 return -EINVAL;
3165
Johannes Berg79c97e92009-07-07 03:56:12 +02003166 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003167
Johannes Berg4c476992010-10-04 21:36:35 +02003168 if (!rdev->ops->scan)
3169 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003170
Johannes Berg4c476992010-10-04 21:36:35 +02003171 if (rdev->scan_req)
3172 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003173
3174 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003175 n_channels = validate_scan_freqs(
3176 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003177 if (!n_channels)
3178 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003179 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003180 n_channels = 0;
3181
Johannes Berg2a519312009-02-10 21:25:55 +01003182 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3183 if (wiphy->bands[band])
3184 n_channels += wiphy->bands[band]->n_channels;
3185 }
3186
3187 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3188 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3189 n_ssids++;
3190
Johannes Berg4c476992010-10-04 21:36:35 +02003191 if (n_ssids > wiphy->max_scan_ssids)
3192 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003193
Jouni Malinen70692ad2009-02-16 19:39:13 +02003194 if (info->attrs[NL80211_ATTR_IE])
3195 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3196 else
3197 ie_len = 0;
3198
Johannes Berg4c476992010-10-04 21:36:35 +02003199 if (ie_len > wiphy->max_scan_ie_len)
3200 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003201
Johannes Berg2a519312009-02-10 21:25:55 +01003202 request = kzalloc(sizeof(*request)
3203 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003204 + sizeof(channel) * n_channels
3205 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003206 if (!request)
3207 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003208
Johannes Berg2a519312009-02-10 21:25:55 +01003209 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003210 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003211 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003212 if (ie_len) {
3213 if (request->ssids)
3214 request->ie = (void *)(request->ssids + n_ssids);
3215 else
3216 request->ie = (void *)(request->channels + n_channels);
3217 }
Johannes Berg2a519312009-02-10 21:25:55 +01003218
Johannes Berg584991d2009-11-02 13:32:03 +01003219 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003220 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3221 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003222 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003223 struct ieee80211_channel *chan;
3224
3225 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3226
3227 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003228 err = -EINVAL;
3229 goto out_free;
3230 }
Johannes Berg584991d2009-11-02 13:32:03 +01003231
3232 /* ignore disabled channels */
3233 if (chan->flags & IEEE80211_CHAN_DISABLED)
3234 continue;
3235
3236 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003237 i++;
3238 }
3239 } else {
3240 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003241 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3242 int j;
3243 if (!wiphy->bands[band])
3244 continue;
3245 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003246 struct ieee80211_channel *chan;
3247
3248 chan = &wiphy->bands[band]->channels[j];
3249
3250 if (chan->flags & IEEE80211_CHAN_DISABLED)
3251 continue;
3252
3253 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003254 i++;
3255 }
3256 }
3257 }
3258
Johannes Berg584991d2009-11-02 13:32:03 +01003259 if (!i) {
3260 err = -EINVAL;
3261 goto out_free;
3262 }
3263
3264 request->n_channels = i;
3265
Johannes Berg2a519312009-02-10 21:25:55 +01003266 i = 0;
3267 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3268 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3269 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3270 err = -EINVAL;
3271 goto out_free;
3272 }
3273 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3274 request->ssids[i].ssid_len = nla_len(attr);
3275 i++;
3276 }
3277 }
3278
Jouni Malinen70692ad2009-02-16 19:39:13 +02003279 if (info->attrs[NL80211_ATTR_IE]) {
3280 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003281 memcpy((void *)request->ie,
3282 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003283 request->ie_len);
3284 }
3285
Johannes Berg463d0182009-07-14 00:33:35 +02003286 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003287 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003288
Johannes Berg79c97e92009-07-07 03:56:12 +02003289 rdev->scan_req = request;
3290 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003291
Johannes Berg463d0182009-07-14 00:33:35 +02003292 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003293 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003294 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003295 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003296 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003297 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003298 kfree(request);
3299 }
Johannes Berg3b858752009-03-12 09:55:09 +01003300
Johannes Berg2a519312009-02-10 21:25:55 +01003301 return err;
3302}
3303
3304static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3305 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003306 struct wireless_dev *wdev,
3307 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003308{
Johannes Berg48ab9052009-07-10 18:42:31 +02003309 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003310 void *hdr;
3311 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003312 int i;
3313
3314 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003315
3316 hdr = nl80211hdr_put(msg, pid, seq, flags,
3317 NL80211_CMD_NEW_SCAN_RESULTS);
3318 if (!hdr)
3319 return -1;
3320
Johannes Bergf5ea9122009-08-07 16:17:38 +02003321 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003322 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003323
3324 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3325 if (!bss)
3326 goto nla_put_failure;
3327 if (!is_zero_ether_addr(res->bssid))
3328 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3329 if (res->information_elements && res->len_information_elements)
3330 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3331 res->len_information_elements,
3332 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003333 if (res->beacon_ies && res->len_beacon_ies &&
3334 res->beacon_ies != res->information_elements)
3335 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3336 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003337 if (res->tsf)
3338 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3339 if (res->beacon_interval)
3340 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3341 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3342 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003343 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3344 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003345
Johannes Berg77965c92009-02-18 18:45:06 +01003346 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003347 case CFG80211_SIGNAL_TYPE_MBM:
3348 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3349 break;
3350 case CFG80211_SIGNAL_TYPE_UNSPEC:
3351 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3352 break;
3353 default:
3354 break;
3355 }
3356
Johannes Berg48ab9052009-07-10 18:42:31 +02003357 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003358 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003359 case NL80211_IFTYPE_STATION:
3360 if (intbss == wdev->current_bss)
3361 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3362 NL80211_BSS_STATUS_ASSOCIATED);
3363 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3364 if (intbss != wdev->auth_bsses[i])
3365 continue;
3366 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3367 NL80211_BSS_STATUS_AUTHENTICATED);
3368 break;
3369 }
3370 break;
3371 case NL80211_IFTYPE_ADHOC:
3372 if (intbss == wdev->current_bss)
3373 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3374 NL80211_BSS_STATUS_IBSS_JOINED);
3375 break;
3376 default:
3377 break;
3378 }
3379
Johannes Berg2a519312009-02-10 21:25:55 +01003380 nla_nest_end(msg, bss);
3381
3382 return genlmsg_end(msg, hdr);
3383
3384 nla_put_failure:
3385 genlmsg_cancel(msg, hdr);
3386 return -EMSGSIZE;
3387}
3388
3389static int nl80211_dump_scan(struct sk_buff *skb,
3390 struct netlink_callback *cb)
3391{
Johannes Berg48ab9052009-07-10 18:42:31 +02003392 struct cfg80211_registered_device *rdev;
3393 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003394 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003395 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003396 int start = cb->args[1], idx = 0;
3397 int err;
3398
Johannes Berg67748892010-10-04 21:14:06 +02003399 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3400 if (err)
3401 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003402
Johannes Berg48ab9052009-07-10 18:42:31 +02003403 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003404
Johannes Berg48ab9052009-07-10 18:42:31 +02003405 wdev_lock(wdev);
3406 spin_lock_bh(&rdev->bss_lock);
3407 cfg80211_bss_expire(rdev);
3408
3409 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003410 if (++idx <= start)
3411 continue;
3412 if (nl80211_send_bss(skb,
3413 NETLINK_CB(cb->skb).pid,
3414 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003415 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003416 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003417 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003418 }
3419 }
3420
Johannes Berg48ab9052009-07-10 18:42:31 +02003421 spin_unlock_bh(&rdev->bss_lock);
3422 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003423
3424 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003425 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003426
Johannes Berg67748892010-10-04 21:14:06 +02003427 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003428}
3429
Holger Schurig61fa7132009-11-11 12:25:40 +01003430static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3431 int flags, struct net_device *dev,
3432 struct survey_info *survey)
3433{
3434 void *hdr;
3435 struct nlattr *infoattr;
3436
3437 /* Survey without a channel doesn't make sense */
3438 if (!survey->channel)
3439 return -EINVAL;
3440
3441 hdr = nl80211hdr_put(msg, pid, seq, flags,
3442 NL80211_CMD_NEW_SURVEY_RESULTS);
3443 if (!hdr)
3444 return -ENOMEM;
3445
3446 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3447
3448 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3449 if (!infoattr)
3450 goto nla_put_failure;
3451
3452 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3453 survey->channel->center_freq);
3454 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3455 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3456 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003457 if (survey->filled & SURVEY_INFO_IN_USE)
3458 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02003459 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3460 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3461 survey->channel_time);
3462 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3463 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3464 survey->channel_time_busy);
3465 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3466 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3467 survey->channel_time_ext_busy);
3468 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3469 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3470 survey->channel_time_rx);
3471 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3472 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3473 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003474
3475 nla_nest_end(msg, infoattr);
3476
3477 return genlmsg_end(msg, hdr);
3478
3479 nla_put_failure:
3480 genlmsg_cancel(msg, hdr);
3481 return -EMSGSIZE;
3482}
3483
3484static int nl80211_dump_survey(struct sk_buff *skb,
3485 struct netlink_callback *cb)
3486{
3487 struct survey_info survey;
3488 struct cfg80211_registered_device *dev;
3489 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003490 int survey_idx = cb->args[1];
3491 int res;
3492
Johannes Berg67748892010-10-04 21:14:06 +02003493 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3494 if (res)
3495 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003496
3497 if (!dev->ops->dump_survey) {
3498 res = -EOPNOTSUPP;
3499 goto out_err;
3500 }
3501
3502 while (1) {
3503 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3504 &survey);
3505 if (res == -ENOENT)
3506 break;
3507 if (res)
3508 goto out_err;
3509
3510 if (nl80211_send_survey(skb,
3511 NETLINK_CB(cb->skb).pid,
3512 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3513 netdev,
3514 &survey) < 0)
3515 goto out;
3516 survey_idx++;
3517 }
3518
3519 out:
3520 cb->args[1] = survey_idx;
3521 res = skb->len;
3522 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003523 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003524 return res;
3525}
3526
Jouni Malinen255e7372009-03-20 21:21:17 +02003527static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3528{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003529 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003530}
3531
Samuel Ortizb23aa672009-07-01 21:26:54 +02003532static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3533{
3534 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3535 NL80211_WPA_VERSION_2));
3536}
3537
3538static bool nl80211_valid_akm_suite(u32 akm)
3539{
3540 return akm == WLAN_AKM_SUITE_8021X ||
3541 akm == WLAN_AKM_SUITE_PSK;
3542}
3543
3544static bool nl80211_valid_cipher_suite(u32 cipher)
3545{
3546 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3547 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3548 cipher == WLAN_CIPHER_SUITE_TKIP ||
3549 cipher == WLAN_CIPHER_SUITE_CCMP ||
3550 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3551}
3552
3553
Jouni Malinen636a5d32009-03-19 13:39:22 +02003554static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3555{
Johannes Berg4c476992010-10-04 21:36:35 +02003556 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3557 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003558 struct ieee80211_channel *chan;
3559 const u8 *bssid, *ssid, *ie = NULL;
3560 int err, ssid_len, ie_len = 0;
3561 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003562 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003563 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003564
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003565 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3566 return -EINVAL;
3567
3568 if (!info->attrs[NL80211_ATTR_MAC])
3569 return -EINVAL;
3570
Jouni Malinen17780922009-03-27 20:52:47 +02003571 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3572 return -EINVAL;
3573
Johannes Berg19957bb2009-07-02 17:20:43 +02003574 if (!info->attrs[NL80211_ATTR_SSID])
3575 return -EINVAL;
3576
3577 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3578 return -EINVAL;
3579
Johannes Bergfffd0932009-07-08 14:22:54 +02003580 err = nl80211_parse_key(info, &key);
3581 if (err)
3582 return err;
3583
3584 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003585 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3586 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003587 if (!key.p.key || !key.p.key_len)
3588 return -EINVAL;
3589 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3590 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3591 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3592 key.p.key_len != WLAN_KEY_LEN_WEP104))
3593 return -EINVAL;
3594 if (key.idx > 4)
3595 return -EINVAL;
3596 } else {
3597 key.p.key_len = 0;
3598 key.p.key = NULL;
3599 }
3600
Johannes Bergafea0b72010-08-10 09:46:42 +02003601 if (key.idx >= 0) {
3602 int i;
3603 bool ok = false;
3604 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3605 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3606 ok = true;
3607 break;
3608 }
3609 }
Johannes Berg4c476992010-10-04 21:36:35 +02003610 if (!ok)
3611 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003612 }
3613
Johannes Berg4c476992010-10-04 21:36:35 +02003614 if (!rdev->ops->auth)
3615 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003616
Johannes Berg074ac8d2010-09-16 14:58:22 +02003617 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003618 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3619 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003620
Johannes Berg19957bb2009-07-02 17:20:43 +02003621 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003622 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003623 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003624 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3625 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003626
Johannes Berg19957bb2009-07-02 17:20:43 +02003627 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3628 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3629
3630 if (info->attrs[NL80211_ATTR_IE]) {
3631 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3632 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3633 }
3634
3635 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003636 if (!nl80211_valid_auth_type(auth_type))
3637 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003638
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003639 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3640
Johannes Berg4c476992010-10-04 21:36:35 +02003641 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3642 ssid, ssid_len, ie, ie_len,
3643 key.p.key, key.p.key_len, key.idx,
3644 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003645}
3646
Johannes Bergc0692b82010-08-27 14:26:53 +03003647static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3648 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003649 struct cfg80211_crypto_settings *settings,
3650 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003651{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003652 memset(settings, 0, sizeof(*settings));
3653
Samuel Ortizb23aa672009-07-01 21:26:54 +02003654 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3655
Johannes Bergc0692b82010-08-27 14:26:53 +03003656 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3657 u16 proto;
3658 proto = nla_get_u16(
3659 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3660 settings->control_port_ethertype = cpu_to_be16(proto);
3661 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3662 proto != ETH_P_PAE)
3663 return -EINVAL;
3664 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3665 settings->control_port_no_encrypt = true;
3666 } else
3667 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3668
Samuel Ortizb23aa672009-07-01 21:26:54 +02003669 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3670 void *data;
3671 int len, i;
3672
3673 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3674 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3675 settings->n_ciphers_pairwise = len / sizeof(u32);
3676
3677 if (len % sizeof(u32))
3678 return -EINVAL;
3679
Johannes Berg3dc27d22009-07-02 21:36:37 +02003680 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003681 return -EINVAL;
3682
3683 memcpy(settings->ciphers_pairwise, data, len);
3684
3685 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3686 if (!nl80211_valid_cipher_suite(
3687 settings->ciphers_pairwise[i]))
3688 return -EINVAL;
3689 }
3690
3691 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3692 settings->cipher_group =
3693 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3694 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3695 return -EINVAL;
3696 }
3697
3698 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3699 settings->wpa_versions =
3700 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3701 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3702 return -EINVAL;
3703 }
3704
3705 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3706 void *data;
3707 int len, i;
3708
3709 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3710 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3711 settings->n_akm_suites = len / sizeof(u32);
3712
3713 if (len % sizeof(u32))
3714 return -EINVAL;
3715
3716 memcpy(settings->akm_suites, data, len);
3717
3718 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3719 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3720 return -EINVAL;
3721 }
3722
3723 return 0;
3724}
3725
Jouni Malinen636a5d32009-03-19 13:39:22 +02003726static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3727{
Johannes Berg4c476992010-10-04 21:36:35 +02003728 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3729 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003730 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003731 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003732 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003733 int err, ssid_len, ie_len = 0;
3734 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003735
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003736 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3737 return -EINVAL;
3738
3739 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003740 !info->attrs[NL80211_ATTR_SSID] ||
3741 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003742 return -EINVAL;
3743
Johannes Berg4c476992010-10-04 21:36:35 +02003744 if (!rdev->ops->assoc)
3745 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003746
Johannes Berg074ac8d2010-09-16 14:58:22 +02003747 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003748 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3749 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003750
Johannes Berg19957bb2009-07-02 17:20:43 +02003751 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003752
Johannes Berg19957bb2009-07-02 17:20:43 +02003753 chan = ieee80211_get_channel(&rdev->wiphy,
3754 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003755 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3756 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003757
Johannes Berg19957bb2009-07-02 17:20:43 +02003758 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3759 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003760
3761 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003762 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3763 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003764 }
3765
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003766 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003767 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003768 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003769 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003770 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02003771 else if (mfp != NL80211_MFP_NO)
3772 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003773 }
3774
Johannes Berg3e5d7642009-07-07 14:37:26 +02003775 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3776 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3777
Johannes Bergc0692b82010-08-27 14:26:53 +03003778 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003779 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003780 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3781 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003782 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003783
Jouni Malinen636a5d32009-03-19 13:39:22 +02003784 return err;
3785}
3786
3787static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3788{
Johannes Berg4c476992010-10-04 21:36:35 +02003789 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3790 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003791 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003792 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003793 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003794 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003795
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003796 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3797 return -EINVAL;
3798
3799 if (!info->attrs[NL80211_ATTR_MAC])
3800 return -EINVAL;
3801
3802 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3803 return -EINVAL;
3804
Johannes Berg4c476992010-10-04 21:36:35 +02003805 if (!rdev->ops->deauth)
3806 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003807
Johannes Berg074ac8d2010-09-16 14:58:22 +02003808 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003809 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3810 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003811
Johannes Berg19957bb2009-07-02 17:20:43 +02003812 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003813
Johannes Berg19957bb2009-07-02 17:20:43 +02003814 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3815 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003816 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003817 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003818 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003819
3820 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003821 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3822 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003823 }
3824
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003825 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3826
Johannes Berg4c476992010-10-04 21:36:35 +02003827 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3828 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003829}
3830
3831static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3832{
Johannes Berg4c476992010-10-04 21:36:35 +02003833 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3834 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003835 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003836 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003837 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003838 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003839
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003840 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3841 return -EINVAL;
3842
3843 if (!info->attrs[NL80211_ATTR_MAC])
3844 return -EINVAL;
3845
3846 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3847 return -EINVAL;
3848
Johannes Berg4c476992010-10-04 21:36:35 +02003849 if (!rdev->ops->disassoc)
3850 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003851
Johannes Berg074ac8d2010-09-16 14:58:22 +02003852 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003853 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3854 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003855
Johannes Berg19957bb2009-07-02 17:20:43 +02003856 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003857
Johannes Berg19957bb2009-07-02 17:20:43 +02003858 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3859 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003860 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003861 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003862 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003863
3864 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003865 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3866 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003867 }
3868
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003869 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3870
Johannes Berg4c476992010-10-04 21:36:35 +02003871 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3872 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003873}
3874
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003875static bool
3876nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
3877 int mcast_rate[IEEE80211_NUM_BANDS],
3878 int rateval)
3879{
3880 struct wiphy *wiphy = &rdev->wiphy;
3881 bool found = false;
3882 int band, i;
3883
3884 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3885 struct ieee80211_supported_band *sband;
3886
3887 sband = wiphy->bands[band];
3888 if (!sband)
3889 continue;
3890
3891 for (i = 0; i < sband->n_bitrates; i++) {
3892 if (sband->bitrates[i].bitrate == rateval) {
3893 mcast_rate[band] = i + 1;
3894 found = true;
3895 break;
3896 }
3897 }
3898 }
3899
3900 return found;
3901}
3902
Johannes Berg04a773a2009-04-19 21:24:32 +02003903static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3904{
Johannes Berg4c476992010-10-04 21:36:35 +02003905 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3906 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003907 struct cfg80211_ibss_params ibss;
3908 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003909 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003910 int err;
3911
Johannes Berg8e30bc52009-04-22 17:45:38 +02003912 memset(&ibss, 0, sizeof(ibss));
3913
Johannes Berg04a773a2009-04-19 21:24:32 +02003914 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3915 return -EINVAL;
3916
3917 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3918 !info->attrs[NL80211_ATTR_SSID] ||
3919 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3920 return -EINVAL;
3921
Johannes Berg8e30bc52009-04-22 17:45:38 +02003922 ibss.beacon_interval = 100;
3923
3924 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3925 ibss.beacon_interval =
3926 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3927 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3928 return -EINVAL;
3929 }
3930
Johannes Berg4c476992010-10-04 21:36:35 +02003931 if (!rdev->ops->join_ibss)
3932 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003933
Johannes Berg4c476992010-10-04 21:36:35 +02003934 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3935 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003936
Johannes Berg79c97e92009-07-07 03:56:12 +02003937 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003938
3939 if (info->attrs[NL80211_ATTR_MAC])
3940 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3941 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3942 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3943
3944 if (info->attrs[NL80211_ATTR_IE]) {
3945 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3946 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3947 }
3948
3949 ibss.channel = ieee80211_get_channel(wiphy,
3950 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3951 if (!ibss.channel ||
3952 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02003953 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3954 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003955
3956 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003957 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003958
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003959 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3960 u8 *rates =
3961 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3962 int n_rates =
3963 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3964 struct ieee80211_supported_band *sband =
3965 wiphy->bands[ibss.channel->band];
3966 int i, j;
3967
Johannes Berg4c476992010-10-04 21:36:35 +02003968 if (n_rates == 0)
3969 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003970
3971 for (i = 0; i < n_rates; i++) {
3972 int rate = (rates[i] & 0x7f) * 5;
3973 bool found = false;
3974
3975 for (j = 0; j < sband->n_bitrates; j++) {
3976 if (sband->bitrates[j].bitrate == rate) {
3977 found = true;
3978 ibss.basic_rates |= BIT(j);
3979 break;
3980 }
3981 }
Johannes Berg4c476992010-10-04 21:36:35 +02003982 if (!found)
3983 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003984 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003985 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003986
3987 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
3988 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
3989 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
3990 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003991
Johannes Berg4c476992010-10-04 21:36:35 +02003992 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3993 connkeys = nl80211_parse_connkeys(rdev,
3994 info->attrs[NL80211_ATTR_KEYS]);
3995 if (IS_ERR(connkeys))
3996 return PTR_ERR(connkeys);
3997 }
Johannes Berg04a773a2009-04-19 21:24:32 +02003998
Johannes Berg4c476992010-10-04 21:36:35 +02003999 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004000 if (err)
4001 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004002 return err;
4003}
4004
4005static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4006{
Johannes Berg4c476992010-10-04 21:36:35 +02004007 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4008 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004009
Johannes Berg4c476992010-10-04 21:36:35 +02004010 if (!rdev->ops->leave_ibss)
4011 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004012
Johannes Berg4c476992010-10-04 21:36:35 +02004013 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4014 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004015
Johannes Berg4c476992010-10-04 21:36:35 +02004016 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004017}
4018
Johannes Bergaff89a92009-07-01 21:26:51 +02004019#ifdef CONFIG_NL80211_TESTMODE
4020static struct genl_multicast_group nl80211_testmode_mcgrp = {
4021 .name = "testmode",
4022};
4023
4024static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4025{
Johannes Berg4c476992010-10-04 21:36:35 +02004026 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004027 int err;
4028
4029 if (!info->attrs[NL80211_ATTR_TESTDATA])
4030 return -EINVAL;
4031
Johannes Bergaff89a92009-07-01 21:26:51 +02004032 err = -EOPNOTSUPP;
4033 if (rdev->ops->testmode_cmd) {
4034 rdev->testmode_info = info;
4035 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4036 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4037 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4038 rdev->testmode_info = NULL;
4039 }
4040
Johannes Bergaff89a92009-07-01 21:26:51 +02004041 return err;
4042}
4043
4044static struct sk_buff *
4045__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4046 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4047{
4048 struct sk_buff *skb;
4049 void *hdr;
4050 struct nlattr *data;
4051
4052 skb = nlmsg_new(approxlen + 100, gfp);
4053 if (!skb)
4054 return NULL;
4055
4056 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4057 if (!hdr) {
4058 kfree_skb(skb);
4059 return NULL;
4060 }
4061
4062 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4063 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4064
4065 ((void **)skb->cb)[0] = rdev;
4066 ((void **)skb->cb)[1] = hdr;
4067 ((void **)skb->cb)[2] = data;
4068
4069 return skb;
4070
4071 nla_put_failure:
4072 kfree_skb(skb);
4073 return NULL;
4074}
4075
4076struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4077 int approxlen)
4078{
4079 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4080
4081 if (WARN_ON(!rdev->testmode_info))
4082 return NULL;
4083
4084 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4085 rdev->testmode_info->snd_pid,
4086 rdev->testmode_info->snd_seq,
4087 GFP_KERNEL);
4088}
4089EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4090
4091int cfg80211_testmode_reply(struct sk_buff *skb)
4092{
4093 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4094 void *hdr = ((void **)skb->cb)[1];
4095 struct nlattr *data = ((void **)skb->cb)[2];
4096
4097 if (WARN_ON(!rdev->testmode_info)) {
4098 kfree_skb(skb);
4099 return -EINVAL;
4100 }
4101
4102 nla_nest_end(skb, data);
4103 genlmsg_end(skb, hdr);
4104 return genlmsg_reply(skb, rdev->testmode_info);
4105}
4106EXPORT_SYMBOL(cfg80211_testmode_reply);
4107
4108struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4109 int approxlen, gfp_t gfp)
4110{
4111 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4112
4113 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4114}
4115EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4116
4117void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4118{
4119 void *hdr = ((void **)skb->cb)[1];
4120 struct nlattr *data = ((void **)skb->cb)[2];
4121
4122 nla_nest_end(skb, data);
4123 genlmsg_end(skb, hdr);
4124 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4125}
4126EXPORT_SYMBOL(cfg80211_testmode_event);
4127#endif
4128
Samuel Ortizb23aa672009-07-01 21:26:54 +02004129static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4130{
Johannes Berg4c476992010-10-04 21:36:35 +02004131 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4132 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004133 struct cfg80211_connect_params connect;
4134 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004135 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004136 int err;
4137
4138 memset(&connect, 0, sizeof(connect));
4139
4140 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4141 return -EINVAL;
4142
4143 if (!info->attrs[NL80211_ATTR_SSID] ||
4144 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4145 return -EINVAL;
4146
4147 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4148 connect.auth_type =
4149 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4150 if (!nl80211_valid_auth_type(connect.auth_type))
4151 return -EINVAL;
4152 } else
4153 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4154
4155 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4156
Johannes Bergc0692b82010-08-27 14:26:53 +03004157 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004158 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004159 if (err)
4160 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004161
Johannes Berg074ac8d2010-09-16 14:58:22 +02004162 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004163 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4164 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004165
Johannes Berg79c97e92009-07-07 03:56:12 +02004166 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004167
Samuel Ortizb23aa672009-07-01 21:26:54 +02004168 if (info->attrs[NL80211_ATTR_MAC])
4169 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4170 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4171 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4172
4173 if (info->attrs[NL80211_ATTR_IE]) {
4174 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4175 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4176 }
4177
4178 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4179 connect.channel =
4180 ieee80211_get_channel(wiphy,
4181 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4182 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004183 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4184 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004185 }
4186
Johannes Bergfffd0932009-07-08 14:22:54 +02004187 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4188 connkeys = nl80211_parse_connkeys(rdev,
4189 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004190 if (IS_ERR(connkeys))
4191 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004192 }
4193
4194 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004195 if (err)
4196 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004197 return err;
4198}
4199
4200static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4201{
Johannes Berg4c476992010-10-04 21:36:35 +02004202 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4203 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004204 u16 reason;
4205
4206 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4207 reason = WLAN_REASON_DEAUTH_LEAVING;
4208 else
4209 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4210
4211 if (reason == 0)
4212 return -EINVAL;
4213
Johannes Berg074ac8d2010-09-16 14:58:22 +02004214 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004215 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4216 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004217
Johannes Berg4c476992010-10-04 21:36:35 +02004218 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004219}
4220
Johannes Berg463d0182009-07-14 00:33:35 +02004221static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4222{
Johannes Berg4c476992010-10-04 21:36:35 +02004223 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004224 struct net *net;
4225 int err;
4226 u32 pid;
4227
4228 if (!info->attrs[NL80211_ATTR_PID])
4229 return -EINVAL;
4230
4231 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4232
Johannes Berg463d0182009-07-14 00:33:35 +02004233 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004234 if (IS_ERR(net))
4235 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004236
4237 err = 0;
4238
4239 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004240 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4241 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004242
Johannes Berg463d0182009-07-14 00:33:35 +02004243 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004244 return err;
4245}
4246
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004247static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4248{
Johannes Berg4c476992010-10-04 21:36:35 +02004249 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004250 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4251 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004252 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004253 struct cfg80211_pmksa pmksa;
4254
4255 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4256
4257 if (!info->attrs[NL80211_ATTR_MAC])
4258 return -EINVAL;
4259
4260 if (!info->attrs[NL80211_ATTR_PMKID])
4261 return -EINVAL;
4262
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004263 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4264 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4265
Johannes Berg074ac8d2010-09-16 14:58:22 +02004266 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004267 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4268 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004269
4270 switch (info->genlhdr->cmd) {
4271 case NL80211_CMD_SET_PMKSA:
4272 rdev_ops = rdev->ops->set_pmksa;
4273 break;
4274 case NL80211_CMD_DEL_PMKSA:
4275 rdev_ops = rdev->ops->del_pmksa;
4276 break;
4277 default:
4278 WARN_ON(1);
4279 break;
4280 }
4281
Johannes Berg4c476992010-10-04 21:36:35 +02004282 if (!rdev_ops)
4283 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004284
Johannes Berg4c476992010-10-04 21:36:35 +02004285 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004286}
4287
4288static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4289{
Johannes Berg4c476992010-10-04 21:36:35 +02004290 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4291 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004292
Johannes Berg074ac8d2010-09-16 14:58:22 +02004293 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004294 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4295 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004296
Johannes Berg4c476992010-10-04 21:36:35 +02004297 if (!rdev->ops->flush_pmksa)
4298 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004299
Johannes Berg4c476992010-10-04 21:36:35 +02004300 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004301}
4302
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004303static int nl80211_remain_on_channel(struct sk_buff *skb,
4304 struct genl_info *info)
4305{
Johannes Berg4c476992010-10-04 21:36:35 +02004306 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4307 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004308 struct ieee80211_channel *chan;
4309 struct sk_buff *msg;
4310 void *hdr;
4311 u64 cookie;
4312 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4313 u32 freq, duration;
4314 int err;
4315
4316 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4317 !info->attrs[NL80211_ATTR_DURATION])
4318 return -EINVAL;
4319
4320 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4321
4322 /*
4323 * We should be on that channel for at least one jiffie,
4324 * and more than 5 seconds seems excessive.
4325 */
Johannes Berga2939112010-12-14 17:54:28 +01004326 if (!duration || !msecs_to_jiffies(duration) ||
4327 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004328 return -EINVAL;
4329
Johannes Berg4c476992010-10-04 21:36:35 +02004330 if (!rdev->ops->remain_on_channel)
4331 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004332
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004333 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4334 channel_type = nla_get_u32(
4335 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4336 if (channel_type != NL80211_CHAN_NO_HT &&
4337 channel_type != NL80211_CHAN_HT20 &&
4338 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004339 channel_type != NL80211_CHAN_HT40MINUS)
4340 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004341 }
4342
4343 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4344 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004345 if (chan == NULL)
4346 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004347
4348 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004349 if (!msg)
4350 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004351
4352 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4353 NL80211_CMD_REMAIN_ON_CHANNEL);
4354
4355 if (IS_ERR(hdr)) {
4356 err = PTR_ERR(hdr);
4357 goto free_msg;
4358 }
4359
4360 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4361 channel_type, duration, &cookie);
4362
4363 if (err)
4364 goto free_msg;
4365
4366 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4367
4368 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004369
4370 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004371
4372 nla_put_failure:
4373 err = -ENOBUFS;
4374 free_msg:
4375 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004376 return err;
4377}
4378
4379static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4380 struct genl_info *info)
4381{
Johannes Berg4c476992010-10-04 21:36:35 +02004382 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4383 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004384 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004385
4386 if (!info->attrs[NL80211_ATTR_COOKIE])
4387 return -EINVAL;
4388
Johannes Berg4c476992010-10-04 21:36:35 +02004389 if (!rdev->ops->cancel_remain_on_channel)
4390 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004391
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004392 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4393
Johannes Berg4c476992010-10-04 21:36:35 +02004394 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004395}
4396
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004397static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4398 u8 *rates, u8 rates_len)
4399{
4400 u8 i;
4401 u32 mask = 0;
4402
4403 for (i = 0; i < rates_len; i++) {
4404 int rate = (rates[i] & 0x7f) * 5;
4405 int ridx;
4406 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4407 struct ieee80211_rate *srate =
4408 &sband->bitrates[ridx];
4409 if (rate == srate->bitrate) {
4410 mask |= 1 << ridx;
4411 break;
4412 }
4413 }
4414 if (ridx == sband->n_bitrates)
4415 return 0; /* rate not found */
4416 }
4417
4418 return mask;
4419}
4420
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004421static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004422 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4423 .len = NL80211_MAX_SUPP_RATES },
4424};
4425
4426static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4427 struct genl_info *info)
4428{
4429 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004430 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004431 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004432 int rem, i;
4433 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004434 struct nlattr *tx_rates;
4435 struct ieee80211_supported_band *sband;
4436
4437 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4438 return -EINVAL;
4439
Johannes Berg4c476992010-10-04 21:36:35 +02004440 if (!rdev->ops->set_bitrate_mask)
4441 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004442
4443 memset(&mask, 0, sizeof(mask));
4444 /* Default to all rates enabled */
4445 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4446 sband = rdev->wiphy.bands[i];
4447 mask.control[i].legacy =
4448 sband ? (1 << sband->n_bitrates) - 1 : 0;
4449 }
4450
4451 /*
4452 * The nested attribute uses enum nl80211_band as the index. This maps
4453 * directly to the enum ieee80211_band values used in cfg80211.
4454 */
4455 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4456 {
4457 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004458 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4459 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004460 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004461 if (sband == NULL)
4462 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004463 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4464 nla_len(tx_rates), nl80211_txattr_policy);
4465 if (tb[NL80211_TXRATE_LEGACY]) {
4466 mask.control[band].legacy = rateset_to_mask(
4467 sband,
4468 nla_data(tb[NL80211_TXRATE_LEGACY]),
4469 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004470 if (mask.control[band].legacy == 0)
4471 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004472 }
4473 }
4474
Johannes Berg4c476992010-10-04 21:36:35 +02004475 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004476}
4477
Johannes Berg2e161f72010-08-12 15:38:38 +02004478static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004479{
Johannes Berg4c476992010-10-04 21:36:35 +02004480 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4481 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004482 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004483
4484 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4485 return -EINVAL;
4486
Johannes Berg2e161f72010-08-12 15:38:38 +02004487 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4488 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004489
Johannes Berg9d38d852010-06-09 17:20:33 +02004490 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004491 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004492 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4493 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4494 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08004495 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004496 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4497 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004498
4499 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004500 if (!rdev->ops->mgmt_tx)
4501 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004502
Johannes Berg4c476992010-10-04 21:36:35 +02004503 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004504 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004505 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4506 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004507}
4508
Johannes Berg2e161f72010-08-12 15:38:38 +02004509static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004510{
Johannes Berg4c476992010-10-04 21:36:35 +02004511 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4512 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004513 struct ieee80211_channel *chan;
4514 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004515 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004516 u32 freq;
4517 int err;
4518 void *hdr;
4519 u64 cookie;
4520 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004521 unsigned int wait = 0;
4522 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02004523
4524 if (!info->attrs[NL80211_ATTR_FRAME] ||
4525 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4526 return -EINVAL;
4527
Johannes Berg4c476992010-10-04 21:36:35 +02004528 if (!rdev->ops->mgmt_tx)
4529 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004530
Johannes Berg9d38d852010-06-09 17:20:33 +02004531 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004532 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004533 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4534 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4535 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08004536 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004537 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4538 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004539
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004540 if (info->attrs[NL80211_ATTR_DURATION]) {
4541 if (!rdev->ops->mgmt_tx_cancel_wait)
4542 return -EINVAL;
4543 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4544 }
4545
Jouni Malinen026331c2010-02-15 12:53:10 +02004546 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4547 channel_type = nla_get_u32(
4548 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4549 if (channel_type != NL80211_CHAN_NO_HT &&
4550 channel_type != NL80211_CHAN_HT20 &&
4551 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004552 channel_type != NL80211_CHAN_HT40MINUS)
4553 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004554 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004555 }
4556
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004557 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
4558
Jouni Malinen026331c2010-02-15 12:53:10 +02004559 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4560 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004561 if (chan == NULL)
4562 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004563
4564 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004565 if (!msg)
4566 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004567
4568 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004569 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004570
4571 if (IS_ERR(hdr)) {
4572 err = PTR_ERR(hdr);
4573 goto free_msg;
4574 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004575 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
4576 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02004577 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4578 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4579 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004580 if (err)
4581 goto free_msg;
4582
4583 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4584
4585 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004586 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004587
4588 nla_put_failure:
4589 err = -ENOBUFS;
4590 free_msg:
4591 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004592 return err;
4593}
4594
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004595static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
4596{
4597 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4598 struct net_device *dev = info->user_ptr[1];
4599 u64 cookie;
4600
4601 if (!info->attrs[NL80211_ATTR_COOKIE])
4602 return -EINVAL;
4603
4604 if (!rdev->ops->mgmt_tx_cancel_wait)
4605 return -EOPNOTSUPP;
4606
4607 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4608 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4609 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4610 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4611 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4612 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4613 return -EOPNOTSUPP;
4614
4615 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4616
4617 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
4618}
4619
Kalle Valoffb9eb32010-02-17 17:58:10 +02004620static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4621{
Johannes Berg4c476992010-10-04 21:36:35 +02004622 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004623 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004624 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004625 u8 ps_state;
4626 bool state;
4627 int err;
4628
Johannes Berg4c476992010-10-04 21:36:35 +02004629 if (!info->attrs[NL80211_ATTR_PS_STATE])
4630 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004631
4632 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4633
Johannes Berg4c476992010-10-04 21:36:35 +02004634 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4635 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004636
4637 wdev = dev->ieee80211_ptr;
4638
Johannes Berg4c476992010-10-04 21:36:35 +02004639 if (!rdev->ops->set_power_mgmt)
4640 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004641
4642 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4643
4644 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004645 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004646
Johannes Berg4c476992010-10-04 21:36:35 +02004647 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4648 wdev->ps_timeout);
4649 if (!err)
4650 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004651 return err;
4652}
4653
4654static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4655{
Johannes Berg4c476992010-10-04 21:36:35 +02004656 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004657 enum nl80211_ps_state ps_state;
4658 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004659 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004660 struct sk_buff *msg;
4661 void *hdr;
4662 int err;
4663
Kalle Valoffb9eb32010-02-17 17:58:10 +02004664 wdev = dev->ieee80211_ptr;
4665
Johannes Berg4c476992010-10-04 21:36:35 +02004666 if (!rdev->ops->set_power_mgmt)
4667 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004668
4669 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004670 if (!msg)
4671 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004672
4673 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4674 NL80211_CMD_GET_POWER_SAVE);
4675 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004676 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004677 goto free_msg;
4678 }
4679
4680 if (wdev->ps)
4681 ps_state = NL80211_PS_ENABLED;
4682 else
4683 ps_state = NL80211_PS_DISABLED;
4684
4685 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4686
4687 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004688 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004689
Johannes Berg4c476992010-10-04 21:36:35 +02004690 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004691 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004692 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004693 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004694 return err;
4695}
4696
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004697static struct nla_policy
4698nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4699 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4700 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4701 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4702};
4703
4704static int nl80211_set_cqm_rssi(struct genl_info *info,
4705 s32 threshold, u32 hysteresis)
4706{
Johannes Berg4c476992010-10-04 21:36:35 +02004707 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004708 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004709 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004710
4711 if (threshold > 0)
4712 return -EINVAL;
4713
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004714 wdev = dev->ieee80211_ptr;
4715
Johannes Berg4c476992010-10-04 21:36:35 +02004716 if (!rdev->ops->set_cqm_rssi_config)
4717 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004718
Johannes Berg074ac8d2010-09-16 14:58:22 +02004719 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004720 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4721 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004722
Johannes Berg4c476992010-10-04 21:36:35 +02004723 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4724 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004725}
4726
4727static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4728{
4729 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4730 struct nlattr *cqm;
4731 int err;
4732
4733 cqm = info->attrs[NL80211_ATTR_CQM];
4734 if (!cqm) {
4735 err = -EINVAL;
4736 goto out;
4737 }
4738
4739 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4740 nl80211_attr_cqm_policy);
4741 if (err)
4742 goto out;
4743
4744 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4745 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4746 s32 threshold;
4747 u32 hysteresis;
4748 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4749 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4750 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4751 } else
4752 err = -EINVAL;
4753
4754out:
4755 return err;
4756}
4757
Johannes Berg29cbe682010-12-03 09:20:44 +01004758static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
4759{
4760 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4761 struct net_device *dev = info->user_ptr[1];
4762 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08004763 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01004764 int err;
4765
4766 /* start with default */
4767 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08004768 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01004769
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004770 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01004771 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004772 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01004773 if (err)
4774 return err;
4775 }
4776
4777 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
4778 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
4779 return -EINVAL;
4780
Javier Cardonac80d5452010-12-16 17:37:49 -08004781 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
4782 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4783
4784 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
4785 /* parse additional setup parameters if given */
4786 err = nl80211_parse_mesh_setup(info, &setup);
4787 if (err)
4788 return err;
4789 }
4790
4791 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004792}
4793
4794static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
4795{
4796 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4797 struct net_device *dev = info->user_ptr[1];
4798
4799 return cfg80211_leave_mesh(rdev, dev);
4800}
4801
Johannes Berg4c476992010-10-04 21:36:35 +02004802#define NL80211_FLAG_NEED_WIPHY 0x01
4803#define NL80211_FLAG_NEED_NETDEV 0x02
4804#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02004805#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
4806#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
4807 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02004808
4809static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4810 struct genl_info *info)
4811{
4812 struct cfg80211_registered_device *rdev;
4813 struct net_device *dev;
4814 int err;
4815 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4816
4817 if (rtnl)
4818 rtnl_lock();
4819
4820 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4821 rdev = cfg80211_get_dev_from_info(info);
4822 if (IS_ERR(rdev)) {
4823 if (rtnl)
4824 rtnl_unlock();
4825 return PTR_ERR(rdev);
4826 }
4827 info->user_ptr[0] = rdev;
4828 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4829 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4830 if (err) {
4831 if (rtnl)
4832 rtnl_unlock();
4833 return err;
4834 }
Johannes Berg41265712010-10-04 21:14:05 +02004835 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4836 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02004837 cfg80211_unlock_rdev(rdev);
4838 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02004839 if (rtnl)
4840 rtnl_unlock();
4841 return -ENETDOWN;
4842 }
Johannes Berg4c476992010-10-04 21:36:35 +02004843 info->user_ptr[0] = rdev;
4844 info->user_ptr[1] = dev;
4845 }
4846
4847 return 0;
4848}
4849
4850static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4851 struct genl_info *info)
4852{
4853 if (info->user_ptr[0])
4854 cfg80211_unlock_rdev(info->user_ptr[0]);
4855 if (info->user_ptr[1])
4856 dev_put(info->user_ptr[1]);
4857 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4858 rtnl_unlock();
4859}
4860
Johannes Berg55682962007-09-20 13:09:35 -04004861static struct genl_ops nl80211_ops[] = {
4862 {
4863 .cmd = NL80211_CMD_GET_WIPHY,
4864 .doit = nl80211_get_wiphy,
4865 .dumpit = nl80211_dump_wiphy,
4866 .policy = nl80211_policy,
4867 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004868 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04004869 },
4870 {
4871 .cmd = NL80211_CMD_SET_WIPHY,
4872 .doit = nl80211_set_wiphy,
4873 .policy = nl80211_policy,
4874 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004875 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004876 },
4877 {
4878 .cmd = NL80211_CMD_GET_INTERFACE,
4879 .doit = nl80211_get_interface,
4880 .dumpit = nl80211_dump_interface,
4881 .policy = nl80211_policy,
4882 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004883 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04004884 },
4885 {
4886 .cmd = NL80211_CMD_SET_INTERFACE,
4887 .doit = nl80211_set_interface,
4888 .policy = nl80211_policy,
4889 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004890 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4891 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004892 },
4893 {
4894 .cmd = NL80211_CMD_NEW_INTERFACE,
4895 .doit = nl80211_new_interface,
4896 .policy = nl80211_policy,
4897 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004898 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4899 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004900 },
4901 {
4902 .cmd = NL80211_CMD_DEL_INTERFACE,
4903 .doit = nl80211_del_interface,
4904 .policy = nl80211_policy,
4905 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004906 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4907 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004908 },
Johannes Berg41ade002007-12-19 02:03:29 +01004909 {
4910 .cmd = NL80211_CMD_GET_KEY,
4911 .doit = nl80211_get_key,
4912 .policy = nl80211_policy,
4913 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004914 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4915 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004916 },
4917 {
4918 .cmd = NL80211_CMD_SET_KEY,
4919 .doit = nl80211_set_key,
4920 .policy = nl80211_policy,
4921 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004922 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004923 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004924 },
4925 {
4926 .cmd = NL80211_CMD_NEW_KEY,
4927 .doit = nl80211_new_key,
4928 .policy = nl80211_policy,
4929 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004930 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004931 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004932 },
4933 {
4934 .cmd = NL80211_CMD_DEL_KEY,
4935 .doit = nl80211_del_key,
4936 .policy = nl80211_policy,
4937 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004938 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004939 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004940 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004941 {
4942 .cmd = NL80211_CMD_SET_BEACON,
4943 .policy = nl80211_policy,
4944 .flags = GENL_ADMIN_PERM,
4945 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004946 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4947 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004948 },
4949 {
4950 .cmd = NL80211_CMD_NEW_BEACON,
4951 .policy = nl80211_policy,
4952 .flags = GENL_ADMIN_PERM,
4953 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004954 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4955 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004956 },
4957 {
4958 .cmd = NL80211_CMD_DEL_BEACON,
4959 .policy = nl80211_policy,
4960 .flags = GENL_ADMIN_PERM,
4961 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004962 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4963 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004964 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004965 {
4966 .cmd = NL80211_CMD_GET_STATION,
4967 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004968 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004969 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02004970 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4971 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004972 },
4973 {
4974 .cmd = NL80211_CMD_SET_STATION,
4975 .doit = nl80211_set_station,
4976 .policy = nl80211_policy,
4977 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004978 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4979 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004980 },
4981 {
4982 .cmd = NL80211_CMD_NEW_STATION,
4983 .doit = nl80211_new_station,
4984 .policy = nl80211_policy,
4985 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004986 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004987 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004988 },
4989 {
4990 .cmd = NL80211_CMD_DEL_STATION,
4991 .doit = nl80211_del_station,
4992 .policy = nl80211_policy,
4993 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004994 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4995 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004996 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004997 {
4998 .cmd = NL80211_CMD_GET_MPATH,
4999 .doit = nl80211_get_mpath,
5000 .dumpit = nl80211_dump_mpath,
5001 .policy = nl80211_policy,
5002 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005003 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005004 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005005 },
5006 {
5007 .cmd = NL80211_CMD_SET_MPATH,
5008 .doit = nl80211_set_mpath,
5009 .policy = nl80211_policy,
5010 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005011 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005012 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005013 },
5014 {
5015 .cmd = NL80211_CMD_NEW_MPATH,
5016 .doit = nl80211_new_mpath,
5017 .policy = nl80211_policy,
5018 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005019 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005020 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005021 },
5022 {
5023 .cmd = NL80211_CMD_DEL_MPATH,
5024 .doit = nl80211_del_mpath,
5025 .policy = nl80211_policy,
5026 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005027 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5028 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005029 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005030 {
5031 .cmd = NL80211_CMD_SET_BSS,
5032 .doit = nl80211_set_bss,
5033 .policy = nl80211_policy,
5034 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005035 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5036 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005037 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005038 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005039 .cmd = NL80211_CMD_GET_REG,
5040 .doit = nl80211_get_reg,
5041 .policy = nl80211_policy,
5042 /* can be retrieved by unprivileged users */
5043 },
5044 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005045 .cmd = NL80211_CMD_SET_REG,
5046 .doit = nl80211_set_reg,
5047 .policy = nl80211_policy,
5048 .flags = GENL_ADMIN_PERM,
5049 },
5050 {
5051 .cmd = NL80211_CMD_REQ_SET_REG,
5052 .doit = nl80211_req_set_reg,
5053 .policy = nl80211_policy,
5054 .flags = GENL_ADMIN_PERM,
5055 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005056 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005057 .cmd = NL80211_CMD_GET_MESH_CONFIG,
5058 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005059 .policy = nl80211_policy,
5060 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005061 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5062 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005063 },
5064 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005065 .cmd = NL80211_CMD_SET_MESH_CONFIG,
5066 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005067 .policy = nl80211_policy,
5068 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01005069 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005070 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005071 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005072 {
Johannes Berg2a519312009-02-10 21:25:55 +01005073 .cmd = NL80211_CMD_TRIGGER_SCAN,
5074 .doit = nl80211_trigger_scan,
5075 .policy = nl80211_policy,
5076 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005077 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005078 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01005079 },
5080 {
5081 .cmd = NL80211_CMD_GET_SCAN,
5082 .policy = nl80211_policy,
5083 .dumpit = nl80211_dump_scan,
5084 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005085 {
5086 .cmd = NL80211_CMD_AUTHENTICATE,
5087 .doit = nl80211_authenticate,
5088 .policy = nl80211_policy,
5089 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005090 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005091 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005092 },
5093 {
5094 .cmd = NL80211_CMD_ASSOCIATE,
5095 .doit = nl80211_associate,
5096 .policy = nl80211_policy,
5097 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005098 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005099 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005100 },
5101 {
5102 .cmd = NL80211_CMD_DEAUTHENTICATE,
5103 .doit = nl80211_deauthenticate,
5104 .policy = nl80211_policy,
5105 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005106 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005107 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005108 },
5109 {
5110 .cmd = NL80211_CMD_DISASSOCIATE,
5111 .doit = nl80211_disassociate,
5112 .policy = nl80211_policy,
5113 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005114 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005115 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005116 },
Johannes Berg04a773a2009-04-19 21:24:32 +02005117 {
5118 .cmd = NL80211_CMD_JOIN_IBSS,
5119 .doit = nl80211_join_ibss,
5120 .policy = nl80211_policy,
5121 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005122 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005123 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005124 },
5125 {
5126 .cmd = NL80211_CMD_LEAVE_IBSS,
5127 .doit = nl80211_leave_ibss,
5128 .policy = nl80211_policy,
5129 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005130 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005131 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005132 },
Johannes Bergaff89a92009-07-01 21:26:51 +02005133#ifdef CONFIG_NL80211_TESTMODE
5134 {
5135 .cmd = NL80211_CMD_TESTMODE,
5136 .doit = nl80211_testmode_do,
5137 .policy = nl80211_policy,
5138 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005139 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5140 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02005141 },
5142#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02005143 {
5144 .cmd = NL80211_CMD_CONNECT,
5145 .doit = nl80211_connect,
5146 .policy = nl80211_policy,
5147 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005148 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005149 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005150 },
5151 {
5152 .cmd = NL80211_CMD_DISCONNECT,
5153 .doit = nl80211_disconnect,
5154 .policy = nl80211_policy,
5155 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005156 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005157 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005158 },
Johannes Berg463d0182009-07-14 00:33:35 +02005159 {
5160 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5161 .doit = nl80211_wiphy_netns,
5162 .policy = nl80211_policy,
5163 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005164 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5165 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02005166 },
Holger Schurig61fa7132009-11-11 12:25:40 +01005167 {
5168 .cmd = NL80211_CMD_GET_SURVEY,
5169 .policy = nl80211_policy,
5170 .dumpit = nl80211_dump_survey,
5171 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005172 {
5173 .cmd = NL80211_CMD_SET_PMKSA,
5174 .doit = nl80211_setdel_pmksa,
5175 .policy = nl80211_policy,
5176 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005177 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5178 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005179 },
5180 {
5181 .cmd = NL80211_CMD_DEL_PMKSA,
5182 .doit = nl80211_setdel_pmksa,
5183 .policy = nl80211_policy,
5184 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005185 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5186 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005187 },
5188 {
5189 .cmd = NL80211_CMD_FLUSH_PMKSA,
5190 .doit = nl80211_flush_pmksa,
5191 .policy = nl80211_policy,
5192 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005193 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5194 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005195 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005196 {
5197 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5198 .doit = nl80211_remain_on_channel,
5199 .policy = nl80211_policy,
5200 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005201 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005202 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005203 },
5204 {
5205 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5206 .doit = nl80211_cancel_remain_on_channel,
5207 .policy = nl80211_policy,
5208 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005209 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005210 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005211 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005212 {
5213 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5214 .doit = nl80211_set_tx_bitrate_mask,
5215 .policy = nl80211_policy,
5216 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005217 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5218 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005219 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005220 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005221 .cmd = NL80211_CMD_REGISTER_FRAME,
5222 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005223 .policy = nl80211_policy,
5224 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005225 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5226 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005227 },
5228 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005229 .cmd = NL80211_CMD_FRAME,
5230 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005231 .policy = nl80211_policy,
5232 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005233 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005234 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005235 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005236 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005237 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
5238 .doit = nl80211_tx_mgmt_cancel_wait,
5239 .policy = nl80211_policy,
5240 .flags = GENL_ADMIN_PERM,
5241 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5242 NL80211_FLAG_NEED_RTNL,
5243 },
5244 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02005245 .cmd = NL80211_CMD_SET_POWER_SAVE,
5246 .doit = nl80211_set_power_save,
5247 .policy = nl80211_policy,
5248 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005249 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5250 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005251 },
5252 {
5253 .cmd = NL80211_CMD_GET_POWER_SAVE,
5254 .doit = nl80211_get_power_save,
5255 .policy = nl80211_policy,
5256 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005257 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5258 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005259 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005260 {
5261 .cmd = NL80211_CMD_SET_CQM,
5262 .doit = nl80211_set_cqm,
5263 .policy = nl80211_policy,
5264 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005265 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5266 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005267 },
Johannes Bergf444de02010-05-05 15:25:02 +02005268 {
5269 .cmd = NL80211_CMD_SET_CHANNEL,
5270 .doit = nl80211_set_channel,
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,
Johannes Bergf444de02010-05-05 15:25:02 +02005275 },
Bill Jordane8347eb2010-10-01 13:54:28 -04005276 {
5277 .cmd = NL80211_CMD_SET_WDS_PEER,
5278 .doit = nl80211_set_wds_peer,
5279 .policy = nl80211_policy,
5280 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02005281 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5282 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04005283 },
Johannes Berg29cbe682010-12-03 09:20:44 +01005284 {
5285 .cmd = NL80211_CMD_JOIN_MESH,
5286 .doit = nl80211_join_mesh,
5287 .policy = nl80211_policy,
5288 .flags = GENL_ADMIN_PERM,
5289 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5290 NL80211_FLAG_NEED_RTNL,
5291 },
5292 {
5293 .cmd = NL80211_CMD_LEAVE_MESH,
5294 .doit = nl80211_leave_mesh,
5295 .policy = nl80211_policy,
5296 .flags = GENL_ADMIN_PERM,
5297 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5298 NL80211_FLAG_NEED_RTNL,
5299 },
Johannes Berg55682962007-09-20 13:09:35 -04005300};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005301
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005302static struct genl_multicast_group nl80211_mlme_mcgrp = {
5303 .name = "mlme",
5304};
Johannes Berg55682962007-09-20 13:09:35 -04005305
5306/* multicast groups */
5307static struct genl_multicast_group nl80211_config_mcgrp = {
5308 .name = "config",
5309};
Johannes Berg2a519312009-02-10 21:25:55 +01005310static struct genl_multicast_group nl80211_scan_mcgrp = {
5311 .name = "scan",
5312};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005313static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5314 .name = "regulatory",
5315};
Johannes Berg55682962007-09-20 13:09:35 -04005316
5317/* notification functions */
5318
5319void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5320{
5321 struct sk_buff *msg;
5322
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005323 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005324 if (!msg)
5325 return;
5326
5327 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5328 nlmsg_free(msg);
5329 return;
5330 }
5331
Johannes Berg463d0182009-07-14 00:33:35 +02005332 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5333 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005334}
5335
Johannes Berg362a4152009-05-24 16:43:15 +02005336static int nl80211_add_scan_req(struct sk_buff *msg,
5337 struct cfg80211_registered_device *rdev)
5338{
5339 struct cfg80211_scan_request *req = rdev->scan_req;
5340 struct nlattr *nest;
5341 int i;
5342
Johannes Berg667503d2009-07-07 03:56:11 +02005343 ASSERT_RDEV_LOCK(rdev);
5344
Johannes Berg362a4152009-05-24 16:43:15 +02005345 if (WARN_ON(!req))
5346 return 0;
5347
5348 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5349 if (!nest)
5350 goto nla_put_failure;
5351 for (i = 0; i < req->n_ssids; i++)
5352 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5353 nla_nest_end(msg, nest);
5354
5355 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5356 if (!nest)
5357 goto nla_put_failure;
5358 for (i = 0; i < req->n_channels; i++)
5359 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5360 nla_nest_end(msg, nest);
5361
5362 if (req->ie)
5363 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5364
5365 return 0;
5366 nla_put_failure:
5367 return -ENOBUFS;
5368}
5369
Johannes Berga538e2d2009-06-16 19:56:42 +02005370static int nl80211_send_scan_msg(struct sk_buff *msg,
5371 struct cfg80211_registered_device *rdev,
5372 struct net_device *netdev,
5373 u32 pid, u32 seq, int flags,
5374 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005375{
5376 void *hdr;
5377
5378 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5379 if (!hdr)
5380 return -1;
5381
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005382 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005383 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5384
Johannes Berg362a4152009-05-24 16:43:15 +02005385 /* ignore errors and send incomplete event anyway */
5386 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005387
5388 return genlmsg_end(msg, hdr);
5389
5390 nla_put_failure:
5391 genlmsg_cancel(msg, hdr);
5392 return -EMSGSIZE;
5393}
5394
Johannes Berga538e2d2009-06-16 19:56:42 +02005395void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5396 struct net_device *netdev)
5397{
5398 struct sk_buff *msg;
5399
5400 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5401 if (!msg)
5402 return;
5403
5404 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5405 NL80211_CMD_TRIGGER_SCAN) < 0) {
5406 nlmsg_free(msg);
5407 return;
5408 }
5409
Johannes Berg463d0182009-07-14 00:33:35 +02005410 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5411 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005412}
5413
Johannes Berg2a519312009-02-10 21:25:55 +01005414void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5415 struct net_device *netdev)
5416{
5417 struct sk_buff *msg;
5418
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005419 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005420 if (!msg)
5421 return;
5422
Johannes Berga538e2d2009-06-16 19:56:42 +02005423 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5424 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005425 nlmsg_free(msg);
5426 return;
5427 }
5428
Johannes Berg463d0182009-07-14 00:33:35 +02005429 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5430 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005431}
5432
5433void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5434 struct net_device *netdev)
5435{
5436 struct sk_buff *msg;
5437
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005438 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005439 if (!msg)
5440 return;
5441
Johannes Berga538e2d2009-06-16 19:56:42 +02005442 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5443 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005444 nlmsg_free(msg);
5445 return;
5446 }
5447
Johannes Berg463d0182009-07-14 00:33:35 +02005448 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5449 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005450}
5451
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005452/*
5453 * This can happen on global regulatory changes or device specific settings
5454 * based on custom world regulatory domains.
5455 */
5456void nl80211_send_reg_change_event(struct regulatory_request *request)
5457{
5458 struct sk_buff *msg;
5459 void *hdr;
5460
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005461 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005462 if (!msg)
5463 return;
5464
5465 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5466 if (!hdr) {
5467 nlmsg_free(msg);
5468 return;
5469 }
5470
5471 /* Userspace can always count this one always being set */
5472 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5473
5474 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5475 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5476 NL80211_REGDOM_TYPE_WORLD);
5477 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5478 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5479 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5480 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5481 request->intersect)
5482 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5483 NL80211_REGDOM_TYPE_INTERSECTION);
5484 else {
5485 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5486 NL80211_REGDOM_TYPE_COUNTRY);
5487 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5488 }
5489
5490 if (wiphy_idx_valid(request->wiphy_idx))
5491 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5492
5493 if (genlmsg_end(msg, hdr) < 0) {
5494 nlmsg_free(msg);
5495 return;
5496 }
5497
Johannes Bergbc43b282009-07-25 10:54:13 +02005498 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005499 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005500 GFP_ATOMIC);
5501 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005502
5503 return;
5504
5505nla_put_failure:
5506 genlmsg_cancel(msg, hdr);
5507 nlmsg_free(msg);
5508}
5509
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005510static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5511 struct net_device *netdev,
5512 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005513 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005514{
5515 struct sk_buff *msg;
5516 void *hdr;
5517
Johannes Berge6d6e342009-07-01 21:26:47 +02005518 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005519 if (!msg)
5520 return;
5521
5522 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5523 if (!hdr) {
5524 nlmsg_free(msg);
5525 return;
5526 }
5527
5528 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5529 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5530 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5531
5532 if (genlmsg_end(msg, hdr) < 0) {
5533 nlmsg_free(msg);
5534 return;
5535 }
5536
Johannes Berg463d0182009-07-14 00:33:35 +02005537 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5538 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005539 return;
5540
5541 nla_put_failure:
5542 genlmsg_cancel(msg, hdr);
5543 nlmsg_free(msg);
5544}
5545
5546void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005547 struct net_device *netdev, const u8 *buf,
5548 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005549{
5550 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005551 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005552}
5553
5554void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5555 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005556 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005557{
Johannes Berge6d6e342009-07-01 21:26:47 +02005558 nl80211_send_mlme_event(rdev, netdev, buf, len,
5559 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005560}
5561
Jouni Malinen53b46b82009-03-27 20:53:56 +02005562void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005563 struct net_device *netdev, const u8 *buf,
5564 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005565{
5566 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005567 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005568}
5569
Jouni Malinen53b46b82009-03-27 20:53:56 +02005570void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5571 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005572 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005573{
5574 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005575 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005576}
5577
Jouni Malinencf4e5942010-12-16 00:52:40 +02005578void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
5579 struct net_device *netdev, const u8 *buf,
5580 size_t len, gfp_t gfp)
5581{
5582 nl80211_send_mlme_event(rdev, netdev, buf, len,
5583 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
5584}
5585
5586void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
5587 struct net_device *netdev, const u8 *buf,
5588 size_t len, gfp_t gfp)
5589{
5590 nl80211_send_mlme_event(rdev, netdev, buf, len,
5591 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
5592}
5593
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005594static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5595 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005596 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005597{
5598 struct sk_buff *msg;
5599 void *hdr;
5600
Johannes Berge6d6e342009-07-01 21:26:47 +02005601 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005602 if (!msg)
5603 return;
5604
5605 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5606 if (!hdr) {
5607 nlmsg_free(msg);
5608 return;
5609 }
5610
5611 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5612 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5613 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5614 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5615
5616 if (genlmsg_end(msg, hdr) < 0) {
5617 nlmsg_free(msg);
5618 return;
5619 }
5620
Johannes Berg463d0182009-07-14 00:33:35 +02005621 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5622 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005623 return;
5624
5625 nla_put_failure:
5626 genlmsg_cancel(msg, hdr);
5627 nlmsg_free(msg);
5628}
5629
5630void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005631 struct net_device *netdev, const u8 *addr,
5632 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005633{
5634 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005635 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005636}
5637
5638void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005639 struct net_device *netdev, const u8 *addr,
5640 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005641{
Johannes Berge6d6e342009-07-01 21:26:47 +02005642 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5643 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005644}
5645
Samuel Ortizb23aa672009-07-01 21:26:54 +02005646void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5647 struct net_device *netdev, const u8 *bssid,
5648 const u8 *req_ie, size_t req_ie_len,
5649 const u8 *resp_ie, size_t resp_ie_len,
5650 u16 status, gfp_t gfp)
5651{
5652 struct sk_buff *msg;
5653 void *hdr;
5654
5655 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5656 if (!msg)
5657 return;
5658
5659 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5660 if (!hdr) {
5661 nlmsg_free(msg);
5662 return;
5663 }
5664
5665 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5666 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5667 if (bssid)
5668 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5669 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5670 if (req_ie)
5671 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5672 if (resp_ie)
5673 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5674
5675 if (genlmsg_end(msg, hdr) < 0) {
5676 nlmsg_free(msg);
5677 return;
5678 }
5679
Johannes Berg463d0182009-07-14 00:33:35 +02005680 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5681 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005682 return;
5683
5684 nla_put_failure:
5685 genlmsg_cancel(msg, hdr);
5686 nlmsg_free(msg);
5687
5688}
5689
5690void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5691 struct net_device *netdev, const u8 *bssid,
5692 const u8 *req_ie, size_t req_ie_len,
5693 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5694{
5695 struct sk_buff *msg;
5696 void *hdr;
5697
5698 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5699 if (!msg)
5700 return;
5701
5702 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5703 if (!hdr) {
5704 nlmsg_free(msg);
5705 return;
5706 }
5707
5708 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5709 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5710 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5711 if (req_ie)
5712 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5713 if (resp_ie)
5714 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5715
5716 if (genlmsg_end(msg, hdr) < 0) {
5717 nlmsg_free(msg);
5718 return;
5719 }
5720
Johannes Berg463d0182009-07-14 00:33:35 +02005721 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5722 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005723 return;
5724
5725 nla_put_failure:
5726 genlmsg_cancel(msg, hdr);
5727 nlmsg_free(msg);
5728
5729}
5730
5731void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5732 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005733 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005734{
5735 struct sk_buff *msg;
5736 void *hdr;
5737
Johannes Berg667503d2009-07-07 03:56:11 +02005738 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005739 if (!msg)
5740 return;
5741
5742 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5743 if (!hdr) {
5744 nlmsg_free(msg);
5745 return;
5746 }
5747
5748 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5749 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5750 if (from_ap && reason)
5751 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5752 if (from_ap)
5753 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5754 if (ie)
5755 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5756
5757 if (genlmsg_end(msg, hdr) < 0) {
5758 nlmsg_free(msg);
5759 return;
5760 }
5761
Johannes Berg463d0182009-07-14 00:33:35 +02005762 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5763 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005764 return;
5765
5766 nla_put_failure:
5767 genlmsg_cancel(msg, hdr);
5768 nlmsg_free(msg);
5769
5770}
5771
Johannes Berg04a773a2009-04-19 21:24:32 +02005772void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5773 struct net_device *netdev, const u8 *bssid,
5774 gfp_t gfp)
5775{
5776 struct sk_buff *msg;
5777 void *hdr;
5778
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005779 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005780 if (!msg)
5781 return;
5782
5783 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5784 if (!hdr) {
5785 nlmsg_free(msg);
5786 return;
5787 }
5788
5789 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5790 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5791 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5792
5793 if (genlmsg_end(msg, hdr) < 0) {
5794 nlmsg_free(msg);
5795 return;
5796 }
5797
Johannes Berg463d0182009-07-14 00:33:35 +02005798 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5799 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005800 return;
5801
5802 nla_put_failure:
5803 genlmsg_cancel(msg, hdr);
5804 nlmsg_free(msg);
5805}
5806
Jouni Malinena3b8b052009-03-27 21:59:49 +02005807void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5808 struct net_device *netdev, const u8 *addr,
5809 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005810 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005811{
5812 struct sk_buff *msg;
5813 void *hdr;
5814
Johannes Berge6d6e342009-07-01 21:26:47 +02005815 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005816 if (!msg)
5817 return;
5818
5819 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5820 if (!hdr) {
5821 nlmsg_free(msg);
5822 return;
5823 }
5824
5825 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5826 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5827 if (addr)
5828 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5829 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5830 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5831 if (tsc)
5832 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5833
5834 if (genlmsg_end(msg, hdr) < 0) {
5835 nlmsg_free(msg);
5836 return;
5837 }
5838
Johannes Berg463d0182009-07-14 00:33:35 +02005839 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5840 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005841 return;
5842
5843 nla_put_failure:
5844 genlmsg_cancel(msg, hdr);
5845 nlmsg_free(msg);
5846}
5847
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005848void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5849 struct ieee80211_channel *channel_before,
5850 struct ieee80211_channel *channel_after)
5851{
5852 struct sk_buff *msg;
5853 void *hdr;
5854 struct nlattr *nl_freq;
5855
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005856 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005857 if (!msg)
5858 return;
5859
5860 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5861 if (!hdr) {
5862 nlmsg_free(msg);
5863 return;
5864 }
5865
5866 /*
5867 * Since we are applying the beacon hint to a wiphy we know its
5868 * wiphy_idx is valid
5869 */
5870 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5871
5872 /* Before */
5873 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5874 if (!nl_freq)
5875 goto nla_put_failure;
5876 if (nl80211_msg_put_channel(msg, channel_before))
5877 goto nla_put_failure;
5878 nla_nest_end(msg, nl_freq);
5879
5880 /* After */
5881 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5882 if (!nl_freq)
5883 goto nla_put_failure;
5884 if (nl80211_msg_put_channel(msg, channel_after))
5885 goto nla_put_failure;
5886 nla_nest_end(msg, nl_freq);
5887
5888 if (genlmsg_end(msg, hdr) < 0) {
5889 nlmsg_free(msg);
5890 return;
5891 }
5892
Johannes Berg463d0182009-07-14 00:33:35 +02005893 rcu_read_lock();
5894 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5895 GFP_ATOMIC);
5896 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005897
5898 return;
5899
5900nla_put_failure:
5901 genlmsg_cancel(msg, hdr);
5902 nlmsg_free(msg);
5903}
5904
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005905static void nl80211_send_remain_on_chan_event(
5906 int cmd, struct cfg80211_registered_device *rdev,
5907 struct net_device *netdev, u64 cookie,
5908 struct ieee80211_channel *chan,
5909 enum nl80211_channel_type channel_type,
5910 unsigned int duration, gfp_t gfp)
5911{
5912 struct sk_buff *msg;
5913 void *hdr;
5914
5915 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5916 if (!msg)
5917 return;
5918
5919 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5920 if (!hdr) {
5921 nlmsg_free(msg);
5922 return;
5923 }
5924
5925 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5926 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5927 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5928 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5929 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5930
5931 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5932 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5933
5934 if (genlmsg_end(msg, hdr) < 0) {
5935 nlmsg_free(msg);
5936 return;
5937 }
5938
5939 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5940 nl80211_mlme_mcgrp.id, gfp);
5941 return;
5942
5943 nla_put_failure:
5944 genlmsg_cancel(msg, hdr);
5945 nlmsg_free(msg);
5946}
5947
5948void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5949 struct net_device *netdev, u64 cookie,
5950 struct ieee80211_channel *chan,
5951 enum nl80211_channel_type channel_type,
5952 unsigned int duration, gfp_t gfp)
5953{
5954 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5955 rdev, netdev, cookie, chan,
5956 channel_type, duration, gfp);
5957}
5958
5959void nl80211_send_remain_on_channel_cancel(
5960 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5961 u64 cookie, struct ieee80211_channel *chan,
5962 enum nl80211_channel_type channel_type, gfp_t gfp)
5963{
5964 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5965 rdev, netdev, cookie, chan,
5966 channel_type, 0, gfp);
5967}
5968
Johannes Berg98b62182009-12-23 13:15:44 +01005969void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5970 struct net_device *dev, const u8 *mac_addr,
5971 struct station_info *sinfo, gfp_t gfp)
5972{
5973 struct sk_buff *msg;
5974
5975 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5976 if (!msg)
5977 return;
5978
5979 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5980 nlmsg_free(msg);
5981 return;
5982 }
5983
5984 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5985 nl80211_mlme_mcgrp.id, gfp);
5986}
5987
Jouni Malinenec15e682011-03-23 15:29:52 +02005988void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
5989 struct net_device *dev, const u8 *mac_addr,
5990 gfp_t gfp)
5991{
5992 struct sk_buff *msg;
5993 void *hdr;
5994
5995 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5996 if (!msg)
5997 return;
5998
5999 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
6000 if (!hdr) {
6001 nlmsg_free(msg);
6002 return;
6003 }
6004
6005 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
6006 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
6007
6008 if (genlmsg_end(msg, hdr) < 0) {
6009 nlmsg_free(msg);
6010 return;
6011 }
6012
6013 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6014 nl80211_mlme_mcgrp.id, gfp);
6015 return;
6016
6017 nla_put_failure:
6018 genlmsg_cancel(msg, hdr);
6019 nlmsg_free(msg);
6020}
6021
Johannes Berg2e161f72010-08-12 15:38:38 +02006022int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
6023 struct net_device *netdev, u32 nlpid,
6024 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006025{
6026 struct sk_buff *msg;
6027 void *hdr;
6028 int err;
6029
6030 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6031 if (!msg)
6032 return -ENOMEM;
6033
Johannes Berg2e161f72010-08-12 15:38:38 +02006034 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02006035 if (!hdr) {
6036 nlmsg_free(msg);
6037 return -ENOMEM;
6038 }
6039
6040 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6041 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6042 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
6043 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6044
6045 err = genlmsg_end(msg, hdr);
6046 if (err < 0) {
6047 nlmsg_free(msg);
6048 return err;
6049 }
6050
6051 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
6052 if (err < 0)
6053 return err;
6054 return 0;
6055
6056 nla_put_failure:
6057 genlmsg_cancel(msg, hdr);
6058 nlmsg_free(msg);
6059 return -ENOBUFS;
6060}
6061
Johannes Berg2e161f72010-08-12 15:38:38 +02006062void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
6063 struct net_device *netdev, u64 cookie,
6064 const u8 *buf, size_t len, bool ack,
6065 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006066{
6067 struct sk_buff *msg;
6068 void *hdr;
6069
6070 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6071 if (!msg)
6072 return;
6073
Johannes Berg2e161f72010-08-12 15:38:38 +02006074 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02006075 if (!hdr) {
6076 nlmsg_free(msg);
6077 return;
6078 }
6079
6080 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6081 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6082 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6083 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6084 if (ack)
6085 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
6086
6087 if (genlmsg_end(msg, hdr) < 0) {
6088 nlmsg_free(msg);
6089 return;
6090 }
6091
6092 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
6093 return;
6094
6095 nla_put_failure:
6096 genlmsg_cancel(msg, hdr);
6097 nlmsg_free(msg);
6098}
6099
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006100void
6101nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
6102 struct net_device *netdev,
6103 enum nl80211_cqm_rssi_threshold_event rssi_event,
6104 gfp_t gfp)
6105{
6106 struct sk_buff *msg;
6107 struct nlattr *pinfoattr;
6108 void *hdr;
6109
6110 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6111 if (!msg)
6112 return;
6113
6114 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6115 if (!hdr) {
6116 nlmsg_free(msg);
6117 return;
6118 }
6119
6120 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6121 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6122
6123 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6124 if (!pinfoattr)
6125 goto nla_put_failure;
6126
6127 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
6128 rssi_event);
6129
6130 nla_nest_end(msg, pinfoattr);
6131
6132 if (genlmsg_end(msg, hdr) < 0) {
6133 nlmsg_free(msg);
6134 return;
6135 }
6136
6137 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6138 nl80211_mlme_mcgrp.id, gfp);
6139 return;
6140
6141 nla_put_failure:
6142 genlmsg_cancel(msg, hdr);
6143 nlmsg_free(msg);
6144}
6145
Johannes Bergc063dbf2010-11-24 08:10:05 +01006146void
6147nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
6148 struct net_device *netdev, const u8 *peer,
6149 u32 num_packets, gfp_t gfp)
6150{
6151 struct sk_buff *msg;
6152 struct nlattr *pinfoattr;
6153 void *hdr;
6154
6155 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6156 if (!msg)
6157 return;
6158
6159 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6160 if (!hdr) {
6161 nlmsg_free(msg);
6162 return;
6163 }
6164
6165 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6166 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6167 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
6168
6169 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6170 if (!pinfoattr)
6171 goto nla_put_failure;
6172
6173 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
6174
6175 nla_nest_end(msg, pinfoattr);
6176
6177 if (genlmsg_end(msg, hdr) < 0) {
6178 nlmsg_free(msg);
6179 return;
6180 }
6181
6182 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6183 nl80211_mlme_mcgrp.id, gfp);
6184 return;
6185
6186 nla_put_failure:
6187 genlmsg_cancel(msg, hdr);
6188 nlmsg_free(msg);
6189}
6190
Jouni Malinen026331c2010-02-15 12:53:10 +02006191static int nl80211_netlink_notify(struct notifier_block * nb,
6192 unsigned long state,
6193 void *_notify)
6194{
6195 struct netlink_notify *notify = _notify;
6196 struct cfg80211_registered_device *rdev;
6197 struct wireless_dev *wdev;
6198
6199 if (state != NETLINK_URELEASE)
6200 return NOTIFY_DONE;
6201
6202 rcu_read_lock();
6203
6204 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
6205 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02006206 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02006207
6208 rcu_read_unlock();
6209
6210 return NOTIFY_DONE;
6211}
6212
6213static struct notifier_block nl80211_netlink_notifier = {
6214 .notifier_call = nl80211_netlink_notify,
6215};
6216
Johannes Berg55682962007-09-20 13:09:35 -04006217/* initialisation/exit functions */
6218
6219int nl80211_init(void)
6220{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006221 int err;
Johannes Berg55682962007-09-20 13:09:35 -04006222
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006223 err = genl_register_family_with_ops(&nl80211_fam,
6224 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04006225 if (err)
6226 return err;
6227
Johannes Berg55682962007-09-20 13:09:35 -04006228 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6229 if (err)
6230 goto err_out;
6231
Johannes Berg2a519312009-02-10 21:25:55 +01006232 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6233 if (err)
6234 goto err_out;
6235
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006236 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6237 if (err)
6238 goto err_out;
6239
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006240 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6241 if (err)
6242 goto err_out;
6243
Johannes Bergaff89a92009-07-01 21:26:51 +02006244#ifdef CONFIG_NL80211_TESTMODE
6245 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6246 if (err)
6247 goto err_out;
6248#endif
6249
Jouni Malinen026331c2010-02-15 12:53:10 +02006250 err = netlink_register_notifier(&nl80211_netlink_notifier);
6251 if (err)
6252 goto err_out;
6253
Johannes Berg55682962007-09-20 13:09:35 -04006254 return 0;
6255 err_out:
6256 genl_unregister_family(&nl80211_fam);
6257 return err;
6258}
6259
6260void nl80211_exit(void)
6261{
Jouni Malinen026331c2010-02-15 12:53:10 +02006262 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006263 genl_unregister_family(&nl80211_fam);
6264}