blob: 2cf03331d4a23d72f1979750ad787debb35743f3 [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 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300124
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700125 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
126
Jouni Malinen36aedc92008-08-25 11:58:58 +0300127 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
128 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200129
130 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
131 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
132 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100133 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
134 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200135
136 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
137 .len = IEEE80211_MAX_SSID_LEN },
138 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
139 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200140 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300141 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382c2009-05-06 22:09:37 +0300142 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300143 [NL80211_ATTR_STA_FLAGS2] = {
144 .len = sizeof(struct nl80211_sta_flag_update),
145 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300146 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300147 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
148 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200149 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
150 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
151 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200152 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100153 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100154 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
155 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100156 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
157 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200158 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200159 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
160 .len = IEEE80211_MAX_DATA_LEN },
161 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200162 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200163 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300164 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200165 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300166 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
167 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200168 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900169 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
170 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100171 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100172 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400173};
174
Johannes Berge31b8212010-10-05 19:39:30 +0200175/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000176static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200177 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200178 [NL80211_KEY_IDX] = { .type = NLA_U8 },
179 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
180 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
181 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
182 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200183 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200184};
185
Holger Schuriga0438972009-11-11 11:30:02 +0100186/* ifidx get helper */
187static int nl80211_get_ifidx(struct netlink_callback *cb)
188{
189 int res;
190
191 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
192 nl80211_fam.attrbuf, nl80211_fam.maxattr,
193 nl80211_policy);
194 if (res)
195 return res;
196
197 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
198 return -EINVAL;
199
200 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
201 if (!res)
202 return -EINVAL;
203 return res;
204}
205
Johannes Berg67748892010-10-04 21:14:06 +0200206static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
207 struct netlink_callback *cb,
208 struct cfg80211_registered_device **rdev,
209 struct net_device **dev)
210{
211 int ifidx = cb->args[0];
212 int err;
213
214 if (!ifidx)
215 ifidx = nl80211_get_ifidx(cb);
216 if (ifidx < 0)
217 return ifidx;
218
219 cb->args[0] = ifidx;
220
221 rtnl_lock();
222
223 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
224 if (!*dev) {
225 err = -ENODEV;
226 goto out_rtnl;
227 }
228
229 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100230 if (IS_ERR(*rdev)) {
231 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200232 goto out_rtnl;
233 }
234
235 return 0;
236 out_rtnl:
237 rtnl_unlock();
238 return err;
239}
240
241static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
242{
243 cfg80211_unlock_rdev(rdev);
244 rtnl_unlock();
245}
246
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100247/* IE validation */
248static bool is_valid_ie_attr(const struct nlattr *attr)
249{
250 const u8 *pos;
251 int len;
252
253 if (!attr)
254 return true;
255
256 pos = nla_data(attr);
257 len = nla_len(attr);
258
259 while (len) {
260 u8 elemlen;
261
262 if (len < 2)
263 return false;
264 len -= 2;
265
266 elemlen = pos[1];
267 if (elemlen > len)
268 return false;
269
270 len -= elemlen;
271 pos += 2 + elemlen;
272 }
273
274 return true;
275}
276
Johannes Berg55682962007-09-20 13:09:35 -0400277/* message building helper */
278static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
279 int flags, u8 cmd)
280{
281 /* since there is no private header just add the generic one */
282 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
283}
284
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400285static int nl80211_msg_put_channel(struct sk_buff *msg,
286 struct ieee80211_channel *chan)
287{
288 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
289 chan->center_freq);
290
291 if (chan->flags & IEEE80211_CHAN_DISABLED)
292 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
293 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
294 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
295 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
296 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
297 if (chan->flags & IEEE80211_CHAN_RADAR)
298 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
299
300 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
301 DBM_TO_MBM(chan->max_power));
302
303 return 0;
304
305 nla_put_failure:
306 return -ENOBUFS;
307}
308
Johannes Berg55682962007-09-20 13:09:35 -0400309/* netlink command implementations */
310
Johannes Bergb9454e82009-07-08 13:29:08 +0200311struct key_parse {
312 struct key_params p;
313 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200314 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200315 bool def, defmgmt;
316};
317
318static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
319{
320 struct nlattr *tb[NL80211_KEY_MAX + 1];
321 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
322 nl80211_key_policy);
323 if (err)
324 return err;
325
326 k->def = !!tb[NL80211_KEY_DEFAULT];
327 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
328
329 if (tb[NL80211_KEY_IDX])
330 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
331
332 if (tb[NL80211_KEY_DATA]) {
333 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
334 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
335 }
336
337 if (tb[NL80211_KEY_SEQ]) {
338 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
339 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
340 }
341
342 if (tb[NL80211_KEY_CIPHER])
343 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
344
Johannes Berge31b8212010-10-05 19:39:30 +0200345 if (tb[NL80211_KEY_TYPE]) {
346 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
347 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
348 return -EINVAL;
349 }
350
Johannes Bergb9454e82009-07-08 13:29:08 +0200351 return 0;
352}
353
354static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
355{
356 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
357 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
358 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
359 }
360
361 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
362 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
363 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
364 }
365
366 if (info->attrs[NL80211_ATTR_KEY_IDX])
367 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
368
369 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
370 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
371
372 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
373 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
374
Johannes Berge31b8212010-10-05 19:39:30 +0200375 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
376 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
377 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
378 return -EINVAL;
379 }
380
Johannes Bergb9454e82009-07-08 13:29:08 +0200381 return 0;
382}
383
384static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
385{
386 int err;
387
388 memset(k, 0, sizeof(*k));
389 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200390 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200391
392 if (info->attrs[NL80211_ATTR_KEY])
393 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
394 else
395 err = nl80211_parse_key_old(info, k);
396
397 if (err)
398 return err;
399
400 if (k->def && k->defmgmt)
401 return -EINVAL;
402
403 if (k->idx != -1) {
404 if (k->defmgmt) {
405 if (k->idx < 4 || k->idx > 5)
406 return -EINVAL;
407 } else if (k->def) {
408 if (k->idx < 0 || k->idx > 3)
409 return -EINVAL;
410 } else {
411 if (k->idx < 0 || k->idx > 5)
412 return -EINVAL;
413 }
414 }
415
416 return 0;
417}
418
Johannes Bergfffd0932009-07-08 14:22:54 +0200419static struct cfg80211_cached_keys *
420nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
421 struct nlattr *keys)
422{
423 struct key_parse parse;
424 struct nlattr *key;
425 struct cfg80211_cached_keys *result;
426 int rem, err, def = 0;
427
428 result = kzalloc(sizeof(*result), GFP_KERNEL);
429 if (!result)
430 return ERR_PTR(-ENOMEM);
431
432 result->def = -1;
433 result->defmgmt = -1;
434
435 nla_for_each_nested(key, keys, rem) {
436 memset(&parse, 0, sizeof(parse));
437 parse.idx = -1;
438
439 err = nl80211_parse_key_new(key, &parse);
440 if (err)
441 goto error;
442 err = -EINVAL;
443 if (!parse.p.key)
444 goto error;
445 if (parse.idx < 0 || parse.idx > 4)
446 goto error;
447 if (parse.def) {
448 if (def)
449 goto error;
450 def = 1;
451 result->def = parse.idx;
452 } else if (parse.defmgmt)
453 goto error;
454 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200455 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200456 if (err)
457 goto error;
458 result->params[parse.idx].cipher = parse.p.cipher;
459 result->params[parse.idx].key_len = parse.p.key_len;
460 result->params[parse.idx].key = result->data[parse.idx];
461 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
462 }
463
464 return result;
465 error:
466 kfree(result);
467 return ERR_PTR(err);
468}
469
470static int nl80211_key_allowed(struct wireless_dev *wdev)
471{
472 ASSERT_WDEV_LOCK(wdev);
473
Johannes Bergfffd0932009-07-08 14:22:54 +0200474 switch (wdev->iftype) {
475 case NL80211_IFTYPE_AP:
476 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200477 case NL80211_IFTYPE_P2P_GO:
Johannes Bergfffd0932009-07-08 14:22:54 +0200478 break;
479 case NL80211_IFTYPE_ADHOC:
480 if (!wdev->current_bss)
481 return -ENOLINK;
482 break;
483 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200484 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200485 if (wdev->sme_state != CFG80211_SME_CONNECTED)
486 return -ENOLINK;
487 break;
488 default:
489 return -EINVAL;
490 }
491
492 return 0;
493}
494
Johannes Berg55682962007-09-20 13:09:35 -0400495static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
496 struct cfg80211_registered_device *dev)
497{
498 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100499 struct nlattr *nl_bands, *nl_band;
500 struct nlattr *nl_freqs, *nl_freq;
501 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700502 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100503 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100504 enum ieee80211_band band;
505 struct ieee80211_channel *chan;
506 struct ieee80211_rate *rate;
507 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700508 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg2e161f72010-08-12 15:38:38 +0200509 const struct ieee80211_txrx_stypes *mgmt_stypes =
510 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400511
512 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
513 if (!hdr)
514 return -1;
515
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500516 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400517 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200518
Johannes Bergf5ea9122009-08-07 16:17:38 +0200519 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
520 cfg80211_rdev_list_generation);
521
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200522 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
523 dev->wiphy.retry_short);
524 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
525 dev->wiphy.retry_long);
526 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
527 dev->wiphy.frag_threshold);
528 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
529 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100530 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
531 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100532 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
533 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200534 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
535 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100536
Johannes Berge31b8212010-10-05 19:39:30 +0200537 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
538 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
539
Johannes Berg25e47c12009-04-02 20:14:06 +0200540 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
541 sizeof(u32) * dev->wiphy.n_cipher_suites,
542 dev->wiphy.cipher_suites);
543
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100544 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
545 dev->wiphy.max_num_pmkids);
546
Johannes Bergc0692b82010-08-27 14:26:53 +0300547 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
548 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
549
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900550 if (dev->ops->get_antenna) {
551 u32 tx_ant = 0, rx_ant = 0;
552 int res;
553 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
554 if (!res) {
555 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
556 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
557 }
558 }
559
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700560 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
561 if (!nl_modes)
562 goto nla_put_failure;
563
564 i = 0;
565 while (ifmodes) {
566 if (ifmodes & 1)
567 NLA_PUT_FLAG(msg, i);
568 ifmodes >>= 1;
569 i++;
570 }
571
572 nla_nest_end(msg, nl_modes);
573
Johannes Bergee688b002008-01-24 19:38:39 +0100574 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
575 if (!nl_bands)
576 goto nla_put_failure;
577
578 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
579 if (!dev->wiphy.bands[band])
580 continue;
581
582 nl_band = nla_nest_start(msg, band);
583 if (!nl_band)
584 goto nla_put_failure;
585
Johannes Bergd51626d2008-10-09 12:20:13 +0200586 /* add HT info */
587 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
588 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
589 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
590 &dev->wiphy.bands[band]->ht_cap.mcs);
591 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
592 dev->wiphy.bands[band]->ht_cap.cap);
593 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
594 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
595 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
596 dev->wiphy.bands[band]->ht_cap.ampdu_density);
597 }
598
Johannes Bergee688b002008-01-24 19:38:39 +0100599 /* add frequencies */
600 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
601 if (!nl_freqs)
602 goto nla_put_failure;
603
604 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
605 nl_freq = nla_nest_start(msg, i);
606 if (!nl_freq)
607 goto nla_put_failure;
608
609 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100610
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400611 if (nl80211_msg_put_channel(msg, chan))
612 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200613
Johannes Bergee688b002008-01-24 19:38:39 +0100614 nla_nest_end(msg, nl_freq);
615 }
616
617 nla_nest_end(msg, nl_freqs);
618
619 /* add bitrates */
620 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
621 if (!nl_rates)
622 goto nla_put_failure;
623
624 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
625 nl_rate = nla_nest_start(msg, i);
626 if (!nl_rate)
627 goto nla_put_failure;
628
629 rate = &dev->wiphy.bands[band]->bitrates[i];
630 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
631 rate->bitrate);
632 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
633 NLA_PUT_FLAG(msg,
634 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
635
636 nla_nest_end(msg, nl_rate);
637 }
638
639 nla_nest_end(msg, nl_rates);
640
641 nla_nest_end(msg, nl_band);
642 }
643 nla_nest_end(msg, nl_bands);
644
Johannes Berg8fdc6212009-03-14 09:34:01 +0100645 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
646 if (!nl_cmds)
647 goto nla_put_failure;
648
649 i = 0;
650#define CMD(op, n) \
651 do { \
652 if (dev->ops->op) { \
653 i++; \
654 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
655 } \
656 } while (0)
657
658 CMD(add_virtual_intf, NEW_INTERFACE);
659 CMD(change_virtual_intf, SET_INTERFACE);
660 CMD(add_key, NEW_KEY);
661 CMD(add_beacon, NEW_BEACON);
662 CMD(add_station, NEW_STATION);
663 CMD(add_mpath, NEW_MPATH);
Johannes Berg29cbe682010-12-03 09:20:44 +0100664 CMD(update_mesh_params, SET_MESH_PARAMS);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100665 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200666 CMD(auth, AUTHENTICATE);
667 CMD(assoc, ASSOCIATE);
668 CMD(deauth, DEAUTHENTICATE);
669 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200670 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100671 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100672 CMD(set_pmksa, SET_PMKSA);
673 CMD(del_pmksa, DEL_PMKSA);
674 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100675 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200676 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200677 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100678 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100679 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200680 i++;
681 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
682 }
Johannes Bergf444de02010-05-05 15:25:02 +0200683 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400684 CMD(set_wds_peer, SET_WDS_PEER);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100685
686#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200687
Johannes Berg6829c872009-07-02 09:13:27 +0200688 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200689 i++;
690 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
691 }
692
Johannes Berg6829c872009-07-02 09:13:27 +0200693 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200694 i++;
695 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
696 }
697
Johannes Berg8fdc6212009-03-14 09:34:01 +0100698 nla_nest_end(msg, nl_cmds);
699
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100700 /* for now at least assume all drivers have it */
701 if (dev->ops->mgmt_tx)
702 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
703
Johannes Berg2e161f72010-08-12 15:38:38 +0200704 if (mgmt_stypes) {
705 u16 stypes;
706 struct nlattr *nl_ftypes, *nl_ifs;
707 enum nl80211_iftype ift;
708
709 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
710 if (!nl_ifs)
711 goto nla_put_failure;
712
713 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
714 nl_ftypes = nla_nest_start(msg, ift);
715 if (!nl_ftypes)
716 goto nla_put_failure;
717 i = 0;
718 stypes = mgmt_stypes[ift].tx;
719 while (stypes) {
720 if (stypes & 1)
721 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
722 (i << 4) | IEEE80211_FTYPE_MGMT);
723 stypes >>= 1;
724 i++;
725 }
726 nla_nest_end(msg, nl_ftypes);
727 }
728
Johannes Berg74b70a42010-08-24 12:15:53 +0200729 nla_nest_end(msg, nl_ifs);
730
Johannes Berg2e161f72010-08-12 15:38:38 +0200731 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
732 if (!nl_ifs)
733 goto nla_put_failure;
734
735 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
736 nl_ftypes = nla_nest_start(msg, ift);
737 if (!nl_ftypes)
738 goto nla_put_failure;
739 i = 0;
740 stypes = mgmt_stypes[ift].rx;
741 while (stypes) {
742 if (stypes & 1)
743 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
744 (i << 4) | IEEE80211_FTYPE_MGMT);
745 stypes >>= 1;
746 i++;
747 }
748 nla_nest_end(msg, nl_ftypes);
749 }
750 nla_nest_end(msg, nl_ifs);
751 }
752
Johannes Berg55682962007-09-20 13:09:35 -0400753 return genlmsg_end(msg, hdr);
754
755 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700756 genlmsg_cancel(msg, hdr);
757 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400758}
759
760static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
761{
762 int idx = 0;
763 int start = cb->args[0];
764 struct cfg80211_registered_device *dev;
765
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500766 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200767 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200768 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
769 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200770 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400771 continue;
772 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
773 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200774 dev) < 0) {
775 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400776 break;
Julius Volzb4637272008-07-08 14:02:19 +0200777 }
Johannes Berg55682962007-09-20 13:09:35 -0400778 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500779 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400780
781 cb->args[0] = idx;
782
783 return skb->len;
784}
785
786static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
787{
788 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200789 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400790
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700791 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400792 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200793 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400794
Johannes Berg4c476992010-10-04 21:36:35 +0200795 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
796 nlmsg_free(msg);
797 return -ENOBUFS;
798 }
Johannes Berg55682962007-09-20 13:09:35 -0400799
Johannes Berg134e6372009-07-10 09:51:34 +0000800 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400801}
802
Jouni Malinen31888482008-10-30 16:59:24 +0200803static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
804 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
805 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
806 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
807 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
808 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
809};
810
811static int parse_txq_params(struct nlattr *tb[],
812 struct ieee80211_txq_params *txq_params)
813{
814 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
815 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
816 !tb[NL80211_TXQ_ATTR_AIFS])
817 return -EINVAL;
818
819 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
820 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
821 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
822 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
823 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
824
825 return 0;
826}
827
Johannes Bergf444de02010-05-05 15:25:02 +0200828static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
829{
830 /*
831 * You can only set the channel explicitly for AP, mesh
832 * and WDS type interfaces; all others have their channel
833 * managed via their respective "establish a connection"
834 * command (connect, join, ...)
835 *
836 * Monitors are special as they are normally slaved to
837 * whatever else is going on, so they behave as though
838 * you tried setting the wiphy channel itself.
839 */
840 return !wdev ||
841 wdev->iftype == NL80211_IFTYPE_AP ||
842 wdev->iftype == NL80211_IFTYPE_WDS ||
843 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200844 wdev->iftype == NL80211_IFTYPE_MONITOR ||
845 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200846}
847
848static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
849 struct wireless_dev *wdev,
850 struct genl_info *info)
851{
852 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
853 u32 freq;
854 int result;
855
856 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
857 return -EINVAL;
858
859 if (!nl80211_can_set_dev_channel(wdev))
860 return -EOPNOTSUPP;
861
862 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
863 channel_type = nla_get_u32(info->attrs[
864 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
865 if (channel_type != NL80211_CHAN_NO_HT &&
866 channel_type != NL80211_CHAN_HT20 &&
867 channel_type != NL80211_CHAN_HT40PLUS &&
868 channel_type != NL80211_CHAN_HT40MINUS)
869 return -EINVAL;
870 }
871
872 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
873
874 mutex_lock(&rdev->devlist_mtx);
875 if (wdev) {
876 wdev_lock(wdev);
877 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
878 wdev_unlock(wdev);
879 } else {
880 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
881 }
882 mutex_unlock(&rdev->devlist_mtx);
883
884 return result;
885}
886
887static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
888{
Johannes Berg4c476992010-10-04 21:36:35 +0200889 struct cfg80211_registered_device *rdev = info->user_ptr[0];
890 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +0200891
Johannes Berg4c476992010-10-04 21:36:35 +0200892 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +0200893}
894
Bill Jordane8347eb2010-10-01 13:54:28 -0400895static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
896{
Johannes Berg43b19952010-10-07 13:10:30 +0200897 struct cfg80211_registered_device *rdev = info->user_ptr[0];
898 struct net_device *dev = info->user_ptr[1];
899 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +0200900 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -0400901
902 if (!info->attrs[NL80211_ATTR_MAC])
903 return -EINVAL;
904
Johannes Berg43b19952010-10-07 13:10:30 +0200905 if (netif_running(dev))
906 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -0400907
Johannes Berg43b19952010-10-07 13:10:30 +0200908 if (!rdev->ops->set_wds_peer)
909 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400910
Johannes Berg43b19952010-10-07 13:10:30 +0200911 if (wdev->iftype != NL80211_IFTYPE_WDS)
912 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400913
914 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +0200915 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -0400916}
917
918
Johannes Berg55682962007-09-20 13:09:35 -0400919static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
920{
921 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200922 struct net_device *netdev = NULL;
923 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -0400924 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200925 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200926 u32 changed;
927 u8 retry_short = 0, retry_long = 0;
928 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100929 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400930
Johannes Bergf444de02010-05-05 15:25:02 +0200931 /*
932 * Try to find the wiphy and netdev. Normally this
933 * function shouldn't need the netdev, but this is
934 * done for backward compatibility -- previously
935 * setting the channel was done per wiphy, but now
936 * it is per netdev. Previous userland like hostapd
937 * also passed a netdev to set_wiphy, so that it is
938 * possible to let that go to the right netdev!
939 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100940 mutex_lock(&cfg80211_mutex);
941
Johannes Bergf444de02010-05-05 15:25:02 +0200942 if (info->attrs[NL80211_ATTR_IFINDEX]) {
943 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
944
945 netdev = dev_get_by_index(genl_info_net(info), ifindex);
946 if (netdev && netdev->ieee80211_ptr) {
947 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
948 mutex_lock(&rdev->mtx);
949 } else
950 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100951 }
952
Johannes Bergf444de02010-05-05 15:25:02 +0200953 if (!netdev) {
954 rdev = __cfg80211_rdev_from_info(info);
955 if (IS_ERR(rdev)) {
956 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +0200957 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +0200958 }
959 wdev = NULL;
960 netdev = NULL;
961 result = 0;
962
963 mutex_lock(&rdev->mtx);
964 } else if (netif_running(netdev) &&
965 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
966 wdev = netdev->ieee80211_ptr;
967 else
968 wdev = NULL;
969
970 /*
971 * end workaround code, by now the rdev is available
972 * and locked, and wdev may or may not be NULL.
973 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100974
975 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200976 result = cfg80211_dev_rename(
977 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100978
979 mutex_unlock(&cfg80211_mutex);
980
981 if (result)
982 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400983
Jouni Malinen31888482008-10-30 16:59:24 +0200984 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
985 struct ieee80211_txq_params txq_params;
986 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
987
988 if (!rdev->ops->set_txq_params) {
989 result = -EOPNOTSUPP;
990 goto bad_res;
991 }
992
993 nla_for_each_nested(nl_txq_params,
994 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
995 rem_txq_params) {
996 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
997 nla_data(nl_txq_params),
998 nla_len(nl_txq_params),
999 txq_params_policy);
1000 result = parse_txq_params(tb, &txq_params);
1001 if (result)
1002 goto bad_res;
1003
1004 result = rdev->ops->set_txq_params(&rdev->wiphy,
1005 &txq_params);
1006 if (result)
1007 goto bad_res;
1008 }
1009 }
1010
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001011 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001012 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001013 if (result)
1014 goto bad_res;
1015 }
1016
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001017 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1018 enum nl80211_tx_power_setting type;
1019 int idx, mbm = 0;
1020
1021 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001022 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001023 goto bad_res;
1024 }
1025
1026 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1027 type = nla_get_u32(info->attrs[idx]);
1028
1029 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1030 (type != NL80211_TX_POWER_AUTOMATIC)) {
1031 result = -EINVAL;
1032 goto bad_res;
1033 }
1034
1035 if (type != NL80211_TX_POWER_AUTOMATIC) {
1036 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1037 mbm = nla_get_u32(info->attrs[idx]);
1038 }
1039
1040 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1041 if (result)
1042 goto bad_res;
1043 }
1044
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001045 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1046 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1047 u32 tx_ant, rx_ant;
1048 if (!rdev->ops->set_antenna) {
1049 result = -EOPNOTSUPP;
1050 goto bad_res;
1051 }
1052
1053 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1054 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1055
1056 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1057 if (result)
1058 goto bad_res;
1059 }
1060
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001061 changed = 0;
1062
1063 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1064 retry_short = nla_get_u8(
1065 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1066 if (retry_short == 0) {
1067 result = -EINVAL;
1068 goto bad_res;
1069 }
1070 changed |= WIPHY_PARAM_RETRY_SHORT;
1071 }
1072
1073 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1074 retry_long = nla_get_u8(
1075 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1076 if (retry_long == 0) {
1077 result = -EINVAL;
1078 goto bad_res;
1079 }
1080 changed |= WIPHY_PARAM_RETRY_LONG;
1081 }
1082
1083 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1084 frag_threshold = nla_get_u32(
1085 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1086 if (frag_threshold < 256) {
1087 result = -EINVAL;
1088 goto bad_res;
1089 }
1090 if (frag_threshold != (u32) -1) {
1091 /*
1092 * Fragments (apart from the last one) are required to
1093 * have even length. Make the fragmentation code
1094 * simpler by stripping LSB should someone try to use
1095 * odd threshold value.
1096 */
1097 frag_threshold &= ~0x1;
1098 }
1099 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1100 }
1101
1102 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1103 rts_threshold = nla_get_u32(
1104 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1105 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1106 }
1107
Lukáš Turek81077e82009-12-21 22:50:47 +01001108 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1109 coverage_class = nla_get_u8(
1110 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1111 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1112 }
1113
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001114 if (changed) {
1115 u8 old_retry_short, old_retry_long;
1116 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001117 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001118
1119 if (!rdev->ops->set_wiphy_params) {
1120 result = -EOPNOTSUPP;
1121 goto bad_res;
1122 }
1123
1124 old_retry_short = rdev->wiphy.retry_short;
1125 old_retry_long = rdev->wiphy.retry_long;
1126 old_frag_threshold = rdev->wiphy.frag_threshold;
1127 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001128 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001129
1130 if (changed & WIPHY_PARAM_RETRY_SHORT)
1131 rdev->wiphy.retry_short = retry_short;
1132 if (changed & WIPHY_PARAM_RETRY_LONG)
1133 rdev->wiphy.retry_long = retry_long;
1134 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1135 rdev->wiphy.frag_threshold = frag_threshold;
1136 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1137 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001138 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1139 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001140
1141 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1142 if (result) {
1143 rdev->wiphy.retry_short = old_retry_short;
1144 rdev->wiphy.retry_long = old_retry_long;
1145 rdev->wiphy.frag_threshold = old_frag_threshold;
1146 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001147 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001148 }
1149 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001150
Johannes Berg306d6112008-12-08 12:39:04 +01001151 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001152 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001153 if (netdev)
1154 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001155 return result;
1156}
1157
1158
1159static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001160 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001161 struct net_device *dev)
1162{
1163 void *hdr;
1164
1165 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1166 if (!hdr)
1167 return -1;
1168
1169 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001170 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001171 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001172 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001173
1174 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1175 rdev->devlist_generation ^
1176 (cfg80211_rdev_list_generation << 2));
1177
Johannes Berg55682962007-09-20 13:09:35 -04001178 return genlmsg_end(msg, hdr);
1179
1180 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001181 genlmsg_cancel(msg, hdr);
1182 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001183}
1184
1185static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1186{
1187 int wp_idx = 0;
1188 int if_idx = 0;
1189 int wp_start = cb->args[0];
1190 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001191 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001192 struct wireless_dev *wdev;
1193
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001194 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001195 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1196 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001197 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001198 if (wp_idx < wp_start) {
1199 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001200 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001201 }
Johannes Berg55682962007-09-20 13:09:35 -04001202 if_idx = 0;
1203
Johannes Bergf5ea9122009-08-07 16:17:38 +02001204 mutex_lock(&rdev->devlist_mtx);
1205 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001206 if (if_idx < if_start) {
1207 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001208 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001209 }
Johannes Berg55682962007-09-20 13:09:35 -04001210 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1211 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001212 rdev, wdev->netdev) < 0) {
1213 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001214 goto out;
1215 }
1216 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001217 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001218 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001219
1220 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001221 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001222 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001223 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001224
1225 cb->args[0] = wp_idx;
1226 cb->args[1] = if_idx;
1227
1228 return skb->len;
1229}
1230
1231static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1232{
1233 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001234 struct cfg80211_registered_device *dev = info->user_ptr[0];
1235 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001236
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001237 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001238 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001239 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001240
Johannes Bergd7264052009-04-19 16:23:20 +02001241 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001242 dev, netdev) < 0) {
1243 nlmsg_free(msg);
1244 return -ENOBUFS;
1245 }
Johannes Berg55682962007-09-20 13:09:35 -04001246
Johannes Berg134e6372009-07-10 09:51:34 +00001247 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001248}
1249
Michael Wu66f7ac52008-01-31 19:48:22 +01001250static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1251 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1252 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1253 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1254 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1255 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1256};
1257
1258static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1259{
1260 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1261 int flag;
1262
1263 *mntrflags = 0;
1264
1265 if (!nla)
1266 return -EINVAL;
1267
1268 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1269 nla, mntr_flags_policy))
1270 return -EINVAL;
1271
1272 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1273 if (flags[flag])
1274 *mntrflags |= (1<<flag);
1275
1276 return 0;
1277}
1278
Johannes Berg9bc383d2009-11-19 11:55:19 +01001279static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001280 struct net_device *netdev, u8 use_4addr,
1281 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001282{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001283 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001284 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001285 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001286 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001287 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001288
1289 switch (iftype) {
1290 case NL80211_IFTYPE_AP_VLAN:
1291 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1292 return 0;
1293 break;
1294 case NL80211_IFTYPE_STATION:
1295 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1296 return 0;
1297 break;
1298 default:
1299 break;
1300 }
1301
1302 return -EOPNOTSUPP;
1303}
1304
Johannes Berg55682962007-09-20 13:09:35 -04001305static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1306{
Johannes Berg4c476992010-10-04 21:36:35 +02001307 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001308 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001309 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001310 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001311 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001312 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001313 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001314
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001315 memset(&params, 0, sizeof(params));
1316
Johannes Berg04a773a2009-04-19 21:24:32 +02001317 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001318
Johannes Berg723b0382008-09-16 20:22:09 +02001319 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001320 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001321 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001322 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001323 if (ntype > NL80211_IFTYPE_MAX)
1324 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001325 }
1326
Johannes Berg92ffe052008-09-16 20:39:36 +02001327 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001328 struct wireless_dev *wdev = dev->ieee80211_ptr;
1329
Johannes Berg4c476992010-10-04 21:36:35 +02001330 if (ntype != NL80211_IFTYPE_MESH_POINT)
1331 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001332 if (netif_running(dev))
1333 return -EBUSY;
1334
1335 wdev_lock(wdev);
1336 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1337 IEEE80211_MAX_MESH_ID_LEN);
1338 wdev->mesh_id_up_len =
1339 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1340 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1341 wdev->mesh_id_up_len);
1342 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001343 }
1344
Felix Fietkau8b787642009-11-10 18:53:10 +01001345 if (info->attrs[NL80211_ATTR_4ADDR]) {
1346 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1347 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001348 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001349 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001350 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001351 } else {
1352 params.use_4addr = -1;
1353 }
1354
Johannes Berg92ffe052008-09-16 20:39:36 +02001355 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001356 if (ntype != NL80211_IFTYPE_MONITOR)
1357 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001358 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1359 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001360 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001361 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001362
1363 flags = &_flags;
1364 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001365 }
Johannes Berg3b858752009-03-12 09:55:09 +01001366
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001367 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001368 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001369 else
1370 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001371
Johannes Berg9bc383d2009-11-19 11:55:19 +01001372 if (!err && params.use_4addr != -1)
1373 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1374
Johannes Berg55682962007-09-20 13:09:35 -04001375 return err;
1376}
1377
1378static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1379{
Johannes Berg4c476992010-10-04 21:36:35 +02001380 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001381 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001382 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001383 int err;
1384 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001385 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001386
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001387 memset(&params, 0, sizeof(params));
1388
Johannes Berg55682962007-09-20 13:09:35 -04001389 if (!info->attrs[NL80211_ATTR_IFNAME])
1390 return -EINVAL;
1391
1392 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1393 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1394 if (type > NL80211_IFTYPE_MAX)
1395 return -EINVAL;
1396 }
1397
Johannes Berg79c97e92009-07-07 03:56:12 +02001398 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001399 !(rdev->wiphy.interface_modes & (1 << type)))
1400 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001401
Johannes Berg9bc383d2009-11-19 11:55:19 +01001402 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001403 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001404 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001405 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001406 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001407 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001408
Michael Wu66f7ac52008-01-31 19:48:22 +01001409 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1410 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1411 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001412 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001413 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001414 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001415 if (IS_ERR(dev))
1416 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001417
Johannes Berg29cbe682010-12-03 09:20:44 +01001418 if (type == NL80211_IFTYPE_MESH_POINT &&
1419 info->attrs[NL80211_ATTR_MESH_ID]) {
1420 struct wireless_dev *wdev = dev->ieee80211_ptr;
1421
1422 wdev_lock(wdev);
1423 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1424 IEEE80211_MAX_MESH_ID_LEN);
1425 wdev->mesh_id_up_len =
1426 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1427 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1428 wdev->mesh_id_up_len);
1429 wdev_unlock(wdev);
1430 }
1431
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001432 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001433}
1434
1435static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1436{
Johannes Berg4c476992010-10-04 21:36:35 +02001437 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1438 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001439
Johannes Berg4c476992010-10-04 21:36:35 +02001440 if (!rdev->ops->del_virtual_intf)
1441 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001442
Johannes Berg4c476992010-10-04 21:36:35 +02001443 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001444}
1445
Johannes Berg41ade002007-12-19 02:03:29 +01001446struct get_key_cookie {
1447 struct sk_buff *msg;
1448 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001449 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001450};
1451
1452static void get_key_callback(void *c, struct key_params *params)
1453{
Johannes Bergb9454e82009-07-08 13:29:08 +02001454 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001455 struct get_key_cookie *cookie = c;
1456
1457 if (params->key)
1458 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1459 params->key_len, params->key);
1460
1461 if (params->seq)
1462 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1463 params->seq_len, params->seq);
1464
1465 if (params->cipher)
1466 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1467 params->cipher);
1468
Johannes Bergb9454e82009-07-08 13:29:08 +02001469 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1470 if (!key)
1471 goto nla_put_failure;
1472
1473 if (params->key)
1474 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1475 params->key_len, params->key);
1476
1477 if (params->seq)
1478 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1479 params->seq_len, params->seq);
1480
1481 if (params->cipher)
1482 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1483 params->cipher);
1484
1485 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1486
1487 nla_nest_end(cookie->msg, key);
1488
Johannes Berg41ade002007-12-19 02:03:29 +01001489 return;
1490 nla_put_failure:
1491 cookie->error = 1;
1492}
1493
1494static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1495{
Johannes Berg4c476992010-10-04 21:36:35 +02001496 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001497 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001498 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001499 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001500 const u8 *mac_addr = NULL;
1501 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001502 struct get_key_cookie cookie = {
1503 .error = 0,
1504 };
1505 void *hdr;
1506 struct sk_buff *msg;
1507
1508 if (info->attrs[NL80211_ATTR_KEY_IDX])
1509 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1510
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001511 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001512 return -EINVAL;
1513
1514 if (info->attrs[NL80211_ATTR_MAC])
1515 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1516
Johannes Berge31b8212010-10-05 19:39:30 +02001517 pairwise = !!mac_addr;
1518 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1519 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1520 if (kt >= NUM_NL80211_KEYTYPES)
1521 return -EINVAL;
1522 if (kt != NL80211_KEYTYPE_GROUP &&
1523 kt != NL80211_KEYTYPE_PAIRWISE)
1524 return -EINVAL;
1525 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1526 }
1527
Johannes Berg4c476992010-10-04 21:36:35 +02001528 if (!rdev->ops->get_key)
1529 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001530
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001531 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001532 if (!msg)
1533 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001534
1535 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1536 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001537 if (IS_ERR(hdr))
1538 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001539
1540 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001541 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001542
1543 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1544 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1545 if (mac_addr)
1546 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1547
Johannes Berge31b8212010-10-05 19:39:30 +02001548 if (pairwise && mac_addr &&
1549 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1550 return -ENOENT;
1551
1552 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1553 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001554
1555 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001556 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001557
1558 if (cookie.error)
1559 goto nla_put_failure;
1560
1561 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001562 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001563
1564 nla_put_failure:
1565 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001566 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001567 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001568 return err;
1569}
1570
1571static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1572{
Johannes Berg4c476992010-10-04 21:36:35 +02001573 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001574 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001575 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001576 struct net_device *dev = info->user_ptr[1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001577 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1578 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001579
Johannes Bergb9454e82009-07-08 13:29:08 +02001580 err = nl80211_parse_key(info, &key);
1581 if (err)
1582 return err;
1583
1584 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001585 return -EINVAL;
1586
Johannes Bergb9454e82009-07-08 13:29:08 +02001587 /* only support setting default key */
1588 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001589 return -EINVAL;
1590
Johannes Bergb9454e82009-07-08 13:29:08 +02001591 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001592 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001593 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001594 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001595
Johannes Berg4c476992010-10-04 21:36:35 +02001596 if (!func)
1597 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001598
Johannes Bergfffd0932009-07-08 14:22:54 +02001599 wdev_lock(dev->ieee80211_ptr);
1600 err = nl80211_key_allowed(dev->ieee80211_ptr);
1601 if (!err)
1602 err = func(&rdev->wiphy, dev, key.idx);
1603
Johannes Berg3d23e342009-09-29 23:27:28 +02001604#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001605 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001606 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001607 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001608 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001609 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001610 }
1611#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001612 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001613
Johannes Berg41ade002007-12-19 02:03:29 +01001614 return err;
1615}
1616
1617static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1618{
Johannes Berg4c476992010-10-04 21:36:35 +02001619 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001620 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001621 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001622 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001623 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001624
Johannes Bergb9454e82009-07-08 13:29:08 +02001625 err = nl80211_parse_key(info, &key);
1626 if (err)
1627 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001628
Johannes Bergb9454e82009-07-08 13:29:08 +02001629 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001630 return -EINVAL;
1631
Johannes Berg41ade002007-12-19 02:03:29 +01001632 if (info->attrs[NL80211_ATTR_MAC])
1633 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1634
Johannes Berge31b8212010-10-05 19:39:30 +02001635 if (key.type == -1) {
1636 if (mac_addr)
1637 key.type = NL80211_KEYTYPE_PAIRWISE;
1638 else
1639 key.type = NL80211_KEYTYPE_GROUP;
1640 }
1641
1642 /* for now */
1643 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1644 key.type != NL80211_KEYTYPE_GROUP)
1645 return -EINVAL;
1646
Johannes Berg4c476992010-10-04 21:36:35 +02001647 if (!rdev->ops->add_key)
1648 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001649
Johannes Berge31b8212010-10-05 19:39:30 +02001650 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1651 key.type == NL80211_KEYTYPE_PAIRWISE,
1652 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001653 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001654
1655 wdev_lock(dev->ieee80211_ptr);
1656 err = nl80211_key_allowed(dev->ieee80211_ptr);
1657 if (!err)
1658 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001659 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001660 mac_addr, &key.p);
1661 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001662
Johannes Berg41ade002007-12-19 02:03:29 +01001663 return err;
1664}
1665
1666static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1667{
Johannes Berg4c476992010-10-04 21:36:35 +02001668 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001669 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001670 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001671 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001672 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001673
Johannes Bergb9454e82009-07-08 13:29:08 +02001674 err = nl80211_parse_key(info, &key);
1675 if (err)
1676 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001677
1678 if (info->attrs[NL80211_ATTR_MAC])
1679 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1680
Johannes Berge31b8212010-10-05 19:39:30 +02001681 if (key.type == -1) {
1682 if (mac_addr)
1683 key.type = NL80211_KEYTYPE_PAIRWISE;
1684 else
1685 key.type = NL80211_KEYTYPE_GROUP;
1686 }
1687
1688 /* for now */
1689 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1690 key.type != NL80211_KEYTYPE_GROUP)
1691 return -EINVAL;
1692
Johannes Berg4c476992010-10-04 21:36:35 +02001693 if (!rdev->ops->del_key)
1694 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001695
Johannes Bergfffd0932009-07-08 14:22:54 +02001696 wdev_lock(dev->ieee80211_ptr);
1697 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001698
1699 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1700 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1701 err = -ENOENT;
1702
Johannes Bergfffd0932009-07-08 14:22:54 +02001703 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001704 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1705 key.type == NL80211_KEYTYPE_PAIRWISE,
1706 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001707
Johannes Berg3d23e342009-09-29 23:27:28 +02001708#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001709 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001710 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001711 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001712 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001713 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1714 }
1715#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001716 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001717
Johannes Berg41ade002007-12-19 02:03:29 +01001718 return err;
1719}
1720
Johannes Berged1b6cc2007-12-19 02:03:32 +01001721static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1722{
1723 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1724 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001725 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1726 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001727 struct beacon_parameters params;
1728 int haveinfo = 0;
1729
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001730 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1731 return -EINVAL;
1732
Johannes Berg074ac8d2010-09-16 14:58:22 +02001733 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001734 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1735 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001736
Johannes Berged1b6cc2007-12-19 02:03:32 +01001737 switch (info->genlhdr->cmd) {
1738 case NL80211_CMD_NEW_BEACON:
1739 /* these are required for NEW_BEACON */
1740 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1741 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001742 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1743 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001744
Johannes Berg79c97e92009-07-07 03:56:12 +02001745 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001746 break;
1747 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001748 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001749 break;
1750 default:
1751 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001752 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001753 }
1754
Johannes Berg4c476992010-10-04 21:36:35 +02001755 if (!call)
1756 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001757
1758 memset(&params, 0, sizeof(params));
1759
1760 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1761 params.interval =
1762 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1763 haveinfo = 1;
1764 }
1765
1766 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1767 params.dtim_period =
1768 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1769 haveinfo = 1;
1770 }
1771
1772 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1773 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1774 params.head_len =
1775 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1776 haveinfo = 1;
1777 }
1778
1779 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1780 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1781 params.tail_len =
1782 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1783 haveinfo = 1;
1784 }
1785
Johannes Berg4c476992010-10-04 21:36:35 +02001786 if (!haveinfo)
1787 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001788
Johannes Berg4c476992010-10-04 21:36:35 +02001789 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001790}
1791
1792static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1793{
Johannes Berg4c476992010-10-04 21:36:35 +02001794 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1795 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001796
Johannes Berg4c476992010-10-04 21:36:35 +02001797 if (!rdev->ops->del_beacon)
1798 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001799
Johannes Berg074ac8d2010-09-16 14:58:22 +02001800 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001801 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1802 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001803
Johannes Berg4c476992010-10-04 21:36:35 +02001804 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001805}
1806
Johannes Berg5727ef12007-12-19 02:03:34 +01001807static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1808 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1809 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1810 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001811 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001812};
1813
Johannes Bergeccb8e82009-05-11 21:57:56 +03001814static int parse_station_flags(struct genl_info *info,
1815 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001816{
1817 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001818 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001819 int flag;
1820
Johannes Bergeccb8e82009-05-11 21:57:56 +03001821 /*
1822 * Try parsing the new attribute first so userspace
1823 * can specify both for older kernels.
1824 */
1825 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1826 if (nla) {
1827 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001828
Johannes Bergeccb8e82009-05-11 21:57:56 +03001829 sta_flags = nla_data(nla);
1830 params->sta_flags_mask = sta_flags->mask;
1831 params->sta_flags_set = sta_flags->set;
1832 if ((params->sta_flags_mask |
1833 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1834 return -EINVAL;
1835 return 0;
1836 }
1837
1838 /* if present, parse the old attribute */
1839
1840 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001841 if (!nla)
1842 return 0;
1843
1844 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1845 nla, sta_flags_policy))
1846 return -EINVAL;
1847
Johannes Bergeccb8e82009-05-11 21:57:56 +03001848 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1849 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001850
1851 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1852 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001853 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001854
1855 return 0;
1856}
1857
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001858static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1859 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001860 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001861{
1862 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001863 struct nlattr *sinfoattr, *txrate;
1864 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001865
1866 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1867 if (!hdr)
1868 return -1;
1869
1870 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1871 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1872
Johannes Bergf5ea9122009-08-07 16:17:38 +02001873 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1874
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001875 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1876 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001877 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001878 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1879 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1880 sinfo->inactive_time);
1881 if (sinfo->filled & STATION_INFO_RX_BYTES)
1882 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1883 sinfo->rx_bytes);
1884 if (sinfo->filled & STATION_INFO_TX_BYTES)
1885 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1886 sinfo->tx_bytes);
1887 if (sinfo->filled & STATION_INFO_LLID)
1888 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1889 sinfo->llid);
1890 if (sinfo->filled & STATION_INFO_PLID)
1891 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1892 sinfo->plid);
1893 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1894 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1895 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001896 if (sinfo->filled & STATION_INFO_SIGNAL)
1897 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1898 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09001899 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
1900 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
1901 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001902 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1903 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1904 if (!txrate)
1905 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001906
John W. Linville254416a2009-12-09 16:43:52 -05001907 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1908 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001909 if (bitrate > 0)
1910 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1911
1912 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1913 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1914 sinfo->txrate.mcs);
1915 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1916 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1917 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1918 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1919
1920 nla_nest_end(msg, txrate);
1921 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001922 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1923 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1924 sinfo->rx_packets);
1925 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1926 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1927 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09001928 if (sinfo->filled & STATION_INFO_TX_RETRIES)
1929 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
1930 sinfo->tx_retries);
1931 if (sinfo->filled & STATION_INFO_TX_FAILED)
1932 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
1933 sinfo->tx_failed);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001934 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001935
1936 return genlmsg_end(msg, hdr);
1937
1938 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001939 genlmsg_cancel(msg, hdr);
1940 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001941}
1942
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001943static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001944 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001945{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001946 struct station_info sinfo;
1947 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001948 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001949 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001950 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001951 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001952
Johannes Berg67748892010-10-04 21:14:06 +02001953 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1954 if (err)
1955 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001956
1957 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001958 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001959 goto out_err;
1960 }
1961
Johannes Bergbba95fe2008-07-29 13:22:51 +02001962 while (1) {
1963 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1964 mac_addr, &sinfo);
1965 if (err == -ENOENT)
1966 break;
1967 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001968 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001969
1970 if (nl80211_send_station(skb,
1971 NETLINK_CB(cb->skb).pid,
1972 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1973 netdev, mac_addr,
1974 &sinfo) < 0)
1975 goto out;
1976
1977 sta_idx++;
1978 }
1979
1980
1981 out:
1982 cb->args[1] = sta_idx;
1983 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001984 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02001985 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001986
1987 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001988}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001989
Johannes Berg5727ef12007-12-19 02:03:34 +01001990static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
1991{
Johannes Berg4c476992010-10-04 21:36:35 +02001992 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1993 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001994 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001995 struct sk_buff *msg;
1996 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02001997 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001998
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001999 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002000
2001 if (!info->attrs[NL80211_ATTR_MAC])
2002 return -EINVAL;
2003
2004 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2005
Johannes Berg4c476992010-10-04 21:36:35 +02002006 if (!rdev->ops->get_station)
2007 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002008
Johannes Berg79c97e92009-07-07 03:56:12 +02002009 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002010 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002011 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002012
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002013 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002014 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002015 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002016
2017 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002018 dev, mac_addr, &sinfo) < 0) {
2019 nlmsg_free(msg);
2020 return -ENOBUFS;
2021 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002022
Johannes Berg4c476992010-10-04 21:36:35 +02002023 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002024}
2025
2026/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002027 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002028 */
Johannes Berg463d0182009-07-14 00:33:35 +02002029static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002030 struct cfg80211_registered_device *rdev,
2031 struct net_device **vlan)
2032{
Johannes Berg463d0182009-07-14 00:33:35 +02002033 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002034 *vlan = NULL;
2035
2036 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002037 *vlan = dev_get_by_index(genl_info_net(info),
2038 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002039 if (!*vlan)
2040 return -ENODEV;
2041 if (!(*vlan)->ieee80211_ptr)
2042 return -EINVAL;
2043 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2044 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002045 if (!netif_running(*vlan))
2046 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002047 }
2048 return 0;
2049}
2050
2051static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2052{
Johannes Berg4c476992010-10-04 21:36:35 +02002053 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002054 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002055 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002056 struct station_parameters params;
2057 u8 *mac_addr = NULL;
2058
2059 memset(&params, 0, sizeof(params));
2060
2061 params.listen_interval = -1;
2062
2063 if (info->attrs[NL80211_ATTR_STA_AID])
2064 return -EINVAL;
2065
2066 if (!info->attrs[NL80211_ATTR_MAC])
2067 return -EINVAL;
2068
2069 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2070
2071 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2072 params.supported_rates =
2073 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2074 params.supported_rates_len =
2075 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2076 }
2077
2078 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2079 params.listen_interval =
2080 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2081
Jouni Malinen36aedc92008-08-25 11:58:58 +03002082 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2083 params.ht_capa =
2084 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2085
Johannes Bergeccb8e82009-05-11 21:57:56 +03002086 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002087 return -EINVAL;
2088
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002089 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2090 params.plink_action =
2091 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2092
Johannes Berg463d0182009-07-14 00:33:35 +02002093 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002094 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002095 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002096
2097 /* validate settings */
2098 err = 0;
2099
2100 switch (dev->ieee80211_ptr->iftype) {
2101 case NL80211_IFTYPE_AP:
2102 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002103 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002104 /* disallow mesh-specific things */
2105 if (params.plink_action)
2106 err = -EINVAL;
2107 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002108 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002109 case NL80211_IFTYPE_STATION:
2110 /* disallow everything but AUTHORIZED flag */
2111 if (params.plink_action)
2112 err = -EINVAL;
2113 if (params.vlan)
2114 err = -EINVAL;
2115 if (params.supported_rates)
2116 err = -EINVAL;
2117 if (params.ht_capa)
2118 err = -EINVAL;
2119 if (params.listen_interval >= 0)
2120 err = -EINVAL;
2121 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2122 err = -EINVAL;
2123 break;
2124 case NL80211_IFTYPE_MESH_POINT:
2125 /* disallow things mesh doesn't support */
2126 if (params.vlan)
2127 err = -EINVAL;
2128 if (params.ht_capa)
2129 err = -EINVAL;
2130 if (params.listen_interval >= 0)
2131 err = -EINVAL;
2132 if (params.supported_rates)
2133 err = -EINVAL;
2134 if (params.sta_flags_mask)
2135 err = -EINVAL;
2136 break;
2137 default:
2138 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002139 }
2140
Johannes Berg5727ef12007-12-19 02:03:34 +01002141 if (err)
2142 goto out;
2143
Johannes Berg79c97e92009-07-07 03:56:12 +02002144 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002145 err = -EOPNOTSUPP;
2146 goto out;
2147 }
2148
Johannes Berg79c97e92009-07-07 03:56:12 +02002149 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002150
2151 out:
2152 if (params.vlan)
2153 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002154
Johannes Berg5727ef12007-12-19 02:03:34 +01002155 return err;
2156}
2157
2158static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2159{
Johannes Berg4c476992010-10-04 21:36:35 +02002160 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002161 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002162 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002163 struct station_parameters params;
2164 u8 *mac_addr = NULL;
2165
2166 memset(&params, 0, sizeof(params));
2167
2168 if (!info->attrs[NL80211_ATTR_MAC])
2169 return -EINVAL;
2170
Johannes Berg5727ef12007-12-19 02:03:34 +01002171 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2172 return -EINVAL;
2173
2174 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2175 return -EINVAL;
2176
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002177 if (!info->attrs[NL80211_ATTR_STA_AID])
2178 return -EINVAL;
2179
Johannes Berg5727ef12007-12-19 02:03:34 +01002180 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2181 params.supported_rates =
2182 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2183 params.supported_rates_len =
2184 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2185 params.listen_interval =
2186 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002187
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002188 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2189 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2190 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002191
Jouni Malinen36aedc92008-08-25 11:58:58 +03002192 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2193 params.ht_capa =
2194 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002195
Johannes Bergeccb8e82009-05-11 21:57:56 +03002196 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002197 return -EINVAL;
2198
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002199 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002200 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02002201 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2202 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002203
Johannes Berg463d0182009-07-14 00:33:35 +02002204 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002205 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002206 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002207
2208 /* validate settings */
2209 err = 0;
2210
Johannes Berg79c97e92009-07-07 03:56:12 +02002211 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002212 err = -EOPNOTSUPP;
2213 goto out;
2214 }
2215
Johannes Berg79c97e92009-07-07 03:56:12 +02002216 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002217
2218 out:
2219 if (params.vlan)
2220 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002221 return err;
2222}
2223
2224static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2225{
Johannes Berg4c476992010-10-04 21:36:35 +02002226 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2227 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002228 u8 *mac_addr = NULL;
2229
2230 if (info->attrs[NL80211_ATTR_MAC])
2231 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2232
Johannes Berge80cf852009-05-11 14:43:13 +02002233 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002234 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002235 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002236 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2237 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002238
Johannes Berg4c476992010-10-04 21:36:35 +02002239 if (!rdev->ops->del_station)
2240 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002241
Johannes Berg4c476992010-10-04 21:36:35 +02002242 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002243}
2244
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002245static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2246 int flags, struct net_device *dev,
2247 u8 *dst, u8 *next_hop,
2248 struct mpath_info *pinfo)
2249{
2250 void *hdr;
2251 struct nlattr *pinfoattr;
2252
2253 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2254 if (!hdr)
2255 return -1;
2256
2257 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2258 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2259 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2260
Johannes Bergf5ea9122009-08-07 16:17:38 +02002261 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2262
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002263 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2264 if (!pinfoattr)
2265 goto nla_put_failure;
2266 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2267 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2268 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002269 if (pinfo->filled & MPATH_INFO_SN)
2270 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2271 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002272 if (pinfo->filled & MPATH_INFO_METRIC)
2273 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2274 pinfo->metric);
2275 if (pinfo->filled & MPATH_INFO_EXPTIME)
2276 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2277 pinfo->exptime);
2278 if (pinfo->filled & MPATH_INFO_FLAGS)
2279 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2280 pinfo->flags);
2281 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2282 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2283 pinfo->discovery_timeout);
2284 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2285 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2286 pinfo->discovery_retries);
2287
2288 nla_nest_end(msg, pinfoattr);
2289
2290 return genlmsg_end(msg, hdr);
2291
2292 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002293 genlmsg_cancel(msg, hdr);
2294 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002295}
2296
2297static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002298 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002299{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002300 struct mpath_info pinfo;
2301 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002302 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002303 u8 dst[ETH_ALEN];
2304 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002305 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002306 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002307
Johannes Berg67748892010-10-04 21:14:06 +02002308 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2309 if (err)
2310 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002311
2312 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002313 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002314 goto out_err;
2315 }
2316
Jouni Malineneec60b02009-03-20 21:21:19 +02002317 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2318 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002319 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002320 }
2321
Johannes Bergbba95fe2008-07-29 13:22:51 +02002322 while (1) {
2323 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2324 dst, next_hop, &pinfo);
2325 if (err == -ENOENT)
2326 break;
2327 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002328 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002329
2330 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2331 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2332 netdev, dst, next_hop,
2333 &pinfo) < 0)
2334 goto out;
2335
2336 path_idx++;
2337 }
2338
2339
2340 out:
2341 cb->args[1] = path_idx;
2342 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002343 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002344 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002345 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002346}
2347
2348static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2349{
Johannes Berg4c476992010-10-04 21:36:35 +02002350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002351 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002352 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002353 struct mpath_info pinfo;
2354 struct sk_buff *msg;
2355 u8 *dst = NULL;
2356 u8 next_hop[ETH_ALEN];
2357
2358 memset(&pinfo, 0, sizeof(pinfo));
2359
2360 if (!info->attrs[NL80211_ATTR_MAC])
2361 return -EINVAL;
2362
2363 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2364
Johannes Berg4c476992010-10-04 21:36:35 +02002365 if (!rdev->ops->get_mpath)
2366 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002367
Johannes Berg4c476992010-10-04 21:36:35 +02002368 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2369 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002370
Johannes Berg79c97e92009-07-07 03:56:12 +02002371 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002372 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002373 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002374
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002375 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002376 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002377 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002378
2379 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002380 dev, dst, next_hop, &pinfo) < 0) {
2381 nlmsg_free(msg);
2382 return -ENOBUFS;
2383 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002384
Johannes Berg4c476992010-10-04 21:36:35 +02002385 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002386}
2387
2388static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2389{
Johannes Berg4c476992010-10-04 21:36:35 +02002390 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2391 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002392 u8 *dst = NULL;
2393 u8 *next_hop = NULL;
2394
2395 if (!info->attrs[NL80211_ATTR_MAC])
2396 return -EINVAL;
2397
2398 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2399 return -EINVAL;
2400
2401 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2402 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2403
Johannes Berg4c476992010-10-04 21:36:35 +02002404 if (!rdev->ops->change_mpath)
2405 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002406
Johannes Berg4c476992010-10-04 21:36:35 +02002407 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2408 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002409
Johannes Berg4c476992010-10-04 21:36:35 +02002410 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002411}
Johannes Berg4c476992010-10-04 21:36:35 +02002412
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002413static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2414{
Johannes Berg4c476992010-10-04 21:36:35 +02002415 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2416 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002417 u8 *dst = NULL;
2418 u8 *next_hop = NULL;
2419
2420 if (!info->attrs[NL80211_ATTR_MAC])
2421 return -EINVAL;
2422
2423 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2424 return -EINVAL;
2425
2426 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2427 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2428
Johannes Berg4c476992010-10-04 21:36:35 +02002429 if (!rdev->ops->add_mpath)
2430 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002431
Johannes Berg4c476992010-10-04 21:36:35 +02002432 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2433 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002434
Johannes Berg4c476992010-10-04 21:36:35 +02002435 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002436}
2437
2438static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2439{
Johannes Berg4c476992010-10-04 21:36:35 +02002440 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2441 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002442 u8 *dst = NULL;
2443
2444 if (info->attrs[NL80211_ATTR_MAC])
2445 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2446
Johannes Berg4c476992010-10-04 21:36:35 +02002447 if (!rdev->ops->del_mpath)
2448 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002449
Johannes Berg4c476992010-10-04 21:36:35 +02002450 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002451}
2452
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002453static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2454{
Johannes Berg4c476992010-10-04 21:36:35 +02002455 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2456 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002457 struct bss_parameters params;
2458
2459 memset(&params, 0, sizeof(params));
2460 /* default to not changing parameters */
2461 params.use_cts_prot = -1;
2462 params.use_short_preamble = -1;
2463 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002464 params.ap_isolate = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002465
2466 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2467 params.use_cts_prot =
2468 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2469 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2470 params.use_short_preamble =
2471 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2472 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2473 params.use_short_slot_time =
2474 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002475 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2476 params.basic_rates =
2477 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2478 params.basic_rates_len =
2479 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2480 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002481 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2482 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002483
Johannes Berg4c476992010-10-04 21:36:35 +02002484 if (!rdev->ops->change_bss)
2485 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002486
Johannes Berg074ac8d2010-09-16 14:58:22 +02002487 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002488 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2489 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002490
Johannes Berg4c476992010-10-04 21:36:35 +02002491 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002492}
2493
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002494static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002495 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2496 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2497 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2498 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2499 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2500 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2501};
2502
2503static int parse_reg_rule(struct nlattr *tb[],
2504 struct ieee80211_reg_rule *reg_rule)
2505{
2506 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2507 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2508
2509 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2510 return -EINVAL;
2511 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2512 return -EINVAL;
2513 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2514 return -EINVAL;
2515 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2516 return -EINVAL;
2517 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2518 return -EINVAL;
2519
2520 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2521
2522 freq_range->start_freq_khz =
2523 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2524 freq_range->end_freq_khz =
2525 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2526 freq_range->max_bandwidth_khz =
2527 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2528
2529 power_rule->max_eirp =
2530 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2531
2532 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2533 power_rule->max_antenna_gain =
2534 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2535
2536 return 0;
2537}
2538
2539static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2540{
2541 int r;
2542 char *data = NULL;
2543
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002544 /*
2545 * You should only get this when cfg80211 hasn't yet initialized
2546 * completely when built-in to the kernel right between the time
2547 * window between nl80211_init() and regulatory_init(), if that is
2548 * even possible.
2549 */
2550 mutex_lock(&cfg80211_mutex);
2551 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002552 mutex_unlock(&cfg80211_mutex);
2553 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002554 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002555 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002556
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002557 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2558 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002559
2560 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2561
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002562 r = regulatory_hint_user(data);
2563
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002564 return r;
2565}
2566
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002567static int nl80211_get_mesh_params(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002568 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002569{
Johannes Berg4c476992010-10-04 21:36:35 +02002570 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02002571 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002572 struct wireless_dev *wdev = dev->ieee80211_ptr;
2573 struct mesh_config cur_params;
2574 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002575 void *hdr;
2576 struct nlattr *pinfoattr;
2577 struct sk_buff *msg;
2578
Johannes Berg29cbe682010-12-03 09:20:44 +01002579 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2580 return -EOPNOTSUPP;
2581
Johannes Berg4c476992010-10-04 21:36:35 +02002582 if (!rdev->ops->get_mesh_params)
2583 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002584
Johannes Berg29cbe682010-12-03 09:20:44 +01002585 wdev_lock(wdev);
2586 /* If not connected, get default parameters */
2587 if (!wdev->mesh_id_len)
2588 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
2589 else
2590 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev,
2591 &cur_params);
2592 wdev_unlock(wdev);
2593
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002594 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002595 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002596
2597 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002598 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002599 if (!msg)
2600 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002601 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2602 NL80211_CMD_GET_MESH_PARAMS);
2603 if (!hdr)
2604 goto nla_put_failure;
2605 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2606 if (!pinfoattr)
2607 goto nla_put_failure;
2608 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2609 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2610 cur_params.dot11MeshRetryTimeout);
2611 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2612 cur_params.dot11MeshConfirmTimeout);
2613 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2614 cur_params.dot11MeshHoldingTimeout);
2615 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2616 cur_params.dot11MeshMaxPeerLinks);
2617 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2618 cur_params.dot11MeshMaxRetries);
2619 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2620 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01002621 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
2622 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002623 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2624 cur_params.auto_open_plinks);
2625 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2626 cur_params.dot11MeshHWMPmaxPREQretries);
2627 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2628 cur_params.path_refresh_time);
2629 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2630 cur_params.min_discovery_timeout);
2631 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2632 cur_params.dot11MeshHWMPactivePathTimeout);
2633 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2634 cur_params.dot11MeshHWMPpreqMinInterval);
2635 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2636 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002637 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2638 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002639 nla_nest_end(msg, pinfoattr);
2640 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002641 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002642
Johannes Berg3b858752009-03-12 09:55:09 +01002643 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002644 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002645 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002646 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002647}
2648
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002649static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002650 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2651 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2652 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2653 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2654 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2655 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01002656 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002657 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2658
2659 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2660 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2661 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2662 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2663 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2664 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2665};
2666
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002667static int nl80211_parse_mesh_params(struct genl_info *info,
2668 struct mesh_config *cfg,
2669 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002670{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002671 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002672 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002673
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002674#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2675do {\
2676 if (table[attr_num]) {\
2677 cfg->param = nla_fn(table[attr_num]); \
2678 mask |= (1 << (attr_num - 1)); \
2679 } \
2680} while (0);\
2681
2682
2683 if (!info->attrs[NL80211_ATTR_MESH_PARAMS])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002684 return -EINVAL;
2685 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002686 info->attrs[NL80211_ATTR_MESH_PARAMS],
2687 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002688 return -EINVAL;
2689
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002690 /* This makes sure that there aren't more than 32 mesh config
2691 * parameters (otherwise our bitfield scheme would not work.) */
2692 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2693
2694 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002695 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2696 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2697 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2698 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2699 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2700 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2701 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2702 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2703 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2704 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2705 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2706 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01002707 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
2708 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002709 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2710 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2712 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2713 nla_get_u8);
2714 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2715 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2716 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2717 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2718 nla_get_u16);
2719 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2720 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2721 nla_get_u32);
2722 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2723 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2724 nla_get_u16);
2725 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2726 dot11MeshHWMPnetDiameterTraversalTime,
2727 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2728 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002729 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2730 dot11MeshHWMPRootMode, mask,
2731 NL80211_MESHCONF_HWMP_ROOTMODE,
2732 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002733
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002734 if (mask_out)
2735 *mask_out = mask;
2736 return 0;
2737
2738#undef FILL_IN_MESH_PARAM_IF_SET
2739}
2740
Johannes Berg29cbe682010-12-03 09:20:44 +01002741static int nl80211_update_mesh_params(struct sk_buff *skb,
2742 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002743{
2744 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2745 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002746 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002747 struct mesh_config cfg;
2748 u32 mask;
2749 int err;
2750
Johannes Berg29cbe682010-12-03 09:20:44 +01002751 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2752 return -EOPNOTSUPP;
2753
2754 if (!rdev->ops->update_mesh_params)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002755 return -EOPNOTSUPP;
2756
2757 err = nl80211_parse_mesh_params(info, &cfg, &mask);
2758 if (err)
2759 return err;
2760
Johannes Berg29cbe682010-12-03 09:20:44 +01002761 wdev_lock(wdev);
2762 if (!wdev->mesh_id_len)
2763 err = -ENOLINK;
2764
2765 if (!err)
2766 err = rdev->ops->update_mesh_params(&rdev->wiphy, dev,
2767 mask, &cfg);
2768
2769 wdev_unlock(wdev);
2770
2771 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002772}
2773
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002774static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2775{
2776 struct sk_buff *msg;
2777 void *hdr = NULL;
2778 struct nlattr *nl_reg_rules;
2779 unsigned int i;
2780 int err = -EINVAL;
2781
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002782 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002783
2784 if (!cfg80211_regdomain)
2785 goto out;
2786
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002787 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002788 if (!msg) {
2789 err = -ENOBUFS;
2790 goto out;
2791 }
2792
2793 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2794 NL80211_CMD_GET_REG);
2795 if (!hdr)
2796 goto nla_put_failure;
2797
2798 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2799 cfg80211_regdomain->alpha2);
2800
2801 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2802 if (!nl_reg_rules)
2803 goto nla_put_failure;
2804
2805 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2806 struct nlattr *nl_reg_rule;
2807 const struct ieee80211_reg_rule *reg_rule;
2808 const struct ieee80211_freq_range *freq_range;
2809 const struct ieee80211_power_rule *power_rule;
2810
2811 reg_rule = &cfg80211_regdomain->reg_rules[i];
2812 freq_range = &reg_rule->freq_range;
2813 power_rule = &reg_rule->power_rule;
2814
2815 nl_reg_rule = nla_nest_start(msg, i);
2816 if (!nl_reg_rule)
2817 goto nla_put_failure;
2818
2819 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2820 reg_rule->flags);
2821 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2822 freq_range->start_freq_khz);
2823 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2824 freq_range->end_freq_khz);
2825 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2826 freq_range->max_bandwidth_khz);
2827 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2828 power_rule->max_antenna_gain);
2829 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2830 power_rule->max_eirp);
2831
2832 nla_nest_end(msg, nl_reg_rule);
2833 }
2834
2835 nla_nest_end(msg, nl_reg_rules);
2836
2837 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002838 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002839 goto out;
2840
2841nla_put_failure:
2842 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002843 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002844 err = -EMSGSIZE;
2845out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002846 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002847 return err;
2848}
2849
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002850static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2851{
2852 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2853 struct nlattr *nl_reg_rule;
2854 char *alpha2 = NULL;
2855 int rem_reg_rules = 0, r = 0;
2856 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2857 struct ieee80211_regdomain *rd = NULL;
2858
2859 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2860 return -EINVAL;
2861
2862 if (!info->attrs[NL80211_ATTR_REG_RULES])
2863 return -EINVAL;
2864
2865 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2866
2867 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2868 rem_reg_rules) {
2869 num_rules++;
2870 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002871 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002872 }
2873
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002874 mutex_lock(&cfg80211_mutex);
2875
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002876 if (!reg_is_valid_request(alpha2)) {
2877 r = -EINVAL;
2878 goto bad_reg;
2879 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002880
2881 size_of_regd = sizeof(struct ieee80211_regdomain) +
2882 (num_rules * sizeof(struct ieee80211_reg_rule));
2883
2884 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002885 if (!rd) {
2886 r = -ENOMEM;
2887 goto bad_reg;
2888 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002889
2890 rd->n_reg_rules = num_rules;
2891 rd->alpha2[0] = alpha2[0];
2892 rd->alpha2[1] = alpha2[1];
2893
2894 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2895 rem_reg_rules) {
2896 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2897 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2898 reg_rule_policy);
2899 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2900 if (r)
2901 goto bad_reg;
2902
2903 rule_idx++;
2904
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002905 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2906 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002907 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002908 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002909 }
2910
2911 BUG_ON(rule_idx != num_rules);
2912
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002913 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002914
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002915 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002916
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002917 return r;
2918
Johannes Bergd2372b32008-10-24 20:32:20 +02002919 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002920 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002921 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002922 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002923}
2924
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002925static int validate_scan_freqs(struct nlattr *freqs)
2926{
2927 struct nlattr *attr1, *attr2;
2928 int n_channels = 0, tmp1, tmp2;
2929
2930 nla_for_each_nested(attr1, freqs, tmp1) {
2931 n_channels++;
2932 /*
2933 * Some hardware has a limited channel list for
2934 * scanning, and it is pretty much nonsensical
2935 * to scan for a channel twice, so disallow that
2936 * and don't require drivers to check that the
2937 * channel list they get isn't longer than what
2938 * they can scan, as long as they can scan all
2939 * the channels they registered at once.
2940 */
2941 nla_for_each_nested(attr2, freqs, tmp2)
2942 if (attr1 != attr2 &&
2943 nla_get_u32(attr1) == nla_get_u32(attr2))
2944 return 0;
2945 }
2946
2947 return n_channels;
2948}
2949
Johannes Berg2a519312009-02-10 21:25:55 +01002950static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2951{
Johannes Berg4c476992010-10-04 21:36:35 +02002952 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2953 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01002954 struct cfg80211_scan_request *request;
2955 struct cfg80211_ssid *ssid;
2956 struct ieee80211_channel *channel;
2957 struct nlattr *attr;
2958 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002959 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002960 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002961 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002962
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002963 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2964 return -EINVAL;
2965
Johannes Berg79c97e92009-07-07 03:56:12 +02002966 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002967
Johannes Berg4c476992010-10-04 21:36:35 +02002968 if (!rdev->ops->scan)
2969 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01002970
Johannes Berg4c476992010-10-04 21:36:35 +02002971 if (rdev->scan_req)
2972 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01002973
2974 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002975 n_channels = validate_scan_freqs(
2976 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02002977 if (!n_channels)
2978 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002979 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002980 n_channels = 0;
2981
Johannes Berg2a519312009-02-10 21:25:55 +01002982 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2983 if (wiphy->bands[band])
2984 n_channels += wiphy->bands[band]->n_channels;
2985 }
2986
2987 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
2988 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
2989 n_ssids++;
2990
Johannes Berg4c476992010-10-04 21:36:35 +02002991 if (n_ssids > wiphy->max_scan_ssids)
2992 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002993
Jouni Malinen70692ad2009-02-16 19:39:13 +02002994 if (info->attrs[NL80211_ATTR_IE])
2995 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2996 else
2997 ie_len = 0;
2998
Johannes Berg4c476992010-10-04 21:36:35 +02002999 if (ie_len > wiphy->max_scan_ie_len)
3000 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003001
Johannes Berg2a519312009-02-10 21:25:55 +01003002 request = kzalloc(sizeof(*request)
3003 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003004 + sizeof(channel) * n_channels
3005 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003006 if (!request)
3007 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003008
Johannes Berg2a519312009-02-10 21:25:55 +01003009 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003010 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003011 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003012 if (ie_len) {
3013 if (request->ssids)
3014 request->ie = (void *)(request->ssids + n_ssids);
3015 else
3016 request->ie = (void *)(request->channels + n_channels);
3017 }
Johannes Berg2a519312009-02-10 21:25:55 +01003018
Johannes Berg584991d2009-11-02 13:32:03 +01003019 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003020 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3021 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003022 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003023 struct ieee80211_channel *chan;
3024
3025 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3026
3027 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003028 err = -EINVAL;
3029 goto out_free;
3030 }
Johannes Berg584991d2009-11-02 13:32:03 +01003031
3032 /* ignore disabled channels */
3033 if (chan->flags & IEEE80211_CHAN_DISABLED)
3034 continue;
3035
3036 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003037 i++;
3038 }
3039 } else {
3040 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003041 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3042 int j;
3043 if (!wiphy->bands[band])
3044 continue;
3045 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003046 struct ieee80211_channel *chan;
3047
3048 chan = &wiphy->bands[band]->channels[j];
3049
3050 if (chan->flags & IEEE80211_CHAN_DISABLED)
3051 continue;
3052
3053 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003054 i++;
3055 }
3056 }
3057 }
3058
Johannes Berg584991d2009-11-02 13:32:03 +01003059 if (!i) {
3060 err = -EINVAL;
3061 goto out_free;
3062 }
3063
3064 request->n_channels = i;
3065
Johannes Berg2a519312009-02-10 21:25:55 +01003066 i = 0;
3067 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3068 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3069 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3070 err = -EINVAL;
3071 goto out_free;
3072 }
3073 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3074 request->ssids[i].ssid_len = nla_len(attr);
3075 i++;
3076 }
3077 }
3078
Jouni Malinen70692ad2009-02-16 19:39:13 +02003079 if (info->attrs[NL80211_ATTR_IE]) {
3080 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003081 memcpy((void *)request->ie,
3082 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003083 request->ie_len);
3084 }
3085
Johannes Berg463d0182009-07-14 00:33:35 +02003086 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003087 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003088
Johannes Berg79c97e92009-07-07 03:56:12 +02003089 rdev->scan_req = request;
3090 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003091
Johannes Berg463d0182009-07-14 00:33:35 +02003092 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003093 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003094 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003095 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003096 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003097 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003098 kfree(request);
3099 }
Johannes Berg3b858752009-03-12 09:55:09 +01003100
Johannes Berg2a519312009-02-10 21:25:55 +01003101 return err;
3102}
3103
3104static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3105 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003106 struct wireless_dev *wdev,
3107 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003108{
Johannes Berg48ab9052009-07-10 18:42:31 +02003109 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003110 void *hdr;
3111 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003112 int i;
3113
3114 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003115
3116 hdr = nl80211hdr_put(msg, pid, seq, flags,
3117 NL80211_CMD_NEW_SCAN_RESULTS);
3118 if (!hdr)
3119 return -1;
3120
Johannes Bergf5ea9122009-08-07 16:17:38 +02003121 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003122 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003123
3124 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3125 if (!bss)
3126 goto nla_put_failure;
3127 if (!is_zero_ether_addr(res->bssid))
3128 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3129 if (res->information_elements && res->len_information_elements)
3130 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3131 res->len_information_elements,
3132 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003133 if (res->beacon_ies && res->len_beacon_ies &&
3134 res->beacon_ies != res->information_elements)
3135 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3136 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003137 if (res->tsf)
3138 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3139 if (res->beacon_interval)
3140 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3141 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3142 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003143 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3144 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003145
Johannes Berg77965c92009-02-18 18:45:06 +01003146 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003147 case CFG80211_SIGNAL_TYPE_MBM:
3148 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3149 break;
3150 case CFG80211_SIGNAL_TYPE_UNSPEC:
3151 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3152 break;
3153 default:
3154 break;
3155 }
3156
Johannes Berg48ab9052009-07-10 18:42:31 +02003157 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003158 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003159 case NL80211_IFTYPE_STATION:
3160 if (intbss == wdev->current_bss)
3161 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3162 NL80211_BSS_STATUS_ASSOCIATED);
3163 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3164 if (intbss != wdev->auth_bsses[i])
3165 continue;
3166 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3167 NL80211_BSS_STATUS_AUTHENTICATED);
3168 break;
3169 }
3170 break;
3171 case NL80211_IFTYPE_ADHOC:
3172 if (intbss == wdev->current_bss)
3173 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3174 NL80211_BSS_STATUS_IBSS_JOINED);
3175 break;
3176 default:
3177 break;
3178 }
3179
Johannes Berg2a519312009-02-10 21:25:55 +01003180 nla_nest_end(msg, bss);
3181
3182 return genlmsg_end(msg, hdr);
3183
3184 nla_put_failure:
3185 genlmsg_cancel(msg, hdr);
3186 return -EMSGSIZE;
3187}
3188
3189static int nl80211_dump_scan(struct sk_buff *skb,
3190 struct netlink_callback *cb)
3191{
Johannes Berg48ab9052009-07-10 18:42:31 +02003192 struct cfg80211_registered_device *rdev;
3193 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003194 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003195 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003196 int start = cb->args[1], idx = 0;
3197 int err;
3198
Johannes Berg67748892010-10-04 21:14:06 +02003199 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3200 if (err)
3201 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003202
Johannes Berg48ab9052009-07-10 18:42:31 +02003203 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003204
Johannes Berg48ab9052009-07-10 18:42:31 +02003205 wdev_lock(wdev);
3206 spin_lock_bh(&rdev->bss_lock);
3207 cfg80211_bss_expire(rdev);
3208
3209 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003210 if (++idx <= start)
3211 continue;
3212 if (nl80211_send_bss(skb,
3213 NETLINK_CB(cb->skb).pid,
3214 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003215 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003216 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003217 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003218 }
3219 }
3220
Johannes Berg48ab9052009-07-10 18:42:31 +02003221 spin_unlock_bh(&rdev->bss_lock);
3222 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003223
3224 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003225 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003226
Johannes Berg67748892010-10-04 21:14:06 +02003227 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003228}
3229
Holger Schurig61fa7132009-11-11 12:25:40 +01003230static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3231 int flags, struct net_device *dev,
3232 struct survey_info *survey)
3233{
3234 void *hdr;
3235 struct nlattr *infoattr;
3236
3237 /* Survey without a channel doesn't make sense */
3238 if (!survey->channel)
3239 return -EINVAL;
3240
3241 hdr = nl80211hdr_put(msg, pid, seq, flags,
3242 NL80211_CMD_NEW_SURVEY_RESULTS);
3243 if (!hdr)
3244 return -ENOMEM;
3245
3246 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3247
3248 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3249 if (!infoattr)
3250 goto nla_put_failure;
3251
3252 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3253 survey->channel->center_freq);
3254 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3255 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3256 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003257 if (survey->filled & SURVEY_INFO_IN_USE)
3258 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02003259 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3260 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3261 survey->channel_time);
3262 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3263 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3264 survey->channel_time_busy);
3265 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3266 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3267 survey->channel_time_ext_busy);
3268 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3269 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3270 survey->channel_time_rx);
3271 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3272 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3273 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003274
3275 nla_nest_end(msg, infoattr);
3276
3277 return genlmsg_end(msg, hdr);
3278
3279 nla_put_failure:
3280 genlmsg_cancel(msg, hdr);
3281 return -EMSGSIZE;
3282}
3283
3284static int nl80211_dump_survey(struct sk_buff *skb,
3285 struct netlink_callback *cb)
3286{
3287 struct survey_info survey;
3288 struct cfg80211_registered_device *dev;
3289 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003290 int survey_idx = cb->args[1];
3291 int res;
3292
Johannes Berg67748892010-10-04 21:14:06 +02003293 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3294 if (res)
3295 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003296
3297 if (!dev->ops->dump_survey) {
3298 res = -EOPNOTSUPP;
3299 goto out_err;
3300 }
3301
3302 while (1) {
3303 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3304 &survey);
3305 if (res == -ENOENT)
3306 break;
3307 if (res)
3308 goto out_err;
3309
3310 if (nl80211_send_survey(skb,
3311 NETLINK_CB(cb->skb).pid,
3312 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3313 netdev,
3314 &survey) < 0)
3315 goto out;
3316 survey_idx++;
3317 }
3318
3319 out:
3320 cb->args[1] = survey_idx;
3321 res = skb->len;
3322 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003323 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003324 return res;
3325}
3326
Jouni Malinen255e7372009-03-20 21:21:17 +02003327static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3328{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003329 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003330}
3331
Samuel Ortizb23aa672009-07-01 21:26:54 +02003332static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3333{
3334 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3335 NL80211_WPA_VERSION_2));
3336}
3337
3338static bool nl80211_valid_akm_suite(u32 akm)
3339{
3340 return akm == WLAN_AKM_SUITE_8021X ||
3341 akm == WLAN_AKM_SUITE_PSK;
3342}
3343
3344static bool nl80211_valid_cipher_suite(u32 cipher)
3345{
3346 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3347 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3348 cipher == WLAN_CIPHER_SUITE_TKIP ||
3349 cipher == WLAN_CIPHER_SUITE_CCMP ||
3350 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3351}
3352
3353
Jouni Malinen636a5d32009-03-19 13:39:22 +02003354static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3355{
Johannes Berg4c476992010-10-04 21:36:35 +02003356 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3357 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003358 struct ieee80211_channel *chan;
3359 const u8 *bssid, *ssid, *ie = NULL;
3360 int err, ssid_len, ie_len = 0;
3361 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003362 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003363 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003364
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003365 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3366 return -EINVAL;
3367
3368 if (!info->attrs[NL80211_ATTR_MAC])
3369 return -EINVAL;
3370
Jouni Malinen17780922009-03-27 20:52:47 +02003371 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3372 return -EINVAL;
3373
Johannes Berg19957bb2009-07-02 17:20:43 +02003374 if (!info->attrs[NL80211_ATTR_SSID])
3375 return -EINVAL;
3376
3377 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3378 return -EINVAL;
3379
Johannes Bergfffd0932009-07-08 14:22:54 +02003380 err = nl80211_parse_key(info, &key);
3381 if (err)
3382 return err;
3383
3384 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003385 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3386 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003387 if (!key.p.key || !key.p.key_len)
3388 return -EINVAL;
3389 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3390 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3391 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3392 key.p.key_len != WLAN_KEY_LEN_WEP104))
3393 return -EINVAL;
3394 if (key.idx > 4)
3395 return -EINVAL;
3396 } else {
3397 key.p.key_len = 0;
3398 key.p.key = NULL;
3399 }
3400
Johannes Bergafea0b72010-08-10 09:46:42 +02003401 if (key.idx >= 0) {
3402 int i;
3403 bool ok = false;
3404 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3405 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3406 ok = true;
3407 break;
3408 }
3409 }
Johannes Berg4c476992010-10-04 21:36:35 +02003410 if (!ok)
3411 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003412 }
3413
Johannes Berg4c476992010-10-04 21:36:35 +02003414 if (!rdev->ops->auth)
3415 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003416
Johannes Berg074ac8d2010-09-16 14:58:22 +02003417 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003418 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3419 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003420
Johannes Berg19957bb2009-07-02 17:20:43 +02003421 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003422 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003423 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003424 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3425 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003426
Johannes Berg19957bb2009-07-02 17:20:43 +02003427 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3428 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3429
3430 if (info->attrs[NL80211_ATTR_IE]) {
3431 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3432 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3433 }
3434
3435 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003436 if (!nl80211_valid_auth_type(auth_type))
3437 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003438
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003439 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3440
Johannes Berg4c476992010-10-04 21:36:35 +02003441 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3442 ssid, ssid_len, ie, ie_len,
3443 key.p.key, key.p.key_len, key.idx,
3444 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003445}
3446
Johannes Bergc0692b82010-08-27 14:26:53 +03003447static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3448 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003449 struct cfg80211_crypto_settings *settings,
3450 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003451{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003452 memset(settings, 0, sizeof(*settings));
3453
Samuel Ortizb23aa672009-07-01 21:26:54 +02003454 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3455
Johannes Bergc0692b82010-08-27 14:26:53 +03003456 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3457 u16 proto;
3458 proto = nla_get_u16(
3459 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3460 settings->control_port_ethertype = cpu_to_be16(proto);
3461 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3462 proto != ETH_P_PAE)
3463 return -EINVAL;
3464 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3465 settings->control_port_no_encrypt = true;
3466 } else
3467 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3468
Samuel Ortizb23aa672009-07-01 21:26:54 +02003469 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3470 void *data;
3471 int len, i;
3472
3473 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3474 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3475 settings->n_ciphers_pairwise = len / sizeof(u32);
3476
3477 if (len % sizeof(u32))
3478 return -EINVAL;
3479
Johannes Berg3dc27d22009-07-02 21:36:37 +02003480 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003481 return -EINVAL;
3482
3483 memcpy(settings->ciphers_pairwise, data, len);
3484
3485 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3486 if (!nl80211_valid_cipher_suite(
3487 settings->ciphers_pairwise[i]))
3488 return -EINVAL;
3489 }
3490
3491 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3492 settings->cipher_group =
3493 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3494 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3495 return -EINVAL;
3496 }
3497
3498 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3499 settings->wpa_versions =
3500 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3501 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3502 return -EINVAL;
3503 }
3504
3505 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3506 void *data;
3507 int len, i;
3508
3509 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3510 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3511 settings->n_akm_suites = len / sizeof(u32);
3512
3513 if (len % sizeof(u32))
3514 return -EINVAL;
3515
3516 memcpy(settings->akm_suites, data, len);
3517
3518 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3519 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3520 return -EINVAL;
3521 }
3522
3523 return 0;
3524}
3525
Jouni Malinen636a5d32009-03-19 13:39:22 +02003526static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3527{
Johannes Berg4c476992010-10-04 21:36:35 +02003528 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3529 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003530 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003531 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003532 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003533 int err, ssid_len, ie_len = 0;
3534 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003535
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003536 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3537 return -EINVAL;
3538
3539 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003540 !info->attrs[NL80211_ATTR_SSID] ||
3541 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003542 return -EINVAL;
3543
Johannes Berg4c476992010-10-04 21:36:35 +02003544 if (!rdev->ops->assoc)
3545 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003546
Johannes Berg074ac8d2010-09-16 14:58:22 +02003547 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003548 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3549 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003550
Johannes Berg19957bb2009-07-02 17:20:43 +02003551 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003552
Johannes Berg19957bb2009-07-02 17:20:43 +02003553 chan = ieee80211_get_channel(&rdev->wiphy,
3554 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003555 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3556 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003557
Johannes Berg19957bb2009-07-02 17:20:43 +02003558 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3559 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003560
3561 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003562 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3563 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003564 }
3565
Jouni Malinendc6382c2009-05-06 22:09:37 +03003566 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003567 enum nl80211_mfp mfp =
Jouni Malinendc6382c2009-05-06 22:09:37 +03003568 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003569 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003570 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02003571 else if (mfp != NL80211_MFP_NO)
3572 return -EINVAL;
Jouni Malinendc6382c2009-05-06 22:09:37 +03003573 }
3574
Johannes Berg3e5d7642009-07-07 14:37:26 +02003575 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3576 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3577
Johannes Bergc0692b82010-08-27 14:26:53 +03003578 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003579 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003580 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3581 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003582 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003583
Jouni Malinen636a5d32009-03-19 13:39:22 +02003584 return err;
3585}
3586
3587static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3588{
Johannes Berg4c476992010-10-04 21:36:35 +02003589 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3590 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003591 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003592 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003593 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003594 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003595
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003596 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3597 return -EINVAL;
3598
3599 if (!info->attrs[NL80211_ATTR_MAC])
3600 return -EINVAL;
3601
3602 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3603 return -EINVAL;
3604
Johannes Berg4c476992010-10-04 21:36:35 +02003605 if (!rdev->ops->deauth)
3606 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003607
Johannes Berg074ac8d2010-09-16 14:58:22 +02003608 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003609 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3610 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003611
Johannes Berg19957bb2009-07-02 17:20:43 +02003612 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003613
Johannes Berg19957bb2009-07-02 17:20:43 +02003614 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3615 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003616 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003617 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003618 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003619
3620 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003621 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3622 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003623 }
3624
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003625 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3626
Johannes Berg4c476992010-10-04 21:36:35 +02003627 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3628 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003629}
3630
3631static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3632{
Johannes Berg4c476992010-10-04 21:36:35 +02003633 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3634 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003635 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003636 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003637 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003638 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003639
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003640 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3641 return -EINVAL;
3642
3643 if (!info->attrs[NL80211_ATTR_MAC])
3644 return -EINVAL;
3645
3646 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3647 return -EINVAL;
3648
Johannes Berg4c476992010-10-04 21:36:35 +02003649 if (!rdev->ops->disassoc)
3650 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003651
Johannes Berg074ac8d2010-09-16 14:58:22 +02003652 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003653 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3654 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003655
Johannes Berg19957bb2009-07-02 17:20:43 +02003656 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003657
Johannes Berg19957bb2009-07-02 17:20:43 +02003658 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3659 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003660 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003661 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003662 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003663
3664 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003665 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3666 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003667 }
3668
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003669 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3670
Johannes Berg4c476992010-10-04 21:36:35 +02003671 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3672 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003673}
3674
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003675static bool
3676nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
3677 int mcast_rate[IEEE80211_NUM_BANDS],
3678 int rateval)
3679{
3680 struct wiphy *wiphy = &rdev->wiphy;
3681 bool found = false;
3682 int band, i;
3683
3684 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3685 struct ieee80211_supported_band *sband;
3686
3687 sband = wiphy->bands[band];
3688 if (!sband)
3689 continue;
3690
3691 for (i = 0; i < sband->n_bitrates; i++) {
3692 if (sband->bitrates[i].bitrate == rateval) {
3693 mcast_rate[band] = i + 1;
3694 found = true;
3695 break;
3696 }
3697 }
3698 }
3699
3700 return found;
3701}
3702
Johannes Berg04a773a2009-04-19 21:24:32 +02003703static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3704{
Johannes Berg4c476992010-10-04 21:36:35 +02003705 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3706 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003707 struct cfg80211_ibss_params ibss;
3708 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003709 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003710 int err;
3711
Johannes Berg8e30bc52009-04-22 17:45:38 +02003712 memset(&ibss, 0, sizeof(ibss));
3713
Johannes Berg04a773a2009-04-19 21:24:32 +02003714 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3715 return -EINVAL;
3716
3717 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3718 !info->attrs[NL80211_ATTR_SSID] ||
3719 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3720 return -EINVAL;
3721
Johannes Berg8e30bc52009-04-22 17:45:38 +02003722 ibss.beacon_interval = 100;
3723
3724 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3725 ibss.beacon_interval =
3726 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3727 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3728 return -EINVAL;
3729 }
3730
Johannes Berg4c476992010-10-04 21:36:35 +02003731 if (!rdev->ops->join_ibss)
3732 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003733
Johannes Berg4c476992010-10-04 21:36:35 +02003734 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3735 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003736
Johannes Berg79c97e92009-07-07 03:56:12 +02003737 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003738
3739 if (info->attrs[NL80211_ATTR_MAC])
3740 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3741 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3742 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3743
3744 if (info->attrs[NL80211_ATTR_IE]) {
3745 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3746 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3747 }
3748
3749 ibss.channel = ieee80211_get_channel(wiphy,
3750 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3751 if (!ibss.channel ||
3752 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02003753 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3754 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003755
3756 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003757 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003758
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003759 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3760 u8 *rates =
3761 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3762 int n_rates =
3763 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3764 struct ieee80211_supported_band *sband =
3765 wiphy->bands[ibss.channel->band];
3766 int i, j;
3767
Johannes Berg4c476992010-10-04 21:36:35 +02003768 if (n_rates == 0)
3769 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003770
3771 for (i = 0; i < n_rates; i++) {
3772 int rate = (rates[i] & 0x7f) * 5;
3773 bool found = false;
3774
3775 for (j = 0; j < sband->n_bitrates; j++) {
3776 if (sband->bitrates[j].bitrate == rate) {
3777 found = true;
3778 ibss.basic_rates |= BIT(j);
3779 break;
3780 }
3781 }
Johannes Berg4c476992010-10-04 21:36:35 +02003782 if (!found)
3783 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003784 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003785 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003786
3787 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
3788 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
3789 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
3790 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003791
Johannes Berg4c476992010-10-04 21:36:35 +02003792 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3793 connkeys = nl80211_parse_connkeys(rdev,
3794 info->attrs[NL80211_ATTR_KEYS]);
3795 if (IS_ERR(connkeys))
3796 return PTR_ERR(connkeys);
3797 }
Johannes Berg04a773a2009-04-19 21:24:32 +02003798
Johannes Berg4c476992010-10-04 21:36:35 +02003799 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003800 if (err)
3801 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003802 return err;
3803}
3804
3805static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3806{
Johannes Berg4c476992010-10-04 21:36:35 +02003807 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3808 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003809
Johannes Berg4c476992010-10-04 21:36:35 +02003810 if (!rdev->ops->leave_ibss)
3811 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003812
Johannes Berg4c476992010-10-04 21:36:35 +02003813 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3814 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003815
Johannes Berg4c476992010-10-04 21:36:35 +02003816 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003817}
3818
Johannes Bergaff89a92009-07-01 21:26:51 +02003819#ifdef CONFIG_NL80211_TESTMODE
3820static struct genl_multicast_group nl80211_testmode_mcgrp = {
3821 .name = "testmode",
3822};
3823
3824static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3825{
Johannes Berg4c476992010-10-04 21:36:35 +02003826 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02003827 int err;
3828
3829 if (!info->attrs[NL80211_ATTR_TESTDATA])
3830 return -EINVAL;
3831
Johannes Bergaff89a92009-07-01 21:26:51 +02003832 err = -EOPNOTSUPP;
3833 if (rdev->ops->testmode_cmd) {
3834 rdev->testmode_info = info;
3835 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3836 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3837 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3838 rdev->testmode_info = NULL;
3839 }
3840
Johannes Bergaff89a92009-07-01 21:26:51 +02003841 return err;
3842}
3843
3844static struct sk_buff *
3845__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3846 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3847{
3848 struct sk_buff *skb;
3849 void *hdr;
3850 struct nlattr *data;
3851
3852 skb = nlmsg_new(approxlen + 100, gfp);
3853 if (!skb)
3854 return NULL;
3855
3856 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3857 if (!hdr) {
3858 kfree_skb(skb);
3859 return NULL;
3860 }
3861
3862 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3863 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3864
3865 ((void **)skb->cb)[0] = rdev;
3866 ((void **)skb->cb)[1] = hdr;
3867 ((void **)skb->cb)[2] = data;
3868
3869 return skb;
3870
3871 nla_put_failure:
3872 kfree_skb(skb);
3873 return NULL;
3874}
3875
3876struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3877 int approxlen)
3878{
3879 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3880
3881 if (WARN_ON(!rdev->testmode_info))
3882 return NULL;
3883
3884 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3885 rdev->testmode_info->snd_pid,
3886 rdev->testmode_info->snd_seq,
3887 GFP_KERNEL);
3888}
3889EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3890
3891int cfg80211_testmode_reply(struct sk_buff *skb)
3892{
3893 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3894 void *hdr = ((void **)skb->cb)[1];
3895 struct nlattr *data = ((void **)skb->cb)[2];
3896
3897 if (WARN_ON(!rdev->testmode_info)) {
3898 kfree_skb(skb);
3899 return -EINVAL;
3900 }
3901
3902 nla_nest_end(skb, data);
3903 genlmsg_end(skb, hdr);
3904 return genlmsg_reply(skb, rdev->testmode_info);
3905}
3906EXPORT_SYMBOL(cfg80211_testmode_reply);
3907
3908struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3909 int approxlen, gfp_t gfp)
3910{
3911 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3912
3913 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3914}
3915EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3916
3917void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3918{
3919 void *hdr = ((void **)skb->cb)[1];
3920 struct nlattr *data = ((void **)skb->cb)[2];
3921
3922 nla_nest_end(skb, data);
3923 genlmsg_end(skb, hdr);
3924 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3925}
3926EXPORT_SYMBOL(cfg80211_testmode_event);
3927#endif
3928
Samuel Ortizb23aa672009-07-01 21:26:54 +02003929static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3930{
Johannes Berg4c476992010-10-04 21:36:35 +02003931 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3932 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003933 struct cfg80211_connect_params connect;
3934 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003935 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003936 int err;
3937
3938 memset(&connect, 0, sizeof(connect));
3939
3940 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3941 return -EINVAL;
3942
3943 if (!info->attrs[NL80211_ATTR_SSID] ||
3944 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3945 return -EINVAL;
3946
3947 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3948 connect.auth_type =
3949 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3950 if (!nl80211_valid_auth_type(connect.auth_type))
3951 return -EINVAL;
3952 } else
3953 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3954
3955 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3956
Johannes Bergc0692b82010-08-27 14:26:53 +03003957 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003958 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003959 if (err)
3960 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003961
Johannes Berg074ac8d2010-09-16 14:58:22 +02003962 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003963 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3964 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003965
Johannes Berg79c97e92009-07-07 03:56:12 +02003966 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003967
Samuel Ortizb23aa672009-07-01 21:26:54 +02003968 if (info->attrs[NL80211_ATTR_MAC])
3969 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3970 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3971 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3972
3973 if (info->attrs[NL80211_ATTR_IE]) {
3974 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3975 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3976 }
3977
3978 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3979 connect.channel =
3980 ieee80211_get_channel(wiphy,
3981 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3982 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02003983 connect.channel->flags & IEEE80211_CHAN_DISABLED)
3984 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003985 }
3986
Johannes Bergfffd0932009-07-08 14:22:54 +02003987 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3988 connkeys = nl80211_parse_connkeys(rdev,
3989 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02003990 if (IS_ERR(connkeys))
3991 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003992 }
3993
3994 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003995 if (err)
3996 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003997 return err;
3998}
3999
4000static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4001{
Johannes Berg4c476992010-10-04 21:36:35 +02004002 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4003 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004004 u16 reason;
4005
4006 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4007 reason = WLAN_REASON_DEAUTH_LEAVING;
4008 else
4009 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4010
4011 if (reason == 0)
4012 return -EINVAL;
4013
Johannes Berg074ac8d2010-09-16 14:58:22 +02004014 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004015 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4016 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004017
Johannes Berg4c476992010-10-04 21:36:35 +02004018 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004019}
4020
Johannes Berg463d0182009-07-14 00:33:35 +02004021static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4022{
Johannes Berg4c476992010-10-04 21:36:35 +02004023 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004024 struct net *net;
4025 int err;
4026 u32 pid;
4027
4028 if (!info->attrs[NL80211_ATTR_PID])
4029 return -EINVAL;
4030
4031 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4032
Johannes Berg463d0182009-07-14 00:33:35 +02004033 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004034 if (IS_ERR(net))
4035 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004036
4037 err = 0;
4038
4039 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004040 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4041 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004042
Johannes Berg463d0182009-07-14 00:33:35 +02004043 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004044 return err;
4045}
4046
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004047static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4048{
Johannes Berg4c476992010-10-04 21:36:35 +02004049 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004050 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4051 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004052 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004053 struct cfg80211_pmksa pmksa;
4054
4055 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4056
4057 if (!info->attrs[NL80211_ATTR_MAC])
4058 return -EINVAL;
4059
4060 if (!info->attrs[NL80211_ATTR_PMKID])
4061 return -EINVAL;
4062
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004063 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4064 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4065
Johannes Berg074ac8d2010-09-16 14:58:22 +02004066 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004067 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4068 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004069
4070 switch (info->genlhdr->cmd) {
4071 case NL80211_CMD_SET_PMKSA:
4072 rdev_ops = rdev->ops->set_pmksa;
4073 break;
4074 case NL80211_CMD_DEL_PMKSA:
4075 rdev_ops = rdev->ops->del_pmksa;
4076 break;
4077 default:
4078 WARN_ON(1);
4079 break;
4080 }
4081
Johannes Berg4c476992010-10-04 21:36:35 +02004082 if (!rdev_ops)
4083 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004084
Johannes Berg4c476992010-10-04 21:36:35 +02004085 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004086}
4087
4088static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4089{
Johannes Berg4c476992010-10-04 21:36:35 +02004090 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4091 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004092
Johannes Berg074ac8d2010-09-16 14:58:22 +02004093 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004094 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4095 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004096
Johannes Berg4c476992010-10-04 21:36:35 +02004097 if (!rdev->ops->flush_pmksa)
4098 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004099
Johannes Berg4c476992010-10-04 21:36:35 +02004100 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004101}
4102
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004103static int nl80211_remain_on_channel(struct sk_buff *skb,
4104 struct genl_info *info)
4105{
Johannes Berg4c476992010-10-04 21:36:35 +02004106 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4107 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004108 struct ieee80211_channel *chan;
4109 struct sk_buff *msg;
4110 void *hdr;
4111 u64 cookie;
4112 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4113 u32 freq, duration;
4114 int err;
4115
4116 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4117 !info->attrs[NL80211_ATTR_DURATION])
4118 return -EINVAL;
4119
4120 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4121
4122 /*
4123 * We should be on that channel for at least one jiffie,
4124 * and more than 5 seconds seems excessive.
4125 */
4126 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4127 return -EINVAL;
4128
Johannes Berg4c476992010-10-04 21:36:35 +02004129 if (!rdev->ops->remain_on_channel)
4130 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004131
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004132 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4133 channel_type = nla_get_u32(
4134 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4135 if (channel_type != NL80211_CHAN_NO_HT &&
4136 channel_type != NL80211_CHAN_HT20 &&
4137 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004138 channel_type != NL80211_CHAN_HT40MINUS)
4139 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004140 }
4141
4142 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4143 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004144 if (chan == NULL)
4145 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004146
4147 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004148 if (!msg)
4149 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004150
4151 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4152 NL80211_CMD_REMAIN_ON_CHANNEL);
4153
4154 if (IS_ERR(hdr)) {
4155 err = PTR_ERR(hdr);
4156 goto free_msg;
4157 }
4158
4159 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4160 channel_type, duration, &cookie);
4161
4162 if (err)
4163 goto free_msg;
4164
4165 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4166
4167 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004168
4169 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004170
4171 nla_put_failure:
4172 err = -ENOBUFS;
4173 free_msg:
4174 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004175 return err;
4176}
4177
4178static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4179 struct genl_info *info)
4180{
Johannes Berg4c476992010-10-04 21:36:35 +02004181 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4182 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004183 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004184
4185 if (!info->attrs[NL80211_ATTR_COOKIE])
4186 return -EINVAL;
4187
Johannes Berg4c476992010-10-04 21:36:35 +02004188 if (!rdev->ops->cancel_remain_on_channel)
4189 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004190
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004191 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4192
Johannes Berg4c476992010-10-04 21:36:35 +02004193 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004194}
4195
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004196static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4197 u8 *rates, u8 rates_len)
4198{
4199 u8 i;
4200 u32 mask = 0;
4201
4202 for (i = 0; i < rates_len; i++) {
4203 int rate = (rates[i] & 0x7f) * 5;
4204 int ridx;
4205 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4206 struct ieee80211_rate *srate =
4207 &sband->bitrates[ridx];
4208 if (rate == srate->bitrate) {
4209 mask |= 1 << ridx;
4210 break;
4211 }
4212 }
4213 if (ridx == sband->n_bitrates)
4214 return 0; /* rate not found */
4215 }
4216
4217 return mask;
4218}
4219
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004220static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004221 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4222 .len = NL80211_MAX_SUPP_RATES },
4223};
4224
4225static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4226 struct genl_info *info)
4227{
4228 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004229 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004230 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004231 int rem, i;
4232 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004233 struct nlattr *tx_rates;
4234 struct ieee80211_supported_band *sband;
4235
4236 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4237 return -EINVAL;
4238
Johannes Berg4c476992010-10-04 21:36:35 +02004239 if (!rdev->ops->set_bitrate_mask)
4240 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004241
4242 memset(&mask, 0, sizeof(mask));
4243 /* Default to all rates enabled */
4244 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4245 sband = rdev->wiphy.bands[i];
4246 mask.control[i].legacy =
4247 sband ? (1 << sband->n_bitrates) - 1 : 0;
4248 }
4249
4250 /*
4251 * The nested attribute uses enum nl80211_band as the index. This maps
4252 * directly to the enum ieee80211_band values used in cfg80211.
4253 */
4254 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4255 {
4256 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004257 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4258 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004259 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004260 if (sband == NULL)
4261 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004262 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4263 nla_len(tx_rates), nl80211_txattr_policy);
4264 if (tb[NL80211_TXRATE_LEGACY]) {
4265 mask.control[band].legacy = rateset_to_mask(
4266 sband,
4267 nla_data(tb[NL80211_TXRATE_LEGACY]),
4268 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004269 if (mask.control[band].legacy == 0)
4270 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004271 }
4272 }
4273
Johannes Berg4c476992010-10-04 21:36:35 +02004274 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004275}
4276
Johannes Berg2e161f72010-08-12 15:38:38 +02004277static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004278{
Johannes Berg4c476992010-10-04 21:36:35 +02004279 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4280 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004281 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004282
4283 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4284 return -EINVAL;
4285
Johannes Berg2e161f72010-08-12 15:38:38 +02004286 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4287 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004288
Johannes Berg9d38d852010-06-09 17:20:33 +02004289 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004290 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004291 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4292 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4293 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004294 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4295 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004296
4297 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004298 if (!rdev->ops->mgmt_tx)
4299 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004300
Johannes Berg4c476992010-10-04 21:36:35 +02004301 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004302 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004303 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4304 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004305}
4306
Johannes Berg2e161f72010-08-12 15:38:38 +02004307static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004308{
Johannes Berg4c476992010-10-04 21:36:35 +02004309 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4310 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004311 struct ieee80211_channel *chan;
4312 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004313 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004314 u32 freq;
4315 int err;
4316 void *hdr;
4317 u64 cookie;
4318 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004319 unsigned int wait = 0;
4320 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02004321
4322 if (!info->attrs[NL80211_ATTR_FRAME] ||
4323 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4324 return -EINVAL;
4325
Johannes Berg4c476992010-10-04 21:36:35 +02004326 if (!rdev->ops->mgmt_tx)
4327 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004328
Johannes Berg9d38d852010-06-09 17:20:33 +02004329 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004330 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004331 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4332 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4333 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004334 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4335 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004336
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004337 if (info->attrs[NL80211_ATTR_DURATION]) {
4338 if (!rdev->ops->mgmt_tx_cancel_wait)
4339 return -EINVAL;
4340 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4341 }
4342
Jouni Malinen026331c2010-02-15 12:53:10 +02004343 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4344 channel_type = nla_get_u32(
4345 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4346 if (channel_type != NL80211_CHAN_NO_HT &&
4347 channel_type != NL80211_CHAN_HT20 &&
4348 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004349 channel_type != NL80211_CHAN_HT40MINUS)
4350 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004351 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004352 }
4353
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004354 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
4355
Jouni Malinen026331c2010-02-15 12:53:10 +02004356 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4357 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004358 if (chan == NULL)
4359 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004360
4361 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004362 if (!msg)
4363 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004364
4365 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004366 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004367
4368 if (IS_ERR(hdr)) {
4369 err = PTR_ERR(hdr);
4370 goto free_msg;
4371 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004372 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
4373 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02004374 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4375 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4376 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004377 if (err)
4378 goto free_msg;
4379
4380 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4381
4382 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004383 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004384
4385 nla_put_failure:
4386 err = -ENOBUFS;
4387 free_msg:
4388 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004389 return err;
4390}
4391
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004392static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
4393{
4394 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4395 struct net_device *dev = info->user_ptr[1];
4396 u64 cookie;
4397
4398 if (!info->attrs[NL80211_ATTR_COOKIE])
4399 return -EINVAL;
4400
4401 if (!rdev->ops->mgmt_tx_cancel_wait)
4402 return -EOPNOTSUPP;
4403
4404 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4405 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4406 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4407 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4408 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4409 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4410 return -EOPNOTSUPP;
4411
4412 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4413
4414 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
4415}
4416
Kalle Valoffb9eb32010-02-17 17:58:10 +02004417static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4418{
Johannes Berg4c476992010-10-04 21:36:35 +02004419 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004420 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004421 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004422 u8 ps_state;
4423 bool state;
4424 int err;
4425
Johannes Berg4c476992010-10-04 21:36:35 +02004426 if (!info->attrs[NL80211_ATTR_PS_STATE])
4427 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004428
4429 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4430
Johannes Berg4c476992010-10-04 21:36:35 +02004431 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4432 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004433
4434 wdev = dev->ieee80211_ptr;
4435
Johannes Berg4c476992010-10-04 21:36:35 +02004436 if (!rdev->ops->set_power_mgmt)
4437 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004438
4439 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4440
4441 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004442 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004443
Johannes Berg4c476992010-10-04 21:36:35 +02004444 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4445 wdev->ps_timeout);
4446 if (!err)
4447 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004448 return err;
4449}
4450
4451static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4452{
Johannes Berg4c476992010-10-04 21:36:35 +02004453 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004454 enum nl80211_ps_state ps_state;
4455 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004456 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004457 struct sk_buff *msg;
4458 void *hdr;
4459 int err;
4460
Kalle Valoffb9eb32010-02-17 17:58:10 +02004461 wdev = dev->ieee80211_ptr;
4462
Johannes Berg4c476992010-10-04 21:36:35 +02004463 if (!rdev->ops->set_power_mgmt)
4464 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004465
4466 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004467 if (!msg)
4468 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004469
4470 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4471 NL80211_CMD_GET_POWER_SAVE);
4472 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004473 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004474 goto free_msg;
4475 }
4476
4477 if (wdev->ps)
4478 ps_state = NL80211_PS_ENABLED;
4479 else
4480 ps_state = NL80211_PS_DISABLED;
4481
4482 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4483
4484 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004485 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004486
Johannes Berg4c476992010-10-04 21:36:35 +02004487 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004488 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004489 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004490 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004491 return err;
4492}
4493
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004494static struct nla_policy
4495nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4496 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4497 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4498 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4499};
4500
4501static int nl80211_set_cqm_rssi(struct genl_info *info,
4502 s32 threshold, u32 hysteresis)
4503{
Johannes Berg4c476992010-10-04 21:36:35 +02004504 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004505 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004506 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004507
4508 if (threshold > 0)
4509 return -EINVAL;
4510
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004511 wdev = dev->ieee80211_ptr;
4512
Johannes Berg4c476992010-10-04 21:36:35 +02004513 if (!rdev->ops->set_cqm_rssi_config)
4514 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004515
Johannes Berg074ac8d2010-09-16 14:58:22 +02004516 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004517 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4518 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004519
Johannes Berg4c476992010-10-04 21:36:35 +02004520 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4521 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004522}
4523
4524static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4525{
4526 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4527 struct nlattr *cqm;
4528 int err;
4529
4530 cqm = info->attrs[NL80211_ATTR_CQM];
4531 if (!cqm) {
4532 err = -EINVAL;
4533 goto out;
4534 }
4535
4536 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4537 nl80211_attr_cqm_policy);
4538 if (err)
4539 goto out;
4540
4541 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4542 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4543 s32 threshold;
4544 u32 hysteresis;
4545 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4546 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4547 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4548 } else
4549 err = -EINVAL;
4550
4551out:
4552 return err;
4553}
4554
Johannes Berg29cbe682010-12-03 09:20:44 +01004555static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
4556{
4557 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4558 struct net_device *dev = info->user_ptr[1];
4559 struct mesh_config cfg;
4560 int err;
4561
4562 /* start with default */
4563 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
4564
4565 if (info->attrs[NL80211_ATTR_MESH_PARAMS]) {
4566 /* and parse parameters if given */
4567 err = nl80211_parse_mesh_params(info, &cfg, NULL);
4568 if (err)
4569 return err;
4570 }
4571
4572 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
4573 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
4574 return -EINVAL;
4575
4576 return cfg80211_join_mesh(rdev, dev,
4577 nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4578 nla_len(info->attrs[NL80211_ATTR_MESH_ID]),
4579 &cfg);
4580}
4581
4582static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
4583{
4584 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4585 struct net_device *dev = info->user_ptr[1];
4586
4587 return cfg80211_leave_mesh(rdev, dev);
4588}
4589
Johannes Berg4c476992010-10-04 21:36:35 +02004590#define NL80211_FLAG_NEED_WIPHY 0x01
4591#define NL80211_FLAG_NEED_NETDEV 0x02
4592#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02004593#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
4594#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
4595 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02004596
4597static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4598 struct genl_info *info)
4599{
4600 struct cfg80211_registered_device *rdev;
4601 struct net_device *dev;
4602 int err;
4603 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4604
4605 if (rtnl)
4606 rtnl_lock();
4607
4608 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4609 rdev = cfg80211_get_dev_from_info(info);
4610 if (IS_ERR(rdev)) {
4611 if (rtnl)
4612 rtnl_unlock();
4613 return PTR_ERR(rdev);
4614 }
4615 info->user_ptr[0] = rdev;
4616 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4617 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4618 if (err) {
4619 if (rtnl)
4620 rtnl_unlock();
4621 return err;
4622 }
Johannes Berg41265712010-10-04 21:14:05 +02004623 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4624 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02004625 cfg80211_unlock_rdev(rdev);
4626 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02004627 if (rtnl)
4628 rtnl_unlock();
4629 return -ENETDOWN;
4630 }
Johannes Berg4c476992010-10-04 21:36:35 +02004631 info->user_ptr[0] = rdev;
4632 info->user_ptr[1] = dev;
4633 }
4634
4635 return 0;
4636}
4637
4638static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4639 struct genl_info *info)
4640{
4641 if (info->user_ptr[0])
4642 cfg80211_unlock_rdev(info->user_ptr[0]);
4643 if (info->user_ptr[1])
4644 dev_put(info->user_ptr[1]);
4645 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4646 rtnl_unlock();
4647}
4648
Johannes Berg55682962007-09-20 13:09:35 -04004649static struct genl_ops nl80211_ops[] = {
4650 {
4651 .cmd = NL80211_CMD_GET_WIPHY,
4652 .doit = nl80211_get_wiphy,
4653 .dumpit = nl80211_dump_wiphy,
4654 .policy = nl80211_policy,
4655 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004656 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04004657 },
4658 {
4659 .cmd = NL80211_CMD_SET_WIPHY,
4660 .doit = nl80211_set_wiphy,
4661 .policy = nl80211_policy,
4662 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004663 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004664 },
4665 {
4666 .cmd = NL80211_CMD_GET_INTERFACE,
4667 .doit = nl80211_get_interface,
4668 .dumpit = nl80211_dump_interface,
4669 .policy = nl80211_policy,
4670 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004671 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04004672 },
4673 {
4674 .cmd = NL80211_CMD_SET_INTERFACE,
4675 .doit = nl80211_set_interface,
4676 .policy = nl80211_policy,
4677 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004678 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4679 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004680 },
4681 {
4682 .cmd = NL80211_CMD_NEW_INTERFACE,
4683 .doit = nl80211_new_interface,
4684 .policy = nl80211_policy,
4685 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004686 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4687 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004688 },
4689 {
4690 .cmd = NL80211_CMD_DEL_INTERFACE,
4691 .doit = nl80211_del_interface,
4692 .policy = nl80211_policy,
4693 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004694 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4695 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004696 },
Johannes Berg41ade002007-12-19 02:03:29 +01004697 {
4698 .cmd = NL80211_CMD_GET_KEY,
4699 .doit = nl80211_get_key,
4700 .policy = nl80211_policy,
4701 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004702 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4703 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004704 },
4705 {
4706 .cmd = NL80211_CMD_SET_KEY,
4707 .doit = nl80211_set_key,
4708 .policy = nl80211_policy,
4709 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004710 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004711 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004712 },
4713 {
4714 .cmd = NL80211_CMD_NEW_KEY,
4715 .doit = nl80211_new_key,
4716 .policy = nl80211_policy,
4717 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004718 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004719 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004720 },
4721 {
4722 .cmd = NL80211_CMD_DEL_KEY,
4723 .doit = nl80211_del_key,
4724 .policy = nl80211_policy,
4725 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004726 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004727 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004728 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004729 {
4730 .cmd = NL80211_CMD_SET_BEACON,
4731 .policy = nl80211_policy,
4732 .flags = GENL_ADMIN_PERM,
4733 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004734 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4735 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004736 },
4737 {
4738 .cmd = NL80211_CMD_NEW_BEACON,
4739 .policy = nl80211_policy,
4740 .flags = GENL_ADMIN_PERM,
4741 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004742 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4743 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004744 },
4745 {
4746 .cmd = NL80211_CMD_DEL_BEACON,
4747 .policy = nl80211_policy,
4748 .flags = GENL_ADMIN_PERM,
4749 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004750 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4751 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004752 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004753 {
4754 .cmd = NL80211_CMD_GET_STATION,
4755 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004756 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004757 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02004758 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4759 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004760 },
4761 {
4762 .cmd = NL80211_CMD_SET_STATION,
4763 .doit = nl80211_set_station,
4764 .policy = nl80211_policy,
4765 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004766 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4767 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004768 },
4769 {
4770 .cmd = NL80211_CMD_NEW_STATION,
4771 .doit = nl80211_new_station,
4772 .policy = nl80211_policy,
4773 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004774 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004775 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004776 },
4777 {
4778 .cmd = NL80211_CMD_DEL_STATION,
4779 .doit = nl80211_del_station,
4780 .policy = nl80211_policy,
4781 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004782 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4783 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004784 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004785 {
4786 .cmd = NL80211_CMD_GET_MPATH,
4787 .doit = nl80211_get_mpath,
4788 .dumpit = nl80211_dump_mpath,
4789 .policy = nl80211_policy,
4790 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004791 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004792 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004793 },
4794 {
4795 .cmd = NL80211_CMD_SET_MPATH,
4796 .doit = nl80211_set_mpath,
4797 .policy = nl80211_policy,
4798 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004799 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004800 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004801 },
4802 {
4803 .cmd = NL80211_CMD_NEW_MPATH,
4804 .doit = nl80211_new_mpath,
4805 .policy = nl80211_policy,
4806 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004807 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004808 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004809 },
4810 {
4811 .cmd = NL80211_CMD_DEL_MPATH,
4812 .doit = nl80211_del_mpath,
4813 .policy = nl80211_policy,
4814 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004815 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4816 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004817 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004818 {
4819 .cmd = NL80211_CMD_SET_BSS,
4820 .doit = nl80211_set_bss,
4821 .policy = nl80211_policy,
4822 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004823 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4824 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004825 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004826 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004827 .cmd = NL80211_CMD_GET_REG,
4828 .doit = nl80211_get_reg,
4829 .policy = nl80211_policy,
4830 /* can be retrieved by unprivileged users */
4831 },
4832 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004833 .cmd = NL80211_CMD_SET_REG,
4834 .doit = nl80211_set_reg,
4835 .policy = nl80211_policy,
4836 .flags = GENL_ADMIN_PERM,
4837 },
4838 {
4839 .cmd = NL80211_CMD_REQ_SET_REG,
4840 .doit = nl80211_req_set_reg,
4841 .policy = nl80211_policy,
4842 .flags = GENL_ADMIN_PERM,
4843 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004844 {
4845 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4846 .doit = nl80211_get_mesh_params,
4847 .policy = nl80211_policy,
4848 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004849 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4850 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004851 },
4852 {
4853 .cmd = NL80211_CMD_SET_MESH_PARAMS,
Johannes Berg29cbe682010-12-03 09:20:44 +01004854 .doit = nl80211_update_mesh_params,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004855 .policy = nl80211_policy,
4856 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01004857 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004858 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004859 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004860 {
Johannes Berg2a519312009-02-10 21:25:55 +01004861 .cmd = NL80211_CMD_TRIGGER_SCAN,
4862 .doit = nl80211_trigger_scan,
4863 .policy = nl80211_policy,
4864 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004865 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004866 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01004867 },
4868 {
4869 .cmd = NL80211_CMD_GET_SCAN,
4870 .policy = nl80211_policy,
4871 .dumpit = nl80211_dump_scan,
4872 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004873 {
4874 .cmd = NL80211_CMD_AUTHENTICATE,
4875 .doit = nl80211_authenticate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004880 },
4881 {
4882 .cmd = NL80211_CMD_ASSOCIATE,
4883 .doit = nl80211_associate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004888 },
4889 {
4890 .cmd = NL80211_CMD_DEAUTHENTICATE,
4891 .doit = nl80211_deauthenticate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004896 },
4897 {
4898 .cmd = NL80211_CMD_DISASSOCIATE,
4899 .doit = nl80211_disassociate,
4900 .policy = nl80211_policy,
4901 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004902 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004903 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004904 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004905 {
4906 .cmd = NL80211_CMD_JOIN_IBSS,
4907 .doit = nl80211_join_ibss,
4908 .policy = nl80211_policy,
4909 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004910 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004911 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004912 },
4913 {
4914 .cmd = NL80211_CMD_LEAVE_IBSS,
4915 .doit = nl80211_leave_ibss,
4916 .policy = nl80211_policy,
4917 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004918 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004919 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004920 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004921#ifdef CONFIG_NL80211_TESTMODE
4922 {
4923 .cmd = NL80211_CMD_TESTMODE,
4924 .doit = nl80211_testmode_do,
4925 .policy = nl80211_policy,
4926 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004927 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4928 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02004929 },
4930#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004931 {
4932 .cmd = NL80211_CMD_CONNECT,
4933 .doit = nl80211_connect,
4934 .policy = nl80211_policy,
4935 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004936 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004937 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004938 },
4939 {
4940 .cmd = NL80211_CMD_DISCONNECT,
4941 .doit = nl80211_disconnect,
4942 .policy = nl80211_policy,
4943 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004944 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004945 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004946 },
Johannes Berg463d0182009-07-14 00:33:35 +02004947 {
4948 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4949 .doit = nl80211_wiphy_netns,
4950 .policy = nl80211_policy,
4951 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004952 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4953 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02004954 },
Holger Schurig61fa7132009-11-11 12:25:40 +01004955 {
4956 .cmd = NL80211_CMD_GET_SURVEY,
4957 .policy = nl80211_policy,
4958 .dumpit = nl80211_dump_survey,
4959 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004960 {
4961 .cmd = NL80211_CMD_SET_PMKSA,
4962 .doit = nl80211_setdel_pmksa,
4963 .policy = nl80211_policy,
4964 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004965 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4966 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004967 },
4968 {
4969 .cmd = NL80211_CMD_DEL_PMKSA,
4970 .doit = nl80211_setdel_pmksa,
4971 .policy = nl80211_policy,
4972 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004973 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4974 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004975 },
4976 {
4977 .cmd = NL80211_CMD_FLUSH_PMKSA,
4978 .doit = nl80211_flush_pmksa,
4979 .policy = nl80211_policy,
4980 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004981 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4982 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004983 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004984 {
4985 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
4986 .doit = nl80211_remain_on_channel,
4987 .policy = nl80211_policy,
4988 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004989 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004990 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004991 },
4992 {
4993 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
4994 .doit = nl80211_cancel_remain_on_channel,
4995 .policy = nl80211_policy,
4996 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004997 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004998 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004999 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005000 {
5001 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5002 .doit = nl80211_set_tx_bitrate_mask,
5003 .policy = nl80211_policy,
5004 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005005 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5006 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005007 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005008 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005009 .cmd = NL80211_CMD_REGISTER_FRAME,
5010 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005011 .policy = nl80211_policy,
5012 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005013 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5014 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005015 },
5016 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005017 .cmd = NL80211_CMD_FRAME,
5018 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005019 .policy = nl80211_policy,
5020 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005021 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005022 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005023 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005024 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005025 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
5026 .doit = nl80211_tx_mgmt_cancel_wait,
5027 .policy = nl80211_policy,
5028 .flags = GENL_ADMIN_PERM,
5029 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5030 NL80211_FLAG_NEED_RTNL,
5031 },
5032 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02005033 .cmd = NL80211_CMD_SET_POWER_SAVE,
5034 .doit = nl80211_set_power_save,
5035 .policy = nl80211_policy,
5036 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005037 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5038 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005039 },
5040 {
5041 .cmd = NL80211_CMD_GET_POWER_SAVE,
5042 .doit = nl80211_get_power_save,
5043 .policy = nl80211_policy,
5044 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005045 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5046 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005047 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005048 {
5049 .cmd = NL80211_CMD_SET_CQM,
5050 .doit = nl80211_set_cqm,
5051 .policy = nl80211_policy,
5052 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005053 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5054 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005055 },
Johannes Bergf444de02010-05-05 15:25:02 +02005056 {
5057 .cmd = NL80211_CMD_SET_CHANNEL,
5058 .doit = nl80211_set_channel,
5059 .policy = nl80211_policy,
5060 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005061 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5062 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02005063 },
Bill Jordane8347eb2010-10-01 13:54:28 -04005064 {
5065 .cmd = NL80211_CMD_SET_WDS_PEER,
5066 .doit = nl80211_set_wds_peer,
5067 .policy = nl80211_policy,
5068 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02005069 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5070 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04005071 },
Johannes Berg29cbe682010-12-03 09:20:44 +01005072 {
5073 .cmd = NL80211_CMD_JOIN_MESH,
5074 .doit = nl80211_join_mesh,
5075 .policy = nl80211_policy,
5076 .flags = GENL_ADMIN_PERM,
5077 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5078 NL80211_FLAG_NEED_RTNL,
5079 },
5080 {
5081 .cmd = NL80211_CMD_LEAVE_MESH,
5082 .doit = nl80211_leave_mesh,
5083 .policy = nl80211_policy,
5084 .flags = GENL_ADMIN_PERM,
5085 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5086 NL80211_FLAG_NEED_RTNL,
5087 },
Johannes Berg55682962007-09-20 13:09:35 -04005088};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005089
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005090static struct genl_multicast_group nl80211_mlme_mcgrp = {
5091 .name = "mlme",
5092};
Johannes Berg55682962007-09-20 13:09:35 -04005093
5094/* multicast groups */
5095static struct genl_multicast_group nl80211_config_mcgrp = {
5096 .name = "config",
5097};
Johannes Berg2a519312009-02-10 21:25:55 +01005098static struct genl_multicast_group nl80211_scan_mcgrp = {
5099 .name = "scan",
5100};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005101static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5102 .name = "regulatory",
5103};
Johannes Berg55682962007-09-20 13:09:35 -04005104
5105/* notification functions */
5106
5107void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5108{
5109 struct sk_buff *msg;
5110
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005111 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005112 if (!msg)
5113 return;
5114
5115 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5116 nlmsg_free(msg);
5117 return;
5118 }
5119
Johannes Berg463d0182009-07-14 00:33:35 +02005120 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5121 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005122}
5123
Johannes Berg362a4152009-05-24 16:43:15 +02005124static int nl80211_add_scan_req(struct sk_buff *msg,
5125 struct cfg80211_registered_device *rdev)
5126{
5127 struct cfg80211_scan_request *req = rdev->scan_req;
5128 struct nlattr *nest;
5129 int i;
5130
Johannes Berg667503dd2009-07-07 03:56:11 +02005131 ASSERT_RDEV_LOCK(rdev);
5132
Johannes Berg362a4152009-05-24 16:43:15 +02005133 if (WARN_ON(!req))
5134 return 0;
5135
5136 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5137 if (!nest)
5138 goto nla_put_failure;
5139 for (i = 0; i < req->n_ssids; i++)
5140 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5141 nla_nest_end(msg, nest);
5142
5143 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5144 if (!nest)
5145 goto nla_put_failure;
5146 for (i = 0; i < req->n_channels; i++)
5147 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5148 nla_nest_end(msg, nest);
5149
5150 if (req->ie)
5151 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5152
5153 return 0;
5154 nla_put_failure:
5155 return -ENOBUFS;
5156}
5157
Johannes Berga538e2d2009-06-16 19:56:42 +02005158static int nl80211_send_scan_msg(struct sk_buff *msg,
5159 struct cfg80211_registered_device *rdev,
5160 struct net_device *netdev,
5161 u32 pid, u32 seq, int flags,
5162 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005163{
5164 void *hdr;
5165
5166 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5167 if (!hdr)
5168 return -1;
5169
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005170 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005171 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5172
Johannes Berg362a4152009-05-24 16:43:15 +02005173 /* ignore errors and send incomplete event anyway */
5174 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005175
5176 return genlmsg_end(msg, hdr);
5177
5178 nla_put_failure:
5179 genlmsg_cancel(msg, hdr);
5180 return -EMSGSIZE;
5181}
5182
Johannes Berga538e2d2009-06-16 19:56:42 +02005183void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5184 struct net_device *netdev)
5185{
5186 struct sk_buff *msg;
5187
5188 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5189 if (!msg)
5190 return;
5191
5192 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5193 NL80211_CMD_TRIGGER_SCAN) < 0) {
5194 nlmsg_free(msg);
5195 return;
5196 }
5197
Johannes Berg463d0182009-07-14 00:33:35 +02005198 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5199 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005200}
5201
Johannes Berg2a519312009-02-10 21:25:55 +01005202void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5203 struct net_device *netdev)
5204{
5205 struct sk_buff *msg;
5206
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005207 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005208 if (!msg)
5209 return;
5210
Johannes Berga538e2d2009-06-16 19:56:42 +02005211 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5212 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005213 nlmsg_free(msg);
5214 return;
5215 }
5216
Johannes Berg463d0182009-07-14 00:33:35 +02005217 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5218 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005219}
5220
5221void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5222 struct net_device *netdev)
5223{
5224 struct sk_buff *msg;
5225
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005226 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005227 if (!msg)
5228 return;
5229
Johannes Berga538e2d2009-06-16 19:56:42 +02005230 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5231 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005232 nlmsg_free(msg);
5233 return;
5234 }
5235
Johannes Berg463d0182009-07-14 00:33:35 +02005236 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5237 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005238}
5239
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005240/*
5241 * This can happen on global regulatory changes or device specific settings
5242 * based on custom world regulatory domains.
5243 */
5244void nl80211_send_reg_change_event(struct regulatory_request *request)
5245{
5246 struct sk_buff *msg;
5247 void *hdr;
5248
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005250 if (!msg)
5251 return;
5252
5253 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5254 if (!hdr) {
5255 nlmsg_free(msg);
5256 return;
5257 }
5258
5259 /* Userspace can always count this one always being set */
5260 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5261
5262 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5263 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5264 NL80211_REGDOM_TYPE_WORLD);
5265 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5266 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5267 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5268 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5269 request->intersect)
5270 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5271 NL80211_REGDOM_TYPE_INTERSECTION);
5272 else {
5273 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5274 NL80211_REGDOM_TYPE_COUNTRY);
5275 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5276 }
5277
5278 if (wiphy_idx_valid(request->wiphy_idx))
5279 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5280
5281 if (genlmsg_end(msg, hdr) < 0) {
5282 nlmsg_free(msg);
5283 return;
5284 }
5285
Johannes Bergbc43b282009-07-25 10:54:13 +02005286 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005287 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005288 GFP_ATOMIC);
5289 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005290
5291 return;
5292
5293nla_put_failure:
5294 genlmsg_cancel(msg, hdr);
5295 nlmsg_free(msg);
5296}
5297
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005298static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5299 struct net_device *netdev,
5300 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005301 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005302{
5303 struct sk_buff *msg;
5304 void *hdr;
5305
Johannes Berge6d6e342009-07-01 21:26:47 +02005306 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005307 if (!msg)
5308 return;
5309
5310 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5311 if (!hdr) {
5312 nlmsg_free(msg);
5313 return;
5314 }
5315
5316 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5317 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5318 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5319
5320 if (genlmsg_end(msg, hdr) < 0) {
5321 nlmsg_free(msg);
5322 return;
5323 }
5324
Johannes Berg463d0182009-07-14 00:33:35 +02005325 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5326 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005327 return;
5328
5329 nla_put_failure:
5330 genlmsg_cancel(msg, hdr);
5331 nlmsg_free(msg);
5332}
5333
5334void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005335 struct net_device *netdev, const u8 *buf,
5336 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005337{
5338 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005339 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005340}
5341
5342void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
5343 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005344 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005345{
Johannes Berge6d6e342009-07-01 21:26:47 +02005346 nl80211_send_mlme_event(rdev, netdev, buf, len,
5347 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005348}
5349
Jouni Malinen53b46b82009-03-27 20:53:56 +02005350void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005351 struct net_device *netdev, const u8 *buf,
5352 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005353{
5354 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005355 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005356}
5357
Jouni Malinen53b46b82009-03-27 20:53:56 +02005358void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5359 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005360 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005361{
5362 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005363 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005364}
5365
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005366static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5367 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005368 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005369{
5370 struct sk_buff *msg;
5371 void *hdr;
5372
Johannes Berge6d6e342009-07-01 21:26:47 +02005373 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005374 if (!msg)
5375 return;
5376
5377 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5378 if (!hdr) {
5379 nlmsg_free(msg);
5380 return;
5381 }
5382
5383 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5384 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5385 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5386 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5387
5388 if (genlmsg_end(msg, hdr) < 0) {
5389 nlmsg_free(msg);
5390 return;
5391 }
5392
Johannes Berg463d0182009-07-14 00:33:35 +02005393 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5394 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005395 return;
5396
5397 nla_put_failure:
5398 genlmsg_cancel(msg, hdr);
5399 nlmsg_free(msg);
5400}
5401
5402void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005403 struct net_device *netdev, const u8 *addr,
5404 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005405{
5406 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005407 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005408}
5409
5410void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005411 struct net_device *netdev, const u8 *addr,
5412 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005413{
Johannes Berge6d6e342009-07-01 21:26:47 +02005414 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5415 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005416}
5417
Samuel Ortizb23aa672009-07-01 21:26:54 +02005418void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5419 struct net_device *netdev, const u8 *bssid,
5420 const u8 *req_ie, size_t req_ie_len,
5421 const u8 *resp_ie, size_t resp_ie_len,
5422 u16 status, gfp_t gfp)
5423{
5424 struct sk_buff *msg;
5425 void *hdr;
5426
5427 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5428 if (!msg)
5429 return;
5430
5431 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5432 if (!hdr) {
5433 nlmsg_free(msg);
5434 return;
5435 }
5436
5437 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5438 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5439 if (bssid)
5440 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5441 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5442 if (req_ie)
5443 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5444 if (resp_ie)
5445 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5446
5447 if (genlmsg_end(msg, hdr) < 0) {
5448 nlmsg_free(msg);
5449 return;
5450 }
5451
Johannes Berg463d0182009-07-14 00:33:35 +02005452 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5453 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005454 return;
5455
5456 nla_put_failure:
5457 genlmsg_cancel(msg, hdr);
5458 nlmsg_free(msg);
5459
5460}
5461
5462void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5463 struct net_device *netdev, const u8 *bssid,
5464 const u8 *req_ie, size_t req_ie_len,
5465 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5466{
5467 struct sk_buff *msg;
5468 void *hdr;
5469
5470 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5471 if (!msg)
5472 return;
5473
5474 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5475 if (!hdr) {
5476 nlmsg_free(msg);
5477 return;
5478 }
5479
5480 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5481 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5482 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5483 if (req_ie)
5484 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5485 if (resp_ie)
5486 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
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);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005495 return;
5496
5497 nla_put_failure:
5498 genlmsg_cancel(msg, hdr);
5499 nlmsg_free(msg);
5500
5501}
5502
5503void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5504 struct net_device *netdev, u16 reason,
Johannes Berg667503dd2009-07-07 03:56:11 +02005505 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005506{
5507 struct sk_buff *msg;
5508 void *hdr;
5509
Johannes Berg667503dd2009-07-07 03:56:11 +02005510 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005511 if (!msg)
5512 return;
5513
5514 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5515 if (!hdr) {
5516 nlmsg_free(msg);
5517 return;
5518 }
5519
5520 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5521 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5522 if (from_ap && reason)
5523 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5524 if (from_ap)
5525 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5526 if (ie)
5527 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5528
5529 if (genlmsg_end(msg, hdr) < 0) {
5530 nlmsg_free(msg);
5531 return;
5532 }
5533
Johannes Berg463d0182009-07-14 00:33:35 +02005534 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5535 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005536 return;
5537
5538 nla_put_failure:
5539 genlmsg_cancel(msg, hdr);
5540 nlmsg_free(msg);
5541
5542}
5543
Johannes Berg04a773a2009-04-19 21:24:32 +02005544void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5545 struct net_device *netdev, const u8 *bssid,
5546 gfp_t gfp)
5547{
5548 struct sk_buff *msg;
5549 void *hdr;
5550
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005551 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005552 if (!msg)
5553 return;
5554
5555 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5556 if (!hdr) {
5557 nlmsg_free(msg);
5558 return;
5559 }
5560
5561 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5562 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5563 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5564
5565 if (genlmsg_end(msg, hdr) < 0) {
5566 nlmsg_free(msg);
5567 return;
5568 }
5569
Johannes Berg463d0182009-07-14 00:33:35 +02005570 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5571 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005572 return;
5573
5574 nla_put_failure:
5575 genlmsg_cancel(msg, hdr);
5576 nlmsg_free(msg);
5577}
5578
Jouni Malinena3b8b052009-03-27 21:59:49 +02005579void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5580 struct net_device *netdev, const u8 *addr,
5581 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005582 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005583{
5584 struct sk_buff *msg;
5585 void *hdr;
5586
Johannes Berge6d6e342009-07-01 21:26:47 +02005587 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005588 if (!msg)
5589 return;
5590
5591 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5592 if (!hdr) {
5593 nlmsg_free(msg);
5594 return;
5595 }
5596
5597 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5598 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5599 if (addr)
5600 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5601 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5602 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5603 if (tsc)
5604 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5605
5606 if (genlmsg_end(msg, hdr) < 0) {
5607 nlmsg_free(msg);
5608 return;
5609 }
5610
Johannes Berg463d0182009-07-14 00:33:35 +02005611 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5612 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005613 return;
5614
5615 nla_put_failure:
5616 genlmsg_cancel(msg, hdr);
5617 nlmsg_free(msg);
5618}
5619
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005620void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5621 struct ieee80211_channel *channel_before,
5622 struct ieee80211_channel *channel_after)
5623{
5624 struct sk_buff *msg;
5625 void *hdr;
5626 struct nlattr *nl_freq;
5627
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005628 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005629 if (!msg)
5630 return;
5631
5632 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5633 if (!hdr) {
5634 nlmsg_free(msg);
5635 return;
5636 }
5637
5638 /*
5639 * Since we are applying the beacon hint to a wiphy we know its
5640 * wiphy_idx is valid
5641 */
5642 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5643
5644 /* Before */
5645 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5646 if (!nl_freq)
5647 goto nla_put_failure;
5648 if (nl80211_msg_put_channel(msg, channel_before))
5649 goto nla_put_failure;
5650 nla_nest_end(msg, nl_freq);
5651
5652 /* After */
5653 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5654 if (!nl_freq)
5655 goto nla_put_failure;
5656 if (nl80211_msg_put_channel(msg, channel_after))
5657 goto nla_put_failure;
5658 nla_nest_end(msg, nl_freq);
5659
5660 if (genlmsg_end(msg, hdr) < 0) {
5661 nlmsg_free(msg);
5662 return;
5663 }
5664
Johannes Berg463d0182009-07-14 00:33:35 +02005665 rcu_read_lock();
5666 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5667 GFP_ATOMIC);
5668 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005669
5670 return;
5671
5672nla_put_failure:
5673 genlmsg_cancel(msg, hdr);
5674 nlmsg_free(msg);
5675}
5676
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005677static void nl80211_send_remain_on_chan_event(
5678 int cmd, struct cfg80211_registered_device *rdev,
5679 struct net_device *netdev, u64 cookie,
5680 struct ieee80211_channel *chan,
5681 enum nl80211_channel_type channel_type,
5682 unsigned int duration, gfp_t gfp)
5683{
5684 struct sk_buff *msg;
5685 void *hdr;
5686
5687 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5688 if (!msg)
5689 return;
5690
5691 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5692 if (!hdr) {
5693 nlmsg_free(msg);
5694 return;
5695 }
5696
5697 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5698 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5699 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5700 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5701 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5702
5703 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5704 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5705
5706 if (genlmsg_end(msg, hdr) < 0) {
5707 nlmsg_free(msg);
5708 return;
5709 }
5710
5711 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5712 nl80211_mlme_mcgrp.id, gfp);
5713 return;
5714
5715 nla_put_failure:
5716 genlmsg_cancel(msg, hdr);
5717 nlmsg_free(msg);
5718}
5719
5720void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5721 struct net_device *netdev, u64 cookie,
5722 struct ieee80211_channel *chan,
5723 enum nl80211_channel_type channel_type,
5724 unsigned int duration, gfp_t gfp)
5725{
5726 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5727 rdev, netdev, cookie, chan,
5728 channel_type, duration, gfp);
5729}
5730
5731void nl80211_send_remain_on_channel_cancel(
5732 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5733 u64 cookie, struct ieee80211_channel *chan,
5734 enum nl80211_channel_type channel_type, gfp_t gfp)
5735{
5736 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5737 rdev, netdev, cookie, chan,
5738 channel_type, 0, gfp);
5739}
5740
Johannes Berg98b62182009-12-23 13:15:44 +01005741void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5742 struct net_device *dev, const u8 *mac_addr,
5743 struct station_info *sinfo, gfp_t gfp)
5744{
5745 struct sk_buff *msg;
5746
5747 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5748 if (!msg)
5749 return;
5750
5751 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5752 nlmsg_free(msg);
5753 return;
5754 }
5755
5756 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5757 nl80211_mlme_mcgrp.id, gfp);
5758}
5759
Johannes Berg2e161f72010-08-12 15:38:38 +02005760int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5761 struct net_device *netdev, u32 nlpid,
5762 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005763{
5764 struct sk_buff *msg;
5765 void *hdr;
5766 int err;
5767
5768 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5769 if (!msg)
5770 return -ENOMEM;
5771
Johannes Berg2e161f72010-08-12 15:38:38 +02005772 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005773 if (!hdr) {
5774 nlmsg_free(msg);
5775 return -ENOMEM;
5776 }
5777
5778 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5779 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5780 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5781 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5782
5783 err = genlmsg_end(msg, hdr);
5784 if (err < 0) {
5785 nlmsg_free(msg);
5786 return err;
5787 }
5788
5789 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5790 if (err < 0)
5791 return err;
5792 return 0;
5793
5794 nla_put_failure:
5795 genlmsg_cancel(msg, hdr);
5796 nlmsg_free(msg);
5797 return -ENOBUFS;
5798}
5799
Johannes Berg2e161f72010-08-12 15:38:38 +02005800void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5801 struct net_device *netdev, u64 cookie,
5802 const u8 *buf, size_t len, bool ack,
5803 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005804{
5805 struct sk_buff *msg;
5806 void *hdr;
5807
5808 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5809 if (!msg)
5810 return;
5811
Johannes Berg2e161f72010-08-12 15:38:38 +02005812 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02005813 if (!hdr) {
5814 nlmsg_free(msg);
5815 return;
5816 }
5817
5818 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5819 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5820 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5821 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5822 if (ack)
5823 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5824
5825 if (genlmsg_end(msg, hdr) < 0) {
5826 nlmsg_free(msg);
5827 return;
5828 }
5829
5830 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5831 return;
5832
5833 nla_put_failure:
5834 genlmsg_cancel(msg, hdr);
5835 nlmsg_free(msg);
5836}
5837
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005838void
5839nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5840 struct net_device *netdev,
5841 enum nl80211_cqm_rssi_threshold_event rssi_event,
5842 gfp_t gfp)
5843{
5844 struct sk_buff *msg;
5845 struct nlattr *pinfoattr;
5846 void *hdr;
5847
5848 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5849 if (!msg)
5850 return;
5851
5852 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5853 if (!hdr) {
5854 nlmsg_free(msg);
5855 return;
5856 }
5857
5858 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5859 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5860
5861 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5862 if (!pinfoattr)
5863 goto nla_put_failure;
5864
5865 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5866 rssi_event);
5867
5868 nla_nest_end(msg, pinfoattr);
5869
5870 if (genlmsg_end(msg, hdr) < 0) {
5871 nlmsg_free(msg);
5872 return;
5873 }
5874
5875 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5876 nl80211_mlme_mcgrp.id, gfp);
5877 return;
5878
5879 nla_put_failure:
5880 genlmsg_cancel(msg, hdr);
5881 nlmsg_free(msg);
5882}
5883
Johannes Bergc063dbf2010-11-24 08:10:05 +01005884void
5885nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
5886 struct net_device *netdev, const u8 *peer,
5887 u32 num_packets, gfp_t gfp)
5888{
5889 struct sk_buff *msg;
5890 struct nlattr *pinfoattr;
5891 void *hdr;
5892
5893 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5894 if (!msg)
5895 return;
5896
5897 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5898 if (!hdr) {
5899 nlmsg_free(msg);
5900 return;
5901 }
5902
5903 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5904 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5905 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
5906
5907 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5908 if (!pinfoattr)
5909 goto nla_put_failure;
5910
5911 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
5912
5913 nla_nest_end(msg, pinfoattr);
5914
5915 if (genlmsg_end(msg, hdr) < 0) {
5916 nlmsg_free(msg);
5917 return;
5918 }
5919
5920 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5921 nl80211_mlme_mcgrp.id, gfp);
5922 return;
5923
5924 nla_put_failure:
5925 genlmsg_cancel(msg, hdr);
5926 nlmsg_free(msg);
5927}
5928
Jouni Malinen026331c2010-02-15 12:53:10 +02005929static int nl80211_netlink_notify(struct notifier_block * nb,
5930 unsigned long state,
5931 void *_notify)
5932{
5933 struct netlink_notify *notify = _notify;
5934 struct cfg80211_registered_device *rdev;
5935 struct wireless_dev *wdev;
5936
5937 if (state != NETLINK_URELEASE)
5938 return NOTIFY_DONE;
5939
5940 rcu_read_lock();
5941
5942 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5943 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02005944 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02005945
5946 rcu_read_unlock();
5947
5948 return NOTIFY_DONE;
5949}
5950
5951static struct notifier_block nl80211_netlink_notifier = {
5952 .notifier_call = nl80211_netlink_notify,
5953};
5954
Johannes Berg55682962007-09-20 13:09:35 -04005955/* initialisation/exit functions */
5956
5957int nl80211_init(void)
5958{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005959 int err;
Johannes Berg55682962007-09-20 13:09:35 -04005960
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005961 err = genl_register_family_with_ops(&nl80211_fam,
5962 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04005963 if (err)
5964 return err;
5965
Johannes Berg55682962007-09-20 13:09:35 -04005966 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5967 if (err)
5968 goto err_out;
5969
Johannes Berg2a519312009-02-10 21:25:55 +01005970 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5971 if (err)
5972 goto err_out;
5973
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005974 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5975 if (err)
5976 goto err_out;
5977
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005978 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5979 if (err)
5980 goto err_out;
5981
Johannes Bergaff89a92009-07-01 21:26:51 +02005982#ifdef CONFIG_NL80211_TESTMODE
5983 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
5984 if (err)
5985 goto err_out;
5986#endif
5987
Jouni Malinen026331c2010-02-15 12:53:10 +02005988 err = netlink_register_notifier(&nl80211_netlink_notifier);
5989 if (err)
5990 goto err_out;
5991
Johannes Berg55682962007-09-20 13:09:35 -04005992 return 0;
5993 err_out:
5994 genl_unregister_family(&nl80211_fam);
5995 return err;
5996}
5997
5998void nl80211_exit(void)
5999{
Jouni Malinen026331c2010-02-15 12:53:10 +02006000 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006001 genl_unregister_family(&nl80211_fam);
6002}