blob: 8d2f5f8d8080bbd7bcd65b653f98c7725ce63f2d [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 Randolf7f531e02010-12-16 11:30:22 +0900608 if ((dev->wiphy.available_antennas_tx ||
609 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900610 u32 tx_ant = 0, rx_ant = 0;
611 int res;
612 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
613 if (!res) {
614 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
615 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
616 }
617 }
618
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700619 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
620 if (!nl_modes)
621 goto nla_put_failure;
622
623 i = 0;
624 while (ifmodes) {
625 if (ifmodes & 1)
626 NLA_PUT_FLAG(msg, i);
627 ifmodes >>= 1;
628 i++;
629 }
630
631 nla_nest_end(msg, nl_modes);
632
Johannes Bergee688b002008-01-24 19:38:39 +0100633 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
634 if (!nl_bands)
635 goto nla_put_failure;
636
637 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
638 if (!dev->wiphy.bands[band])
639 continue;
640
641 nl_band = nla_nest_start(msg, band);
642 if (!nl_band)
643 goto nla_put_failure;
644
Johannes Bergd51626d2008-10-09 12:20:13 +0200645 /* add HT info */
646 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
647 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
648 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
649 &dev->wiphy.bands[band]->ht_cap.mcs);
650 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
651 dev->wiphy.bands[band]->ht_cap.cap);
652 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
653 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
654 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
655 dev->wiphy.bands[band]->ht_cap.ampdu_density);
656 }
657
Johannes Bergee688b002008-01-24 19:38:39 +0100658 /* add frequencies */
659 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
660 if (!nl_freqs)
661 goto nla_put_failure;
662
663 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
664 nl_freq = nla_nest_start(msg, i);
665 if (!nl_freq)
666 goto nla_put_failure;
667
668 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100669
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400670 if (nl80211_msg_put_channel(msg, chan))
671 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200672
Johannes Bergee688b002008-01-24 19:38:39 +0100673 nla_nest_end(msg, nl_freq);
674 }
675
676 nla_nest_end(msg, nl_freqs);
677
678 /* add bitrates */
679 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
680 if (!nl_rates)
681 goto nla_put_failure;
682
683 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
684 nl_rate = nla_nest_start(msg, i);
685 if (!nl_rate)
686 goto nla_put_failure;
687
688 rate = &dev->wiphy.bands[band]->bitrates[i];
689 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
690 rate->bitrate);
691 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
692 NLA_PUT_FLAG(msg,
693 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
694
695 nla_nest_end(msg, nl_rate);
696 }
697
698 nla_nest_end(msg, nl_rates);
699
700 nla_nest_end(msg, nl_band);
701 }
702 nla_nest_end(msg, nl_bands);
703
Johannes Berg8fdc6212009-03-14 09:34:01 +0100704 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
705 if (!nl_cmds)
706 goto nla_put_failure;
707
708 i = 0;
709#define CMD(op, n) \
710 do { \
711 if (dev->ops->op) { \
712 i++; \
713 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
714 } \
715 } while (0)
716
717 CMD(add_virtual_intf, NEW_INTERFACE);
718 CMD(change_virtual_intf, SET_INTERFACE);
719 CMD(add_key, NEW_KEY);
720 CMD(add_beacon, NEW_BEACON);
721 CMD(add_station, NEW_STATION);
722 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800723 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100724 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200725 CMD(auth, AUTHENTICATE);
726 CMD(assoc, ASSOCIATE);
727 CMD(deauth, DEAUTHENTICATE);
728 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200729 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100730 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100731 CMD(set_pmksa, SET_PMKSA);
732 CMD(del_pmksa, DEL_PMKSA);
733 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100734 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200735 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200736 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100737 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100738 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200739 i++;
740 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
741 }
Johannes Bergf444de02010-05-05 15:25:02 +0200742 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400743 CMD(set_wds_peer, SET_WDS_PEER);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100744
745#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200746
Johannes Berg6829c8782009-07-02 09:13:27 +0200747 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200748 i++;
749 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
750 }
751
Johannes Berg6829c8782009-07-02 09:13:27 +0200752 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200753 i++;
754 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
755 }
756
Johannes Berg8fdc6212009-03-14 09:34:01 +0100757 nla_nest_end(msg, nl_cmds);
758
Johannes Berga2939112010-12-14 17:54:28 +0100759 if (dev->ops->remain_on_channel)
760 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
761 dev->wiphy.max_remain_on_channel_duration);
762
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100763 /* for now at least assume all drivers have it */
764 if (dev->ops->mgmt_tx)
765 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
766
Johannes Berg2e161f72010-08-12 15:38:38 +0200767 if (mgmt_stypes) {
768 u16 stypes;
769 struct nlattr *nl_ftypes, *nl_ifs;
770 enum nl80211_iftype ift;
771
772 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
773 if (!nl_ifs)
774 goto nla_put_failure;
775
776 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
777 nl_ftypes = nla_nest_start(msg, ift);
778 if (!nl_ftypes)
779 goto nla_put_failure;
780 i = 0;
781 stypes = mgmt_stypes[ift].tx;
782 while (stypes) {
783 if (stypes & 1)
784 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
785 (i << 4) | IEEE80211_FTYPE_MGMT);
786 stypes >>= 1;
787 i++;
788 }
789 nla_nest_end(msg, nl_ftypes);
790 }
791
Johannes Berg74b70a42010-08-24 12:15:53 +0200792 nla_nest_end(msg, nl_ifs);
793
Johannes Berg2e161f72010-08-12 15:38:38 +0200794 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
795 if (!nl_ifs)
796 goto nla_put_failure;
797
798 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
799 nl_ftypes = nla_nest_start(msg, ift);
800 if (!nl_ftypes)
801 goto nla_put_failure;
802 i = 0;
803 stypes = mgmt_stypes[ift].rx;
804 while (stypes) {
805 if (stypes & 1)
806 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
807 (i << 4) | IEEE80211_FTYPE_MGMT);
808 stypes >>= 1;
809 i++;
810 }
811 nla_nest_end(msg, nl_ftypes);
812 }
813 nla_nest_end(msg, nl_ifs);
814 }
815
Johannes Berg55682962007-09-20 13:09:35 -0400816 return genlmsg_end(msg, hdr);
817
818 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700819 genlmsg_cancel(msg, hdr);
820 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400821}
822
823static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
824{
825 int idx = 0;
826 int start = cb->args[0];
827 struct cfg80211_registered_device *dev;
828
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500829 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200830 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200831 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
832 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200833 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400834 continue;
835 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
836 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200837 dev) < 0) {
838 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400839 break;
Julius Volzb4637272008-07-08 14:02:19 +0200840 }
Johannes Berg55682962007-09-20 13:09:35 -0400841 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500842 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400843
844 cb->args[0] = idx;
845
846 return skb->len;
847}
848
849static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
850{
851 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200852 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400853
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700854 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400855 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200856 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400857
Johannes Berg4c476992010-10-04 21:36:35 +0200858 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
859 nlmsg_free(msg);
860 return -ENOBUFS;
861 }
Johannes Berg55682962007-09-20 13:09:35 -0400862
Johannes Berg134e6372009-07-10 09:51:34 +0000863 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400864}
865
Jouni Malinen31888482008-10-30 16:59:24 +0200866static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
867 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
868 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
869 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
870 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
871 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
872};
873
874static int parse_txq_params(struct nlattr *tb[],
875 struct ieee80211_txq_params *txq_params)
876{
877 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
878 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
879 !tb[NL80211_TXQ_ATTR_AIFS])
880 return -EINVAL;
881
882 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
883 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
884 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
885 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
886 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
887
888 return 0;
889}
890
Johannes Bergf444de02010-05-05 15:25:02 +0200891static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
892{
893 /*
894 * You can only set the channel explicitly for AP, mesh
895 * and WDS type interfaces; all others have their channel
896 * managed via their respective "establish a connection"
897 * command (connect, join, ...)
898 *
899 * Monitors are special as they are normally slaved to
900 * whatever else is going on, so they behave as though
901 * you tried setting the wiphy channel itself.
902 */
903 return !wdev ||
904 wdev->iftype == NL80211_IFTYPE_AP ||
905 wdev->iftype == NL80211_IFTYPE_WDS ||
906 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200907 wdev->iftype == NL80211_IFTYPE_MONITOR ||
908 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200909}
910
911static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
912 struct wireless_dev *wdev,
913 struct genl_info *info)
914{
915 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
916 u32 freq;
917 int result;
918
919 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
920 return -EINVAL;
921
922 if (!nl80211_can_set_dev_channel(wdev))
923 return -EOPNOTSUPP;
924
925 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
926 channel_type = nla_get_u32(info->attrs[
927 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
928 if (channel_type != NL80211_CHAN_NO_HT &&
929 channel_type != NL80211_CHAN_HT20 &&
930 channel_type != NL80211_CHAN_HT40PLUS &&
931 channel_type != NL80211_CHAN_HT40MINUS)
932 return -EINVAL;
933 }
934
935 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
936
937 mutex_lock(&rdev->devlist_mtx);
938 if (wdev) {
939 wdev_lock(wdev);
940 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
941 wdev_unlock(wdev);
942 } else {
943 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
944 }
945 mutex_unlock(&rdev->devlist_mtx);
946
947 return result;
948}
949
950static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
951{
Johannes Berg4c476992010-10-04 21:36:35 +0200952 struct cfg80211_registered_device *rdev = info->user_ptr[0];
953 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +0200954
Johannes Berg4c476992010-10-04 21:36:35 +0200955 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +0200956}
957
Bill Jordane8347eb2010-10-01 13:54:28 -0400958static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
959{
Johannes Berg43b19952010-10-07 13:10:30 +0200960 struct cfg80211_registered_device *rdev = info->user_ptr[0];
961 struct net_device *dev = info->user_ptr[1];
962 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +0200963 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -0400964
965 if (!info->attrs[NL80211_ATTR_MAC])
966 return -EINVAL;
967
Johannes Berg43b19952010-10-07 13:10:30 +0200968 if (netif_running(dev))
969 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -0400970
Johannes Berg43b19952010-10-07 13:10:30 +0200971 if (!rdev->ops->set_wds_peer)
972 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400973
Johannes Berg43b19952010-10-07 13:10:30 +0200974 if (wdev->iftype != NL80211_IFTYPE_WDS)
975 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400976
977 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +0200978 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -0400979}
980
981
Johannes Berg55682962007-09-20 13:09:35 -0400982static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
983{
984 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200985 struct net_device *netdev = NULL;
986 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -0400987 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200988 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200989 u32 changed;
990 u8 retry_short = 0, retry_long = 0;
991 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100992 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400993
Johannes Bergf444de02010-05-05 15:25:02 +0200994 /*
995 * Try to find the wiphy and netdev. Normally this
996 * function shouldn't need the netdev, but this is
997 * done for backward compatibility -- previously
998 * setting the channel was done per wiphy, but now
999 * it is per netdev. Previous userland like hostapd
1000 * also passed a netdev to set_wiphy, so that it is
1001 * possible to let that go to the right netdev!
1002 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001003 mutex_lock(&cfg80211_mutex);
1004
Johannes Bergf444de02010-05-05 15:25:02 +02001005 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1006 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1007
1008 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1009 if (netdev && netdev->ieee80211_ptr) {
1010 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1011 mutex_lock(&rdev->mtx);
1012 } else
1013 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001014 }
1015
Johannes Bergf444de02010-05-05 15:25:02 +02001016 if (!netdev) {
1017 rdev = __cfg80211_rdev_from_info(info);
1018 if (IS_ERR(rdev)) {
1019 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001020 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001021 }
1022 wdev = NULL;
1023 netdev = NULL;
1024 result = 0;
1025
1026 mutex_lock(&rdev->mtx);
1027 } else if (netif_running(netdev) &&
1028 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1029 wdev = netdev->ieee80211_ptr;
1030 else
1031 wdev = NULL;
1032
1033 /*
1034 * end workaround code, by now the rdev is available
1035 * and locked, and wdev may or may not be NULL.
1036 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001037
1038 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001039 result = cfg80211_dev_rename(
1040 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001041
1042 mutex_unlock(&cfg80211_mutex);
1043
1044 if (result)
1045 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001046
Jouni Malinen31888482008-10-30 16:59:24 +02001047 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1048 struct ieee80211_txq_params txq_params;
1049 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1050
1051 if (!rdev->ops->set_txq_params) {
1052 result = -EOPNOTSUPP;
1053 goto bad_res;
1054 }
1055
1056 nla_for_each_nested(nl_txq_params,
1057 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1058 rem_txq_params) {
1059 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1060 nla_data(nl_txq_params),
1061 nla_len(nl_txq_params),
1062 txq_params_policy);
1063 result = parse_txq_params(tb, &txq_params);
1064 if (result)
1065 goto bad_res;
1066
1067 result = rdev->ops->set_txq_params(&rdev->wiphy,
1068 &txq_params);
1069 if (result)
1070 goto bad_res;
1071 }
1072 }
1073
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001074 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001075 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001076 if (result)
1077 goto bad_res;
1078 }
1079
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001080 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1081 enum nl80211_tx_power_setting type;
1082 int idx, mbm = 0;
1083
1084 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001085 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001086 goto bad_res;
1087 }
1088
1089 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1090 type = nla_get_u32(info->attrs[idx]);
1091
1092 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1093 (type != NL80211_TX_POWER_AUTOMATIC)) {
1094 result = -EINVAL;
1095 goto bad_res;
1096 }
1097
1098 if (type != NL80211_TX_POWER_AUTOMATIC) {
1099 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1100 mbm = nla_get_u32(info->attrs[idx]);
1101 }
1102
1103 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1104 if (result)
1105 goto bad_res;
1106 }
1107
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001108 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1109 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1110 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001111 if ((!rdev->wiphy.available_antennas_tx &&
1112 !rdev->wiphy.available_antennas_rx) ||
1113 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001114 result = -EOPNOTSUPP;
1115 goto bad_res;
1116 }
1117
1118 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1119 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1120
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001121 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001122 * available antenna masks, except for the "all" mask */
1123 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1124 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001125 result = -EINVAL;
1126 goto bad_res;
1127 }
1128
Bruno Randolf7f531e02010-12-16 11:30:22 +09001129 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1130 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001131
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001132 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1133 if (result)
1134 goto bad_res;
1135 }
1136
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001137 changed = 0;
1138
1139 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1140 retry_short = nla_get_u8(
1141 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1142 if (retry_short == 0) {
1143 result = -EINVAL;
1144 goto bad_res;
1145 }
1146 changed |= WIPHY_PARAM_RETRY_SHORT;
1147 }
1148
1149 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1150 retry_long = nla_get_u8(
1151 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1152 if (retry_long == 0) {
1153 result = -EINVAL;
1154 goto bad_res;
1155 }
1156 changed |= WIPHY_PARAM_RETRY_LONG;
1157 }
1158
1159 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1160 frag_threshold = nla_get_u32(
1161 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1162 if (frag_threshold < 256) {
1163 result = -EINVAL;
1164 goto bad_res;
1165 }
1166 if (frag_threshold != (u32) -1) {
1167 /*
1168 * Fragments (apart from the last one) are required to
1169 * have even length. Make the fragmentation code
1170 * simpler by stripping LSB should someone try to use
1171 * odd threshold value.
1172 */
1173 frag_threshold &= ~0x1;
1174 }
1175 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1176 }
1177
1178 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1179 rts_threshold = nla_get_u32(
1180 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1181 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1182 }
1183
Lukáš Turek81077e82009-12-21 22:50:47 +01001184 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1185 coverage_class = nla_get_u8(
1186 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1187 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1188 }
1189
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001190 if (changed) {
1191 u8 old_retry_short, old_retry_long;
1192 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001193 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001194
1195 if (!rdev->ops->set_wiphy_params) {
1196 result = -EOPNOTSUPP;
1197 goto bad_res;
1198 }
1199
1200 old_retry_short = rdev->wiphy.retry_short;
1201 old_retry_long = rdev->wiphy.retry_long;
1202 old_frag_threshold = rdev->wiphy.frag_threshold;
1203 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001204 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001205
1206 if (changed & WIPHY_PARAM_RETRY_SHORT)
1207 rdev->wiphy.retry_short = retry_short;
1208 if (changed & WIPHY_PARAM_RETRY_LONG)
1209 rdev->wiphy.retry_long = retry_long;
1210 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1211 rdev->wiphy.frag_threshold = frag_threshold;
1212 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1213 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001214 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1215 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001216
1217 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1218 if (result) {
1219 rdev->wiphy.retry_short = old_retry_short;
1220 rdev->wiphy.retry_long = old_retry_long;
1221 rdev->wiphy.frag_threshold = old_frag_threshold;
1222 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001223 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001224 }
1225 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001226
Johannes Berg306d6112008-12-08 12:39:04 +01001227 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001228 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001229 if (netdev)
1230 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001231 return result;
1232}
1233
1234
1235static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001236 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001237 struct net_device *dev)
1238{
1239 void *hdr;
1240
1241 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1242 if (!hdr)
1243 return -1;
1244
1245 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001246 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001247 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001248 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001249
1250 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1251 rdev->devlist_generation ^
1252 (cfg80211_rdev_list_generation << 2));
1253
Johannes Berg55682962007-09-20 13:09:35 -04001254 return genlmsg_end(msg, hdr);
1255
1256 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001257 genlmsg_cancel(msg, hdr);
1258 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001259}
1260
1261static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1262{
1263 int wp_idx = 0;
1264 int if_idx = 0;
1265 int wp_start = cb->args[0];
1266 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001267 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001268 struct wireless_dev *wdev;
1269
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001270 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001271 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1272 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001273 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001274 if (wp_idx < wp_start) {
1275 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001276 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001277 }
Johannes Berg55682962007-09-20 13:09:35 -04001278 if_idx = 0;
1279
Johannes Bergf5ea9122009-08-07 16:17:38 +02001280 mutex_lock(&rdev->devlist_mtx);
1281 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001282 if (if_idx < if_start) {
1283 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001284 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001285 }
Johannes Berg55682962007-09-20 13:09:35 -04001286 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1287 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001288 rdev, wdev->netdev) < 0) {
1289 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001290 goto out;
1291 }
1292 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001293 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001294 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001295
1296 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001297 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001298 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001299 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001300
1301 cb->args[0] = wp_idx;
1302 cb->args[1] = if_idx;
1303
1304 return skb->len;
1305}
1306
1307static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1308{
1309 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001310 struct cfg80211_registered_device *dev = info->user_ptr[0];
1311 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001312
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001313 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001314 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001315 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001316
Johannes Bergd7264052009-04-19 16:23:20 +02001317 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001318 dev, netdev) < 0) {
1319 nlmsg_free(msg);
1320 return -ENOBUFS;
1321 }
Johannes Berg55682962007-09-20 13:09:35 -04001322
Johannes Berg134e6372009-07-10 09:51:34 +00001323 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001324}
1325
Michael Wu66f7ac52008-01-31 19:48:22 +01001326static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1327 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1328 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1329 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1330 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1331 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1332};
1333
1334static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1335{
1336 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1337 int flag;
1338
1339 *mntrflags = 0;
1340
1341 if (!nla)
1342 return -EINVAL;
1343
1344 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1345 nla, mntr_flags_policy))
1346 return -EINVAL;
1347
1348 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1349 if (flags[flag])
1350 *mntrflags |= (1<<flag);
1351
1352 return 0;
1353}
1354
Johannes Berg9bc383d2009-11-19 11:55:19 +01001355static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001356 struct net_device *netdev, u8 use_4addr,
1357 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001358{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001359 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001360 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001361 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001362 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001363 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001364
1365 switch (iftype) {
1366 case NL80211_IFTYPE_AP_VLAN:
1367 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1368 return 0;
1369 break;
1370 case NL80211_IFTYPE_STATION:
1371 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1372 return 0;
1373 break;
1374 default:
1375 break;
1376 }
1377
1378 return -EOPNOTSUPP;
1379}
1380
Johannes Berg55682962007-09-20 13:09:35 -04001381static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1382{
Johannes Berg4c476992010-10-04 21:36:35 +02001383 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001384 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001385 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001386 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001387 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001388 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001389 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001390
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001391 memset(&params, 0, sizeof(params));
1392
Johannes Berg04a773a2009-04-19 21:24:32 +02001393 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001394
Johannes Berg723b0382008-09-16 20:22:09 +02001395 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001396 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001397 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001398 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001399 if (ntype > NL80211_IFTYPE_MAX)
1400 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001401 }
1402
Johannes Berg92ffe052008-09-16 20:39:36 +02001403 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001404 struct wireless_dev *wdev = dev->ieee80211_ptr;
1405
Johannes Berg4c476992010-10-04 21:36:35 +02001406 if (ntype != NL80211_IFTYPE_MESH_POINT)
1407 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001408 if (netif_running(dev))
1409 return -EBUSY;
1410
1411 wdev_lock(wdev);
1412 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1413 IEEE80211_MAX_MESH_ID_LEN);
1414 wdev->mesh_id_up_len =
1415 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1416 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1417 wdev->mesh_id_up_len);
1418 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001419 }
1420
Felix Fietkau8b787642009-11-10 18:53:10 +01001421 if (info->attrs[NL80211_ATTR_4ADDR]) {
1422 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1423 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001424 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001425 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001426 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001427 } else {
1428 params.use_4addr = -1;
1429 }
1430
Johannes Berg92ffe052008-09-16 20:39:36 +02001431 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001432 if (ntype != NL80211_IFTYPE_MONITOR)
1433 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001434 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1435 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001436 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001437 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001438
1439 flags = &_flags;
1440 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001441 }
Johannes Berg3b858752009-03-12 09:55:09 +01001442
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001443 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001444 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001445 else
1446 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001447
Johannes Berg9bc383d2009-11-19 11:55:19 +01001448 if (!err && params.use_4addr != -1)
1449 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1450
Johannes Berg55682962007-09-20 13:09:35 -04001451 return err;
1452}
1453
1454static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1455{
Johannes Berg4c476992010-10-04 21:36:35 +02001456 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001457 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001458 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001459 int err;
1460 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001461 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001462
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001463 memset(&params, 0, sizeof(params));
1464
Johannes Berg55682962007-09-20 13:09:35 -04001465 if (!info->attrs[NL80211_ATTR_IFNAME])
1466 return -EINVAL;
1467
1468 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1469 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1470 if (type > NL80211_IFTYPE_MAX)
1471 return -EINVAL;
1472 }
1473
Johannes Berg79c97e92009-07-07 03:56:12 +02001474 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001475 !(rdev->wiphy.interface_modes & (1 << type)))
1476 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001477
Johannes Berg9bc383d2009-11-19 11:55:19 +01001478 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001479 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001480 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001481 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001482 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001483 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001484
Michael Wu66f7ac52008-01-31 19:48:22 +01001485 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1486 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1487 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001488 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001489 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001490 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001491 if (IS_ERR(dev))
1492 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001493
Johannes Berg29cbe682010-12-03 09:20:44 +01001494 if (type == NL80211_IFTYPE_MESH_POINT &&
1495 info->attrs[NL80211_ATTR_MESH_ID]) {
1496 struct wireless_dev *wdev = dev->ieee80211_ptr;
1497
1498 wdev_lock(wdev);
1499 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1500 IEEE80211_MAX_MESH_ID_LEN);
1501 wdev->mesh_id_up_len =
1502 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1503 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1504 wdev->mesh_id_up_len);
1505 wdev_unlock(wdev);
1506 }
1507
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001508 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001509}
1510
1511static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1512{
Johannes Berg4c476992010-10-04 21:36:35 +02001513 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1514 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001515
Johannes Berg4c476992010-10-04 21:36:35 +02001516 if (!rdev->ops->del_virtual_intf)
1517 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001518
Johannes Berg4c476992010-10-04 21:36:35 +02001519 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001520}
1521
Johannes Berg41ade002007-12-19 02:03:29 +01001522struct get_key_cookie {
1523 struct sk_buff *msg;
1524 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001525 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001526};
1527
1528static void get_key_callback(void *c, struct key_params *params)
1529{
Johannes Bergb9454e82009-07-08 13:29:08 +02001530 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001531 struct get_key_cookie *cookie = c;
1532
1533 if (params->key)
1534 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1535 params->key_len, params->key);
1536
1537 if (params->seq)
1538 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1539 params->seq_len, params->seq);
1540
1541 if (params->cipher)
1542 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1543 params->cipher);
1544
Johannes Bergb9454e82009-07-08 13:29:08 +02001545 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1546 if (!key)
1547 goto nla_put_failure;
1548
1549 if (params->key)
1550 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1551 params->key_len, params->key);
1552
1553 if (params->seq)
1554 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1555 params->seq_len, params->seq);
1556
1557 if (params->cipher)
1558 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1559 params->cipher);
1560
1561 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1562
1563 nla_nest_end(cookie->msg, key);
1564
Johannes Berg41ade002007-12-19 02:03:29 +01001565 return;
1566 nla_put_failure:
1567 cookie->error = 1;
1568}
1569
1570static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1571{
Johannes Berg4c476992010-10-04 21:36:35 +02001572 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001573 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001574 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001575 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001576 const u8 *mac_addr = NULL;
1577 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001578 struct get_key_cookie cookie = {
1579 .error = 0,
1580 };
1581 void *hdr;
1582 struct sk_buff *msg;
1583
1584 if (info->attrs[NL80211_ATTR_KEY_IDX])
1585 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1586
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001587 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001588 return -EINVAL;
1589
1590 if (info->attrs[NL80211_ATTR_MAC])
1591 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1592
Johannes Berge31b8212010-10-05 19:39:30 +02001593 pairwise = !!mac_addr;
1594 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1595 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1596 if (kt >= NUM_NL80211_KEYTYPES)
1597 return -EINVAL;
1598 if (kt != NL80211_KEYTYPE_GROUP &&
1599 kt != NL80211_KEYTYPE_PAIRWISE)
1600 return -EINVAL;
1601 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1602 }
1603
Johannes Berg4c476992010-10-04 21:36:35 +02001604 if (!rdev->ops->get_key)
1605 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001606
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001607 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001608 if (!msg)
1609 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001610
1611 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1612 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001613 if (IS_ERR(hdr))
1614 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001615
1616 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001617 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001618
1619 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1620 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1621 if (mac_addr)
1622 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1623
Johannes Berge31b8212010-10-05 19:39:30 +02001624 if (pairwise && mac_addr &&
1625 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1626 return -ENOENT;
1627
1628 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1629 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001630
1631 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001632 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001633
1634 if (cookie.error)
1635 goto nla_put_failure;
1636
1637 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001638 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001639
1640 nla_put_failure:
1641 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001642 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001643 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001644 return err;
1645}
1646
1647static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1648{
Johannes Berg4c476992010-10-04 21:36:35 +02001649 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001650 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001651 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001652 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001653
Johannes Bergb9454e82009-07-08 13:29:08 +02001654 err = nl80211_parse_key(info, &key);
1655 if (err)
1656 return err;
1657
1658 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001659 return -EINVAL;
1660
Johannes Bergb9454e82009-07-08 13:29:08 +02001661 /* only support setting default key */
1662 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001663 return -EINVAL;
1664
Johannes Bergfffd0932009-07-08 14:22:54 +02001665 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001666
1667 if (key.def) {
1668 if (!rdev->ops->set_default_key) {
1669 err = -EOPNOTSUPP;
1670 goto out;
1671 }
1672
1673 err = nl80211_key_allowed(dev->ieee80211_ptr);
1674 if (err)
1675 goto out;
1676
1677 if (!(rdev->wiphy.flags &
1678 WIPHY_FLAG_SUPPORTS_SEPARATE_DEFAULT_KEYS)) {
1679 if (!key.def_uni || !key.def_multi) {
1680 err = -EOPNOTSUPP;
1681 goto out;
1682 }
1683 }
1684
1685 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1686 key.def_uni, key.def_multi);
1687
1688 if (err)
1689 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001690
Johannes Berg3d23e342009-09-29 23:27:28 +02001691#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001692 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001693#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001694 } else {
1695 if (key.def_uni || !key.def_multi) {
1696 err = -EINVAL;
1697 goto out;
1698 }
1699
1700 if (!rdev->ops->set_default_mgmt_key) {
1701 err = -EOPNOTSUPP;
1702 goto out;
1703 }
1704
1705 err = nl80211_key_allowed(dev->ieee80211_ptr);
1706 if (err)
1707 goto out;
1708
1709 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1710 dev, key.idx);
1711 if (err)
1712 goto out;
1713
1714#ifdef CONFIG_CFG80211_WEXT
1715 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1716#endif
1717 }
1718
1719 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001720 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001721
Johannes Berg41ade002007-12-19 02:03:29 +01001722 return err;
1723}
1724
1725static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1726{
Johannes Berg4c476992010-10-04 21:36:35 +02001727 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001728 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001729 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001730 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001731 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001732
Johannes Bergb9454e82009-07-08 13:29:08 +02001733 err = nl80211_parse_key(info, &key);
1734 if (err)
1735 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001736
Johannes Bergb9454e82009-07-08 13:29:08 +02001737 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001738 return -EINVAL;
1739
Johannes Berg41ade002007-12-19 02:03:29 +01001740 if (info->attrs[NL80211_ATTR_MAC])
1741 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1742
Johannes Berge31b8212010-10-05 19:39:30 +02001743 if (key.type == -1) {
1744 if (mac_addr)
1745 key.type = NL80211_KEYTYPE_PAIRWISE;
1746 else
1747 key.type = NL80211_KEYTYPE_GROUP;
1748 }
1749
1750 /* for now */
1751 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1752 key.type != NL80211_KEYTYPE_GROUP)
1753 return -EINVAL;
1754
Johannes Berg4c476992010-10-04 21:36:35 +02001755 if (!rdev->ops->add_key)
1756 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001757
Johannes Berge31b8212010-10-05 19:39:30 +02001758 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1759 key.type == NL80211_KEYTYPE_PAIRWISE,
1760 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001761 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001762
1763 wdev_lock(dev->ieee80211_ptr);
1764 err = nl80211_key_allowed(dev->ieee80211_ptr);
1765 if (!err)
1766 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001767 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001768 mac_addr, &key.p);
1769 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001770
Johannes Berg41ade002007-12-19 02:03:29 +01001771 return err;
1772}
1773
1774static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1775{
Johannes Berg4c476992010-10-04 21:36:35 +02001776 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001777 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001778 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001779 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001780 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001781
Johannes Bergb9454e82009-07-08 13:29:08 +02001782 err = nl80211_parse_key(info, &key);
1783 if (err)
1784 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001785
1786 if (info->attrs[NL80211_ATTR_MAC])
1787 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1788
Johannes Berge31b8212010-10-05 19:39:30 +02001789 if (key.type == -1) {
1790 if (mac_addr)
1791 key.type = NL80211_KEYTYPE_PAIRWISE;
1792 else
1793 key.type = NL80211_KEYTYPE_GROUP;
1794 }
1795
1796 /* for now */
1797 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1798 key.type != NL80211_KEYTYPE_GROUP)
1799 return -EINVAL;
1800
Johannes Berg4c476992010-10-04 21:36:35 +02001801 if (!rdev->ops->del_key)
1802 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001803
Johannes Bergfffd0932009-07-08 14:22:54 +02001804 wdev_lock(dev->ieee80211_ptr);
1805 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001806
1807 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1808 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1809 err = -ENOENT;
1810
Johannes Bergfffd0932009-07-08 14:22:54 +02001811 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001812 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1813 key.type == NL80211_KEYTYPE_PAIRWISE,
1814 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001815
Johannes Berg3d23e342009-09-29 23:27:28 +02001816#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001817 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001818 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001819 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001820 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001821 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1822 }
1823#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001824 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001825
Johannes Berg41ade002007-12-19 02:03:29 +01001826 return err;
1827}
1828
Johannes Berged1b6cc2007-12-19 02:03:32 +01001829static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1830{
1831 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1832 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001833 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1834 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001835 struct beacon_parameters params;
1836 int haveinfo = 0;
1837
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001838 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1839 return -EINVAL;
1840
Johannes Berg074ac8d2010-09-16 14:58:22 +02001841 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001842 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1843 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001844
Johannes Berged1b6cc2007-12-19 02:03:32 +01001845 switch (info->genlhdr->cmd) {
1846 case NL80211_CMD_NEW_BEACON:
1847 /* these are required for NEW_BEACON */
1848 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1849 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001850 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1851 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001852
Johannes Berg79c97e92009-07-07 03:56:12 +02001853 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001854 break;
1855 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001856 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001857 break;
1858 default:
1859 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001860 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001861 }
1862
Johannes Berg4c476992010-10-04 21:36:35 +02001863 if (!call)
1864 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001865
1866 memset(&params, 0, sizeof(params));
1867
1868 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1869 params.interval =
1870 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1871 haveinfo = 1;
1872 }
1873
1874 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1875 params.dtim_period =
1876 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1877 haveinfo = 1;
1878 }
1879
1880 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1881 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1882 params.head_len =
1883 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1884 haveinfo = 1;
1885 }
1886
1887 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1888 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1889 params.tail_len =
1890 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1891 haveinfo = 1;
1892 }
1893
Johannes Berg4c476992010-10-04 21:36:35 +02001894 if (!haveinfo)
1895 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001896
Johannes Berg4c476992010-10-04 21:36:35 +02001897 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001898}
1899
1900static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1901{
Johannes Berg4c476992010-10-04 21:36:35 +02001902 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1903 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001904
Johannes Berg4c476992010-10-04 21:36:35 +02001905 if (!rdev->ops->del_beacon)
1906 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001907
Johannes Berg074ac8d2010-09-16 14:58:22 +02001908 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001909 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1910 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001911
Johannes Berg4c476992010-10-04 21:36:35 +02001912 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001913}
1914
Johannes Berg5727ef12007-12-19 02:03:34 +01001915static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1916 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1917 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1918 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001919 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001920};
1921
Johannes Bergeccb8e82009-05-11 21:57:56 +03001922static int parse_station_flags(struct genl_info *info,
1923 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001924{
1925 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001926 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001927 int flag;
1928
Johannes Bergeccb8e82009-05-11 21:57:56 +03001929 /*
1930 * Try parsing the new attribute first so userspace
1931 * can specify both for older kernels.
1932 */
1933 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1934 if (nla) {
1935 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001936
Johannes Bergeccb8e82009-05-11 21:57:56 +03001937 sta_flags = nla_data(nla);
1938 params->sta_flags_mask = sta_flags->mask;
1939 params->sta_flags_set = sta_flags->set;
1940 if ((params->sta_flags_mask |
1941 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1942 return -EINVAL;
1943 return 0;
1944 }
1945
1946 /* if present, parse the old attribute */
1947
1948 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001949 if (!nla)
1950 return 0;
1951
1952 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1953 nla, sta_flags_policy))
1954 return -EINVAL;
1955
Johannes Bergeccb8e82009-05-11 21:57:56 +03001956 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1957 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001958
1959 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1960 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001961 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001962
1963 return 0;
1964}
1965
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001966static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1967 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001968 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001969{
1970 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001971 struct nlattr *sinfoattr, *txrate;
1972 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001973
1974 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1975 if (!hdr)
1976 return -1;
1977
1978 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1979 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1980
Johannes Bergf5ea9122009-08-07 16:17:38 +02001981 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1982
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001983 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1984 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001985 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001986 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1987 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1988 sinfo->inactive_time);
1989 if (sinfo->filled & STATION_INFO_RX_BYTES)
1990 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1991 sinfo->rx_bytes);
1992 if (sinfo->filled & STATION_INFO_TX_BYTES)
1993 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1994 sinfo->tx_bytes);
1995 if (sinfo->filled & STATION_INFO_LLID)
1996 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1997 sinfo->llid);
1998 if (sinfo->filled & STATION_INFO_PLID)
1999 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2000 sinfo->plid);
2001 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2002 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2003 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002004 if (sinfo->filled & STATION_INFO_SIGNAL)
2005 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2006 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002007 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2008 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2009 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002010 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
2011 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
2012 if (!txrate)
2013 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002014
John W. Linville254416a2009-12-09 16:43:52 -05002015 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2016 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002017 if (bitrate > 0)
2018 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2019
2020 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
2021 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
2022 sinfo->txrate.mcs);
2023 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2024 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2025 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
2026 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2027
2028 nla_nest_end(msg, txrate);
2029 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002030 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2031 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2032 sinfo->rx_packets);
2033 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2034 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2035 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002036 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2037 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2038 sinfo->tx_retries);
2039 if (sinfo->filled & STATION_INFO_TX_FAILED)
2040 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2041 sinfo->tx_failed);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002042 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002043
2044 return genlmsg_end(msg, hdr);
2045
2046 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002047 genlmsg_cancel(msg, hdr);
2048 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002049}
2050
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002051static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002052 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002053{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002054 struct station_info sinfo;
2055 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002056 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002057 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002058 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002059 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002060
Johannes Berg67748892010-10-04 21:14:06 +02002061 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2062 if (err)
2063 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002064
2065 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002066 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002067 goto out_err;
2068 }
2069
Johannes Bergbba95fe2008-07-29 13:22:51 +02002070 while (1) {
2071 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2072 mac_addr, &sinfo);
2073 if (err == -ENOENT)
2074 break;
2075 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002076 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002077
2078 if (nl80211_send_station(skb,
2079 NETLINK_CB(cb->skb).pid,
2080 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2081 netdev, mac_addr,
2082 &sinfo) < 0)
2083 goto out;
2084
2085 sta_idx++;
2086 }
2087
2088
2089 out:
2090 cb->args[1] = sta_idx;
2091 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002092 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002093 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002094
2095 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002096}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002097
Johannes Berg5727ef12007-12-19 02:03:34 +01002098static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2099{
Johannes Berg4c476992010-10-04 21:36:35 +02002100 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2101 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002102 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002103 struct sk_buff *msg;
2104 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002105 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002106
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002107 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002108
2109 if (!info->attrs[NL80211_ATTR_MAC])
2110 return -EINVAL;
2111
2112 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2113
Johannes Berg4c476992010-10-04 21:36:35 +02002114 if (!rdev->ops->get_station)
2115 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002116
Johannes Berg79c97e92009-07-07 03:56:12 +02002117 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002118 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002119 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002120
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002121 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002122 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002123 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002124
2125 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002126 dev, mac_addr, &sinfo) < 0) {
2127 nlmsg_free(msg);
2128 return -ENOBUFS;
2129 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002130
Johannes Berg4c476992010-10-04 21:36:35 +02002131 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002132}
2133
2134/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002135 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002136 */
Johannes Berg463d0182009-07-14 00:33:35 +02002137static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002138 struct cfg80211_registered_device *rdev,
2139 struct net_device **vlan)
2140{
Johannes Berg463d0182009-07-14 00:33:35 +02002141 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002142 *vlan = NULL;
2143
2144 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002145 *vlan = dev_get_by_index(genl_info_net(info),
2146 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002147 if (!*vlan)
2148 return -ENODEV;
2149 if (!(*vlan)->ieee80211_ptr)
2150 return -EINVAL;
2151 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2152 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002153 if (!netif_running(*vlan))
2154 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002155 }
2156 return 0;
2157}
2158
2159static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2160{
Johannes Berg4c476992010-10-04 21:36:35 +02002161 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002162 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002163 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002164 struct station_parameters params;
2165 u8 *mac_addr = NULL;
2166
2167 memset(&params, 0, sizeof(params));
2168
2169 params.listen_interval = -1;
2170
2171 if (info->attrs[NL80211_ATTR_STA_AID])
2172 return -EINVAL;
2173
2174 if (!info->attrs[NL80211_ATTR_MAC])
2175 return -EINVAL;
2176
2177 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2178
2179 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2180 params.supported_rates =
2181 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2182 params.supported_rates_len =
2183 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2184 }
2185
2186 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2187 params.listen_interval =
2188 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2189
Jouni Malinen36aedc92008-08-25 11:58:58 +03002190 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2191 params.ht_capa =
2192 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2193
Johannes Bergeccb8e82009-05-11 21:57:56 +03002194 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002195 return -EINVAL;
2196
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002197 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2198 params.plink_action =
2199 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2200
Johannes Berg463d0182009-07-14 00:33:35 +02002201 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002202 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002203 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002204
2205 /* validate settings */
2206 err = 0;
2207
2208 switch (dev->ieee80211_ptr->iftype) {
2209 case NL80211_IFTYPE_AP:
2210 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002211 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002212 /* disallow mesh-specific things */
2213 if (params.plink_action)
2214 err = -EINVAL;
2215 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002216 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002217 case NL80211_IFTYPE_STATION:
2218 /* disallow everything but AUTHORIZED flag */
2219 if (params.plink_action)
2220 err = -EINVAL;
2221 if (params.vlan)
2222 err = -EINVAL;
2223 if (params.supported_rates)
2224 err = -EINVAL;
2225 if (params.ht_capa)
2226 err = -EINVAL;
2227 if (params.listen_interval >= 0)
2228 err = -EINVAL;
2229 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2230 err = -EINVAL;
2231 break;
2232 case NL80211_IFTYPE_MESH_POINT:
2233 /* disallow things mesh doesn't support */
2234 if (params.vlan)
2235 err = -EINVAL;
2236 if (params.ht_capa)
2237 err = -EINVAL;
2238 if (params.listen_interval >= 0)
2239 err = -EINVAL;
2240 if (params.supported_rates)
2241 err = -EINVAL;
2242 if (params.sta_flags_mask)
2243 err = -EINVAL;
2244 break;
2245 default:
2246 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002247 }
2248
Johannes Berg5727ef12007-12-19 02:03:34 +01002249 if (err)
2250 goto out;
2251
Johannes Berg79c97e92009-07-07 03:56:12 +02002252 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002253 err = -EOPNOTSUPP;
2254 goto out;
2255 }
2256
Johannes Berg79c97e92009-07-07 03:56:12 +02002257 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002258
2259 out:
2260 if (params.vlan)
2261 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002262
Johannes Berg5727ef12007-12-19 02:03:34 +01002263 return err;
2264}
2265
2266static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2267{
Johannes Berg4c476992010-10-04 21:36:35 +02002268 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002269 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002270 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002271 struct station_parameters params;
2272 u8 *mac_addr = NULL;
2273
2274 memset(&params, 0, sizeof(params));
2275
2276 if (!info->attrs[NL80211_ATTR_MAC])
2277 return -EINVAL;
2278
Johannes Berg5727ef12007-12-19 02:03:34 +01002279 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2280 return -EINVAL;
2281
2282 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2283 return -EINVAL;
2284
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002285 if (!info->attrs[NL80211_ATTR_STA_AID])
2286 return -EINVAL;
2287
Johannes Berg5727ef12007-12-19 02:03:34 +01002288 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2289 params.supported_rates =
2290 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2291 params.supported_rates_len =
2292 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2293 params.listen_interval =
2294 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002295
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002296 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2297 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2298 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002299
Jouni Malinen36aedc92008-08-25 11:58:58 +03002300 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2301 params.ht_capa =
2302 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002303
Johannes Bergeccb8e82009-05-11 21:57:56 +03002304 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002305 return -EINVAL;
2306
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002307 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002308 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02002309 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2310 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002311
Johannes Berg463d0182009-07-14 00:33:35 +02002312 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002313 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002314 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002315
2316 /* validate settings */
2317 err = 0;
2318
Johannes Berg79c97e92009-07-07 03:56:12 +02002319 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002320 err = -EOPNOTSUPP;
2321 goto out;
2322 }
2323
Johannes Berg79c97e92009-07-07 03:56:12 +02002324 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002325
2326 out:
2327 if (params.vlan)
2328 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002329 return err;
2330}
2331
2332static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2333{
Johannes Berg4c476992010-10-04 21:36:35 +02002334 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2335 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002336 u8 *mac_addr = NULL;
2337
2338 if (info->attrs[NL80211_ATTR_MAC])
2339 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2340
Johannes Berge80cf852009-05-11 14:43:13 +02002341 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002342 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002343 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002344 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2345 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002346
Johannes Berg4c476992010-10-04 21:36:35 +02002347 if (!rdev->ops->del_station)
2348 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002349
Johannes Berg4c476992010-10-04 21:36:35 +02002350 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002351}
2352
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002353static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2354 int flags, struct net_device *dev,
2355 u8 *dst, u8 *next_hop,
2356 struct mpath_info *pinfo)
2357{
2358 void *hdr;
2359 struct nlattr *pinfoattr;
2360
2361 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2362 if (!hdr)
2363 return -1;
2364
2365 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2366 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2367 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2368
Johannes Bergf5ea9122009-08-07 16:17:38 +02002369 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2370
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002371 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2372 if (!pinfoattr)
2373 goto nla_put_failure;
2374 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2375 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2376 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002377 if (pinfo->filled & MPATH_INFO_SN)
2378 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2379 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002380 if (pinfo->filled & MPATH_INFO_METRIC)
2381 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2382 pinfo->metric);
2383 if (pinfo->filled & MPATH_INFO_EXPTIME)
2384 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2385 pinfo->exptime);
2386 if (pinfo->filled & MPATH_INFO_FLAGS)
2387 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2388 pinfo->flags);
2389 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2390 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2391 pinfo->discovery_timeout);
2392 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2393 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2394 pinfo->discovery_retries);
2395
2396 nla_nest_end(msg, pinfoattr);
2397
2398 return genlmsg_end(msg, hdr);
2399
2400 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002401 genlmsg_cancel(msg, hdr);
2402 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002403}
2404
2405static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002406 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002407{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002408 struct mpath_info pinfo;
2409 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002410 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002411 u8 dst[ETH_ALEN];
2412 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002413 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002414 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002415
Johannes Berg67748892010-10-04 21:14:06 +02002416 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2417 if (err)
2418 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002419
2420 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002421 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002422 goto out_err;
2423 }
2424
Jouni Malineneec60b02009-03-20 21:21:19 +02002425 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2426 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002427 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002428 }
2429
Johannes Bergbba95fe2008-07-29 13:22:51 +02002430 while (1) {
2431 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2432 dst, next_hop, &pinfo);
2433 if (err == -ENOENT)
2434 break;
2435 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002436 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002437
2438 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2439 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2440 netdev, dst, next_hop,
2441 &pinfo) < 0)
2442 goto out;
2443
2444 path_idx++;
2445 }
2446
2447
2448 out:
2449 cb->args[1] = path_idx;
2450 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002451 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002452 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002453 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002454}
2455
2456static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2457{
Johannes Berg4c476992010-10-04 21:36:35 +02002458 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002459 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002460 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002461 struct mpath_info pinfo;
2462 struct sk_buff *msg;
2463 u8 *dst = NULL;
2464 u8 next_hop[ETH_ALEN];
2465
2466 memset(&pinfo, 0, sizeof(pinfo));
2467
2468 if (!info->attrs[NL80211_ATTR_MAC])
2469 return -EINVAL;
2470
2471 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2472
Johannes Berg4c476992010-10-04 21:36:35 +02002473 if (!rdev->ops->get_mpath)
2474 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002475
Johannes Berg4c476992010-10-04 21:36:35 +02002476 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2477 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002478
Johannes Berg79c97e92009-07-07 03:56:12 +02002479 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002480 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002481 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002482
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002483 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002484 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002485 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002486
2487 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002488 dev, dst, next_hop, &pinfo) < 0) {
2489 nlmsg_free(msg);
2490 return -ENOBUFS;
2491 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002492
Johannes Berg4c476992010-10-04 21:36:35 +02002493 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002494}
2495
2496static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2497{
Johannes Berg4c476992010-10-04 21:36:35 +02002498 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2499 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002500 u8 *dst = NULL;
2501 u8 *next_hop = NULL;
2502
2503 if (!info->attrs[NL80211_ATTR_MAC])
2504 return -EINVAL;
2505
2506 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2507 return -EINVAL;
2508
2509 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2510 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2511
Johannes Berg4c476992010-10-04 21:36:35 +02002512 if (!rdev->ops->change_mpath)
2513 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002514
Johannes Berg4c476992010-10-04 21:36:35 +02002515 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2516 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002517
Johannes Berg4c476992010-10-04 21:36:35 +02002518 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002519}
Johannes Berg4c476992010-10-04 21:36:35 +02002520
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002521static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2522{
Johannes Berg4c476992010-10-04 21:36:35 +02002523 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2524 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002525 u8 *dst = NULL;
2526 u8 *next_hop = NULL;
2527
2528 if (!info->attrs[NL80211_ATTR_MAC])
2529 return -EINVAL;
2530
2531 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2532 return -EINVAL;
2533
2534 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2535 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2536
Johannes Berg4c476992010-10-04 21:36:35 +02002537 if (!rdev->ops->add_mpath)
2538 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002539
Johannes Berg4c476992010-10-04 21:36:35 +02002540 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2541 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002542
Johannes Berg4c476992010-10-04 21:36:35 +02002543 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002544}
2545
2546static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2547{
Johannes Berg4c476992010-10-04 21:36:35 +02002548 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2549 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002550 u8 *dst = NULL;
2551
2552 if (info->attrs[NL80211_ATTR_MAC])
2553 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2554
Johannes Berg4c476992010-10-04 21:36:35 +02002555 if (!rdev->ops->del_mpath)
2556 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002557
Johannes Berg4c476992010-10-04 21:36:35 +02002558 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002559}
2560
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002561static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2562{
Johannes Berg4c476992010-10-04 21:36:35 +02002563 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2564 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002565 struct bss_parameters params;
2566
2567 memset(&params, 0, sizeof(params));
2568 /* default to not changing parameters */
2569 params.use_cts_prot = -1;
2570 params.use_short_preamble = -1;
2571 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002572 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002573 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002574
2575 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2576 params.use_cts_prot =
2577 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2578 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2579 params.use_short_preamble =
2580 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2581 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2582 params.use_short_slot_time =
2583 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002584 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2585 params.basic_rates =
2586 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2587 params.basic_rates_len =
2588 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2589 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002590 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2591 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002592 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2593 params.ht_opmode =
2594 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002595
Johannes Berg4c476992010-10-04 21:36:35 +02002596 if (!rdev->ops->change_bss)
2597 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002598
Johannes Berg074ac8d2010-09-16 14:58:22 +02002599 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002600 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2601 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002602
Johannes Berg4c476992010-10-04 21:36:35 +02002603 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002604}
2605
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002606static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002607 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2608 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2609 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2610 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2611 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2612 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2613};
2614
2615static int parse_reg_rule(struct nlattr *tb[],
2616 struct ieee80211_reg_rule *reg_rule)
2617{
2618 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2619 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2620
2621 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2622 return -EINVAL;
2623 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2624 return -EINVAL;
2625 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2626 return -EINVAL;
2627 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2628 return -EINVAL;
2629 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2630 return -EINVAL;
2631
2632 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2633
2634 freq_range->start_freq_khz =
2635 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2636 freq_range->end_freq_khz =
2637 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2638 freq_range->max_bandwidth_khz =
2639 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2640
2641 power_rule->max_eirp =
2642 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2643
2644 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2645 power_rule->max_antenna_gain =
2646 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2647
2648 return 0;
2649}
2650
2651static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2652{
2653 int r;
2654 char *data = NULL;
2655
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002656 /*
2657 * You should only get this when cfg80211 hasn't yet initialized
2658 * completely when built-in to the kernel right between the time
2659 * window between nl80211_init() and regulatory_init(), if that is
2660 * even possible.
2661 */
2662 mutex_lock(&cfg80211_mutex);
2663 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002664 mutex_unlock(&cfg80211_mutex);
2665 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002666 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002667 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002668
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002669 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2670 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002671
2672 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2673
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002674 r = regulatory_hint_user(data);
2675
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002676 return r;
2677}
2678
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002679static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002680 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002681{
Johannes Berg4c476992010-10-04 21:36:35 +02002682 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02002683 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002684 struct wireless_dev *wdev = dev->ieee80211_ptr;
2685 struct mesh_config cur_params;
2686 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002687 void *hdr;
2688 struct nlattr *pinfoattr;
2689 struct sk_buff *msg;
2690
Johannes Berg29cbe682010-12-03 09:20:44 +01002691 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2692 return -EOPNOTSUPP;
2693
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002694 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02002695 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002696
Johannes Berg29cbe682010-12-03 09:20:44 +01002697 wdev_lock(wdev);
2698 /* If not connected, get default parameters */
2699 if (!wdev->mesh_id_len)
2700 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
2701 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002702 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01002703 &cur_params);
2704 wdev_unlock(wdev);
2705
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002706 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002707 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002708
2709 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002710 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002711 if (!msg)
2712 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002713 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002714 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002715 if (!hdr)
2716 goto nla_put_failure;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002717 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002718 if (!pinfoattr)
2719 goto nla_put_failure;
2720 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2721 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2722 cur_params.dot11MeshRetryTimeout);
2723 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2724 cur_params.dot11MeshConfirmTimeout);
2725 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2726 cur_params.dot11MeshHoldingTimeout);
2727 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2728 cur_params.dot11MeshMaxPeerLinks);
2729 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2730 cur_params.dot11MeshMaxRetries);
2731 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2732 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01002733 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
2734 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002735 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2736 cur_params.auto_open_plinks);
2737 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2738 cur_params.dot11MeshHWMPmaxPREQretries);
2739 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2740 cur_params.path_refresh_time);
2741 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2742 cur_params.min_discovery_timeout);
2743 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2744 cur_params.dot11MeshHWMPactivePathTimeout);
2745 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2746 cur_params.dot11MeshHWMPpreqMinInterval);
2747 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2748 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002749 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2750 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002751 nla_nest_end(msg, pinfoattr);
2752 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002753 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002754
Johannes Berg3b858752009-03-12 09:55:09 +01002755 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002756 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002757 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002758 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002759}
2760
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002761static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002762 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2763 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2764 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2765 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2766 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2767 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01002768 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002769 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2770
2771 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2772 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2773 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2774 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2775 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2776 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2777};
2778
Javier Cardonac80d5452010-12-16 17:37:49 -08002779static const struct nla_policy
2780 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
2781 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
2782 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
2783 [NL80211_MESH_SETUP_VENDOR_PATH_SEL_IE] = { .type = NLA_BINARY,
2784 .len = IEEE80211_MAX_DATA_LEN },
2785};
2786
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002787static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002788 struct mesh_config *cfg,
2789 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002790{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002791 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002792 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002793
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002794#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2795do {\
2796 if (table[attr_num]) {\
2797 cfg->param = nla_fn(table[attr_num]); \
2798 mask |= (1 << (attr_num - 1)); \
2799 } \
2800} while (0);\
2801
2802
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002803 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002804 return -EINVAL;
2805 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002806 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002807 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002808 return -EINVAL;
2809
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002810 /* This makes sure that there aren't more than 32 mesh config
2811 * parameters (otherwise our bitfield scheme would not work.) */
2812 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2813
2814 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002815 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2816 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2817 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2818 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2819 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2820 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2821 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2822 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2823 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2824 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2825 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2826 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01002827 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
2828 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002829 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2830 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2831 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2832 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2833 nla_get_u8);
2834 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2835 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2836 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2837 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2838 nla_get_u16);
2839 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2840 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2841 nla_get_u32);
2842 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2843 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2844 nla_get_u16);
2845 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2846 dot11MeshHWMPnetDiameterTraversalTime,
2847 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2848 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002849 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2850 dot11MeshHWMPRootMode, mask,
2851 NL80211_MESHCONF_HWMP_ROOTMODE,
2852 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002853 if (mask_out)
2854 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08002855
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002856 return 0;
2857
2858#undef FILL_IN_MESH_PARAM_IF_SET
2859}
2860
Javier Cardonac80d5452010-12-16 17:37:49 -08002861static int nl80211_parse_mesh_setup(struct genl_info *info,
2862 struct mesh_setup *setup)
2863{
2864 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
2865
2866 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
2867 return -EINVAL;
2868 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
2869 info->attrs[NL80211_ATTR_MESH_SETUP],
2870 nl80211_mesh_setup_params_policy))
2871 return -EINVAL;
2872
2873 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
2874 setup->path_sel_proto =
2875 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
2876 IEEE80211_PATH_PROTOCOL_VENDOR :
2877 IEEE80211_PATH_PROTOCOL_HWMP;
2878
2879 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
2880 setup->path_metric =
2881 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
2882 IEEE80211_PATH_METRIC_VENDOR :
2883 IEEE80211_PATH_METRIC_AIRTIME;
2884
2885 if (tb[NL80211_MESH_SETUP_VENDOR_PATH_SEL_IE]) {
2886 struct nlattr *ieattr =
2887 tb[NL80211_MESH_SETUP_VENDOR_PATH_SEL_IE];
2888 if (!is_valid_ie_attr(ieattr))
2889 return -EINVAL;
2890 setup->vendor_ie = nla_data(ieattr);
2891 setup->vendor_ie_len = nla_len(ieattr);
2892 }
2893
2894 return 0;
2895}
2896
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002897static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002898 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002899{
2900 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2901 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002902 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002903 struct mesh_config cfg;
2904 u32 mask;
2905 int err;
2906
Johannes Berg29cbe682010-12-03 09:20:44 +01002907 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2908 return -EOPNOTSUPP;
2909
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002910 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002911 return -EOPNOTSUPP;
2912
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002913 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002914 if (err)
2915 return err;
2916
Johannes Berg29cbe682010-12-03 09:20:44 +01002917 wdev_lock(wdev);
2918 if (!wdev->mesh_id_len)
2919 err = -ENOLINK;
2920
2921 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002922 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01002923 mask, &cfg);
2924
2925 wdev_unlock(wdev);
2926
2927 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002928}
2929
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002930static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2931{
2932 struct sk_buff *msg;
2933 void *hdr = NULL;
2934 struct nlattr *nl_reg_rules;
2935 unsigned int i;
2936 int err = -EINVAL;
2937
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002938 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002939
2940 if (!cfg80211_regdomain)
2941 goto out;
2942
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002943 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002944 if (!msg) {
2945 err = -ENOBUFS;
2946 goto out;
2947 }
2948
2949 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2950 NL80211_CMD_GET_REG);
2951 if (!hdr)
2952 goto nla_put_failure;
2953
2954 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2955 cfg80211_regdomain->alpha2);
2956
2957 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2958 if (!nl_reg_rules)
2959 goto nla_put_failure;
2960
2961 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2962 struct nlattr *nl_reg_rule;
2963 const struct ieee80211_reg_rule *reg_rule;
2964 const struct ieee80211_freq_range *freq_range;
2965 const struct ieee80211_power_rule *power_rule;
2966
2967 reg_rule = &cfg80211_regdomain->reg_rules[i];
2968 freq_range = &reg_rule->freq_range;
2969 power_rule = &reg_rule->power_rule;
2970
2971 nl_reg_rule = nla_nest_start(msg, i);
2972 if (!nl_reg_rule)
2973 goto nla_put_failure;
2974
2975 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2976 reg_rule->flags);
2977 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2978 freq_range->start_freq_khz);
2979 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2980 freq_range->end_freq_khz);
2981 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2982 freq_range->max_bandwidth_khz);
2983 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2984 power_rule->max_antenna_gain);
2985 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2986 power_rule->max_eirp);
2987
2988 nla_nest_end(msg, nl_reg_rule);
2989 }
2990
2991 nla_nest_end(msg, nl_reg_rules);
2992
2993 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002994 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002995 goto out;
2996
2997nla_put_failure:
2998 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002999 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003000 err = -EMSGSIZE;
3001out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003002 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003003 return err;
3004}
3005
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003006static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3007{
3008 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3009 struct nlattr *nl_reg_rule;
3010 char *alpha2 = NULL;
3011 int rem_reg_rules = 0, r = 0;
3012 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3013 struct ieee80211_regdomain *rd = NULL;
3014
3015 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3016 return -EINVAL;
3017
3018 if (!info->attrs[NL80211_ATTR_REG_RULES])
3019 return -EINVAL;
3020
3021 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3022
3023 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3024 rem_reg_rules) {
3025 num_rules++;
3026 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003027 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003028 }
3029
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003030 mutex_lock(&cfg80211_mutex);
3031
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003032 if (!reg_is_valid_request(alpha2)) {
3033 r = -EINVAL;
3034 goto bad_reg;
3035 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003036
3037 size_of_regd = sizeof(struct ieee80211_regdomain) +
3038 (num_rules * sizeof(struct ieee80211_reg_rule));
3039
3040 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003041 if (!rd) {
3042 r = -ENOMEM;
3043 goto bad_reg;
3044 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003045
3046 rd->n_reg_rules = num_rules;
3047 rd->alpha2[0] = alpha2[0];
3048 rd->alpha2[1] = alpha2[1];
3049
3050 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3051 rem_reg_rules) {
3052 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3053 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3054 reg_rule_policy);
3055 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3056 if (r)
3057 goto bad_reg;
3058
3059 rule_idx++;
3060
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003061 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3062 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003063 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003064 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003065 }
3066
3067 BUG_ON(rule_idx != num_rules);
3068
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003069 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003070
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003071 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003072
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003073 return r;
3074
Johannes Bergd2372b32008-10-24 20:32:20 +02003075 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003076 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003077 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003078 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003079}
3080
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003081static int validate_scan_freqs(struct nlattr *freqs)
3082{
3083 struct nlattr *attr1, *attr2;
3084 int n_channels = 0, tmp1, tmp2;
3085
3086 nla_for_each_nested(attr1, freqs, tmp1) {
3087 n_channels++;
3088 /*
3089 * Some hardware has a limited channel list for
3090 * scanning, and it is pretty much nonsensical
3091 * to scan for a channel twice, so disallow that
3092 * and don't require drivers to check that the
3093 * channel list they get isn't longer than what
3094 * they can scan, as long as they can scan all
3095 * the channels they registered at once.
3096 */
3097 nla_for_each_nested(attr2, freqs, tmp2)
3098 if (attr1 != attr2 &&
3099 nla_get_u32(attr1) == nla_get_u32(attr2))
3100 return 0;
3101 }
3102
3103 return n_channels;
3104}
3105
Johannes Berg2a519312009-02-10 21:25:55 +01003106static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3107{
Johannes Berg4c476992010-10-04 21:36:35 +02003108 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3109 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003110 struct cfg80211_scan_request *request;
3111 struct cfg80211_ssid *ssid;
3112 struct ieee80211_channel *channel;
3113 struct nlattr *attr;
3114 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003115 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01003116 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003117 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003118
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003119 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3120 return -EINVAL;
3121
Johannes Berg79c97e92009-07-07 03:56:12 +02003122 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003123
Johannes Berg4c476992010-10-04 21:36:35 +02003124 if (!rdev->ops->scan)
3125 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003126
Johannes Berg4c476992010-10-04 21:36:35 +02003127 if (rdev->scan_req)
3128 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003129
3130 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003131 n_channels = validate_scan_freqs(
3132 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003133 if (!n_channels)
3134 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003135 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003136 n_channels = 0;
3137
Johannes Berg2a519312009-02-10 21:25:55 +01003138 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3139 if (wiphy->bands[band])
3140 n_channels += wiphy->bands[band]->n_channels;
3141 }
3142
3143 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3144 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3145 n_ssids++;
3146
Johannes Berg4c476992010-10-04 21:36:35 +02003147 if (n_ssids > wiphy->max_scan_ssids)
3148 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003149
Jouni Malinen70692ad2009-02-16 19:39:13 +02003150 if (info->attrs[NL80211_ATTR_IE])
3151 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3152 else
3153 ie_len = 0;
3154
Johannes Berg4c476992010-10-04 21:36:35 +02003155 if (ie_len > wiphy->max_scan_ie_len)
3156 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003157
Johannes Berg2a519312009-02-10 21:25:55 +01003158 request = kzalloc(sizeof(*request)
3159 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003160 + sizeof(channel) * n_channels
3161 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003162 if (!request)
3163 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003164
Johannes Berg2a519312009-02-10 21:25:55 +01003165 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003166 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003167 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003168 if (ie_len) {
3169 if (request->ssids)
3170 request->ie = (void *)(request->ssids + n_ssids);
3171 else
3172 request->ie = (void *)(request->channels + n_channels);
3173 }
Johannes Berg2a519312009-02-10 21:25:55 +01003174
Johannes Berg584991d2009-11-02 13:32:03 +01003175 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003176 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3177 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003178 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003179 struct ieee80211_channel *chan;
3180
3181 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3182
3183 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003184 err = -EINVAL;
3185 goto out_free;
3186 }
Johannes Berg584991d2009-11-02 13:32:03 +01003187
3188 /* ignore disabled channels */
3189 if (chan->flags & IEEE80211_CHAN_DISABLED)
3190 continue;
3191
3192 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003193 i++;
3194 }
3195 } else {
3196 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003197 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3198 int j;
3199 if (!wiphy->bands[band])
3200 continue;
3201 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003202 struct ieee80211_channel *chan;
3203
3204 chan = &wiphy->bands[band]->channels[j];
3205
3206 if (chan->flags & IEEE80211_CHAN_DISABLED)
3207 continue;
3208
3209 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003210 i++;
3211 }
3212 }
3213 }
3214
Johannes Berg584991d2009-11-02 13:32:03 +01003215 if (!i) {
3216 err = -EINVAL;
3217 goto out_free;
3218 }
3219
3220 request->n_channels = i;
3221
Johannes Berg2a519312009-02-10 21:25:55 +01003222 i = 0;
3223 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3224 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3225 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3226 err = -EINVAL;
3227 goto out_free;
3228 }
3229 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3230 request->ssids[i].ssid_len = nla_len(attr);
3231 i++;
3232 }
3233 }
3234
Jouni Malinen70692ad2009-02-16 19:39:13 +02003235 if (info->attrs[NL80211_ATTR_IE]) {
3236 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003237 memcpy((void *)request->ie,
3238 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003239 request->ie_len);
3240 }
3241
Johannes Berg463d0182009-07-14 00:33:35 +02003242 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003243 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003244
Johannes Berg79c97e92009-07-07 03:56:12 +02003245 rdev->scan_req = request;
3246 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003247
Johannes Berg463d0182009-07-14 00:33:35 +02003248 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003249 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003250 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003251 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003252 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003253 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003254 kfree(request);
3255 }
Johannes Berg3b858752009-03-12 09:55:09 +01003256
Johannes Berg2a519312009-02-10 21:25:55 +01003257 return err;
3258}
3259
3260static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3261 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003262 struct wireless_dev *wdev,
3263 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003264{
Johannes Berg48ab9052009-07-10 18:42:31 +02003265 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003266 void *hdr;
3267 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003268 int i;
3269
3270 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003271
3272 hdr = nl80211hdr_put(msg, pid, seq, flags,
3273 NL80211_CMD_NEW_SCAN_RESULTS);
3274 if (!hdr)
3275 return -1;
3276
Johannes Bergf5ea9122009-08-07 16:17:38 +02003277 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003278 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003279
3280 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3281 if (!bss)
3282 goto nla_put_failure;
3283 if (!is_zero_ether_addr(res->bssid))
3284 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3285 if (res->information_elements && res->len_information_elements)
3286 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3287 res->len_information_elements,
3288 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003289 if (res->beacon_ies && res->len_beacon_ies &&
3290 res->beacon_ies != res->information_elements)
3291 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3292 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003293 if (res->tsf)
3294 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3295 if (res->beacon_interval)
3296 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3297 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3298 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003299 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3300 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003301
Johannes Berg77965c92009-02-18 18:45:06 +01003302 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003303 case CFG80211_SIGNAL_TYPE_MBM:
3304 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3305 break;
3306 case CFG80211_SIGNAL_TYPE_UNSPEC:
3307 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3308 break;
3309 default:
3310 break;
3311 }
3312
Johannes Berg48ab9052009-07-10 18:42:31 +02003313 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003314 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003315 case NL80211_IFTYPE_STATION:
3316 if (intbss == wdev->current_bss)
3317 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3318 NL80211_BSS_STATUS_ASSOCIATED);
3319 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3320 if (intbss != wdev->auth_bsses[i])
3321 continue;
3322 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3323 NL80211_BSS_STATUS_AUTHENTICATED);
3324 break;
3325 }
3326 break;
3327 case NL80211_IFTYPE_ADHOC:
3328 if (intbss == wdev->current_bss)
3329 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3330 NL80211_BSS_STATUS_IBSS_JOINED);
3331 break;
3332 default:
3333 break;
3334 }
3335
Johannes Berg2a519312009-02-10 21:25:55 +01003336 nla_nest_end(msg, bss);
3337
3338 return genlmsg_end(msg, hdr);
3339
3340 nla_put_failure:
3341 genlmsg_cancel(msg, hdr);
3342 return -EMSGSIZE;
3343}
3344
3345static int nl80211_dump_scan(struct sk_buff *skb,
3346 struct netlink_callback *cb)
3347{
Johannes Berg48ab9052009-07-10 18:42:31 +02003348 struct cfg80211_registered_device *rdev;
3349 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003350 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003351 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003352 int start = cb->args[1], idx = 0;
3353 int err;
3354
Johannes Berg67748892010-10-04 21:14:06 +02003355 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3356 if (err)
3357 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003358
Johannes Berg48ab9052009-07-10 18:42:31 +02003359 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003360
Johannes Berg48ab9052009-07-10 18:42:31 +02003361 wdev_lock(wdev);
3362 spin_lock_bh(&rdev->bss_lock);
3363 cfg80211_bss_expire(rdev);
3364
3365 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003366 if (++idx <= start)
3367 continue;
3368 if (nl80211_send_bss(skb,
3369 NETLINK_CB(cb->skb).pid,
3370 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003371 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003372 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003373 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003374 }
3375 }
3376
Johannes Berg48ab9052009-07-10 18:42:31 +02003377 spin_unlock_bh(&rdev->bss_lock);
3378 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003379
3380 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003381 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003382
Johannes Berg67748892010-10-04 21:14:06 +02003383 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003384}
3385
Holger Schurig61fa7132009-11-11 12:25:40 +01003386static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3387 int flags, struct net_device *dev,
3388 struct survey_info *survey)
3389{
3390 void *hdr;
3391 struct nlattr *infoattr;
3392
3393 /* Survey without a channel doesn't make sense */
3394 if (!survey->channel)
3395 return -EINVAL;
3396
3397 hdr = nl80211hdr_put(msg, pid, seq, flags,
3398 NL80211_CMD_NEW_SURVEY_RESULTS);
3399 if (!hdr)
3400 return -ENOMEM;
3401
3402 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3403
3404 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3405 if (!infoattr)
3406 goto nla_put_failure;
3407
3408 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3409 survey->channel->center_freq);
3410 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3411 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3412 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003413 if (survey->filled & SURVEY_INFO_IN_USE)
3414 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02003415 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3416 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3417 survey->channel_time);
3418 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3419 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3420 survey->channel_time_busy);
3421 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3422 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3423 survey->channel_time_ext_busy);
3424 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3425 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3426 survey->channel_time_rx);
3427 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3428 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3429 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003430
3431 nla_nest_end(msg, infoattr);
3432
3433 return genlmsg_end(msg, hdr);
3434
3435 nla_put_failure:
3436 genlmsg_cancel(msg, hdr);
3437 return -EMSGSIZE;
3438}
3439
3440static int nl80211_dump_survey(struct sk_buff *skb,
3441 struct netlink_callback *cb)
3442{
3443 struct survey_info survey;
3444 struct cfg80211_registered_device *dev;
3445 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003446 int survey_idx = cb->args[1];
3447 int res;
3448
Johannes Berg67748892010-10-04 21:14:06 +02003449 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3450 if (res)
3451 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003452
3453 if (!dev->ops->dump_survey) {
3454 res = -EOPNOTSUPP;
3455 goto out_err;
3456 }
3457
3458 while (1) {
3459 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3460 &survey);
3461 if (res == -ENOENT)
3462 break;
3463 if (res)
3464 goto out_err;
3465
3466 if (nl80211_send_survey(skb,
3467 NETLINK_CB(cb->skb).pid,
3468 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3469 netdev,
3470 &survey) < 0)
3471 goto out;
3472 survey_idx++;
3473 }
3474
3475 out:
3476 cb->args[1] = survey_idx;
3477 res = skb->len;
3478 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003479 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003480 return res;
3481}
3482
Jouni Malinen255e7372009-03-20 21:21:17 +02003483static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3484{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003485 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003486}
3487
Samuel Ortizb23aa672009-07-01 21:26:54 +02003488static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3489{
3490 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3491 NL80211_WPA_VERSION_2));
3492}
3493
3494static bool nl80211_valid_akm_suite(u32 akm)
3495{
3496 return akm == WLAN_AKM_SUITE_8021X ||
3497 akm == WLAN_AKM_SUITE_PSK;
3498}
3499
3500static bool nl80211_valid_cipher_suite(u32 cipher)
3501{
3502 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3503 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3504 cipher == WLAN_CIPHER_SUITE_TKIP ||
3505 cipher == WLAN_CIPHER_SUITE_CCMP ||
3506 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3507}
3508
3509
Jouni Malinen636a5d32009-03-19 13:39:22 +02003510static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3511{
Johannes Berg4c476992010-10-04 21:36:35 +02003512 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3513 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003514 struct ieee80211_channel *chan;
3515 const u8 *bssid, *ssid, *ie = NULL;
3516 int err, ssid_len, ie_len = 0;
3517 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003518 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003519 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003520
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003521 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3522 return -EINVAL;
3523
3524 if (!info->attrs[NL80211_ATTR_MAC])
3525 return -EINVAL;
3526
Jouni Malinen17780922009-03-27 20:52:47 +02003527 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3528 return -EINVAL;
3529
Johannes Berg19957bb2009-07-02 17:20:43 +02003530 if (!info->attrs[NL80211_ATTR_SSID])
3531 return -EINVAL;
3532
3533 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3534 return -EINVAL;
3535
Johannes Bergfffd0932009-07-08 14:22:54 +02003536 err = nl80211_parse_key(info, &key);
3537 if (err)
3538 return err;
3539
3540 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003541 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3542 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003543 if (!key.p.key || !key.p.key_len)
3544 return -EINVAL;
3545 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3546 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3547 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3548 key.p.key_len != WLAN_KEY_LEN_WEP104))
3549 return -EINVAL;
3550 if (key.idx > 4)
3551 return -EINVAL;
3552 } else {
3553 key.p.key_len = 0;
3554 key.p.key = NULL;
3555 }
3556
Johannes Bergafea0b72010-08-10 09:46:42 +02003557 if (key.idx >= 0) {
3558 int i;
3559 bool ok = false;
3560 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3561 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3562 ok = true;
3563 break;
3564 }
3565 }
Johannes Berg4c476992010-10-04 21:36:35 +02003566 if (!ok)
3567 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003568 }
3569
Johannes Berg4c476992010-10-04 21:36:35 +02003570 if (!rdev->ops->auth)
3571 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003572
Johannes Berg074ac8d2010-09-16 14:58:22 +02003573 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003574 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3575 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003576
Johannes Berg19957bb2009-07-02 17:20:43 +02003577 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003578 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003579 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003580 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3581 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003582
Johannes Berg19957bb2009-07-02 17:20:43 +02003583 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3584 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3585
3586 if (info->attrs[NL80211_ATTR_IE]) {
3587 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3588 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3589 }
3590
3591 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003592 if (!nl80211_valid_auth_type(auth_type))
3593 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003594
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003595 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3596
Johannes Berg4c476992010-10-04 21:36:35 +02003597 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3598 ssid, ssid_len, ie, ie_len,
3599 key.p.key, key.p.key_len, key.idx,
3600 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003601}
3602
Johannes Bergc0692b82010-08-27 14:26:53 +03003603static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3604 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003605 struct cfg80211_crypto_settings *settings,
3606 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003607{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003608 memset(settings, 0, sizeof(*settings));
3609
Samuel Ortizb23aa672009-07-01 21:26:54 +02003610 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3611
Johannes Bergc0692b82010-08-27 14:26:53 +03003612 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3613 u16 proto;
3614 proto = nla_get_u16(
3615 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3616 settings->control_port_ethertype = cpu_to_be16(proto);
3617 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3618 proto != ETH_P_PAE)
3619 return -EINVAL;
3620 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3621 settings->control_port_no_encrypt = true;
3622 } else
3623 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3624
Samuel Ortizb23aa672009-07-01 21:26:54 +02003625 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3626 void *data;
3627 int len, i;
3628
3629 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3630 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3631 settings->n_ciphers_pairwise = len / sizeof(u32);
3632
3633 if (len % sizeof(u32))
3634 return -EINVAL;
3635
Johannes Berg3dc27d22009-07-02 21:36:37 +02003636 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003637 return -EINVAL;
3638
3639 memcpy(settings->ciphers_pairwise, data, len);
3640
3641 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3642 if (!nl80211_valid_cipher_suite(
3643 settings->ciphers_pairwise[i]))
3644 return -EINVAL;
3645 }
3646
3647 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3648 settings->cipher_group =
3649 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3650 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3651 return -EINVAL;
3652 }
3653
3654 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3655 settings->wpa_versions =
3656 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3657 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3658 return -EINVAL;
3659 }
3660
3661 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3662 void *data;
3663 int len, i;
3664
3665 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3666 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3667 settings->n_akm_suites = len / sizeof(u32);
3668
3669 if (len % sizeof(u32))
3670 return -EINVAL;
3671
3672 memcpy(settings->akm_suites, data, len);
3673
3674 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3675 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3676 return -EINVAL;
3677 }
3678
3679 return 0;
3680}
3681
Jouni Malinen636a5d32009-03-19 13:39:22 +02003682static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3683{
Johannes Berg4c476992010-10-04 21:36:35 +02003684 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3685 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003686 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003687 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003688 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003689 int err, ssid_len, ie_len = 0;
3690 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003691
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003692 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3693 return -EINVAL;
3694
3695 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003696 !info->attrs[NL80211_ATTR_SSID] ||
3697 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003698 return -EINVAL;
3699
Johannes Berg4c476992010-10-04 21:36:35 +02003700 if (!rdev->ops->assoc)
3701 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003702
Johannes Berg074ac8d2010-09-16 14:58:22 +02003703 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003704 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3705 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003706
Johannes Berg19957bb2009-07-02 17:20:43 +02003707 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003708
Johannes Berg19957bb2009-07-02 17:20:43 +02003709 chan = ieee80211_get_channel(&rdev->wiphy,
3710 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003711 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3712 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003713
Johannes Berg19957bb2009-07-02 17:20:43 +02003714 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3715 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003716
3717 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003718 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3719 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003720 }
3721
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003722 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003723 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003724 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003725 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003726 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02003727 else if (mfp != NL80211_MFP_NO)
3728 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003729 }
3730
Johannes Berg3e5d7642009-07-07 14:37:26 +02003731 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3732 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3733
Johannes Bergc0692b82010-08-27 14:26:53 +03003734 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003735 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003736 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3737 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003738 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003739
Jouni Malinen636a5d32009-03-19 13:39:22 +02003740 return err;
3741}
3742
3743static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3744{
Johannes Berg4c476992010-10-04 21:36:35 +02003745 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3746 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003747 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003748 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003749 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003750 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003751
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003752 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3753 return -EINVAL;
3754
3755 if (!info->attrs[NL80211_ATTR_MAC])
3756 return -EINVAL;
3757
3758 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3759 return -EINVAL;
3760
Johannes Berg4c476992010-10-04 21:36:35 +02003761 if (!rdev->ops->deauth)
3762 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003763
Johannes Berg074ac8d2010-09-16 14:58:22 +02003764 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003765 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3766 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003767
Johannes Berg19957bb2009-07-02 17:20:43 +02003768 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003769
Johannes Berg19957bb2009-07-02 17:20:43 +02003770 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3771 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003772 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003773 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003774 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003775
3776 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003777 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3778 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003779 }
3780
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003781 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3782
Johannes Berg4c476992010-10-04 21:36:35 +02003783 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3784 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003785}
3786
3787static int nl80211_disassociate(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->disassoc)
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_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3828 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003829}
3830
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003831static bool
3832nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
3833 int mcast_rate[IEEE80211_NUM_BANDS],
3834 int rateval)
3835{
3836 struct wiphy *wiphy = &rdev->wiphy;
3837 bool found = false;
3838 int band, i;
3839
3840 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3841 struct ieee80211_supported_band *sband;
3842
3843 sband = wiphy->bands[band];
3844 if (!sband)
3845 continue;
3846
3847 for (i = 0; i < sband->n_bitrates; i++) {
3848 if (sband->bitrates[i].bitrate == rateval) {
3849 mcast_rate[band] = i + 1;
3850 found = true;
3851 break;
3852 }
3853 }
3854 }
3855
3856 return found;
3857}
3858
Johannes Berg04a773a2009-04-19 21:24:32 +02003859static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3860{
Johannes Berg4c476992010-10-04 21:36:35 +02003861 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3862 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003863 struct cfg80211_ibss_params ibss;
3864 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003865 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003866 int err;
3867
Johannes Berg8e30bc52009-04-22 17:45:38 +02003868 memset(&ibss, 0, sizeof(ibss));
3869
Johannes Berg04a773a2009-04-19 21:24:32 +02003870 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3871 return -EINVAL;
3872
3873 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3874 !info->attrs[NL80211_ATTR_SSID] ||
3875 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3876 return -EINVAL;
3877
Johannes Berg8e30bc52009-04-22 17:45:38 +02003878 ibss.beacon_interval = 100;
3879
3880 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3881 ibss.beacon_interval =
3882 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3883 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3884 return -EINVAL;
3885 }
3886
Johannes Berg4c476992010-10-04 21:36:35 +02003887 if (!rdev->ops->join_ibss)
3888 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003889
Johannes Berg4c476992010-10-04 21:36:35 +02003890 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3891 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003892
Johannes Berg79c97e92009-07-07 03:56:12 +02003893 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003894
3895 if (info->attrs[NL80211_ATTR_MAC])
3896 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3897 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3898 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3899
3900 if (info->attrs[NL80211_ATTR_IE]) {
3901 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3902 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3903 }
3904
3905 ibss.channel = ieee80211_get_channel(wiphy,
3906 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3907 if (!ibss.channel ||
3908 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02003909 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3910 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003911
3912 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003913 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003914
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003915 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3916 u8 *rates =
3917 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3918 int n_rates =
3919 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3920 struct ieee80211_supported_band *sband =
3921 wiphy->bands[ibss.channel->band];
3922 int i, j;
3923
Johannes Berg4c476992010-10-04 21:36:35 +02003924 if (n_rates == 0)
3925 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003926
3927 for (i = 0; i < n_rates; i++) {
3928 int rate = (rates[i] & 0x7f) * 5;
3929 bool found = false;
3930
3931 for (j = 0; j < sband->n_bitrates; j++) {
3932 if (sband->bitrates[j].bitrate == rate) {
3933 found = true;
3934 ibss.basic_rates |= BIT(j);
3935 break;
3936 }
3937 }
Johannes Berg4c476992010-10-04 21:36:35 +02003938 if (!found)
3939 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003940 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003941 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003942
3943 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
3944 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
3945 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
3946 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003947
Johannes Berg4c476992010-10-04 21:36:35 +02003948 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3949 connkeys = nl80211_parse_connkeys(rdev,
3950 info->attrs[NL80211_ATTR_KEYS]);
3951 if (IS_ERR(connkeys))
3952 return PTR_ERR(connkeys);
3953 }
Johannes Berg04a773a2009-04-19 21:24:32 +02003954
Johannes Berg4c476992010-10-04 21:36:35 +02003955 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003956 if (err)
3957 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003958 return err;
3959}
3960
3961static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3962{
Johannes Berg4c476992010-10-04 21:36:35 +02003963 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3964 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003965
Johannes Berg4c476992010-10-04 21:36:35 +02003966 if (!rdev->ops->leave_ibss)
3967 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003968
Johannes Berg4c476992010-10-04 21:36:35 +02003969 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3970 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003971
Johannes Berg4c476992010-10-04 21:36:35 +02003972 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003973}
3974
Johannes Bergaff89a92009-07-01 21:26:51 +02003975#ifdef CONFIG_NL80211_TESTMODE
3976static struct genl_multicast_group nl80211_testmode_mcgrp = {
3977 .name = "testmode",
3978};
3979
3980static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3981{
Johannes Berg4c476992010-10-04 21:36:35 +02003982 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02003983 int err;
3984
3985 if (!info->attrs[NL80211_ATTR_TESTDATA])
3986 return -EINVAL;
3987
Johannes Bergaff89a92009-07-01 21:26:51 +02003988 err = -EOPNOTSUPP;
3989 if (rdev->ops->testmode_cmd) {
3990 rdev->testmode_info = info;
3991 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3992 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3993 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3994 rdev->testmode_info = NULL;
3995 }
3996
Johannes Bergaff89a92009-07-01 21:26:51 +02003997 return err;
3998}
3999
4000static struct sk_buff *
4001__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4002 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4003{
4004 struct sk_buff *skb;
4005 void *hdr;
4006 struct nlattr *data;
4007
4008 skb = nlmsg_new(approxlen + 100, gfp);
4009 if (!skb)
4010 return NULL;
4011
4012 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4013 if (!hdr) {
4014 kfree_skb(skb);
4015 return NULL;
4016 }
4017
4018 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4019 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4020
4021 ((void **)skb->cb)[0] = rdev;
4022 ((void **)skb->cb)[1] = hdr;
4023 ((void **)skb->cb)[2] = data;
4024
4025 return skb;
4026
4027 nla_put_failure:
4028 kfree_skb(skb);
4029 return NULL;
4030}
4031
4032struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4033 int approxlen)
4034{
4035 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4036
4037 if (WARN_ON(!rdev->testmode_info))
4038 return NULL;
4039
4040 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4041 rdev->testmode_info->snd_pid,
4042 rdev->testmode_info->snd_seq,
4043 GFP_KERNEL);
4044}
4045EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4046
4047int cfg80211_testmode_reply(struct sk_buff *skb)
4048{
4049 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4050 void *hdr = ((void **)skb->cb)[1];
4051 struct nlattr *data = ((void **)skb->cb)[2];
4052
4053 if (WARN_ON(!rdev->testmode_info)) {
4054 kfree_skb(skb);
4055 return -EINVAL;
4056 }
4057
4058 nla_nest_end(skb, data);
4059 genlmsg_end(skb, hdr);
4060 return genlmsg_reply(skb, rdev->testmode_info);
4061}
4062EXPORT_SYMBOL(cfg80211_testmode_reply);
4063
4064struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4065 int approxlen, gfp_t gfp)
4066{
4067 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4068
4069 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4070}
4071EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4072
4073void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4074{
4075 void *hdr = ((void **)skb->cb)[1];
4076 struct nlattr *data = ((void **)skb->cb)[2];
4077
4078 nla_nest_end(skb, data);
4079 genlmsg_end(skb, hdr);
4080 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4081}
4082EXPORT_SYMBOL(cfg80211_testmode_event);
4083#endif
4084
Samuel Ortizb23aa672009-07-01 21:26:54 +02004085static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4086{
Johannes Berg4c476992010-10-04 21:36:35 +02004087 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4088 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004089 struct cfg80211_connect_params connect;
4090 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004091 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004092 int err;
4093
4094 memset(&connect, 0, sizeof(connect));
4095
4096 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4097 return -EINVAL;
4098
4099 if (!info->attrs[NL80211_ATTR_SSID] ||
4100 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4101 return -EINVAL;
4102
4103 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4104 connect.auth_type =
4105 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4106 if (!nl80211_valid_auth_type(connect.auth_type))
4107 return -EINVAL;
4108 } else
4109 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4110
4111 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4112
Johannes Bergc0692b82010-08-27 14:26:53 +03004113 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004114 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004115 if (err)
4116 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004117
Johannes Berg074ac8d2010-09-16 14:58:22 +02004118 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004119 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4120 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004121
Johannes Berg79c97e92009-07-07 03:56:12 +02004122 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004123
Samuel Ortizb23aa672009-07-01 21:26:54 +02004124 if (info->attrs[NL80211_ATTR_MAC])
4125 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4126 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4127 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4128
4129 if (info->attrs[NL80211_ATTR_IE]) {
4130 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4131 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4132 }
4133
4134 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4135 connect.channel =
4136 ieee80211_get_channel(wiphy,
4137 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4138 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004139 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4140 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004141 }
4142
Johannes Bergfffd0932009-07-08 14:22:54 +02004143 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4144 connkeys = nl80211_parse_connkeys(rdev,
4145 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004146 if (IS_ERR(connkeys))
4147 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004148 }
4149
4150 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004151 if (err)
4152 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004153 return err;
4154}
4155
4156static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4157{
Johannes Berg4c476992010-10-04 21:36:35 +02004158 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4159 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004160 u16 reason;
4161
4162 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4163 reason = WLAN_REASON_DEAUTH_LEAVING;
4164 else
4165 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4166
4167 if (reason == 0)
4168 return -EINVAL;
4169
Johannes Berg074ac8d2010-09-16 14:58:22 +02004170 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004171 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4172 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004173
Johannes Berg4c476992010-10-04 21:36:35 +02004174 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004175}
4176
Johannes Berg463d0182009-07-14 00:33:35 +02004177static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4178{
Johannes Berg4c476992010-10-04 21:36:35 +02004179 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004180 struct net *net;
4181 int err;
4182 u32 pid;
4183
4184 if (!info->attrs[NL80211_ATTR_PID])
4185 return -EINVAL;
4186
4187 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4188
Johannes Berg463d0182009-07-14 00:33:35 +02004189 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004190 if (IS_ERR(net))
4191 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004192
4193 err = 0;
4194
4195 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004196 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4197 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004198
Johannes Berg463d0182009-07-14 00:33:35 +02004199 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004200 return err;
4201}
4202
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004203static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4204{
Johannes Berg4c476992010-10-04 21:36:35 +02004205 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004206 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4207 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004208 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004209 struct cfg80211_pmksa pmksa;
4210
4211 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4212
4213 if (!info->attrs[NL80211_ATTR_MAC])
4214 return -EINVAL;
4215
4216 if (!info->attrs[NL80211_ATTR_PMKID])
4217 return -EINVAL;
4218
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004219 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4220 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4221
Johannes Berg074ac8d2010-09-16 14:58:22 +02004222 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004223 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4224 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004225
4226 switch (info->genlhdr->cmd) {
4227 case NL80211_CMD_SET_PMKSA:
4228 rdev_ops = rdev->ops->set_pmksa;
4229 break;
4230 case NL80211_CMD_DEL_PMKSA:
4231 rdev_ops = rdev->ops->del_pmksa;
4232 break;
4233 default:
4234 WARN_ON(1);
4235 break;
4236 }
4237
Johannes Berg4c476992010-10-04 21:36:35 +02004238 if (!rdev_ops)
4239 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004240
Johannes Berg4c476992010-10-04 21:36:35 +02004241 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004242}
4243
4244static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4245{
Johannes Berg4c476992010-10-04 21:36:35 +02004246 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4247 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004248
Johannes Berg074ac8d2010-09-16 14:58:22 +02004249 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004250 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4251 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004252
Johannes Berg4c476992010-10-04 21:36:35 +02004253 if (!rdev->ops->flush_pmksa)
4254 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004255
Johannes Berg4c476992010-10-04 21:36:35 +02004256 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004257}
4258
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004259static int nl80211_remain_on_channel(struct sk_buff *skb,
4260 struct genl_info *info)
4261{
Johannes Berg4c476992010-10-04 21:36:35 +02004262 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4263 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004264 struct ieee80211_channel *chan;
4265 struct sk_buff *msg;
4266 void *hdr;
4267 u64 cookie;
4268 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4269 u32 freq, duration;
4270 int err;
4271
4272 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4273 !info->attrs[NL80211_ATTR_DURATION])
4274 return -EINVAL;
4275
4276 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4277
4278 /*
4279 * We should be on that channel for at least one jiffie,
4280 * and more than 5 seconds seems excessive.
4281 */
Johannes Berga2939112010-12-14 17:54:28 +01004282 if (!duration || !msecs_to_jiffies(duration) ||
4283 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004284 return -EINVAL;
4285
Johannes Berg4c476992010-10-04 21:36:35 +02004286 if (!rdev->ops->remain_on_channel)
4287 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004288
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004289 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4290 channel_type = nla_get_u32(
4291 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4292 if (channel_type != NL80211_CHAN_NO_HT &&
4293 channel_type != NL80211_CHAN_HT20 &&
4294 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004295 channel_type != NL80211_CHAN_HT40MINUS)
4296 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004297 }
4298
4299 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4300 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004301 if (chan == NULL)
4302 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004303
4304 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004305 if (!msg)
4306 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004307
4308 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4309 NL80211_CMD_REMAIN_ON_CHANNEL);
4310
4311 if (IS_ERR(hdr)) {
4312 err = PTR_ERR(hdr);
4313 goto free_msg;
4314 }
4315
4316 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4317 channel_type, duration, &cookie);
4318
4319 if (err)
4320 goto free_msg;
4321
4322 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4323
4324 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004325
4326 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004327
4328 nla_put_failure:
4329 err = -ENOBUFS;
4330 free_msg:
4331 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004332 return err;
4333}
4334
4335static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4336 struct genl_info *info)
4337{
Johannes Berg4c476992010-10-04 21:36:35 +02004338 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4339 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004340 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004341
4342 if (!info->attrs[NL80211_ATTR_COOKIE])
4343 return -EINVAL;
4344
Johannes Berg4c476992010-10-04 21:36:35 +02004345 if (!rdev->ops->cancel_remain_on_channel)
4346 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004347
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004348 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4349
Johannes Berg4c476992010-10-04 21:36:35 +02004350 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004351}
4352
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004353static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4354 u8 *rates, u8 rates_len)
4355{
4356 u8 i;
4357 u32 mask = 0;
4358
4359 for (i = 0; i < rates_len; i++) {
4360 int rate = (rates[i] & 0x7f) * 5;
4361 int ridx;
4362 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4363 struct ieee80211_rate *srate =
4364 &sband->bitrates[ridx];
4365 if (rate == srate->bitrate) {
4366 mask |= 1 << ridx;
4367 break;
4368 }
4369 }
4370 if (ridx == sband->n_bitrates)
4371 return 0; /* rate not found */
4372 }
4373
4374 return mask;
4375}
4376
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004377static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004378 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4379 .len = NL80211_MAX_SUPP_RATES },
4380};
4381
4382static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4383 struct genl_info *info)
4384{
4385 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004386 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004387 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004388 int rem, i;
4389 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004390 struct nlattr *tx_rates;
4391 struct ieee80211_supported_band *sband;
4392
4393 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4394 return -EINVAL;
4395
Johannes Berg4c476992010-10-04 21:36:35 +02004396 if (!rdev->ops->set_bitrate_mask)
4397 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004398
4399 memset(&mask, 0, sizeof(mask));
4400 /* Default to all rates enabled */
4401 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4402 sband = rdev->wiphy.bands[i];
4403 mask.control[i].legacy =
4404 sband ? (1 << sband->n_bitrates) - 1 : 0;
4405 }
4406
4407 /*
4408 * The nested attribute uses enum nl80211_band as the index. This maps
4409 * directly to the enum ieee80211_band values used in cfg80211.
4410 */
4411 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4412 {
4413 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004414 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4415 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004416 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004417 if (sband == NULL)
4418 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004419 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4420 nla_len(tx_rates), nl80211_txattr_policy);
4421 if (tb[NL80211_TXRATE_LEGACY]) {
4422 mask.control[band].legacy = rateset_to_mask(
4423 sband,
4424 nla_data(tb[NL80211_TXRATE_LEGACY]),
4425 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004426 if (mask.control[band].legacy == 0)
4427 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004428 }
4429 }
4430
Johannes Berg4c476992010-10-04 21:36:35 +02004431 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004432}
4433
Johannes Berg2e161f72010-08-12 15:38:38 +02004434static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004435{
Johannes Berg4c476992010-10-04 21:36:35 +02004436 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4437 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004438 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004439
4440 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4441 return -EINVAL;
4442
Johannes Berg2e161f72010-08-12 15:38:38 +02004443 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4444 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004445
Johannes Berg9d38d852010-06-09 17:20:33 +02004446 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004447 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004448 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4449 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4450 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08004451 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004452 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4453 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004454
4455 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004456 if (!rdev->ops->mgmt_tx)
4457 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004458
Johannes Berg4c476992010-10-04 21:36:35 +02004459 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004460 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004461 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4462 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004463}
4464
Johannes Berg2e161f72010-08-12 15:38:38 +02004465static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004466{
Johannes Berg4c476992010-10-04 21:36:35 +02004467 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4468 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004469 struct ieee80211_channel *chan;
4470 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004471 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004472 u32 freq;
4473 int err;
4474 void *hdr;
4475 u64 cookie;
4476 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004477 unsigned int wait = 0;
4478 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02004479
4480 if (!info->attrs[NL80211_ATTR_FRAME] ||
4481 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4482 return -EINVAL;
4483
Johannes Berg4c476992010-10-04 21:36:35 +02004484 if (!rdev->ops->mgmt_tx)
4485 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004486
Johannes Berg9d38d852010-06-09 17:20:33 +02004487 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004488 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004489 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4490 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4491 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08004492 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02004493 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4494 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004495
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004496 if (info->attrs[NL80211_ATTR_DURATION]) {
4497 if (!rdev->ops->mgmt_tx_cancel_wait)
4498 return -EINVAL;
4499 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4500 }
4501
Jouni Malinen026331c2010-02-15 12:53:10 +02004502 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4503 channel_type = nla_get_u32(
4504 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4505 if (channel_type != NL80211_CHAN_NO_HT &&
4506 channel_type != NL80211_CHAN_HT20 &&
4507 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004508 channel_type != NL80211_CHAN_HT40MINUS)
4509 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004510 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004511 }
4512
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004513 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
4514
Jouni Malinen026331c2010-02-15 12:53:10 +02004515 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4516 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004517 if (chan == NULL)
4518 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004519
4520 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004521 if (!msg)
4522 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004523
4524 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004525 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004526
4527 if (IS_ERR(hdr)) {
4528 err = PTR_ERR(hdr);
4529 goto free_msg;
4530 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004531 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
4532 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02004533 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4534 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4535 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004536 if (err)
4537 goto free_msg;
4538
4539 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4540
4541 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004542 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004543
4544 nla_put_failure:
4545 err = -ENOBUFS;
4546 free_msg:
4547 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004548 return err;
4549}
4550
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004551static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
4552{
4553 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4554 struct net_device *dev = info->user_ptr[1];
4555 u64 cookie;
4556
4557 if (!info->attrs[NL80211_ATTR_COOKIE])
4558 return -EINVAL;
4559
4560 if (!rdev->ops->mgmt_tx_cancel_wait)
4561 return -EOPNOTSUPP;
4562
4563 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4564 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4565 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4566 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4567 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4568 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4569 return -EOPNOTSUPP;
4570
4571 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4572
4573 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
4574}
4575
Kalle Valoffb9eb32010-02-17 17:58:10 +02004576static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4577{
Johannes Berg4c476992010-10-04 21:36:35 +02004578 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004579 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004580 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004581 u8 ps_state;
4582 bool state;
4583 int err;
4584
Johannes Berg4c476992010-10-04 21:36:35 +02004585 if (!info->attrs[NL80211_ATTR_PS_STATE])
4586 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004587
4588 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4589
Johannes Berg4c476992010-10-04 21:36:35 +02004590 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4591 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004592
4593 wdev = dev->ieee80211_ptr;
4594
Johannes Berg4c476992010-10-04 21:36:35 +02004595 if (!rdev->ops->set_power_mgmt)
4596 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004597
4598 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4599
4600 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004601 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004602
Johannes Berg4c476992010-10-04 21:36:35 +02004603 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4604 wdev->ps_timeout);
4605 if (!err)
4606 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004607 return err;
4608}
4609
4610static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4611{
Johannes Berg4c476992010-10-04 21:36:35 +02004612 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004613 enum nl80211_ps_state ps_state;
4614 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004615 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004616 struct sk_buff *msg;
4617 void *hdr;
4618 int err;
4619
Kalle Valoffb9eb32010-02-17 17:58:10 +02004620 wdev = dev->ieee80211_ptr;
4621
Johannes Berg4c476992010-10-04 21:36:35 +02004622 if (!rdev->ops->set_power_mgmt)
4623 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004624
4625 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004626 if (!msg)
4627 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004628
4629 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4630 NL80211_CMD_GET_POWER_SAVE);
4631 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004632 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004633 goto free_msg;
4634 }
4635
4636 if (wdev->ps)
4637 ps_state = NL80211_PS_ENABLED;
4638 else
4639 ps_state = NL80211_PS_DISABLED;
4640
4641 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4642
4643 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004644 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004645
Johannes Berg4c476992010-10-04 21:36:35 +02004646 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004647 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004648 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004649 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004650 return err;
4651}
4652
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004653static struct nla_policy
4654nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4655 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4656 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4657 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4658};
4659
4660static int nl80211_set_cqm_rssi(struct genl_info *info,
4661 s32 threshold, u32 hysteresis)
4662{
Johannes Berg4c476992010-10-04 21:36:35 +02004663 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004664 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004665 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004666
4667 if (threshold > 0)
4668 return -EINVAL;
4669
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004670 wdev = dev->ieee80211_ptr;
4671
Johannes Berg4c476992010-10-04 21:36:35 +02004672 if (!rdev->ops->set_cqm_rssi_config)
4673 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004674
Johannes Berg074ac8d2010-09-16 14:58:22 +02004675 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004676 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4677 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004678
Johannes Berg4c476992010-10-04 21:36:35 +02004679 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4680 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004681}
4682
4683static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4684{
4685 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4686 struct nlattr *cqm;
4687 int err;
4688
4689 cqm = info->attrs[NL80211_ATTR_CQM];
4690 if (!cqm) {
4691 err = -EINVAL;
4692 goto out;
4693 }
4694
4695 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4696 nl80211_attr_cqm_policy);
4697 if (err)
4698 goto out;
4699
4700 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4701 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4702 s32 threshold;
4703 u32 hysteresis;
4704 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4705 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4706 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4707 } else
4708 err = -EINVAL;
4709
4710out:
4711 return err;
4712}
4713
Johannes Berg29cbe682010-12-03 09:20:44 +01004714static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
4715{
4716 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4717 struct net_device *dev = info->user_ptr[1];
4718 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08004719 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01004720 int err;
4721
4722 /* start with default */
4723 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08004724 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01004725
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004726 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01004727 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08004728 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01004729 if (err)
4730 return err;
4731 }
4732
4733 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
4734 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
4735 return -EINVAL;
4736
Javier Cardonac80d5452010-12-16 17:37:49 -08004737 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
4738 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
4739
4740 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
4741 /* parse additional setup parameters if given */
4742 err = nl80211_parse_mesh_setup(info, &setup);
4743 if (err)
4744 return err;
4745 }
4746
4747 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01004748}
4749
4750static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
4751{
4752 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4753 struct net_device *dev = info->user_ptr[1];
4754
4755 return cfg80211_leave_mesh(rdev, dev);
4756}
4757
Johannes Berg4c476992010-10-04 21:36:35 +02004758#define NL80211_FLAG_NEED_WIPHY 0x01
4759#define NL80211_FLAG_NEED_NETDEV 0x02
4760#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02004761#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
4762#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
4763 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02004764
4765static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4766 struct genl_info *info)
4767{
4768 struct cfg80211_registered_device *rdev;
4769 struct net_device *dev;
4770 int err;
4771 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4772
4773 if (rtnl)
4774 rtnl_lock();
4775
4776 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4777 rdev = cfg80211_get_dev_from_info(info);
4778 if (IS_ERR(rdev)) {
4779 if (rtnl)
4780 rtnl_unlock();
4781 return PTR_ERR(rdev);
4782 }
4783 info->user_ptr[0] = rdev;
4784 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4785 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4786 if (err) {
4787 if (rtnl)
4788 rtnl_unlock();
4789 return err;
4790 }
Johannes Berg41265712010-10-04 21:14:05 +02004791 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4792 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02004793 cfg80211_unlock_rdev(rdev);
4794 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02004795 if (rtnl)
4796 rtnl_unlock();
4797 return -ENETDOWN;
4798 }
Johannes Berg4c476992010-10-04 21:36:35 +02004799 info->user_ptr[0] = rdev;
4800 info->user_ptr[1] = dev;
4801 }
4802
4803 return 0;
4804}
4805
4806static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4807 struct genl_info *info)
4808{
4809 if (info->user_ptr[0])
4810 cfg80211_unlock_rdev(info->user_ptr[0]);
4811 if (info->user_ptr[1])
4812 dev_put(info->user_ptr[1]);
4813 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4814 rtnl_unlock();
4815}
4816
Johannes Berg55682962007-09-20 13:09:35 -04004817static struct genl_ops nl80211_ops[] = {
4818 {
4819 .cmd = NL80211_CMD_GET_WIPHY,
4820 .doit = nl80211_get_wiphy,
4821 .dumpit = nl80211_dump_wiphy,
4822 .policy = nl80211_policy,
4823 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004824 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04004825 },
4826 {
4827 .cmd = NL80211_CMD_SET_WIPHY,
4828 .doit = nl80211_set_wiphy,
4829 .policy = nl80211_policy,
4830 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004831 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004832 },
4833 {
4834 .cmd = NL80211_CMD_GET_INTERFACE,
4835 .doit = nl80211_get_interface,
4836 .dumpit = nl80211_dump_interface,
4837 .policy = nl80211_policy,
4838 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004839 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04004840 },
4841 {
4842 .cmd = NL80211_CMD_SET_INTERFACE,
4843 .doit = nl80211_set_interface,
4844 .policy = nl80211_policy,
4845 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004846 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4847 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004848 },
4849 {
4850 .cmd = NL80211_CMD_NEW_INTERFACE,
4851 .doit = nl80211_new_interface,
4852 .policy = nl80211_policy,
4853 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004854 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4855 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004856 },
4857 {
4858 .cmd = NL80211_CMD_DEL_INTERFACE,
4859 .doit = nl80211_del_interface,
4860 .policy = nl80211_policy,
4861 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004862 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4863 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004864 },
Johannes Berg41ade002007-12-19 02:03:29 +01004865 {
4866 .cmd = NL80211_CMD_GET_KEY,
4867 .doit = nl80211_get_key,
4868 .policy = nl80211_policy,
4869 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004870 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4871 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004872 },
4873 {
4874 .cmd = NL80211_CMD_SET_KEY,
4875 .doit = nl80211_set_key,
4876 .policy = nl80211_policy,
4877 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004878 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004879 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004880 },
4881 {
4882 .cmd = NL80211_CMD_NEW_KEY,
4883 .doit = nl80211_new_key,
4884 .policy = nl80211_policy,
4885 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004886 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004887 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004888 },
4889 {
4890 .cmd = NL80211_CMD_DEL_KEY,
4891 .doit = nl80211_del_key,
4892 .policy = nl80211_policy,
4893 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004894 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004895 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004896 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004897 {
4898 .cmd = NL80211_CMD_SET_BEACON,
4899 .policy = nl80211_policy,
4900 .flags = GENL_ADMIN_PERM,
4901 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004902 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4903 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004904 },
4905 {
4906 .cmd = NL80211_CMD_NEW_BEACON,
4907 .policy = nl80211_policy,
4908 .flags = GENL_ADMIN_PERM,
4909 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004910 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4911 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004912 },
4913 {
4914 .cmd = NL80211_CMD_DEL_BEACON,
4915 .policy = nl80211_policy,
4916 .flags = GENL_ADMIN_PERM,
4917 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004918 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4919 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004920 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004921 {
4922 .cmd = NL80211_CMD_GET_STATION,
4923 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004924 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004925 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02004926 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4927 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004928 },
4929 {
4930 .cmd = NL80211_CMD_SET_STATION,
4931 .doit = nl80211_set_station,
4932 .policy = nl80211_policy,
4933 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004934 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4935 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004936 },
4937 {
4938 .cmd = NL80211_CMD_NEW_STATION,
4939 .doit = nl80211_new_station,
4940 .policy = nl80211_policy,
4941 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004942 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004943 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004944 },
4945 {
4946 .cmd = NL80211_CMD_DEL_STATION,
4947 .doit = nl80211_del_station,
4948 .policy = nl80211_policy,
4949 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004950 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4951 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004952 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004953 {
4954 .cmd = NL80211_CMD_GET_MPATH,
4955 .doit = nl80211_get_mpath,
4956 .dumpit = nl80211_dump_mpath,
4957 .policy = nl80211_policy,
4958 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004959 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004960 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004961 },
4962 {
4963 .cmd = NL80211_CMD_SET_MPATH,
4964 .doit = nl80211_set_mpath,
4965 .policy = nl80211_policy,
4966 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004967 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004968 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004969 },
4970 {
4971 .cmd = NL80211_CMD_NEW_MPATH,
4972 .doit = nl80211_new_mpath,
4973 .policy = nl80211_policy,
4974 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004975 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004976 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004977 },
4978 {
4979 .cmd = NL80211_CMD_DEL_MPATH,
4980 .doit = nl80211_del_mpath,
4981 .policy = nl80211_policy,
4982 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004983 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4984 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004985 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004986 {
4987 .cmd = NL80211_CMD_SET_BSS,
4988 .doit = nl80211_set_bss,
4989 .policy = nl80211_policy,
4990 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004991 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4992 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004993 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004994 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004995 .cmd = NL80211_CMD_GET_REG,
4996 .doit = nl80211_get_reg,
4997 .policy = nl80211_policy,
4998 /* can be retrieved by unprivileged users */
4999 },
5000 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005001 .cmd = NL80211_CMD_SET_REG,
5002 .doit = nl80211_set_reg,
5003 .policy = nl80211_policy,
5004 .flags = GENL_ADMIN_PERM,
5005 },
5006 {
5007 .cmd = NL80211_CMD_REQ_SET_REG,
5008 .doit = nl80211_req_set_reg,
5009 .policy = nl80211_policy,
5010 .flags = GENL_ADMIN_PERM,
5011 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005012 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005013 .cmd = NL80211_CMD_GET_MESH_CONFIG,
5014 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005015 .policy = nl80211_policy,
5016 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005017 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5018 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005019 },
5020 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005021 .cmd = NL80211_CMD_SET_MESH_CONFIG,
5022 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005023 .policy = nl80211_policy,
5024 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01005025 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005026 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005027 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005028 {
Johannes Berg2a519312009-02-10 21:25:55 +01005029 .cmd = NL80211_CMD_TRIGGER_SCAN,
5030 .doit = nl80211_trigger_scan,
5031 .policy = nl80211_policy,
5032 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005033 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005034 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01005035 },
5036 {
5037 .cmd = NL80211_CMD_GET_SCAN,
5038 .policy = nl80211_policy,
5039 .dumpit = nl80211_dump_scan,
5040 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005041 {
5042 .cmd = NL80211_CMD_AUTHENTICATE,
5043 .doit = nl80211_authenticate,
5044 .policy = nl80211_policy,
5045 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005046 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005047 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005048 },
5049 {
5050 .cmd = NL80211_CMD_ASSOCIATE,
5051 .doit = nl80211_associate,
5052 .policy = nl80211_policy,
5053 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005054 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005055 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005056 },
5057 {
5058 .cmd = NL80211_CMD_DEAUTHENTICATE,
5059 .doit = nl80211_deauthenticate,
5060 .policy = nl80211_policy,
5061 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005062 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005063 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005064 },
5065 {
5066 .cmd = NL80211_CMD_DISASSOCIATE,
5067 .doit = nl80211_disassociate,
5068 .policy = nl80211_policy,
5069 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005070 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005071 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005072 },
Johannes Berg04a773a2009-04-19 21:24:32 +02005073 {
5074 .cmd = NL80211_CMD_JOIN_IBSS,
5075 .doit = nl80211_join_ibss,
5076 .policy = nl80211_policy,
5077 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005078 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005079 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005080 },
5081 {
5082 .cmd = NL80211_CMD_LEAVE_IBSS,
5083 .doit = nl80211_leave_ibss,
5084 .policy = nl80211_policy,
5085 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005086 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005087 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005088 },
Johannes Bergaff89a92009-07-01 21:26:51 +02005089#ifdef CONFIG_NL80211_TESTMODE
5090 {
5091 .cmd = NL80211_CMD_TESTMODE,
5092 .doit = nl80211_testmode_do,
5093 .policy = nl80211_policy,
5094 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005095 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5096 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02005097 },
5098#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02005099 {
5100 .cmd = NL80211_CMD_CONNECT,
5101 .doit = nl80211_connect,
5102 .policy = nl80211_policy,
5103 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005104 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005105 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005106 },
5107 {
5108 .cmd = NL80211_CMD_DISCONNECT,
5109 .doit = nl80211_disconnect,
5110 .policy = nl80211_policy,
5111 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005112 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005113 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005114 },
Johannes Berg463d0182009-07-14 00:33:35 +02005115 {
5116 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
5117 .doit = nl80211_wiphy_netns,
5118 .policy = nl80211_policy,
5119 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005120 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5121 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02005122 },
Holger Schurig61fa7132009-11-11 12:25:40 +01005123 {
5124 .cmd = NL80211_CMD_GET_SURVEY,
5125 .policy = nl80211_policy,
5126 .dumpit = nl80211_dump_survey,
5127 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005128 {
5129 .cmd = NL80211_CMD_SET_PMKSA,
5130 .doit = nl80211_setdel_pmksa,
5131 .policy = nl80211_policy,
5132 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005133 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5134 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005135 },
5136 {
5137 .cmd = NL80211_CMD_DEL_PMKSA,
5138 .doit = nl80211_setdel_pmksa,
5139 .policy = nl80211_policy,
5140 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005141 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5142 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005143 },
5144 {
5145 .cmd = NL80211_CMD_FLUSH_PMKSA,
5146 .doit = nl80211_flush_pmksa,
5147 .policy = nl80211_policy,
5148 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005149 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5150 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01005151 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005152 {
5153 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5154 .doit = nl80211_remain_on_channel,
5155 .policy = nl80211_policy,
5156 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005157 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005158 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005159 },
5160 {
5161 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5162 .doit = nl80211_cancel_remain_on_channel,
5163 .policy = nl80211_policy,
5164 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005165 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005166 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005167 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005168 {
5169 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5170 .doit = nl80211_set_tx_bitrate_mask,
5171 .policy = nl80211_policy,
5172 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005173 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5174 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005175 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005176 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005177 .cmd = NL80211_CMD_REGISTER_FRAME,
5178 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005179 .policy = nl80211_policy,
5180 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005181 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5182 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005183 },
5184 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005185 .cmd = NL80211_CMD_FRAME,
5186 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005187 .policy = nl80211_policy,
5188 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005189 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005190 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005191 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005192 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005193 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
5194 .doit = nl80211_tx_mgmt_cancel_wait,
5195 .policy = nl80211_policy,
5196 .flags = GENL_ADMIN_PERM,
5197 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5198 NL80211_FLAG_NEED_RTNL,
5199 },
5200 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02005201 .cmd = NL80211_CMD_SET_POWER_SAVE,
5202 .doit = nl80211_set_power_save,
5203 .policy = nl80211_policy,
5204 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005205 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5206 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005207 },
5208 {
5209 .cmd = NL80211_CMD_GET_POWER_SAVE,
5210 .doit = nl80211_get_power_save,
5211 .policy = nl80211_policy,
5212 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005213 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5214 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005215 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005216 {
5217 .cmd = NL80211_CMD_SET_CQM,
5218 .doit = nl80211_set_cqm,
5219 .policy = nl80211_policy,
5220 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005221 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5222 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005223 },
Johannes Bergf444de02010-05-05 15:25:02 +02005224 {
5225 .cmd = NL80211_CMD_SET_CHANNEL,
5226 .doit = nl80211_set_channel,
5227 .policy = nl80211_policy,
5228 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005229 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5230 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02005231 },
Bill Jordane8347eb2010-10-01 13:54:28 -04005232 {
5233 .cmd = NL80211_CMD_SET_WDS_PEER,
5234 .doit = nl80211_set_wds_peer,
5235 .policy = nl80211_policy,
5236 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02005237 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5238 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04005239 },
Johannes Berg29cbe682010-12-03 09:20:44 +01005240 {
5241 .cmd = NL80211_CMD_JOIN_MESH,
5242 .doit = nl80211_join_mesh,
5243 .policy = nl80211_policy,
5244 .flags = GENL_ADMIN_PERM,
5245 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5246 NL80211_FLAG_NEED_RTNL,
5247 },
5248 {
5249 .cmd = NL80211_CMD_LEAVE_MESH,
5250 .doit = nl80211_leave_mesh,
5251 .policy = nl80211_policy,
5252 .flags = GENL_ADMIN_PERM,
5253 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5254 NL80211_FLAG_NEED_RTNL,
5255 },
Johannes Berg55682962007-09-20 13:09:35 -04005256};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005257
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005258static struct genl_multicast_group nl80211_mlme_mcgrp = {
5259 .name = "mlme",
5260};
Johannes Berg55682962007-09-20 13:09:35 -04005261
5262/* multicast groups */
5263static struct genl_multicast_group nl80211_config_mcgrp = {
5264 .name = "config",
5265};
Johannes Berg2a519312009-02-10 21:25:55 +01005266static struct genl_multicast_group nl80211_scan_mcgrp = {
5267 .name = "scan",
5268};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005269static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5270 .name = "regulatory",
5271};
Johannes Berg55682962007-09-20 13:09:35 -04005272
5273/* notification functions */
5274
5275void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5276{
5277 struct sk_buff *msg;
5278
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005279 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005280 if (!msg)
5281 return;
5282
5283 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5284 nlmsg_free(msg);
5285 return;
5286 }
5287
Johannes Berg463d0182009-07-14 00:33:35 +02005288 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5289 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005290}
5291
Johannes Berg362a4152009-05-24 16:43:15 +02005292static int nl80211_add_scan_req(struct sk_buff *msg,
5293 struct cfg80211_registered_device *rdev)
5294{
5295 struct cfg80211_scan_request *req = rdev->scan_req;
5296 struct nlattr *nest;
5297 int i;
5298
Johannes Berg667503d2009-07-07 03:56:11 +02005299 ASSERT_RDEV_LOCK(rdev);
5300
Johannes Berg362a4152009-05-24 16:43:15 +02005301 if (WARN_ON(!req))
5302 return 0;
5303
5304 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5305 if (!nest)
5306 goto nla_put_failure;
5307 for (i = 0; i < req->n_ssids; i++)
5308 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5309 nla_nest_end(msg, nest);
5310
5311 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5312 if (!nest)
5313 goto nla_put_failure;
5314 for (i = 0; i < req->n_channels; i++)
5315 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5316 nla_nest_end(msg, nest);
5317
5318 if (req->ie)
5319 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5320
5321 return 0;
5322 nla_put_failure:
5323 return -ENOBUFS;
5324}
5325
Johannes Berga538e2d2009-06-16 19:56:42 +02005326static int nl80211_send_scan_msg(struct sk_buff *msg,
5327 struct cfg80211_registered_device *rdev,
5328 struct net_device *netdev,
5329 u32 pid, u32 seq, int flags,
5330 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005331{
5332 void *hdr;
5333
5334 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5335 if (!hdr)
5336 return -1;
5337
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005338 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005339 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5340
Johannes Berg362a4152009-05-24 16:43:15 +02005341 /* ignore errors and send incomplete event anyway */
5342 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005343
5344 return genlmsg_end(msg, hdr);
5345
5346 nla_put_failure:
5347 genlmsg_cancel(msg, hdr);
5348 return -EMSGSIZE;
5349}
5350
Johannes Berga538e2d2009-06-16 19:56:42 +02005351void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5352 struct net_device *netdev)
5353{
5354 struct sk_buff *msg;
5355
5356 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5357 if (!msg)
5358 return;
5359
5360 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5361 NL80211_CMD_TRIGGER_SCAN) < 0) {
5362 nlmsg_free(msg);
5363 return;
5364 }
5365
Johannes Berg463d0182009-07-14 00:33:35 +02005366 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5367 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005368}
5369
Johannes Berg2a519312009-02-10 21:25:55 +01005370void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5371 struct net_device *netdev)
5372{
5373 struct sk_buff *msg;
5374
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005375 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005376 if (!msg)
5377 return;
5378
Johannes Berga538e2d2009-06-16 19:56:42 +02005379 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5380 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005381 nlmsg_free(msg);
5382 return;
5383 }
5384
Johannes Berg463d0182009-07-14 00:33:35 +02005385 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5386 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005387}
5388
5389void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5390 struct net_device *netdev)
5391{
5392 struct sk_buff *msg;
5393
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005394 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005395 if (!msg)
5396 return;
5397
Johannes Berga538e2d2009-06-16 19:56:42 +02005398 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5399 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005400 nlmsg_free(msg);
5401 return;
5402 }
5403
Johannes Berg463d0182009-07-14 00:33:35 +02005404 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5405 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005406}
5407
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005408/*
5409 * This can happen on global regulatory changes or device specific settings
5410 * based on custom world regulatory domains.
5411 */
5412void nl80211_send_reg_change_event(struct regulatory_request *request)
5413{
5414 struct sk_buff *msg;
5415 void *hdr;
5416
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005417 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005418 if (!msg)
5419 return;
5420
5421 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5422 if (!hdr) {
5423 nlmsg_free(msg);
5424 return;
5425 }
5426
5427 /* Userspace can always count this one always being set */
5428 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5429
5430 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5431 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5432 NL80211_REGDOM_TYPE_WORLD);
5433 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5434 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5435 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5436 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5437 request->intersect)
5438 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5439 NL80211_REGDOM_TYPE_INTERSECTION);
5440 else {
5441 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5442 NL80211_REGDOM_TYPE_COUNTRY);
5443 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5444 }
5445
5446 if (wiphy_idx_valid(request->wiphy_idx))
5447 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5448
5449 if (genlmsg_end(msg, hdr) < 0) {
5450 nlmsg_free(msg);
5451 return;
5452 }
5453
Johannes Bergbc43b282009-07-25 10:54:13 +02005454 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005455 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005456 GFP_ATOMIC);
5457 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005458
5459 return;
5460
5461nla_put_failure:
5462 genlmsg_cancel(msg, hdr);
5463 nlmsg_free(msg);
5464}
5465
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005466static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5467 struct net_device *netdev,
5468 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005469 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005470{
5471 struct sk_buff *msg;
5472 void *hdr;
5473
Johannes Berge6d6e342009-07-01 21:26:47 +02005474 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005475 if (!msg)
5476 return;
5477
5478 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5479 if (!hdr) {
5480 nlmsg_free(msg);
5481 return;
5482 }
5483
5484 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5485 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5486 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5487
5488 if (genlmsg_end(msg, hdr) < 0) {
5489 nlmsg_free(msg);
5490 return;
5491 }
5492
Johannes Berg463d0182009-07-14 00:33:35 +02005493 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5494 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005495 return;
5496
5497 nla_put_failure:
5498 genlmsg_cancel(msg, hdr);
5499 nlmsg_free(msg);
5500}
5501
5502void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005503 struct net_device *netdev, const u8 *buf,
5504 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005505{
5506 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005507 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005508}
5509
5510void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5511 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005512 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005513{
Johannes Berge6d6e342009-07-01 21:26:47 +02005514 nl80211_send_mlme_event(rdev, netdev, buf, len,
5515 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005516}
5517
Jouni Malinen53b46b82009-03-27 20:53:56 +02005518void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005519 struct net_device *netdev, const u8 *buf,
5520 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005521{
5522 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005523 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005524}
5525
Jouni Malinen53b46b82009-03-27 20:53:56 +02005526void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5527 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005528 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005529{
5530 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005531 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005532}
5533
Jouni Malinencf4e5942010-12-16 00:52:40 +02005534void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
5535 struct net_device *netdev, const u8 *buf,
5536 size_t len, gfp_t gfp)
5537{
5538 nl80211_send_mlme_event(rdev, netdev, buf, len,
5539 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
5540}
5541
5542void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
5543 struct net_device *netdev, const u8 *buf,
5544 size_t len, gfp_t gfp)
5545{
5546 nl80211_send_mlme_event(rdev, netdev, buf, len,
5547 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
5548}
5549
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005550static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5551 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005552 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005553{
5554 struct sk_buff *msg;
5555 void *hdr;
5556
Johannes Berge6d6e342009-07-01 21:26:47 +02005557 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005558 if (!msg)
5559 return;
5560
5561 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5562 if (!hdr) {
5563 nlmsg_free(msg);
5564 return;
5565 }
5566
5567 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5568 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5569 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5570 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5571
5572 if (genlmsg_end(msg, hdr) < 0) {
5573 nlmsg_free(msg);
5574 return;
5575 }
5576
Johannes Berg463d0182009-07-14 00:33:35 +02005577 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5578 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005579 return;
5580
5581 nla_put_failure:
5582 genlmsg_cancel(msg, hdr);
5583 nlmsg_free(msg);
5584}
5585
5586void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005587 struct net_device *netdev, const u8 *addr,
5588 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005589{
5590 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005591 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005592}
5593
5594void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005595 struct net_device *netdev, const u8 *addr,
5596 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005597{
Johannes Berge6d6e342009-07-01 21:26:47 +02005598 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5599 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005600}
5601
Samuel Ortizb23aa672009-07-01 21:26:54 +02005602void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5603 struct net_device *netdev, const u8 *bssid,
5604 const u8 *req_ie, size_t req_ie_len,
5605 const u8 *resp_ie, size_t resp_ie_len,
5606 u16 status, gfp_t gfp)
5607{
5608 struct sk_buff *msg;
5609 void *hdr;
5610
5611 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5612 if (!msg)
5613 return;
5614
5615 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5616 if (!hdr) {
5617 nlmsg_free(msg);
5618 return;
5619 }
5620
5621 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5622 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5623 if (bssid)
5624 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5625 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5626 if (req_ie)
5627 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5628 if (resp_ie)
5629 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5630
5631 if (genlmsg_end(msg, hdr) < 0) {
5632 nlmsg_free(msg);
5633 return;
5634 }
5635
Johannes Berg463d0182009-07-14 00:33:35 +02005636 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5637 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005638 return;
5639
5640 nla_put_failure:
5641 genlmsg_cancel(msg, hdr);
5642 nlmsg_free(msg);
5643
5644}
5645
5646void nl80211_send_roamed(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, gfp_t gfp)
5650{
5651 struct sk_buff *msg;
5652 void *hdr;
5653
5654 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5655 if (!msg)
5656 return;
5657
5658 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5659 if (!hdr) {
5660 nlmsg_free(msg);
5661 return;
5662 }
5663
5664 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5665 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5666 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5667 if (req_ie)
5668 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5669 if (resp_ie)
5670 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5671
5672 if (genlmsg_end(msg, hdr) < 0) {
5673 nlmsg_free(msg);
5674 return;
5675 }
5676
Johannes Berg463d0182009-07-14 00:33:35 +02005677 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5678 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005679 return;
5680
5681 nla_put_failure:
5682 genlmsg_cancel(msg, hdr);
5683 nlmsg_free(msg);
5684
5685}
5686
5687void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5688 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005689 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005690{
5691 struct sk_buff *msg;
5692 void *hdr;
5693
Johannes Berg667503d2009-07-07 03:56:11 +02005694 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005695 if (!msg)
5696 return;
5697
5698 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5699 if (!hdr) {
5700 nlmsg_free(msg);
5701 return;
5702 }
5703
5704 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5705 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5706 if (from_ap && reason)
5707 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5708 if (from_ap)
5709 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5710 if (ie)
5711 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5712
5713 if (genlmsg_end(msg, hdr) < 0) {
5714 nlmsg_free(msg);
5715 return;
5716 }
5717
Johannes Berg463d0182009-07-14 00:33:35 +02005718 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5719 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005720 return;
5721
5722 nla_put_failure:
5723 genlmsg_cancel(msg, hdr);
5724 nlmsg_free(msg);
5725
5726}
5727
Johannes Berg04a773a2009-04-19 21:24:32 +02005728void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5729 struct net_device *netdev, const u8 *bssid,
5730 gfp_t gfp)
5731{
5732 struct sk_buff *msg;
5733 void *hdr;
5734
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005735 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005736 if (!msg)
5737 return;
5738
5739 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5740 if (!hdr) {
5741 nlmsg_free(msg);
5742 return;
5743 }
5744
5745 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5746 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5747 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5748
5749 if (genlmsg_end(msg, hdr) < 0) {
5750 nlmsg_free(msg);
5751 return;
5752 }
5753
Johannes Berg463d0182009-07-14 00:33:35 +02005754 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5755 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005756 return;
5757
5758 nla_put_failure:
5759 genlmsg_cancel(msg, hdr);
5760 nlmsg_free(msg);
5761}
5762
Jouni Malinena3b8b052009-03-27 21:59:49 +02005763void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5764 struct net_device *netdev, const u8 *addr,
5765 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005766 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005767{
5768 struct sk_buff *msg;
5769 void *hdr;
5770
Johannes Berge6d6e342009-07-01 21:26:47 +02005771 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005772 if (!msg)
5773 return;
5774
5775 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5776 if (!hdr) {
5777 nlmsg_free(msg);
5778 return;
5779 }
5780
5781 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5782 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5783 if (addr)
5784 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5785 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5786 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5787 if (tsc)
5788 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5789
5790 if (genlmsg_end(msg, hdr) < 0) {
5791 nlmsg_free(msg);
5792 return;
5793 }
5794
Johannes Berg463d0182009-07-14 00:33:35 +02005795 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5796 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005797 return;
5798
5799 nla_put_failure:
5800 genlmsg_cancel(msg, hdr);
5801 nlmsg_free(msg);
5802}
5803
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005804void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5805 struct ieee80211_channel *channel_before,
5806 struct ieee80211_channel *channel_after)
5807{
5808 struct sk_buff *msg;
5809 void *hdr;
5810 struct nlattr *nl_freq;
5811
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005812 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005813 if (!msg)
5814 return;
5815
5816 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5817 if (!hdr) {
5818 nlmsg_free(msg);
5819 return;
5820 }
5821
5822 /*
5823 * Since we are applying the beacon hint to a wiphy we know its
5824 * wiphy_idx is valid
5825 */
5826 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5827
5828 /* Before */
5829 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5830 if (!nl_freq)
5831 goto nla_put_failure;
5832 if (nl80211_msg_put_channel(msg, channel_before))
5833 goto nla_put_failure;
5834 nla_nest_end(msg, nl_freq);
5835
5836 /* After */
5837 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5838 if (!nl_freq)
5839 goto nla_put_failure;
5840 if (nl80211_msg_put_channel(msg, channel_after))
5841 goto nla_put_failure;
5842 nla_nest_end(msg, nl_freq);
5843
5844 if (genlmsg_end(msg, hdr) < 0) {
5845 nlmsg_free(msg);
5846 return;
5847 }
5848
Johannes Berg463d0182009-07-14 00:33:35 +02005849 rcu_read_lock();
5850 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5851 GFP_ATOMIC);
5852 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005853
5854 return;
5855
5856nla_put_failure:
5857 genlmsg_cancel(msg, hdr);
5858 nlmsg_free(msg);
5859}
5860
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005861static void nl80211_send_remain_on_chan_event(
5862 int cmd, struct cfg80211_registered_device *rdev,
5863 struct net_device *netdev, u64 cookie,
5864 struct ieee80211_channel *chan,
5865 enum nl80211_channel_type channel_type,
5866 unsigned int duration, gfp_t gfp)
5867{
5868 struct sk_buff *msg;
5869 void *hdr;
5870
5871 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5872 if (!msg)
5873 return;
5874
5875 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5876 if (!hdr) {
5877 nlmsg_free(msg);
5878 return;
5879 }
5880
5881 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5882 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5883 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5884 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5885 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5886
5887 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5888 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5889
5890 if (genlmsg_end(msg, hdr) < 0) {
5891 nlmsg_free(msg);
5892 return;
5893 }
5894
5895 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5896 nl80211_mlme_mcgrp.id, gfp);
5897 return;
5898
5899 nla_put_failure:
5900 genlmsg_cancel(msg, hdr);
5901 nlmsg_free(msg);
5902}
5903
5904void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5905 struct net_device *netdev, u64 cookie,
5906 struct ieee80211_channel *chan,
5907 enum nl80211_channel_type channel_type,
5908 unsigned int duration, gfp_t gfp)
5909{
5910 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5911 rdev, netdev, cookie, chan,
5912 channel_type, duration, gfp);
5913}
5914
5915void nl80211_send_remain_on_channel_cancel(
5916 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5917 u64 cookie, struct ieee80211_channel *chan,
5918 enum nl80211_channel_type channel_type, gfp_t gfp)
5919{
5920 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5921 rdev, netdev, cookie, chan,
5922 channel_type, 0, gfp);
5923}
5924
Johannes Berg98b62182009-12-23 13:15:44 +01005925void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5926 struct net_device *dev, const u8 *mac_addr,
5927 struct station_info *sinfo, gfp_t gfp)
5928{
5929 struct sk_buff *msg;
5930
5931 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5932 if (!msg)
5933 return;
5934
5935 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5936 nlmsg_free(msg);
5937 return;
5938 }
5939
5940 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5941 nl80211_mlme_mcgrp.id, gfp);
5942}
5943
Johannes Berg2e161f72010-08-12 15:38:38 +02005944int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5945 struct net_device *netdev, u32 nlpid,
5946 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005947{
5948 struct sk_buff *msg;
5949 void *hdr;
5950 int err;
5951
5952 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5953 if (!msg)
5954 return -ENOMEM;
5955
Johannes Berg2e161f72010-08-12 15:38:38 +02005956 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005957 if (!hdr) {
5958 nlmsg_free(msg);
5959 return -ENOMEM;
5960 }
5961
5962 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5963 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5964 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5965 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5966
5967 err = genlmsg_end(msg, hdr);
5968 if (err < 0) {
5969 nlmsg_free(msg);
5970 return err;
5971 }
5972
5973 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5974 if (err < 0)
5975 return err;
5976 return 0;
5977
5978 nla_put_failure:
5979 genlmsg_cancel(msg, hdr);
5980 nlmsg_free(msg);
5981 return -ENOBUFS;
5982}
5983
Johannes Berg2e161f72010-08-12 15:38:38 +02005984void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5985 struct net_device *netdev, u64 cookie,
5986 const u8 *buf, size_t len, bool ack,
5987 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005988{
5989 struct sk_buff *msg;
5990 void *hdr;
5991
5992 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5993 if (!msg)
5994 return;
5995
Johannes Berg2e161f72010-08-12 15:38:38 +02005996 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02005997 if (!hdr) {
5998 nlmsg_free(msg);
5999 return;
6000 }
6001
6002 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6003 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6004 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6005 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6006 if (ack)
6007 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
6008
6009 if (genlmsg_end(msg, hdr) < 0) {
6010 nlmsg_free(msg);
6011 return;
6012 }
6013
6014 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
6015 return;
6016
6017 nla_put_failure:
6018 genlmsg_cancel(msg, hdr);
6019 nlmsg_free(msg);
6020}
6021
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006022void
6023nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
6024 struct net_device *netdev,
6025 enum nl80211_cqm_rssi_threshold_event rssi_event,
6026 gfp_t gfp)
6027{
6028 struct sk_buff *msg;
6029 struct nlattr *pinfoattr;
6030 void *hdr;
6031
6032 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6033 if (!msg)
6034 return;
6035
6036 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6037 if (!hdr) {
6038 nlmsg_free(msg);
6039 return;
6040 }
6041
6042 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6043 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6044
6045 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6046 if (!pinfoattr)
6047 goto nla_put_failure;
6048
6049 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
6050 rssi_event);
6051
6052 nla_nest_end(msg, pinfoattr);
6053
6054 if (genlmsg_end(msg, hdr) < 0) {
6055 nlmsg_free(msg);
6056 return;
6057 }
6058
6059 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6060 nl80211_mlme_mcgrp.id, gfp);
6061 return;
6062
6063 nla_put_failure:
6064 genlmsg_cancel(msg, hdr);
6065 nlmsg_free(msg);
6066}
6067
Johannes Bergc063dbf2010-11-24 08:10:05 +01006068void
6069nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
6070 struct net_device *netdev, const u8 *peer,
6071 u32 num_packets, gfp_t gfp)
6072{
6073 struct sk_buff *msg;
6074 struct nlattr *pinfoattr;
6075 void *hdr;
6076
6077 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6078 if (!msg)
6079 return;
6080
6081 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
6082 if (!hdr) {
6083 nlmsg_free(msg);
6084 return;
6085 }
6086
6087 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6088 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6089 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
6090
6091 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
6092 if (!pinfoattr)
6093 goto nla_put_failure;
6094
6095 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
6096
6097 nla_nest_end(msg, pinfoattr);
6098
6099 if (genlmsg_end(msg, hdr) < 0) {
6100 nlmsg_free(msg);
6101 return;
6102 }
6103
6104 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6105 nl80211_mlme_mcgrp.id, gfp);
6106 return;
6107
6108 nla_put_failure:
6109 genlmsg_cancel(msg, hdr);
6110 nlmsg_free(msg);
6111}
6112
Jouni Malinen026331c2010-02-15 12:53:10 +02006113static int nl80211_netlink_notify(struct notifier_block * nb,
6114 unsigned long state,
6115 void *_notify)
6116{
6117 struct netlink_notify *notify = _notify;
6118 struct cfg80211_registered_device *rdev;
6119 struct wireless_dev *wdev;
6120
6121 if (state != NETLINK_URELEASE)
6122 return NOTIFY_DONE;
6123
6124 rcu_read_lock();
6125
6126 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
6127 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02006128 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02006129
6130 rcu_read_unlock();
6131
6132 return NOTIFY_DONE;
6133}
6134
6135static struct notifier_block nl80211_netlink_notifier = {
6136 .notifier_call = nl80211_netlink_notify,
6137};
6138
Johannes Berg55682962007-09-20 13:09:35 -04006139/* initialisation/exit functions */
6140
6141int nl80211_init(void)
6142{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006143 int err;
Johannes Berg55682962007-09-20 13:09:35 -04006144
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00006145 err = genl_register_family_with_ops(&nl80211_fam,
6146 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04006147 if (err)
6148 return err;
6149
Johannes Berg55682962007-09-20 13:09:35 -04006150 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
6151 if (err)
6152 goto err_out;
6153
Johannes Berg2a519312009-02-10 21:25:55 +01006154 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
6155 if (err)
6156 goto err_out;
6157
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006158 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
6159 if (err)
6160 goto err_out;
6161
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006162 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
6163 if (err)
6164 goto err_out;
6165
Johannes Bergaff89a92009-07-01 21:26:51 +02006166#ifdef CONFIG_NL80211_TESTMODE
6167 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6168 if (err)
6169 goto err_out;
6170#endif
6171
Jouni Malinen026331c2010-02-15 12:53:10 +02006172 err = netlink_register_notifier(&nl80211_netlink_notifier);
6173 if (err)
6174 goto err_out;
6175
Johannes Berg55682962007-09-20 13:09:35 -04006176 return 0;
6177 err_out:
6178 genl_unregister_family(&nl80211_fam);
6179 return err;
6180}
6181
6182void nl80211_exit(void)
6183{
Jouni Malinen026331c2010-02-15 12:53:10 +02006184 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006185 genl_unregister_family(&nl80211_fam);
6186}