blob: 73a7f6d354c96a346f8f41a798de4c32243f8618 [file] [log] [blame]
Johannes Berg55682962007-09-20 13:09:35 -04001/*
2 * This is the new netlink-based wireless configuration interface.
3 *
Jouni Malinen026331c2010-02-15 12:53:10 +02004 * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
Johannes Berg55682962007-09-20 13:09:35 -04005 */
6
7#include <linux/if.h>
8#include <linux/module.h>
9#include <linux/err.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Johannes Berg55682962007-09-20 13:09:35 -040011#include <linux/list.h>
12#include <linux/if_ether.h>
13#include <linux/ieee80211.h>
14#include <linux/nl80211.h>
15#include <linux/rtnetlink.h>
16#include <linux/netlink.h>
Johannes Berg2a519312009-02-10 21:25:55 +010017#include <linux/etherdevice.h>
Johannes Berg463d0182009-07-14 00:33:35 +020018#include <net/net_namespace.h>
Johannes Berg55682962007-09-20 13:09:35 -040019#include <net/genetlink.h>
20#include <net/cfg80211.h>
Johannes Berg463d0182009-07-14 00:33:35 +020021#include <net/sock.h>
Johannes Berg55682962007-09-20 13:09:35 -040022#include "core.h"
23#include "nl80211.h"
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -070024#include "reg.h"
Johannes Berg55682962007-09-20 13:09:35 -040025
Johannes Berg4c476992010-10-04 21:36:35 +020026static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
27 struct genl_info *info);
28static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
29 struct genl_info *info);
30
Johannes Berg55682962007-09-20 13:09:35 -040031/* the netlink family */
32static struct genl_family nl80211_fam = {
33 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
34 .name = "nl80211", /* have users key off the name instead */
35 .hdrsize = 0, /* no private header */
36 .version = 1, /* no particular meaning now */
37 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020038 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020039 .pre_doit = nl80211_pre_doit,
40 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040041};
42
Johannes Berg79c97e92009-07-07 03:56:12 +020043/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020044static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020045 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040046 struct net_device **dev)
47{
Johannes Berg463d0182009-07-14 00:33:35 +020048 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040049 int ifindex;
50
Johannes Bergbba95fe2008-07-29 13:22:51 +020051 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040052 return -EINVAL;
53
Johannes Bergbba95fe2008-07-29 13:22:51 +020054 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020055 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040056 if (!*dev)
57 return -ENODEV;
58
Johannes Berg463d0182009-07-14 00:33:35 +020059 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020060 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040061 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020062 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040063 }
64
65 return 0;
66}
67
68/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000069static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040070 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
71 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070072 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020073 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020074 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053075 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020076 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
77 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
78 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
79 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010080 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040081
82 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
83 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
84 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010085
86 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020087 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010088
Johannes Bergb9454e82009-07-08 13:29:08 +020089 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010090 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
91 .len = WLAN_MAX_KEY_LEN },
92 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
93 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
94 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +030095 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berge31b8212010-10-05 19:39:30 +020096 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +010097
98 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
99 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
100 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
101 .len = IEEE80211_MAX_DATA_LEN },
102 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
103 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100104 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
105 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
106 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
107 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
108 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100109 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100110 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200111 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100112 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
113 .len = IEEE80211_MAX_MESH_ID_LEN },
114 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300115
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700116 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
117 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
118
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300119 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
120 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
121 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200122 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
123 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100124 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300125
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700126 [NL80211_ATTR_MESH_PARAMS] = { .type = NLA_NESTED },
127
Jouni Malinen36aedc92008-08-25 11:58:58 +0300128 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
129 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200130
131 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
132 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
133 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100134 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
135 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200136
137 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
138 .len = IEEE80211_MAX_SSID_LEN },
139 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
140 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200141 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300142 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300143 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300144 [NL80211_ATTR_STA_FLAGS2] = {
145 .len = sizeof(struct nl80211_sta_flag_update),
146 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300147 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300148 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
149 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200150 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
151 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
152 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200153 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100154 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100155 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
156 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100157 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
158 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200159 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200160 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
161 .len = IEEE80211_MAX_DATA_LEN },
162 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200163 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200164 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300165 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200166 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300167 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
168 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200169 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900170 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
171 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100172 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100173 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Berg55682962007-09-20 13:09:35 -0400174};
175
Johannes Berge31b8212010-10-05 19:39:30 +0200176/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000177static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200178 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200179 [NL80211_KEY_IDX] = { .type = NLA_U8 },
180 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
181 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
182 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
183 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200184 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergb9454e82009-07-08 13:29:08 +0200185};
186
Holger Schuriga0438972009-11-11 11:30:02 +0100187/* ifidx get helper */
188static int nl80211_get_ifidx(struct netlink_callback *cb)
189{
190 int res;
191
192 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
193 nl80211_fam.attrbuf, nl80211_fam.maxattr,
194 nl80211_policy);
195 if (res)
196 return res;
197
198 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
199 return -EINVAL;
200
201 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
202 if (!res)
203 return -EINVAL;
204 return res;
205}
206
Johannes Berg67748892010-10-04 21:14:06 +0200207static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
208 struct netlink_callback *cb,
209 struct cfg80211_registered_device **rdev,
210 struct net_device **dev)
211{
212 int ifidx = cb->args[0];
213 int err;
214
215 if (!ifidx)
216 ifidx = nl80211_get_ifidx(cb);
217 if (ifidx < 0)
218 return ifidx;
219
220 cb->args[0] = ifidx;
221
222 rtnl_lock();
223
224 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
225 if (!*dev) {
226 err = -ENODEV;
227 goto out_rtnl;
228 }
229
230 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100231 if (IS_ERR(*rdev)) {
232 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200233 goto out_rtnl;
234 }
235
236 return 0;
237 out_rtnl:
238 rtnl_unlock();
239 return err;
240}
241
242static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
243{
244 cfg80211_unlock_rdev(rdev);
245 rtnl_unlock();
246}
247
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100248/* IE validation */
249static bool is_valid_ie_attr(const struct nlattr *attr)
250{
251 const u8 *pos;
252 int len;
253
254 if (!attr)
255 return true;
256
257 pos = nla_data(attr);
258 len = nla_len(attr);
259
260 while (len) {
261 u8 elemlen;
262
263 if (len < 2)
264 return false;
265 len -= 2;
266
267 elemlen = pos[1];
268 if (elemlen > len)
269 return false;
270
271 len -= elemlen;
272 pos += 2 + elemlen;
273 }
274
275 return true;
276}
277
Johannes Berg55682962007-09-20 13:09:35 -0400278/* message building helper */
279static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
280 int flags, u8 cmd)
281{
282 /* since there is no private header just add the generic one */
283 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
284}
285
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400286static int nl80211_msg_put_channel(struct sk_buff *msg,
287 struct ieee80211_channel *chan)
288{
289 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
290 chan->center_freq);
291
292 if (chan->flags & IEEE80211_CHAN_DISABLED)
293 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
294 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
295 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
296 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
297 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
298 if (chan->flags & IEEE80211_CHAN_RADAR)
299 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
300
301 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
302 DBM_TO_MBM(chan->max_power));
303
304 return 0;
305
306 nla_put_failure:
307 return -ENOBUFS;
308}
309
Johannes Berg55682962007-09-20 13:09:35 -0400310/* netlink command implementations */
311
Johannes Bergb9454e82009-07-08 13:29:08 +0200312struct key_parse {
313 struct key_params p;
314 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200315 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200316 bool def, defmgmt;
317};
318
319static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
320{
321 struct nlattr *tb[NL80211_KEY_MAX + 1];
322 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
323 nl80211_key_policy);
324 if (err)
325 return err;
326
327 k->def = !!tb[NL80211_KEY_DEFAULT];
328 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
329
330 if (tb[NL80211_KEY_IDX])
331 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
332
333 if (tb[NL80211_KEY_DATA]) {
334 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
335 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
336 }
337
338 if (tb[NL80211_KEY_SEQ]) {
339 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
340 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
341 }
342
343 if (tb[NL80211_KEY_CIPHER])
344 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
345
Johannes Berge31b8212010-10-05 19:39:30 +0200346 if (tb[NL80211_KEY_TYPE]) {
347 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
348 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
349 return -EINVAL;
350 }
351
Johannes Bergb9454e82009-07-08 13:29:08 +0200352 return 0;
353}
354
355static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
356{
357 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
358 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
359 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
360 }
361
362 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
363 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
364 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
365 }
366
367 if (info->attrs[NL80211_ATTR_KEY_IDX])
368 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
369
370 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
371 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
372
373 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
374 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
375
Johannes Berge31b8212010-10-05 19:39:30 +0200376 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
377 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
378 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
379 return -EINVAL;
380 }
381
Johannes Bergb9454e82009-07-08 13:29:08 +0200382 return 0;
383}
384
385static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
386{
387 int err;
388
389 memset(k, 0, sizeof(*k));
390 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200391 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200392
393 if (info->attrs[NL80211_ATTR_KEY])
394 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
395 else
396 err = nl80211_parse_key_old(info, k);
397
398 if (err)
399 return err;
400
401 if (k->def && k->defmgmt)
402 return -EINVAL;
403
404 if (k->idx != -1) {
405 if (k->defmgmt) {
406 if (k->idx < 4 || k->idx > 5)
407 return -EINVAL;
408 } else if (k->def) {
409 if (k->idx < 0 || k->idx > 3)
410 return -EINVAL;
411 } else {
412 if (k->idx < 0 || k->idx > 5)
413 return -EINVAL;
414 }
415 }
416
417 return 0;
418}
419
Johannes Bergfffd0932009-07-08 14:22:54 +0200420static struct cfg80211_cached_keys *
421nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
422 struct nlattr *keys)
423{
424 struct key_parse parse;
425 struct nlattr *key;
426 struct cfg80211_cached_keys *result;
427 int rem, err, def = 0;
428
429 result = kzalloc(sizeof(*result), GFP_KERNEL);
430 if (!result)
431 return ERR_PTR(-ENOMEM);
432
433 result->def = -1;
434 result->defmgmt = -1;
435
436 nla_for_each_nested(key, keys, rem) {
437 memset(&parse, 0, sizeof(parse));
438 parse.idx = -1;
439
440 err = nl80211_parse_key_new(key, &parse);
441 if (err)
442 goto error;
443 err = -EINVAL;
444 if (!parse.p.key)
445 goto error;
446 if (parse.idx < 0 || parse.idx > 4)
447 goto error;
448 if (parse.def) {
449 if (def)
450 goto error;
451 def = 1;
452 result->def = parse.idx;
453 } else if (parse.defmgmt)
454 goto error;
455 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200456 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200457 if (err)
458 goto error;
459 result->params[parse.idx].cipher = parse.p.cipher;
460 result->params[parse.idx].key_len = parse.p.key_len;
461 result->params[parse.idx].key = result->data[parse.idx];
462 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
463 }
464
465 return result;
466 error:
467 kfree(result);
468 return ERR_PTR(err);
469}
470
471static int nl80211_key_allowed(struct wireless_dev *wdev)
472{
473 ASSERT_WDEV_LOCK(wdev);
474
Johannes Bergfffd0932009-07-08 14:22:54 +0200475 switch (wdev->iftype) {
476 case NL80211_IFTYPE_AP:
477 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200478 case NL80211_IFTYPE_P2P_GO:
Johannes Bergfffd0932009-07-08 14:22:54 +0200479 break;
480 case NL80211_IFTYPE_ADHOC:
481 if (!wdev->current_bss)
482 return -ENOLINK;
483 break;
484 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200485 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200486 if (wdev->sme_state != CFG80211_SME_CONNECTED)
487 return -ENOLINK;
488 break;
489 default:
490 return -EINVAL;
491 }
492
493 return 0;
494}
495
Johannes Berg55682962007-09-20 13:09:35 -0400496static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
497 struct cfg80211_registered_device *dev)
498{
499 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100500 struct nlattr *nl_bands, *nl_band;
501 struct nlattr *nl_freqs, *nl_freq;
502 struct nlattr *nl_rates, *nl_rate;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700503 struct nlattr *nl_modes;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100504 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100505 enum ieee80211_band band;
506 struct ieee80211_channel *chan;
507 struct ieee80211_rate *rate;
508 int i;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700509 u16 ifmodes = dev->wiphy.interface_modes;
Johannes Berg2e161f72010-08-12 15:38:38 +0200510 const struct ieee80211_txrx_stypes *mgmt_stypes =
511 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400512
513 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
514 if (!hdr)
515 return -1;
516
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500517 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400518 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200519
Johannes Bergf5ea9122009-08-07 16:17:38 +0200520 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
521 cfg80211_rdev_list_generation);
522
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200523 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
524 dev->wiphy.retry_short);
525 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
526 dev->wiphy.retry_long);
527 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
528 dev->wiphy.frag_threshold);
529 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
530 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100531 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
532 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100533 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
534 dev->wiphy.max_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200535 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
536 dev->wiphy.max_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100537
Johannes Berge31b8212010-10-05 19:39:30 +0200538 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
539 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
540
Johannes Berg25e47c182009-04-02 20:14:06 +0200541 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
542 sizeof(u32) * dev->wiphy.n_cipher_suites,
543 dev->wiphy.cipher_suites);
544
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100545 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
546 dev->wiphy.max_num_pmkids);
547
Johannes Bergc0692b82010-08-27 14:26:53 +0300548 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
549 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
550
Bruno Randolfa7ffac92010-12-08 13:59:24 +0900551 if (dev->wiphy.available_antennas && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900552 u32 tx_ant = 0, rx_ant = 0;
553 int res;
554 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
555 if (!res) {
556 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
557 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
558 }
559 }
560
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700561 nl_modes = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_IFTYPES);
562 if (!nl_modes)
563 goto nla_put_failure;
564
565 i = 0;
566 while (ifmodes) {
567 if (ifmodes & 1)
568 NLA_PUT_FLAG(msg, i);
569 ifmodes >>= 1;
570 i++;
571 }
572
573 nla_nest_end(msg, nl_modes);
574
Johannes Bergee688b002008-01-24 19:38:39 +0100575 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
576 if (!nl_bands)
577 goto nla_put_failure;
578
579 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
580 if (!dev->wiphy.bands[band])
581 continue;
582
583 nl_band = nla_nest_start(msg, band);
584 if (!nl_band)
585 goto nla_put_failure;
586
Johannes Bergd51626d2008-10-09 12:20:13 +0200587 /* add HT info */
588 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
589 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
590 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
591 &dev->wiphy.bands[band]->ht_cap.mcs);
592 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
593 dev->wiphy.bands[band]->ht_cap.cap);
594 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
595 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
596 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
597 dev->wiphy.bands[band]->ht_cap.ampdu_density);
598 }
599
Johannes Bergee688b002008-01-24 19:38:39 +0100600 /* add frequencies */
601 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
602 if (!nl_freqs)
603 goto nla_put_failure;
604
605 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
606 nl_freq = nla_nest_start(msg, i);
607 if (!nl_freq)
608 goto nla_put_failure;
609
610 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100611
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400612 if (nl80211_msg_put_channel(msg, chan))
613 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200614
Johannes Bergee688b002008-01-24 19:38:39 +0100615 nla_nest_end(msg, nl_freq);
616 }
617
618 nla_nest_end(msg, nl_freqs);
619
620 /* add bitrates */
621 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
622 if (!nl_rates)
623 goto nla_put_failure;
624
625 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
626 nl_rate = nla_nest_start(msg, i);
627 if (!nl_rate)
628 goto nla_put_failure;
629
630 rate = &dev->wiphy.bands[band]->bitrates[i];
631 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
632 rate->bitrate);
633 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
634 NLA_PUT_FLAG(msg,
635 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
636
637 nla_nest_end(msg, nl_rate);
638 }
639
640 nla_nest_end(msg, nl_rates);
641
642 nla_nest_end(msg, nl_band);
643 }
644 nla_nest_end(msg, nl_bands);
645
Johannes Berg8fdc6212009-03-14 09:34:01 +0100646 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
647 if (!nl_cmds)
648 goto nla_put_failure;
649
650 i = 0;
651#define CMD(op, n) \
652 do { \
653 if (dev->ops->op) { \
654 i++; \
655 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
656 } \
657 } while (0)
658
659 CMD(add_virtual_intf, NEW_INTERFACE);
660 CMD(change_virtual_intf, SET_INTERFACE);
661 CMD(add_key, NEW_KEY);
662 CMD(add_beacon, NEW_BEACON);
663 CMD(add_station, NEW_STATION);
664 CMD(add_mpath, NEW_MPATH);
Johannes Berg29cbe682010-12-03 09:20:44 +0100665 CMD(update_mesh_params, SET_MESH_PARAMS);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100666 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200667 CMD(auth, AUTHENTICATE);
668 CMD(assoc, ASSOCIATE);
669 CMD(deauth, DEAUTHENTICATE);
670 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200671 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100672 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100673 CMD(set_pmksa, SET_PMKSA);
674 CMD(del_pmksa, DEL_PMKSA);
675 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100676 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200677 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200678 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100679 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100680 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200681 i++;
682 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
683 }
Johannes Bergf444de02010-05-05 15:25:02 +0200684 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400685 CMD(set_wds_peer, SET_WDS_PEER);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100686
687#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200688
Johannes Berg6829c8782009-07-02 09:13:27 +0200689 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200690 i++;
691 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
692 }
693
Johannes Berg6829c8782009-07-02 09:13:27 +0200694 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200695 i++;
696 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
697 }
698
Johannes Berg8fdc6212009-03-14 09:34:01 +0100699 nla_nest_end(msg, nl_cmds);
700
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100701 /* for now at least assume all drivers have it */
702 if (dev->ops->mgmt_tx)
703 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
704
Johannes Berg2e161f72010-08-12 15:38:38 +0200705 if (mgmt_stypes) {
706 u16 stypes;
707 struct nlattr *nl_ftypes, *nl_ifs;
708 enum nl80211_iftype ift;
709
710 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
711 if (!nl_ifs)
712 goto nla_put_failure;
713
714 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
715 nl_ftypes = nla_nest_start(msg, ift);
716 if (!nl_ftypes)
717 goto nla_put_failure;
718 i = 0;
719 stypes = mgmt_stypes[ift].tx;
720 while (stypes) {
721 if (stypes & 1)
722 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
723 (i << 4) | IEEE80211_FTYPE_MGMT);
724 stypes >>= 1;
725 i++;
726 }
727 nla_nest_end(msg, nl_ftypes);
728 }
729
Johannes Berg74b70a42010-08-24 12:15:53 +0200730 nla_nest_end(msg, nl_ifs);
731
Johannes Berg2e161f72010-08-12 15:38:38 +0200732 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
733 if (!nl_ifs)
734 goto nla_put_failure;
735
736 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
737 nl_ftypes = nla_nest_start(msg, ift);
738 if (!nl_ftypes)
739 goto nla_put_failure;
740 i = 0;
741 stypes = mgmt_stypes[ift].rx;
742 while (stypes) {
743 if (stypes & 1)
744 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
745 (i << 4) | IEEE80211_FTYPE_MGMT);
746 stypes >>= 1;
747 i++;
748 }
749 nla_nest_end(msg, nl_ftypes);
750 }
751 nla_nest_end(msg, nl_ifs);
752 }
753
Johannes Berg55682962007-09-20 13:09:35 -0400754 return genlmsg_end(msg, hdr);
755
756 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700757 genlmsg_cancel(msg, hdr);
758 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400759}
760
761static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
762{
763 int idx = 0;
764 int start = cb->args[0];
765 struct cfg80211_registered_device *dev;
766
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500767 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200768 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200769 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
770 continue;
Julius Volzb4637272008-07-08 14:02:19 +0200771 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -0400772 continue;
773 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
774 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +0200775 dev) < 0) {
776 idx--;
Johannes Berg55682962007-09-20 13:09:35 -0400777 break;
Julius Volzb4637272008-07-08 14:02:19 +0200778 }
Johannes Berg55682962007-09-20 13:09:35 -0400779 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500780 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -0400781
782 cb->args[0] = idx;
783
784 return skb->len;
785}
786
787static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
788{
789 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +0200790 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -0400791
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -0700792 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -0400793 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +0200794 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -0400795
Johannes Berg4c476992010-10-04 21:36:35 +0200796 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
797 nlmsg_free(msg);
798 return -ENOBUFS;
799 }
Johannes Berg55682962007-09-20 13:09:35 -0400800
Johannes Berg134e6372009-07-10 09:51:34 +0000801 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -0400802}
803
Jouni Malinen31888482008-10-30 16:59:24 +0200804static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
805 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
806 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
807 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
808 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
809 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
810};
811
812static int parse_txq_params(struct nlattr *tb[],
813 struct ieee80211_txq_params *txq_params)
814{
815 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
816 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
817 !tb[NL80211_TXQ_ATTR_AIFS])
818 return -EINVAL;
819
820 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
821 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
822 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
823 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
824 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
825
826 return 0;
827}
828
Johannes Bergf444de02010-05-05 15:25:02 +0200829static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
830{
831 /*
832 * You can only set the channel explicitly for AP, mesh
833 * and WDS type interfaces; all others have their channel
834 * managed via their respective "establish a connection"
835 * command (connect, join, ...)
836 *
837 * Monitors are special as they are normally slaved to
838 * whatever else is going on, so they behave as though
839 * you tried setting the wiphy channel itself.
840 */
841 return !wdev ||
842 wdev->iftype == NL80211_IFTYPE_AP ||
843 wdev->iftype == NL80211_IFTYPE_WDS ||
844 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +0200845 wdev->iftype == NL80211_IFTYPE_MONITOR ||
846 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +0200847}
848
849static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
850 struct wireless_dev *wdev,
851 struct genl_info *info)
852{
853 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
854 u32 freq;
855 int result;
856
857 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
858 return -EINVAL;
859
860 if (!nl80211_can_set_dev_channel(wdev))
861 return -EOPNOTSUPP;
862
863 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
864 channel_type = nla_get_u32(info->attrs[
865 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
866 if (channel_type != NL80211_CHAN_NO_HT &&
867 channel_type != NL80211_CHAN_HT20 &&
868 channel_type != NL80211_CHAN_HT40PLUS &&
869 channel_type != NL80211_CHAN_HT40MINUS)
870 return -EINVAL;
871 }
872
873 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
874
875 mutex_lock(&rdev->devlist_mtx);
876 if (wdev) {
877 wdev_lock(wdev);
878 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
879 wdev_unlock(wdev);
880 } else {
881 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
882 }
883 mutex_unlock(&rdev->devlist_mtx);
884
885 return result;
886}
887
888static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
889{
Johannes Berg4c476992010-10-04 21:36:35 +0200890 struct cfg80211_registered_device *rdev = info->user_ptr[0];
891 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +0200892
Johannes Berg4c476992010-10-04 21:36:35 +0200893 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +0200894}
895
Bill Jordane8347eb2010-10-01 13:54:28 -0400896static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
897{
Johannes Berg43b19952010-10-07 13:10:30 +0200898 struct cfg80211_registered_device *rdev = info->user_ptr[0];
899 struct net_device *dev = info->user_ptr[1];
900 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +0200901 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -0400902
903 if (!info->attrs[NL80211_ATTR_MAC])
904 return -EINVAL;
905
Johannes Berg43b19952010-10-07 13:10:30 +0200906 if (netif_running(dev))
907 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -0400908
Johannes Berg43b19952010-10-07 13:10:30 +0200909 if (!rdev->ops->set_wds_peer)
910 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400911
Johannes Berg43b19952010-10-07 13:10:30 +0200912 if (wdev->iftype != NL80211_IFTYPE_WDS)
913 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -0400914
915 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +0200916 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -0400917}
918
919
Johannes Berg55682962007-09-20 13:09:35 -0400920static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
921{
922 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +0200923 struct net_device *netdev = NULL;
924 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -0400925 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +0200926 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200927 u32 changed;
928 u8 retry_short = 0, retry_long = 0;
929 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +0100930 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -0400931
Johannes Bergf444de02010-05-05 15:25:02 +0200932 /*
933 * Try to find the wiphy and netdev. Normally this
934 * function shouldn't need the netdev, but this is
935 * done for backward compatibility -- previously
936 * setting the channel was done per wiphy, but now
937 * it is per netdev. Previous userland like hostapd
938 * also passed a netdev to set_wiphy, so that it is
939 * possible to let that go to the right netdev!
940 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100941 mutex_lock(&cfg80211_mutex);
942
Johannes Bergf444de02010-05-05 15:25:02 +0200943 if (info->attrs[NL80211_ATTR_IFINDEX]) {
944 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
945
946 netdev = dev_get_by_index(genl_info_net(info), ifindex);
947 if (netdev && netdev->ieee80211_ptr) {
948 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
949 mutex_lock(&rdev->mtx);
950 } else
951 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100952 }
953
Johannes Bergf444de02010-05-05 15:25:02 +0200954 if (!netdev) {
955 rdev = __cfg80211_rdev_from_info(info);
956 if (IS_ERR(rdev)) {
957 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +0200958 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +0200959 }
960 wdev = NULL;
961 netdev = NULL;
962 result = 0;
963
964 mutex_lock(&rdev->mtx);
965 } else if (netif_running(netdev) &&
966 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
967 wdev = netdev->ieee80211_ptr;
968 else
969 wdev = NULL;
970
971 /*
972 * end workaround code, by now the rdev is available
973 * and locked, and wdev may or may not be NULL.
974 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100975
976 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +0200977 result = cfg80211_dev_rename(
978 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +0100979
980 mutex_unlock(&cfg80211_mutex);
981
982 if (result)
983 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -0400984
Jouni Malinen31888482008-10-30 16:59:24 +0200985 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
986 struct ieee80211_txq_params txq_params;
987 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
988
989 if (!rdev->ops->set_txq_params) {
990 result = -EOPNOTSUPP;
991 goto bad_res;
992 }
993
994 nla_for_each_nested(nl_txq_params,
995 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
996 rem_txq_params) {
997 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
998 nla_data(nl_txq_params),
999 nla_len(nl_txq_params),
1000 txq_params_policy);
1001 result = parse_txq_params(tb, &txq_params);
1002 if (result)
1003 goto bad_res;
1004
1005 result = rdev->ops->set_txq_params(&rdev->wiphy,
1006 &txq_params);
1007 if (result)
1008 goto bad_res;
1009 }
1010 }
1011
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001012 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001013 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001014 if (result)
1015 goto bad_res;
1016 }
1017
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001018 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1019 enum nl80211_tx_power_setting type;
1020 int idx, mbm = 0;
1021
1022 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001023 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001024 goto bad_res;
1025 }
1026
1027 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1028 type = nla_get_u32(info->attrs[idx]);
1029
1030 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1031 (type != NL80211_TX_POWER_AUTOMATIC)) {
1032 result = -EINVAL;
1033 goto bad_res;
1034 }
1035
1036 if (type != NL80211_TX_POWER_AUTOMATIC) {
1037 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1038 mbm = nla_get_u32(info->attrs[idx]);
1039 }
1040
1041 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1042 if (result)
1043 goto bad_res;
1044 }
1045
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001046 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1047 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1048 u32 tx_ant, rx_ant;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001049 if (!rdev->wiphy.available_antennas || !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001050 result = -EOPNOTSUPP;
1051 goto bad_res;
1052 }
1053
1054 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1055 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1056
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001057 /* reject antenna configurations which don't match the
1058 * available antenna mask, except for the "all" mask */
1059 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas)) ||
1060 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas))) {
1061 result = -EINVAL;
1062 goto bad_res;
1063 }
1064
1065 tx_ant = tx_ant & rdev->wiphy.available_antennas;
1066 rx_ant = rx_ant & rdev->wiphy.available_antennas;
1067
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001068 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1069 if (result)
1070 goto bad_res;
1071 }
1072
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001073 changed = 0;
1074
1075 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1076 retry_short = nla_get_u8(
1077 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1078 if (retry_short == 0) {
1079 result = -EINVAL;
1080 goto bad_res;
1081 }
1082 changed |= WIPHY_PARAM_RETRY_SHORT;
1083 }
1084
1085 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1086 retry_long = nla_get_u8(
1087 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1088 if (retry_long == 0) {
1089 result = -EINVAL;
1090 goto bad_res;
1091 }
1092 changed |= WIPHY_PARAM_RETRY_LONG;
1093 }
1094
1095 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1096 frag_threshold = nla_get_u32(
1097 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1098 if (frag_threshold < 256) {
1099 result = -EINVAL;
1100 goto bad_res;
1101 }
1102 if (frag_threshold != (u32) -1) {
1103 /*
1104 * Fragments (apart from the last one) are required to
1105 * have even length. Make the fragmentation code
1106 * simpler by stripping LSB should someone try to use
1107 * odd threshold value.
1108 */
1109 frag_threshold &= ~0x1;
1110 }
1111 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1112 }
1113
1114 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1115 rts_threshold = nla_get_u32(
1116 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1117 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1118 }
1119
Lukáš Turek81077e82009-12-21 22:50:47 +01001120 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1121 coverage_class = nla_get_u8(
1122 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1123 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1124 }
1125
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001126 if (changed) {
1127 u8 old_retry_short, old_retry_long;
1128 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001129 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001130
1131 if (!rdev->ops->set_wiphy_params) {
1132 result = -EOPNOTSUPP;
1133 goto bad_res;
1134 }
1135
1136 old_retry_short = rdev->wiphy.retry_short;
1137 old_retry_long = rdev->wiphy.retry_long;
1138 old_frag_threshold = rdev->wiphy.frag_threshold;
1139 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001140 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001141
1142 if (changed & WIPHY_PARAM_RETRY_SHORT)
1143 rdev->wiphy.retry_short = retry_short;
1144 if (changed & WIPHY_PARAM_RETRY_LONG)
1145 rdev->wiphy.retry_long = retry_long;
1146 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1147 rdev->wiphy.frag_threshold = frag_threshold;
1148 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1149 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001150 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1151 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001152
1153 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1154 if (result) {
1155 rdev->wiphy.retry_short = old_retry_short;
1156 rdev->wiphy.retry_long = old_retry_long;
1157 rdev->wiphy.frag_threshold = old_frag_threshold;
1158 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001159 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001160 }
1161 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001162
Johannes Berg306d6112008-12-08 12:39:04 +01001163 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001164 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001165 if (netdev)
1166 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001167 return result;
1168}
1169
1170
1171static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001172 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001173 struct net_device *dev)
1174{
1175 void *hdr;
1176
1177 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1178 if (!hdr)
1179 return -1;
1180
1181 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001182 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001183 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001184 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001185
1186 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1187 rdev->devlist_generation ^
1188 (cfg80211_rdev_list_generation << 2));
1189
Johannes Berg55682962007-09-20 13:09:35 -04001190 return genlmsg_end(msg, hdr);
1191
1192 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001193 genlmsg_cancel(msg, hdr);
1194 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001195}
1196
1197static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1198{
1199 int wp_idx = 0;
1200 int if_idx = 0;
1201 int wp_start = cb->args[0];
1202 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001203 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001204 struct wireless_dev *wdev;
1205
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001206 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001207 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1208 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001209 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001210 if (wp_idx < wp_start) {
1211 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001212 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001213 }
Johannes Berg55682962007-09-20 13:09:35 -04001214 if_idx = 0;
1215
Johannes Bergf5ea9122009-08-07 16:17:38 +02001216 mutex_lock(&rdev->devlist_mtx);
1217 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001218 if (if_idx < if_start) {
1219 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001220 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001221 }
Johannes Berg55682962007-09-20 13:09:35 -04001222 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1223 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001224 rdev, wdev->netdev) < 0) {
1225 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001226 goto out;
1227 }
1228 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001229 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001230 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001231
1232 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001233 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001234 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001235 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001236
1237 cb->args[0] = wp_idx;
1238 cb->args[1] = if_idx;
1239
1240 return skb->len;
1241}
1242
1243static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1244{
1245 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001246 struct cfg80211_registered_device *dev = info->user_ptr[0];
1247 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001248
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001249 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001250 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001251 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001252
Johannes Bergd7264052009-04-19 16:23:20 +02001253 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001254 dev, netdev) < 0) {
1255 nlmsg_free(msg);
1256 return -ENOBUFS;
1257 }
Johannes Berg55682962007-09-20 13:09:35 -04001258
Johannes Berg134e6372009-07-10 09:51:34 +00001259 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001260}
1261
Michael Wu66f7ac52008-01-31 19:48:22 +01001262static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1263 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1264 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1265 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1266 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1267 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1268};
1269
1270static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1271{
1272 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1273 int flag;
1274
1275 *mntrflags = 0;
1276
1277 if (!nla)
1278 return -EINVAL;
1279
1280 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1281 nla, mntr_flags_policy))
1282 return -EINVAL;
1283
1284 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1285 if (flags[flag])
1286 *mntrflags |= (1<<flag);
1287
1288 return 0;
1289}
1290
Johannes Berg9bc383d2009-11-19 11:55:19 +01001291static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001292 struct net_device *netdev, u8 use_4addr,
1293 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001294{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001295 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001296 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001297 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001298 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001299 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001300
1301 switch (iftype) {
1302 case NL80211_IFTYPE_AP_VLAN:
1303 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1304 return 0;
1305 break;
1306 case NL80211_IFTYPE_STATION:
1307 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1308 return 0;
1309 break;
1310 default:
1311 break;
1312 }
1313
1314 return -EOPNOTSUPP;
1315}
1316
Johannes Berg55682962007-09-20 13:09:35 -04001317static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1318{
Johannes Berg4c476992010-10-04 21:36:35 +02001319 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001320 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001321 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001322 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001323 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001324 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001325 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001326
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001327 memset(&params, 0, sizeof(params));
1328
Johannes Berg04a773a2009-04-19 21:24:32 +02001329 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001330
Johannes Berg723b0382008-09-16 20:22:09 +02001331 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001332 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001333 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001334 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001335 if (ntype > NL80211_IFTYPE_MAX)
1336 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001337 }
1338
Johannes Berg92ffe052008-09-16 20:39:36 +02001339 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001340 struct wireless_dev *wdev = dev->ieee80211_ptr;
1341
Johannes Berg4c476992010-10-04 21:36:35 +02001342 if (ntype != NL80211_IFTYPE_MESH_POINT)
1343 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001344 if (netif_running(dev))
1345 return -EBUSY;
1346
1347 wdev_lock(wdev);
1348 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1349 IEEE80211_MAX_MESH_ID_LEN);
1350 wdev->mesh_id_up_len =
1351 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1352 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1353 wdev->mesh_id_up_len);
1354 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001355 }
1356
Felix Fietkau8b787642009-11-10 18:53:10 +01001357 if (info->attrs[NL80211_ATTR_4ADDR]) {
1358 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1359 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001360 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001361 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001362 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001363 } else {
1364 params.use_4addr = -1;
1365 }
1366
Johannes Berg92ffe052008-09-16 20:39:36 +02001367 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001368 if (ntype != NL80211_IFTYPE_MONITOR)
1369 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001370 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1371 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001372 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001373 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001374
1375 flags = &_flags;
1376 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001377 }
Johannes Berg3b858752009-03-12 09:55:09 +01001378
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001379 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001380 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001381 else
1382 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001383
Johannes Berg9bc383d2009-11-19 11:55:19 +01001384 if (!err && params.use_4addr != -1)
1385 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1386
Johannes Berg55682962007-09-20 13:09:35 -04001387 return err;
1388}
1389
1390static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1391{
Johannes Berg4c476992010-10-04 21:36:35 +02001392 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001393 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001394 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001395 int err;
1396 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001397 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001398
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001399 memset(&params, 0, sizeof(params));
1400
Johannes Berg55682962007-09-20 13:09:35 -04001401 if (!info->attrs[NL80211_ATTR_IFNAME])
1402 return -EINVAL;
1403
1404 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1405 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1406 if (type > NL80211_IFTYPE_MAX)
1407 return -EINVAL;
1408 }
1409
Johannes Berg79c97e92009-07-07 03:56:12 +02001410 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001411 !(rdev->wiphy.interface_modes & (1 << type)))
1412 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001413
Johannes Berg9bc383d2009-11-19 11:55:19 +01001414 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001415 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001416 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001417 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001418 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001419 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001420
Michael Wu66f7ac52008-01-31 19:48:22 +01001421 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1422 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1423 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001424 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001425 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001426 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001427 if (IS_ERR(dev))
1428 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001429
Johannes Berg29cbe682010-12-03 09:20:44 +01001430 if (type == NL80211_IFTYPE_MESH_POINT &&
1431 info->attrs[NL80211_ATTR_MESH_ID]) {
1432 struct wireless_dev *wdev = dev->ieee80211_ptr;
1433
1434 wdev_lock(wdev);
1435 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1436 IEEE80211_MAX_MESH_ID_LEN);
1437 wdev->mesh_id_up_len =
1438 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1439 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1440 wdev->mesh_id_up_len);
1441 wdev_unlock(wdev);
1442 }
1443
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001444 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001445}
1446
1447static int nl80211_del_interface(struct sk_buff *skb, struct genl_info *info)
1448{
Johannes Berg4c476992010-10-04 21:36:35 +02001449 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1450 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001451
Johannes Berg4c476992010-10-04 21:36:35 +02001452 if (!rdev->ops->del_virtual_intf)
1453 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001454
Johannes Berg4c476992010-10-04 21:36:35 +02001455 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001456}
1457
Johannes Berg41ade002007-12-19 02:03:29 +01001458struct get_key_cookie {
1459 struct sk_buff *msg;
1460 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001461 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001462};
1463
1464static void get_key_callback(void *c, struct key_params *params)
1465{
Johannes Bergb9454e82009-07-08 13:29:08 +02001466 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001467 struct get_key_cookie *cookie = c;
1468
1469 if (params->key)
1470 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1471 params->key_len, params->key);
1472
1473 if (params->seq)
1474 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1475 params->seq_len, params->seq);
1476
1477 if (params->cipher)
1478 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1479 params->cipher);
1480
Johannes Bergb9454e82009-07-08 13:29:08 +02001481 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1482 if (!key)
1483 goto nla_put_failure;
1484
1485 if (params->key)
1486 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1487 params->key_len, params->key);
1488
1489 if (params->seq)
1490 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1491 params->seq_len, params->seq);
1492
1493 if (params->cipher)
1494 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1495 params->cipher);
1496
1497 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1498
1499 nla_nest_end(cookie->msg, key);
1500
Johannes Berg41ade002007-12-19 02:03:29 +01001501 return;
1502 nla_put_failure:
1503 cookie->error = 1;
1504}
1505
1506static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1507{
Johannes Berg4c476992010-10-04 21:36:35 +02001508 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001509 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001510 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001511 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001512 const u8 *mac_addr = NULL;
1513 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001514 struct get_key_cookie cookie = {
1515 .error = 0,
1516 };
1517 void *hdr;
1518 struct sk_buff *msg;
1519
1520 if (info->attrs[NL80211_ATTR_KEY_IDX])
1521 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1522
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001523 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001524 return -EINVAL;
1525
1526 if (info->attrs[NL80211_ATTR_MAC])
1527 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1528
Johannes Berge31b8212010-10-05 19:39:30 +02001529 pairwise = !!mac_addr;
1530 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1531 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1532 if (kt >= NUM_NL80211_KEYTYPES)
1533 return -EINVAL;
1534 if (kt != NL80211_KEYTYPE_GROUP &&
1535 kt != NL80211_KEYTYPE_PAIRWISE)
1536 return -EINVAL;
1537 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1538 }
1539
Johannes Berg4c476992010-10-04 21:36:35 +02001540 if (!rdev->ops->get_key)
1541 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001542
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001543 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001544 if (!msg)
1545 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001546
1547 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1548 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001549 if (IS_ERR(hdr))
1550 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001551
1552 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001553 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001554
1555 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1556 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1557 if (mac_addr)
1558 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1559
Johannes Berge31b8212010-10-05 19:39:30 +02001560 if (pairwise && mac_addr &&
1561 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1562 return -ENOENT;
1563
1564 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1565 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001566
1567 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001568 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001569
1570 if (cookie.error)
1571 goto nla_put_failure;
1572
1573 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001574 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001575
1576 nla_put_failure:
1577 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001578 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001579 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001580 return err;
1581}
1582
1583static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1584{
Johannes Berg4c476992010-10-04 21:36:35 +02001585 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001586 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001587 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001588 struct net_device *dev = info->user_ptr[1];
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001589 int (*func)(struct wiphy *wiphy, struct net_device *netdev,
1590 u8 key_index);
Johannes Berg41ade002007-12-19 02:03:29 +01001591
Johannes Bergb9454e82009-07-08 13:29:08 +02001592 err = nl80211_parse_key(info, &key);
1593 if (err)
1594 return err;
1595
1596 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001597 return -EINVAL;
1598
Johannes Bergb9454e82009-07-08 13:29:08 +02001599 /* only support setting default key */
1600 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001601 return -EINVAL;
1602
Johannes Bergb9454e82009-07-08 13:29:08 +02001603 if (key.def)
Johannes Berg79c97e92009-07-07 03:56:12 +02001604 func = rdev->ops->set_default_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001605 else
Johannes Berg79c97e92009-07-07 03:56:12 +02001606 func = rdev->ops->set_default_mgmt_key;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001607
Johannes Berg4c476992010-10-04 21:36:35 +02001608 if (!func)
1609 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001610
Johannes Bergfffd0932009-07-08 14:22:54 +02001611 wdev_lock(dev->ieee80211_ptr);
1612 err = nl80211_key_allowed(dev->ieee80211_ptr);
1613 if (!err)
1614 err = func(&rdev->wiphy, dev, key.idx);
1615
Johannes Berg3d23e342009-09-29 23:27:28 +02001616#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001617 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02001618 if (func == rdev->ops->set_default_key)
Johannes Bergb9454e82009-07-08 13:29:08 +02001619 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001620 else
Johannes Bergb9454e82009-07-08 13:29:08 +02001621 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001622 }
1623#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001624 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001625
Johannes Berg41ade002007-12-19 02:03:29 +01001626 return err;
1627}
1628
1629static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1630{
Johannes Berg4c476992010-10-04 21:36:35 +02001631 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001632 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001633 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001634 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001635 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001636
Johannes Bergb9454e82009-07-08 13:29:08 +02001637 err = nl80211_parse_key(info, &key);
1638 if (err)
1639 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001640
Johannes Bergb9454e82009-07-08 13:29:08 +02001641 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001642 return -EINVAL;
1643
Johannes Berg41ade002007-12-19 02:03:29 +01001644 if (info->attrs[NL80211_ATTR_MAC])
1645 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1646
Johannes Berge31b8212010-10-05 19:39:30 +02001647 if (key.type == -1) {
1648 if (mac_addr)
1649 key.type = NL80211_KEYTYPE_PAIRWISE;
1650 else
1651 key.type = NL80211_KEYTYPE_GROUP;
1652 }
1653
1654 /* for now */
1655 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1656 key.type != NL80211_KEYTYPE_GROUP)
1657 return -EINVAL;
1658
Johannes Berg4c476992010-10-04 21:36:35 +02001659 if (!rdev->ops->add_key)
1660 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001661
Johannes Berge31b8212010-10-05 19:39:30 +02001662 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1663 key.type == NL80211_KEYTYPE_PAIRWISE,
1664 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001665 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001666
1667 wdev_lock(dev->ieee80211_ptr);
1668 err = nl80211_key_allowed(dev->ieee80211_ptr);
1669 if (!err)
1670 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001671 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001672 mac_addr, &key.p);
1673 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001674
Johannes Berg41ade002007-12-19 02:03:29 +01001675 return err;
1676}
1677
1678static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1679{
Johannes Berg4c476992010-10-04 21:36:35 +02001680 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001681 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001682 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001683 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001684 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001685
Johannes Bergb9454e82009-07-08 13:29:08 +02001686 err = nl80211_parse_key(info, &key);
1687 if (err)
1688 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001689
1690 if (info->attrs[NL80211_ATTR_MAC])
1691 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1692
Johannes Berge31b8212010-10-05 19:39:30 +02001693 if (key.type == -1) {
1694 if (mac_addr)
1695 key.type = NL80211_KEYTYPE_PAIRWISE;
1696 else
1697 key.type = NL80211_KEYTYPE_GROUP;
1698 }
1699
1700 /* for now */
1701 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1702 key.type != NL80211_KEYTYPE_GROUP)
1703 return -EINVAL;
1704
Johannes Berg4c476992010-10-04 21:36:35 +02001705 if (!rdev->ops->del_key)
1706 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001707
Johannes Bergfffd0932009-07-08 14:22:54 +02001708 wdev_lock(dev->ieee80211_ptr);
1709 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001710
1711 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1712 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1713 err = -ENOENT;
1714
Johannes Bergfffd0932009-07-08 14:22:54 +02001715 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001716 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1717 key.type == NL80211_KEYTYPE_PAIRWISE,
1718 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001719
Johannes Berg3d23e342009-09-29 23:27:28 +02001720#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001721 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001722 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001723 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001724 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001725 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1726 }
1727#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001728 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001729
Johannes Berg41ade002007-12-19 02:03:29 +01001730 return err;
1731}
1732
Johannes Berged1b6cc2007-12-19 02:03:32 +01001733static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1734{
1735 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1736 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001737 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1738 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001739 struct beacon_parameters params;
1740 int haveinfo = 0;
1741
Johannes Bergf4a11bb2009-03-27 12:40:28 +01001742 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]))
1743 return -EINVAL;
1744
Johannes Berg074ac8d2010-09-16 14:58:22 +02001745 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001746 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1747 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02001748
Johannes Berged1b6cc2007-12-19 02:03:32 +01001749 switch (info->genlhdr->cmd) {
1750 case NL80211_CMD_NEW_BEACON:
1751 /* these are required for NEW_BEACON */
1752 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
1753 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02001754 !info->attrs[NL80211_ATTR_BEACON_HEAD])
1755 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001756
Johannes Berg79c97e92009-07-07 03:56:12 +02001757 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001758 break;
1759 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02001760 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001761 break;
1762 default:
1763 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02001764 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001765 }
1766
Johannes Berg4c476992010-10-04 21:36:35 +02001767 if (!call)
1768 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001769
1770 memset(&params, 0, sizeof(params));
1771
1772 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
1773 params.interval =
1774 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
1775 haveinfo = 1;
1776 }
1777
1778 if (info->attrs[NL80211_ATTR_DTIM_PERIOD]) {
1779 params.dtim_period =
1780 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
1781 haveinfo = 1;
1782 }
1783
1784 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
1785 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1786 params.head_len =
1787 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
1788 haveinfo = 1;
1789 }
1790
1791 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
1792 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1793 params.tail_len =
1794 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
1795 haveinfo = 1;
1796 }
1797
Johannes Berg4c476992010-10-04 21:36:35 +02001798 if (!haveinfo)
1799 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001800
Johannes Berg4c476992010-10-04 21:36:35 +02001801 return call(&rdev->wiphy, dev, &params);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001802}
1803
1804static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
1805{
Johannes Berg4c476992010-10-04 21:36:35 +02001806 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1807 struct net_device *dev = info->user_ptr[1];
Johannes Berged1b6cc2007-12-19 02:03:32 +01001808
Johannes Berg4c476992010-10-04 21:36:35 +02001809 if (!rdev->ops->del_beacon)
1810 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001811
Johannes Berg074ac8d2010-09-16 14:58:22 +02001812 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02001813 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
1814 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001815
Johannes Berg4c476992010-10-04 21:36:35 +02001816 return rdev->ops->del_beacon(&rdev->wiphy, dev);
Johannes Berged1b6cc2007-12-19 02:03:32 +01001817}
1818
Johannes Berg5727ef12007-12-19 02:03:34 +01001819static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
1820 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
1821 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
1822 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03001823 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01001824};
1825
Johannes Bergeccb8e82009-05-11 21:57:56 +03001826static int parse_station_flags(struct genl_info *info,
1827 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01001828{
1829 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03001830 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01001831 int flag;
1832
Johannes Bergeccb8e82009-05-11 21:57:56 +03001833 /*
1834 * Try parsing the new attribute first so userspace
1835 * can specify both for older kernels.
1836 */
1837 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
1838 if (nla) {
1839 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01001840
Johannes Bergeccb8e82009-05-11 21:57:56 +03001841 sta_flags = nla_data(nla);
1842 params->sta_flags_mask = sta_flags->mask;
1843 params->sta_flags_set = sta_flags->set;
1844 if ((params->sta_flags_mask |
1845 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
1846 return -EINVAL;
1847 return 0;
1848 }
1849
1850 /* if present, parse the old attribute */
1851
1852 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01001853 if (!nla)
1854 return 0;
1855
1856 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
1857 nla, sta_flags_policy))
1858 return -EINVAL;
1859
Johannes Bergeccb8e82009-05-11 21:57:56 +03001860 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
1861 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01001862
1863 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
1864 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03001865 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01001866
1867 return 0;
1868}
1869
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001870static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
1871 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01001872 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001873{
1874 void *hdr;
Henning Rogge420e7fa2008-12-11 22:04:19 +01001875 struct nlattr *sinfoattr, *txrate;
1876 u16 bitrate;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001877
1878 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
1879 if (!hdr)
1880 return -1;
1881
1882 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1883 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1884
Johannes Bergf5ea9122009-08-07 16:17:38 +02001885 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
1886
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001887 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
1888 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001889 goto nla_put_failure;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001890 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
1891 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
1892 sinfo->inactive_time);
1893 if (sinfo->filled & STATION_INFO_RX_BYTES)
1894 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
1895 sinfo->rx_bytes);
1896 if (sinfo->filled & STATION_INFO_TX_BYTES)
1897 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
1898 sinfo->tx_bytes);
1899 if (sinfo->filled & STATION_INFO_LLID)
1900 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
1901 sinfo->llid);
1902 if (sinfo->filled & STATION_INFO_PLID)
1903 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
1904 sinfo->plid);
1905 if (sinfo->filled & STATION_INFO_PLINK_STATE)
1906 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
1907 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001908 if (sinfo->filled & STATION_INFO_SIGNAL)
1909 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
1910 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09001911 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
1912 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
1913 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001914 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
1915 txrate = nla_nest_start(msg, NL80211_STA_INFO_TX_BITRATE);
1916 if (!txrate)
1917 goto nla_put_failure;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001918
John W. Linville254416a2009-12-09 16:43:52 -05001919 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
1920 bitrate = cfg80211_calculate_bitrate(&sinfo->txrate);
Henning Rogge420e7fa2008-12-11 22:04:19 +01001921 if (bitrate > 0)
1922 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
1923
1924 if (sinfo->txrate.flags & RATE_INFO_FLAGS_MCS)
1925 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS,
1926 sinfo->txrate.mcs);
1927 if (sinfo->txrate.flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
1928 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
1929 if (sinfo->txrate.flags & RATE_INFO_FLAGS_SHORT_GI)
1930 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
1931
1932 nla_nest_end(msg, txrate);
1933 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02001934 if (sinfo->filled & STATION_INFO_RX_PACKETS)
1935 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
1936 sinfo->rx_packets);
1937 if (sinfo->filled & STATION_INFO_TX_PACKETS)
1938 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
1939 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09001940 if (sinfo->filled & STATION_INFO_TX_RETRIES)
1941 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
1942 sinfo->tx_retries);
1943 if (sinfo->filled & STATION_INFO_TX_FAILED)
1944 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
1945 sinfo->tx_failed);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001946 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001947
1948 return genlmsg_end(msg, hdr);
1949
1950 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001951 genlmsg_cancel(msg, hdr);
1952 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01001953}
1954
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001955static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02001956 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001957{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001958 struct station_info sinfo;
1959 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001960 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001961 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02001962 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001963 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001964
Johannes Berg67748892010-10-04 21:14:06 +02001965 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
1966 if (err)
1967 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001968
1969 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02001970 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001971 goto out_err;
1972 }
1973
Johannes Bergbba95fe2008-07-29 13:22:51 +02001974 while (1) {
1975 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
1976 mac_addr, &sinfo);
1977 if (err == -ENOENT)
1978 break;
1979 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01001980 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001981
1982 if (nl80211_send_station(skb,
1983 NETLINK_CB(cb->skb).pid,
1984 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1985 netdev, mac_addr,
1986 &sinfo) < 0)
1987 goto out;
1988
1989 sta_idx++;
1990 }
1991
1992
1993 out:
1994 cb->args[1] = sta_idx;
1995 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001996 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02001997 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001998
1999 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002000}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002001
Johannes Berg5727ef12007-12-19 02:03:34 +01002002static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2003{
Johannes Berg4c476992010-10-04 21:36:35 +02002004 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2005 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002006 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002007 struct sk_buff *msg;
2008 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002009 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002010
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002011 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002012
2013 if (!info->attrs[NL80211_ATTR_MAC])
2014 return -EINVAL;
2015
2016 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2017
Johannes Berg4c476992010-10-04 21:36:35 +02002018 if (!rdev->ops->get_station)
2019 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002020
Johannes Berg79c97e92009-07-07 03:56:12 +02002021 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002022 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002023 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002024
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002025 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002026 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002027 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002028
2029 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002030 dev, mac_addr, &sinfo) < 0) {
2031 nlmsg_free(msg);
2032 return -ENOBUFS;
2033 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002034
Johannes Berg4c476992010-10-04 21:36:35 +02002035 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002036}
2037
2038/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002039 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002040 */
Johannes Berg463d0182009-07-14 00:33:35 +02002041static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002042 struct cfg80211_registered_device *rdev,
2043 struct net_device **vlan)
2044{
Johannes Berg463d0182009-07-14 00:33:35 +02002045 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002046 *vlan = NULL;
2047
2048 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002049 *vlan = dev_get_by_index(genl_info_net(info),
2050 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002051 if (!*vlan)
2052 return -ENODEV;
2053 if (!(*vlan)->ieee80211_ptr)
2054 return -EINVAL;
2055 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2056 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002057 if (!netif_running(*vlan))
2058 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002059 }
2060 return 0;
2061}
2062
2063static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2064{
Johannes Berg4c476992010-10-04 21:36:35 +02002065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002066 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002067 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002068 struct station_parameters params;
2069 u8 *mac_addr = NULL;
2070
2071 memset(&params, 0, sizeof(params));
2072
2073 params.listen_interval = -1;
2074
2075 if (info->attrs[NL80211_ATTR_STA_AID])
2076 return -EINVAL;
2077
2078 if (!info->attrs[NL80211_ATTR_MAC])
2079 return -EINVAL;
2080
2081 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2082
2083 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2084 params.supported_rates =
2085 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2086 params.supported_rates_len =
2087 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2088 }
2089
2090 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2091 params.listen_interval =
2092 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2093
Jouni Malinen36aedc92008-08-25 11:58:58 +03002094 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2095 params.ht_capa =
2096 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2097
Johannes Bergeccb8e82009-05-11 21:57:56 +03002098 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002099 return -EINVAL;
2100
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002101 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2102 params.plink_action =
2103 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2104
Johannes Berg463d0182009-07-14 00:33:35 +02002105 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002106 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002107 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002108
2109 /* validate settings */
2110 err = 0;
2111
2112 switch (dev->ieee80211_ptr->iftype) {
2113 case NL80211_IFTYPE_AP:
2114 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002115 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002116 /* disallow mesh-specific things */
2117 if (params.plink_action)
2118 err = -EINVAL;
2119 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002120 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002121 case NL80211_IFTYPE_STATION:
2122 /* disallow everything but AUTHORIZED flag */
2123 if (params.plink_action)
2124 err = -EINVAL;
2125 if (params.vlan)
2126 err = -EINVAL;
2127 if (params.supported_rates)
2128 err = -EINVAL;
2129 if (params.ht_capa)
2130 err = -EINVAL;
2131 if (params.listen_interval >= 0)
2132 err = -EINVAL;
2133 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2134 err = -EINVAL;
2135 break;
2136 case NL80211_IFTYPE_MESH_POINT:
2137 /* disallow things mesh doesn't support */
2138 if (params.vlan)
2139 err = -EINVAL;
2140 if (params.ht_capa)
2141 err = -EINVAL;
2142 if (params.listen_interval >= 0)
2143 err = -EINVAL;
2144 if (params.supported_rates)
2145 err = -EINVAL;
2146 if (params.sta_flags_mask)
2147 err = -EINVAL;
2148 break;
2149 default:
2150 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002151 }
2152
Johannes Berg5727ef12007-12-19 02:03:34 +01002153 if (err)
2154 goto out;
2155
Johannes Berg79c97e92009-07-07 03:56:12 +02002156 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002157 err = -EOPNOTSUPP;
2158 goto out;
2159 }
2160
Johannes Berg79c97e92009-07-07 03:56:12 +02002161 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002162
2163 out:
2164 if (params.vlan)
2165 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002166
Johannes Berg5727ef12007-12-19 02:03:34 +01002167 return err;
2168}
2169
2170static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2171{
Johannes Berg4c476992010-10-04 21:36:35 +02002172 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002173 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002174 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002175 struct station_parameters params;
2176 u8 *mac_addr = NULL;
2177
2178 memset(&params, 0, sizeof(params));
2179
2180 if (!info->attrs[NL80211_ATTR_MAC])
2181 return -EINVAL;
2182
Johannes Berg5727ef12007-12-19 02:03:34 +01002183 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2184 return -EINVAL;
2185
2186 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2187 return -EINVAL;
2188
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002189 if (!info->attrs[NL80211_ATTR_STA_AID])
2190 return -EINVAL;
2191
Johannes Berg5727ef12007-12-19 02:03:34 +01002192 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2193 params.supported_rates =
2194 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2195 params.supported_rates_len =
2196 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2197 params.listen_interval =
2198 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002199
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002200 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2201 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2202 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002203
Jouni Malinen36aedc92008-08-25 11:58:58 +03002204 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2205 params.ht_capa =
2206 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002207
Johannes Bergeccb8e82009-05-11 21:57:56 +03002208 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002209 return -EINVAL;
2210
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002211 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002212 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02002213 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2214 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002215
Johannes Berg463d0182009-07-14 00:33:35 +02002216 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002217 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002218 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002219
2220 /* validate settings */
2221 err = 0;
2222
Johannes Berg79c97e92009-07-07 03:56:12 +02002223 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002224 err = -EOPNOTSUPP;
2225 goto out;
2226 }
2227
Johannes Berg79c97e92009-07-07 03:56:12 +02002228 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002229
2230 out:
2231 if (params.vlan)
2232 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002233 return err;
2234}
2235
2236static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2237{
Johannes Berg4c476992010-10-04 21:36:35 +02002238 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2239 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002240 u8 *mac_addr = NULL;
2241
2242 if (info->attrs[NL80211_ATTR_MAC])
2243 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2244
Johannes Berge80cf852009-05-11 14:43:13 +02002245 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002246 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002247 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002248 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2249 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002250
Johannes Berg4c476992010-10-04 21:36:35 +02002251 if (!rdev->ops->del_station)
2252 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002253
Johannes Berg4c476992010-10-04 21:36:35 +02002254 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002255}
2256
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002257static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2258 int flags, struct net_device *dev,
2259 u8 *dst, u8 *next_hop,
2260 struct mpath_info *pinfo)
2261{
2262 void *hdr;
2263 struct nlattr *pinfoattr;
2264
2265 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2266 if (!hdr)
2267 return -1;
2268
2269 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2270 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2271 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2272
Johannes Bergf5ea9122009-08-07 16:17:38 +02002273 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2274
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002275 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2276 if (!pinfoattr)
2277 goto nla_put_failure;
2278 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2279 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2280 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002281 if (pinfo->filled & MPATH_INFO_SN)
2282 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2283 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002284 if (pinfo->filled & MPATH_INFO_METRIC)
2285 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2286 pinfo->metric);
2287 if (pinfo->filled & MPATH_INFO_EXPTIME)
2288 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2289 pinfo->exptime);
2290 if (pinfo->filled & MPATH_INFO_FLAGS)
2291 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2292 pinfo->flags);
2293 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2294 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2295 pinfo->discovery_timeout);
2296 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2297 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2298 pinfo->discovery_retries);
2299
2300 nla_nest_end(msg, pinfoattr);
2301
2302 return genlmsg_end(msg, hdr);
2303
2304 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002305 genlmsg_cancel(msg, hdr);
2306 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002307}
2308
2309static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002310 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002311{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002312 struct mpath_info pinfo;
2313 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002314 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002315 u8 dst[ETH_ALEN];
2316 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002317 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002318 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002319
Johannes Berg67748892010-10-04 21:14:06 +02002320 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2321 if (err)
2322 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002323
2324 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002325 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002326 goto out_err;
2327 }
2328
Jouni Malineneec60b02009-03-20 21:21:19 +02002329 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2330 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002331 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002332 }
2333
Johannes Bergbba95fe2008-07-29 13:22:51 +02002334 while (1) {
2335 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2336 dst, next_hop, &pinfo);
2337 if (err == -ENOENT)
2338 break;
2339 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002340 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002341
2342 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2343 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2344 netdev, dst, next_hop,
2345 &pinfo) < 0)
2346 goto out;
2347
2348 path_idx++;
2349 }
2350
2351
2352 out:
2353 cb->args[1] = path_idx;
2354 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002355 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002356 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002357 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002358}
2359
2360static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2361{
Johannes Berg4c476992010-10-04 21:36:35 +02002362 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002363 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002364 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002365 struct mpath_info pinfo;
2366 struct sk_buff *msg;
2367 u8 *dst = NULL;
2368 u8 next_hop[ETH_ALEN];
2369
2370 memset(&pinfo, 0, sizeof(pinfo));
2371
2372 if (!info->attrs[NL80211_ATTR_MAC])
2373 return -EINVAL;
2374
2375 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2376
Johannes Berg4c476992010-10-04 21:36:35 +02002377 if (!rdev->ops->get_mpath)
2378 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002379
Johannes Berg4c476992010-10-04 21:36:35 +02002380 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2381 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002382
Johannes Berg79c97e92009-07-07 03:56:12 +02002383 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002384 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002385 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002386
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002387 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002388 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002389 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002390
2391 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002392 dev, dst, next_hop, &pinfo) < 0) {
2393 nlmsg_free(msg);
2394 return -ENOBUFS;
2395 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002396
Johannes Berg4c476992010-10-04 21:36:35 +02002397 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002398}
2399
2400static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2401{
Johannes Berg4c476992010-10-04 21:36:35 +02002402 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2403 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002404 u8 *dst = NULL;
2405 u8 *next_hop = NULL;
2406
2407 if (!info->attrs[NL80211_ATTR_MAC])
2408 return -EINVAL;
2409
2410 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2411 return -EINVAL;
2412
2413 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2414 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2415
Johannes Berg4c476992010-10-04 21:36:35 +02002416 if (!rdev->ops->change_mpath)
2417 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002418
Johannes Berg4c476992010-10-04 21:36:35 +02002419 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2420 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002421
Johannes Berg4c476992010-10-04 21:36:35 +02002422 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002423}
Johannes Berg4c476992010-10-04 21:36:35 +02002424
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002425static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2426{
Johannes Berg4c476992010-10-04 21:36:35 +02002427 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2428 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002429 u8 *dst = NULL;
2430 u8 *next_hop = NULL;
2431
2432 if (!info->attrs[NL80211_ATTR_MAC])
2433 return -EINVAL;
2434
2435 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2436 return -EINVAL;
2437
2438 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2439 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2440
Johannes Berg4c476992010-10-04 21:36:35 +02002441 if (!rdev->ops->add_mpath)
2442 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002443
Johannes Berg4c476992010-10-04 21:36:35 +02002444 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2445 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002446
Johannes Berg4c476992010-10-04 21:36:35 +02002447 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002448}
2449
2450static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2451{
Johannes Berg4c476992010-10-04 21:36:35 +02002452 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2453 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002454 u8 *dst = NULL;
2455
2456 if (info->attrs[NL80211_ATTR_MAC])
2457 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2458
Johannes Berg4c476992010-10-04 21:36:35 +02002459 if (!rdev->ops->del_mpath)
2460 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002461
Johannes Berg4c476992010-10-04 21:36:35 +02002462 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002463}
2464
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002465static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2466{
Johannes Berg4c476992010-10-04 21:36:35 +02002467 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2468 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002469 struct bss_parameters params;
2470
2471 memset(&params, 0, sizeof(params));
2472 /* default to not changing parameters */
2473 params.use_cts_prot = -1;
2474 params.use_short_preamble = -1;
2475 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002476 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002477 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002478
2479 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2480 params.use_cts_prot =
2481 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2482 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2483 params.use_short_preamble =
2484 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2485 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2486 params.use_short_slot_time =
2487 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002488 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2489 params.basic_rates =
2490 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2491 params.basic_rates_len =
2492 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2493 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002494 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2495 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002496 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2497 params.ht_opmode =
2498 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002499
Johannes Berg4c476992010-10-04 21:36:35 +02002500 if (!rdev->ops->change_bss)
2501 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002502
Johannes Berg074ac8d2010-09-16 14:58:22 +02002503 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002504 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2505 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002506
Johannes Berg4c476992010-10-04 21:36:35 +02002507 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002508}
2509
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002510static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002511 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2512 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2513 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2514 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2515 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2516 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2517};
2518
2519static int parse_reg_rule(struct nlattr *tb[],
2520 struct ieee80211_reg_rule *reg_rule)
2521{
2522 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2523 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2524
2525 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2526 return -EINVAL;
2527 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2528 return -EINVAL;
2529 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2530 return -EINVAL;
2531 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2532 return -EINVAL;
2533 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2534 return -EINVAL;
2535
2536 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2537
2538 freq_range->start_freq_khz =
2539 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2540 freq_range->end_freq_khz =
2541 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2542 freq_range->max_bandwidth_khz =
2543 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2544
2545 power_rule->max_eirp =
2546 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2547
2548 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2549 power_rule->max_antenna_gain =
2550 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2551
2552 return 0;
2553}
2554
2555static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2556{
2557 int r;
2558 char *data = NULL;
2559
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002560 /*
2561 * You should only get this when cfg80211 hasn't yet initialized
2562 * completely when built-in to the kernel right between the time
2563 * window between nl80211_init() and regulatory_init(), if that is
2564 * even possible.
2565 */
2566 mutex_lock(&cfg80211_mutex);
2567 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002568 mutex_unlock(&cfg80211_mutex);
2569 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002570 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002571 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002572
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002573 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2574 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002575
2576 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2577
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002578 r = regulatory_hint_user(data);
2579
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002580 return r;
2581}
2582
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002583static int nl80211_get_mesh_params(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002584 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002585{
Johannes Berg4c476992010-10-04 21:36:35 +02002586 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02002587 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002588 struct wireless_dev *wdev = dev->ieee80211_ptr;
2589 struct mesh_config cur_params;
2590 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002591 void *hdr;
2592 struct nlattr *pinfoattr;
2593 struct sk_buff *msg;
2594
Johannes Berg29cbe682010-12-03 09:20:44 +01002595 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2596 return -EOPNOTSUPP;
2597
Johannes Berg4c476992010-10-04 21:36:35 +02002598 if (!rdev->ops->get_mesh_params)
2599 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002600
Johannes Berg29cbe682010-12-03 09:20:44 +01002601 wdev_lock(wdev);
2602 /* If not connected, get default parameters */
2603 if (!wdev->mesh_id_len)
2604 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
2605 else
2606 err = rdev->ops->get_mesh_params(&rdev->wiphy, dev,
2607 &cur_params);
2608 wdev_unlock(wdev);
2609
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002610 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002611 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002612
2613 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002614 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002615 if (!msg)
2616 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002617 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2618 NL80211_CMD_GET_MESH_PARAMS);
2619 if (!hdr)
2620 goto nla_put_failure;
2621 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_PARAMS);
2622 if (!pinfoattr)
2623 goto nla_put_failure;
2624 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2625 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
2626 cur_params.dot11MeshRetryTimeout);
2627 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
2628 cur_params.dot11MeshConfirmTimeout);
2629 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
2630 cur_params.dot11MeshHoldingTimeout);
2631 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
2632 cur_params.dot11MeshMaxPeerLinks);
2633 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
2634 cur_params.dot11MeshMaxRetries);
2635 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
2636 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01002637 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
2638 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002639 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
2640 cur_params.auto_open_plinks);
2641 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2642 cur_params.dot11MeshHWMPmaxPREQretries);
2643 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
2644 cur_params.path_refresh_time);
2645 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2646 cur_params.min_discovery_timeout);
2647 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2648 cur_params.dot11MeshHWMPactivePathTimeout);
2649 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2650 cur_params.dot11MeshHWMPpreqMinInterval);
2651 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2652 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00002653 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
2654 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002655 nla_nest_end(msg, pinfoattr);
2656 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02002657 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002658
Johannes Berg3b858752009-03-12 09:55:09 +01002659 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002660 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002661 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02002662 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002663}
2664
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002665static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002666 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
2667 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
2668 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
2669 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
2670 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
2671 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01002672 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002673 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
2674
2675 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
2676 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
2677 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
2678 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
2679 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
2680 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
2681};
2682
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002683static int nl80211_parse_mesh_params(struct genl_info *info,
2684 struct mesh_config *cfg,
2685 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002686{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002687 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002688 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002689
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002690#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
2691do {\
2692 if (table[attr_num]) {\
2693 cfg->param = nla_fn(table[attr_num]); \
2694 mask |= (1 << (attr_num - 1)); \
2695 } \
2696} while (0);\
2697
2698
2699 if (!info->attrs[NL80211_ATTR_MESH_PARAMS])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002700 return -EINVAL;
2701 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002702 info->attrs[NL80211_ATTR_MESH_PARAMS],
2703 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002704 return -EINVAL;
2705
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002706 /* This makes sure that there aren't more than 32 mesh config
2707 * parameters (otherwise our bitfield scheme would not work.) */
2708 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
2709
2710 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002711 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
2712 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
2713 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
2714 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
2715 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
2716 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
2717 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
2718 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
2719 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
2720 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
2721 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
2722 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01002723 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
2724 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002725 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
2726 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
2727 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
2728 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
2729 nla_get_u8);
2730 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
2731 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
2732 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
2733 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
2734 nla_get_u16);
2735 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
2736 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
2737 nla_get_u32);
2738 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
2739 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
2740 nla_get_u16);
2741 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2742 dot11MeshHWMPnetDiameterTraversalTime,
2743 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
2744 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00002745 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
2746 dot11MeshHWMPRootMode, mask,
2747 NL80211_MESHCONF_HWMP_ROOTMODE,
2748 nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002749
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002750 if (mask_out)
2751 *mask_out = mask;
2752 return 0;
2753
2754#undef FILL_IN_MESH_PARAM_IF_SET
2755}
2756
Johannes Berg29cbe682010-12-03 09:20:44 +01002757static int nl80211_update_mesh_params(struct sk_buff *skb,
2758 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002759{
2760 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2761 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002762 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002763 struct mesh_config cfg;
2764 u32 mask;
2765 int err;
2766
Johannes Berg29cbe682010-12-03 09:20:44 +01002767 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2768 return -EOPNOTSUPP;
2769
2770 if (!rdev->ops->update_mesh_params)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01002771 return -EOPNOTSUPP;
2772
2773 err = nl80211_parse_mesh_params(info, &cfg, &mask);
2774 if (err)
2775 return err;
2776
Johannes Berg29cbe682010-12-03 09:20:44 +01002777 wdev_lock(wdev);
2778 if (!wdev->mesh_id_len)
2779 err = -ENOLINK;
2780
2781 if (!err)
2782 err = rdev->ops->update_mesh_params(&rdev->wiphy, dev,
2783 mask, &cfg);
2784
2785 wdev_unlock(wdev);
2786
2787 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002788}
2789
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002790static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
2791{
2792 struct sk_buff *msg;
2793 void *hdr = NULL;
2794 struct nlattr *nl_reg_rules;
2795 unsigned int i;
2796 int err = -EINVAL;
2797
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002798 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002799
2800 if (!cfg80211_regdomain)
2801 goto out;
2802
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002803 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002804 if (!msg) {
2805 err = -ENOBUFS;
2806 goto out;
2807 }
2808
2809 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
2810 NL80211_CMD_GET_REG);
2811 if (!hdr)
2812 goto nla_put_failure;
2813
2814 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
2815 cfg80211_regdomain->alpha2);
2816
2817 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
2818 if (!nl_reg_rules)
2819 goto nla_put_failure;
2820
2821 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
2822 struct nlattr *nl_reg_rule;
2823 const struct ieee80211_reg_rule *reg_rule;
2824 const struct ieee80211_freq_range *freq_range;
2825 const struct ieee80211_power_rule *power_rule;
2826
2827 reg_rule = &cfg80211_regdomain->reg_rules[i];
2828 freq_range = &reg_rule->freq_range;
2829 power_rule = &reg_rule->power_rule;
2830
2831 nl_reg_rule = nla_nest_start(msg, i);
2832 if (!nl_reg_rule)
2833 goto nla_put_failure;
2834
2835 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
2836 reg_rule->flags);
2837 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
2838 freq_range->start_freq_khz);
2839 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
2840 freq_range->end_freq_khz);
2841 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
2842 freq_range->max_bandwidth_khz);
2843 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
2844 power_rule->max_antenna_gain);
2845 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
2846 power_rule->max_eirp);
2847
2848 nla_nest_end(msg, nl_reg_rule);
2849 }
2850
2851 nla_nest_end(msg, nl_reg_rules);
2852
2853 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00002854 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002855 goto out;
2856
2857nla_put_failure:
2858 genlmsg_cancel(msg, hdr);
Yuri Ershovd080e272010-06-29 15:08:07 +04002859 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002860 err = -EMSGSIZE;
2861out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002862 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08002863 return err;
2864}
2865
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002866static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
2867{
2868 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
2869 struct nlattr *nl_reg_rule;
2870 char *alpha2 = NULL;
2871 int rem_reg_rules = 0, r = 0;
2872 u32 num_rules = 0, rule_idx = 0, size_of_regd;
2873 struct ieee80211_regdomain *rd = NULL;
2874
2875 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2876 return -EINVAL;
2877
2878 if (!info->attrs[NL80211_ATTR_REG_RULES])
2879 return -EINVAL;
2880
2881 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2882
2883 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2884 rem_reg_rules) {
2885 num_rules++;
2886 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04002887 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002888 }
2889
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002890 mutex_lock(&cfg80211_mutex);
2891
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002892 if (!reg_is_valid_request(alpha2)) {
2893 r = -EINVAL;
2894 goto bad_reg;
2895 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002896
2897 size_of_regd = sizeof(struct ieee80211_regdomain) +
2898 (num_rules * sizeof(struct ieee80211_reg_rule));
2899
2900 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002901 if (!rd) {
2902 r = -ENOMEM;
2903 goto bad_reg;
2904 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002905
2906 rd->n_reg_rules = num_rules;
2907 rd->alpha2[0] = alpha2[0];
2908 rd->alpha2[1] = alpha2[1];
2909
2910 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
2911 rem_reg_rules) {
2912 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
2913 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
2914 reg_rule_policy);
2915 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
2916 if (r)
2917 goto bad_reg;
2918
2919 rule_idx++;
2920
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002921 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
2922 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002923 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002924 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002925 }
2926
2927 BUG_ON(rule_idx != num_rules);
2928
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002929 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002930
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05002931 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002932
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002933 return r;
2934
Johannes Bergd2372b32008-10-24 20:32:20 +02002935 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04002936 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002937 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04002938 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002939}
2940
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002941static int validate_scan_freqs(struct nlattr *freqs)
2942{
2943 struct nlattr *attr1, *attr2;
2944 int n_channels = 0, tmp1, tmp2;
2945
2946 nla_for_each_nested(attr1, freqs, tmp1) {
2947 n_channels++;
2948 /*
2949 * Some hardware has a limited channel list for
2950 * scanning, and it is pretty much nonsensical
2951 * to scan for a channel twice, so disallow that
2952 * and don't require drivers to check that the
2953 * channel list they get isn't longer than what
2954 * they can scan, as long as they can scan all
2955 * the channels they registered at once.
2956 */
2957 nla_for_each_nested(attr2, freqs, tmp2)
2958 if (attr1 != attr2 &&
2959 nla_get_u32(attr1) == nla_get_u32(attr2))
2960 return 0;
2961 }
2962
2963 return n_channels;
2964}
2965
Johannes Berg2a519312009-02-10 21:25:55 +01002966static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
2967{
Johannes Berg4c476992010-10-04 21:36:35 +02002968 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2969 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01002970 struct cfg80211_scan_request *request;
2971 struct cfg80211_ssid *ssid;
2972 struct ieee80211_channel *channel;
2973 struct nlattr *attr;
2974 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002975 int err, tmp, n_ssids = 0, n_channels, i;
Johannes Berg2a519312009-02-10 21:25:55 +01002976 enum ieee80211_band band;
Jouni Malinen70692ad2009-02-16 19:39:13 +02002977 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01002978
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002979 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
2980 return -EINVAL;
2981
Johannes Berg79c97e92009-07-07 03:56:12 +02002982 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01002983
Johannes Berg4c476992010-10-04 21:36:35 +02002984 if (!rdev->ops->scan)
2985 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01002986
Johannes Berg4c476992010-10-04 21:36:35 +02002987 if (rdev->scan_req)
2988 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01002989
2990 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002991 n_channels = validate_scan_freqs(
2992 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02002993 if (!n_channels)
2994 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01002995 } else {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02002996 n_channels = 0;
2997
Johannes Berg2a519312009-02-10 21:25:55 +01002998 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
2999 if (wiphy->bands[band])
3000 n_channels += wiphy->bands[band]->n_channels;
3001 }
3002
3003 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3004 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3005 n_ssids++;
3006
Johannes Berg4c476992010-10-04 21:36:35 +02003007 if (n_ssids > wiphy->max_scan_ssids)
3008 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003009
Jouni Malinen70692ad2009-02-16 19:39:13 +02003010 if (info->attrs[NL80211_ATTR_IE])
3011 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3012 else
3013 ie_len = 0;
3014
Johannes Berg4c476992010-10-04 21:36:35 +02003015 if (ie_len > wiphy->max_scan_ie_len)
3016 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003017
Johannes Berg2a519312009-02-10 21:25:55 +01003018 request = kzalloc(sizeof(*request)
3019 + sizeof(*ssid) * n_ssids
Jouni Malinen70692ad2009-02-16 19:39:13 +02003020 + sizeof(channel) * n_channels
3021 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003022 if (!request)
3023 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003024
Johannes Berg2a519312009-02-10 21:25:55 +01003025 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003026 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003027 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003028 if (ie_len) {
3029 if (request->ssids)
3030 request->ie = (void *)(request->ssids + n_ssids);
3031 else
3032 request->ie = (void *)(request->channels + n_channels);
3033 }
Johannes Berg2a519312009-02-10 21:25:55 +01003034
Johannes Berg584991d2009-11-02 13:32:03 +01003035 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003036 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3037 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003038 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003039 struct ieee80211_channel *chan;
3040
3041 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3042
3043 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003044 err = -EINVAL;
3045 goto out_free;
3046 }
Johannes Berg584991d2009-11-02 13:32:03 +01003047
3048 /* ignore disabled channels */
3049 if (chan->flags & IEEE80211_CHAN_DISABLED)
3050 continue;
3051
3052 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003053 i++;
3054 }
3055 } else {
3056 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003057 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3058 int j;
3059 if (!wiphy->bands[band])
3060 continue;
3061 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003062 struct ieee80211_channel *chan;
3063
3064 chan = &wiphy->bands[band]->channels[j];
3065
3066 if (chan->flags & IEEE80211_CHAN_DISABLED)
3067 continue;
3068
3069 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003070 i++;
3071 }
3072 }
3073 }
3074
Johannes Berg584991d2009-11-02 13:32:03 +01003075 if (!i) {
3076 err = -EINVAL;
3077 goto out_free;
3078 }
3079
3080 request->n_channels = i;
3081
Johannes Berg2a519312009-02-10 21:25:55 +01003082 i = 0;
3083 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3084 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
3085 if (request->ssids[i].ssid_len > IEEE80211_MAX_SSID_LEN) {
3086 err = -EINVAL;
3087 goto out_free;
3088 }
3089 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
3090 request->ssids[i].ssid_len = nla_len(attr);
3091 i++;
3092 }
3093 }
3094
Jouni Malinen70692ad2009-02-16 19:39:13 +02003095 if (info->attrs[NL80211_ATTR_IE]) {
3096 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003097 memcpy((void *)request->ie,
3098 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003099 request->ie_len);
3100 }
3101
Johannes Berg463d0182009-07-14 00:33:35 +02003102 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003103 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003104
Johannes Berg79c97e92009-07-07 03:56:12 +02003105 rdev->scan_req = request;
3106 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003107
Johannes Berg463d0182009-07-14 00:33:35 +02003108 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003109 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003110 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003111 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003112 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003113 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003114 kfree(request);
3115 }
Johannes Berg3b858752009-03-12 09:55:09 +01003116
Johannes Berg2a519312009-02-10 21:25:55 +01003117 return err;
3118}
3119
3120static int nl80211_send_bss(struct sk_buff *msg, u32 pid, u32 seq, int flags,
3121 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003122 struct wireless_dev *wdev,
3123 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003124{
Johannes Berg48ab9052009-07-10 18:42:31 +02003125 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003126 void *hdr;
3127 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003128 int i;
3129
3130 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003131
3132 hdr = nl80211hdr_put(msg, pid, seq, flags,
3133 NL80211_CMD_NEW_SCAN_RESULTS);
3134 if (!hdr)
3135 return -1;
3136
Johannes Bergf5ea9122009-08-07 16:17:38 +02003137 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003138 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003139
3140 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3141 if (!bss)
3142 goto nla_put_failure;
3143 if (!is_zero_ether_addr(res->bssid))
3144 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3145 if (res->information_elements && res->len_information_elements)
3146 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3147 res->len_information_elements,
3148 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003149 if (res->beacon_ies && res->len_beacon_ies &&
3150 res->beacon_ies != res->information_elements)
3151 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3152 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003153 if (res->tsf)
3154 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3155 if (res->beacon_interval)
3156 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3157 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3158 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003159 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3160 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003161
Johannes Berg77965c92009-02-18 18:45:06 +01003162 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003163 case CFG80211_SIGNAL_TYPE_MBM:
3164 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3165 break;
3166 case CFG80211_SIGNAL_TYPE_UNSPEC:
3167 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3168 break;
3169 default:
3170 break;
3171 }
3172
Johannes Berg48ab9052009-07-10 18:42:31 +02003173 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003174 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003175 case NL80211_IFTYPE_STATION:
3176 if (intbss == wdev->current_bss)
3177 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3178 NL80211_BSS_STATUS_ASSOCIATED);
3179 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3180 if (intbss != wdev->auth_bsses[i])
3181 continue;
3182 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3183 NL80211_BSS_STATUS_AUTHENTICATED);
3184 break;
3185 }
3186 break;
3187 case NL80211_IFTYPE_ADHOC:
3188 if (intbss == wdev->current_bss)
3189 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3190 NL80211_BSS_STATUS_IBSS_JOINED);
3191 break;
3192 default:
3193 break;
3194 }
3195
Johannes Berg2a519312009-02-10 21:25:55 +01003196 nla_nest_end(msg, bss);
3197
3198 return genlmsg_end(msg, hdr);
3199
3200 nla_put_failure:
3201 genlmsg_cancel(msg, hdr);
3202 return -EMSGSIZE;
3203}
3204
3205static int nl80211_dump_scan(struct sk_buff *skb,
3206 struct netlink_callback *cb)
3207{
Johannes Berg48ab9052009-07-10 18:42:31 +02003208 struct cfg80211_registered_device *rdev;
3209 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003210 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003211 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003212 int start = cb->args[1], idx = 0;
3213 int err;
3214
Johannes Berg67748892010-10-04 21:14:06 +02003215 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3216 if (err)
3217 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003218
Johannes Berg48ab9052009-07-10 18:42:31 +02003219 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003220
Johannes Berg48ab9052009-07-10 18:42:31 +02003221 wdev_lock(wdev);
3222 spin_lock_bh(&rdev->bss_lock);
3223 cfg80211_bss_expire(rdev);
3224
3225 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003226 if (++idx <= start)
3227 continue;
3228 if (nl80211_send_bss(skb,
3229 NETLINK_CB(cb->skb).pid,
3230 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003231 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003232 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003233 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003234 }
3235 }
3236
Johannes Berg48ab9052009-07-10 18:42:31 +02003237 spin_unlock_bh(&rdev->bss_lock);
3238 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003239
3240 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003241 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003242
Johannes Berg67748892010-10-04 21:14:06 +02003243 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003244}
3245
Holger Schurig61fa7132009-11-11 12:25:40 +01003246static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3247 int flags, struct net_device *dev,
3248 struct survey_info *survey)
3249{
3250 void *hdr;
3251 struct nlattr *infoattr;
3252
3253 /* Survey without a channel doesn't make sense */
3254 if (!survey->channel)
3255 return -EINVAL;
3256
3257 hdr = nl80211hdr_put(msg, pid, seq, flags,
3258 NL80211_CMD_NEW_SURVEY_RESULTS);
3259 if (!hdr)
3260 return -ENOMEM;
3261
3262 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3263
3264 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3265 if (!infoattr)
3266 goto nla_put_failure;
3267
3268 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3269 survey->channel->center_freq);
3270 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3271 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3272 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003273 if (survey->filled & SURVEY_INFO_IN_USE)
3274 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02003275 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3276 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3277 survey->channel_time);
3278 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3279 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3280 survey->channel_time_busy);
3281 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3282 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3283 survey->channel_time_ext_busy);
3284 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3285 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3286 survey->channel_time_rx);
3287 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3288 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3289 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003290
3291 nla_nest_end(msg, infoattr);
3292
3293 return genlmsg_end(msg, hdr);
3294
3295 nla_put_failure:
3296 genlmsg_cancel(msg, hdr);
3297 return -EMSGSIZE;
3298}
3299
3300static int nl80211_dump_survey(struct sk_buff *skb,
3301 struct netlink_callback *cb)
3302{
3303 struct survey_info survey;
3304 struct cfg80211_registered_device *dev;
3305 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003306 int survey_idx = cb->args[1];
3307 int res;
3308
Johannes Berg67748892010-10-04 21:14:06 +02003309 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3310 if (res)
3311 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003312
3313 if (!dev->ops->dump_survey) {
3314 res = -EOPNOTSUPP;
3315 goto out_err;
3316 }
3317
3318 while (1) {
3319 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3320 &survey);
3321 if (res == -ENOENT)
3322 break;
3323 if (res)
3324 goto out_err;
3325
3326 if (nl80211_send_survey(skb,
3327 NETLINK_CB(cb->skb).pid,
3328 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3329 netdev,
3330 &survey) < 0)
3331 goto out;
3332 survey_idx++;
3333 }
3334
3335 out:
3336 cb->args[1] = survey_idx;
3337 res = skb->len;
3338 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02003339 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01003340 return res;
3341}
3342
Jouni Malinen255e7372009-03-20 21:21:17 +02003343static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
3344{
Samuel Ortizb23aa672009-07-01 21:26:54 +02003345 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02003346}
3347
Samuel Ortizb23aa672009-07-01 21:26:54 +02003348static bool nl80211_valid_wpa_versions(u32 wpa_versions)
3349{
3350 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
3351 NL80211_WPA_VERSION_2));
3352}
3353
3354static bool nl80211_valid_akm_suite(u32 akm)
3355{
3356 return akm == WLAN_AKM_SUITE_8021X ||
3357 akm == WLAN_AKM_SUITE_PSK;
3358}
3359
3360static bool nl80211_valid_cipher_suite(u32 cipher)
3361{
3362 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
3363 cipher == WLAN_CIPHER_SUITE_WEP104 ||
3364 cipher == WLAN_CIPHER_SUITE_TKIP ||
3365 cipher == WLAN_CIPHER_SUITE_CCMP ||
3366 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
3367}
3368
3369
Jouni Malinen636a5d32009-03-19 13:39:22 +02003370static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
3371{
Johannes Berg4c476992010-10-04 21:36:35 +02003372 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3373 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003374 struct ieee80211_channel *chan;
3375 const u8 *bssid, *ssid, *ie = NULL;
3376 int err, ssid_len, ie_len = 0;
3377 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02003378 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003379 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003380
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003381 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3382 return -EINVAL;
3383
3384 if (!info->attrs[NL80211_ATTR_MAC])
3385 return -EINVAL;
3386
Jouni Malinen17780922009-03-27 20:52:47 +02003387 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
3388 return -EINVAL;
3389
Johannes Berg19957bb2009-07-02 17:20:43 +02003390 if (!info->attrs[NL80211_ATTR_SSID])
3391 return -EINVAL;
3392
3393 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
3394 return -EINVAL;
3395
Johannes Bergfffd0932009-07-08 14:22:54 +02003396 err = nl80211_parse_key(info, &key);
3397 if (err)
3398 return err;
3399
3400 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02003401 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
3402 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02003403 if (!key.p.key || !key.p.key_len)
3404 return -EINVAL;
3405 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
3406 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
3407 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
3408 key.p.key_len != WLAN_KEY_LEN_WEP104))
3409 return -EINVAL;
3410 if (key.idx > 4)
3411 return -EINVAL;
3412 } else {
3413 key.p.key_len = 0;
3414 key.p.key = NULL;
3415 }
3416
Johannes Bergafea0b72010-08-10 09:46:42 +02003417 if (key.idx >= 0) {
3418 int i;
3419 bool ok = false;
3420 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
3421 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
3422 ok = true;
3423 break;
3424 }
3425 }
Johannes Berg4c476992010-10-04 21:36:35 +02003426 if (!ok)
3427 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02003428 }
3429
Johannes Berg4c476992010-10-04 21:36:35 +02003430 if (!rdev->ops->auth)
3431 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003432
Johannes Berg074ac8d2010-09-16 14:58:22 +02003433 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003434 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3435 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003436
Johannes Berg19957bb2009-07-02 17:20:43 +02003437 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02003438 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02003439 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003440 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3441 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003442
Johannes Berg19957bb2009-07-02 17:20:43 +02003443 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3444 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3445
3446 if (info->attrs[NL80211_ATTR_IE]) {
3447 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3448 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3449 }
3450
3451 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02003452 if (!nl80211_valid_auth_type(auth_type))
3453 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003454
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003455 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3456
Johannes Berg4c476992010-10-04 21:36:35 +02003457 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
3458 ssid, ssid_len, ie, ie_len,
3459 key.p.key, key.p.key_len, key.idx,
3460 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003461}
3462
Johannes Bergc0692b82010-08-27 14:26:53 +03003463static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
3464 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003465 struct cfg80211_crypto_settings *settings,
3466 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003467{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02003468 memset(settings, 0, sizeof(*settings));
3469
Samuel Ortizb23aa672009-07-01 21:26:54 +02003470 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
3471
Johannes Bergc0692b82010-08-27 14:26:53 +03003472 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
3473 u16 proto;
3474 proto = nla_get_u16(
3475 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
3476 settings->control_port_ethertype = cpu_to_be16(proto);
3477 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
3478 proto != ETH_P_PAE)
3479 return -EINVAL;
3480 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
3481 settings->control_port_no_encrypt = true;
3482 } else
3483 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
3484
Samuel Ortizb23aa672009-07-01 21:26:54 +02003485 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
3486 void *data;
3487 int len, i;
3488
3489 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3490 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
3491 settings->n_ciphers_pairwise = len / sizeof(u32);
3492
3493 if (len % sizeof(u32))
3494 return -EINVAL;
3495
Johannes Berg3dc27d22009-07-02 21:36:37 +02003496 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02003497 return -EINVAL;
3498
3499 memcpy(settings->ciphers_pairwise, data, len);
3500
3501 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3502 if (!nl80211_valid_cipher_suite(
3503 settings->ciphers_pairwise[i]))
3504 return -EINVAL;
3505 }
3506
3507 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
3508 settings->cipher_group =
3509 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
3510 if (!nl80211_valid_cipher_suite(settings->cipher_group))
3511 return -EINVAL;
3512 }
3513
3514 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
3515 settings->wpa_versions =
3516 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
3517 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
3518 return -EINVAL;
3519 }
3520
3521 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
3522 void *data;
3523 int len, i;
3524
3525 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
3526 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
3527 settings->n_akm_suites = len / sizeof(u32);
3528
3529 if (len % sizeof(u32))
3530 return -EINVAL;
3531
3532 memcpy(settings->akm_suites, data, len);
3533
3534 for (i = 0; i < settings->n_ciphers_pairwise; i++)
3535 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
3536 return -EINVAL;
3537 }
3538
3539 return 0;
3540}
3541
Jouni Malinen636a5d32009-03-19 13:39:22 +02003542static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
3543{
Johannes Berg4c476992010-10-04 21:36:35 +02003544 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3545 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003546 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02003547 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02003548 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02003549 int err, ssid_len, ie_len = 0;
3550 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003551
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003552 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3553 return -EINVAL;
3554
3555 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02003556 !info->attrs[NL80211_ATTR_SSID] ||
3557 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003558 return -EINVAL;
3559
Johannes Berg4c476992010-10-04 21:36:35 +02003560 if (!rdev->ops->assoc)
3561 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003562
Johannes Berg074ac8d2010-09-16 14:58:22 +02003563 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003564 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3565 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003566
Johannes Berg19957bb2009-07-02 17:20:43 +02003567 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003568
Johannes Berg19957bb2009-07-02 17:20:43 +02003569 chan = ieee80211_get_channel(&rdev->wiphy,
3570 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02003571 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
3572 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003573
Johannes Berg19957bb2009-07-02 17:20:43 +02003574 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3575 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003576
3577 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003578 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3579 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003580 }
3581
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003582 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003583 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003584 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02003585 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02003586 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02003587 else if (mfp != NL80211_MFP_NO)
3588 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03003589 }
3590
Johannes Berg3e5d7642009-07-07 14:37:26 +02003591 if (info->attrs[NL80211_ATTR_PREV_BSSID])
3592 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
3593
Johannes Bergc0692b82010-08-27 14:26:53 +03003594 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003595 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02003596 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
3597 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02003598 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003599
Jouni Malinen636a5d32009-03-19 13:39:22 +02003600 return err;
3601}
3602
3603static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
3604{
Johannes Berg4c476992010-10-04 21:36:35 +02003605 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3606 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003607 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003608 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003609 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003610 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003611
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003612 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3613 return -EINVAL;
3614
3615 if (!info->attrs[NL80211_ATTR_MAC])
3616 return -EINVAL;
3617
3618 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3619 return -EINVAL;
3620
Johannes Berg4c476992010-10-04 21:36:35 +02003621 if (!rdev->ops->deauth)
3622 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003623
Johannes Berg074ac8d2010-09-16 14:58:22 +02003624 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003625 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3626 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003627
Johannes Berg19957bb2009-07-02 17:20:43 +02003628 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003629
Johannes Berg19957bb2009-07-02 17:20:43 +02003630 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3631 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003632 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003633 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003634 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003635
3636 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003637 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3638 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003639 }
3640
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003641 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3642
Johannes Berg4c476992010-10-04 21:36:35 +02003643 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
3644 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003645}
3646
3647static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
3648{
Johannes Berg4c476992010-10-04 21:36:35 +02003649 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3650 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02003651 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02003652 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02003653 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003654 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003655
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003656 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3657 return -EINVAL;
3658
3659 if (!info->attrs[NL80211_ATTR_MAC])
3660 return -EINVAL;
3661
3662 if (!info->attrs[NL80211_ATTR_REASON_CODE])
3663 return -EINVAL;
3664
Johannes Berg4c476992010-10-04 21:36:35 +02003665 if (!rdev->ops->disassoc)
3666 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02003667
Johannes Berg074ac8d2010-09-16 14:58:22 +02003668 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003669 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3670 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02003671
Johannes Berg19957bb2009-07-02 17:20:43 +02003672 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003673
Johannes Berg19957bb2009-07-02 17:20:43 +02003674 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
3675 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003676 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02003677 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02003678 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02003679
3680 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02003681 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3682 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003683 }
3684
Jouni Malinend5cdfac2010-04-04 09:37:19 +03003685 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
3686
Johannes Berg4c476992010-10-04 21:36:35 +02003687 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
3688 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02003689}
3690
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003691static bool
3692nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
3693 int mcast_rate[IEEE80211_NUM_BANDS],
3694 int rateval)
3695{
3696 struct wiphy *wiphy = &rdev->wiphy;
3697 bool found = false;
3698 int band, i;
3699
3700 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3701 struct ieee80211_supported_band *sband;
3702
3703 sband = wiphy->bands[band];
3704 if (!sband)
3705 continue;
3706
3707 for (i = 0; i < sband->n_bitrates; i++) {
3708 if (sband->bitrates[i].bitrate == rateval) {
3709 mcast_rate[band] = i + 1;
3710 found = true;
3711 break;
3712 }
3713 }
3714 }
3715
3716 return found;
3717}
3718
Johannes Berg04a773a2009-04-19 21:24:32 +02003719static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
3720{
Johannes Berg4c476992010-10-04 21:36:35 +02003721 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3722 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003723 struct cfg80211_ibss_params ibss;
3724 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003725 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003726 int err;
3727
Johannes Berg8e30bc52009-04-22 17:45:38 +02003728 memset(&ibss, 0, sizeof(ibss));
3729
Johannes Berg04a773a2009-04-19 21:24:32 +02003730 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3731 return -EINVAL;
3732
3733 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
3734 !info->attrs[NL80211_ATTR_SSID] ||
3735 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3736 return -EINVAL;
3737
Johannes Berg8e30bc52009-04-22 17:45:38 +02003738 ibss.beacon_interval = 100;
3739
3740 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
3741 ibss.beacon_interval =
3742 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
3743 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
3744 return -EINVAL;
3745 }
3746
Johannes Berg4c476992010-10-04 21:36:35 +02003747 if (!rdev->ops->join_ibss)
3748 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003749
Johannes Berg4c476992010-10-04 21:36:35 +02003750 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3751 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003752
Johannes Berg79c97e92009-07-07 03:56:12 +02003753 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02003754
3755 if (info->attrs[NL80211_ATTR_MAC])
3756 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3757 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3758 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3759
3760 if (info->attrs[NL80211_ATTR_IE]) {
3761 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3762 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3763 }
3764
3765 ibss.channel = ieee80211_get_channel(wiphy,
3766 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3767 if (!ibss.channel ||
3768 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02003769 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
3770 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02003771
3772 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02003773 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02003774
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003775 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
3776 u8 *rates =
3777 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3778 int n_rates =
3779 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
3780 struct ieee80211_supported_band *sband =
3781 wiphy->bands[ibss.channel->band];
3782 int i, j;
3783
Johannes Berg4c476992010-10-04 21:36:35 +02003784 if (n_rates == 0)
3785 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003786
3787 for (i = 0; i < n_rates; i++) {
3788 int rate = (rates[i] & 0x7f) * 5;
3789 bool found = false;
3790
3791 for (j = 0; j < sband->n_bitrates; j++) {
3792 if (sband->bitrates[j].bitrate == rate) {
3793 found = true;
3794 ibss.basic_rates |= BIT(j);
3795 break;
3796 }
3797 }
Johannes Berg4c476992010-10-04 21:36:35 +02003798 if (!found)
3799 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003800 }
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003801 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01003802
3803 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
3804 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
3805 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
3806 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03003807
Johannes Berg4c476992010-10-04 21:36:35 +02003808 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
3809 connkeys = nl80211_parse_connkeys(rdev,
3810 info->attrs[NL80211_ATTR_KEYS]);
3811 if (IS_ERR(connkeys))
3812 return PTR_ERR(connkeys);
3813 }
Johannes Berg04a773a2009-04-19 21:24:32 +02003814
Johannes Berg4c476992010-10-04 21:36:35 +02003815 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02003816 if (err)
3817 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02003818 return err;
3819}
3820
3821static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
3822{
Johannes Berg4c476992010-10-04 21:36:35 +02003823 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3824 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02003825
Johannes Berg4c476992010-10-04 21:36:35 +02003826 if (!rdev->ops->leave_ibss)
3827 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003828
Johannes Berg4c476992010-10-04 21:36:35 +02003829 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
3830 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02003831
Johannes Berg4c476992010-10-04 21:36:35 +02003832 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02003833}
3834
Johannes Bergaff89a92009-07-01 21:26:51 +02003835#ifdef CONFIG_NL80211_TESTMODE
3836static struct genl_multicast_group nl80211_testmode_mcgrp = {
3837 .name = "testmode",
3838};
3839
3840static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
3841{
Johannes Berg4c476992010-10-04 21:36:35 +02003842 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02003843 int err;
3844
3845 if (!info->attrs[NL80211_ATTR_TESTDATA])
3846 return -EINVAL;
3847
Johannes Bergaff89a92009-07-01 21:26:51 +02003848 err = -EOPNOTSUPP;
3849 if (rdev->ops->testmode_cmd) {
3850 rdev->testmode_info = info;
3851 err = rdev->ops->testmode_cmd(&rdev->wiphy,
3852 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
3853 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
3854 rdev->testmode_info = NULL;
3855 }
3856
Johannes Bergaff89a92009-07-01 21:26:51 +02003857 return err;
3858}
3859
3860static struct sk_buff *
3861__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
3862 int approxlen, u32 pid, u32 seq, gfp_t gfp)
3863{
3864 struct sk_buff *skb;
3865 void *hdr;
3866 struct nlattr *data;
3867
3868 skb = nlmsg_new(approxlen + 100, gfp);
3869 if (!skb)
3870 return NULL;
3871
3872 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
3873 if (!hdr) {
3874 kfree_skb(skb);
3875 return NULL;
3876 }
3877
3878 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
3879 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
3880
3881 ((void **)skb->cb)[0] = rdev;
3882 ((void **)skb->cb)[1] = hdr;
3883 ((void **)skb->cb)[2] = data;
3884
3885 return skb;
3886
3887 nla_put_failure:
3888 kfree_skb(skb);
3889 return NULL;
3890}
3891
3892struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
3893 int approxlen)
3894{
3895 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3896
3897 if (WARN_ON(!rdev->testmode_info))
3898 return NULL;
3899
3900 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
3901 rdev->testmode_info->snd_pid,
3902 rdev->testmode_info->snd_seq,
3903 GFP_KERNEL);
3904}
3905EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
3906
3907int cfg80211_testmode_reply(struct sk_buff *skb)
3908{
3909 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
3910 void *hdr = ((void **)skb->cb)[1];
3911 struct nlattr *data = ((void **)skb->cb)[2];
3912
3913 if (WARN_ON(!rdev->testmode_info)) {
3914 kfree_skb(skb);
3915 return -EINVAL;
3916 }
3917
3918 nla_nest_end(skb, data);
3919 genlmsg_end(skb, hdr);
3920 return genlmsg_reply(skb, rdev->testmode_info);
3921}
3922EXPORT_SYMBOL(cfg80211_testmode_reply);
3923
3924struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
3925 int approxlen, gfp_t gfp)
3926{
3927 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
3928
3929 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
3930}
3931EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
3932
3933void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
3934{
3935 void *hdr = ((void **)skb->cb)[1];
3936 struct nlattr *data = ((void **)skb->cb)[2];
3937
3938 nla_nest_end(skb, data);
3939 genlmsg_end(skb, hdr);
3940 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
3941}
3942EXPORT_SYMBOL(cfg80211_testmode_event);
3943#endif
3944
Samuel Ortizb23aa672009-07-01 21:26:54 +02003945static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
3946{
Johannes Berg4c476992010-10-04 21:36:35 +02003947 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3948 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02003949 struct cfg80211_connect_params connect;
3950 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02003951 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003952 int err;
3953
3954 memset(&connect, 0, sizeof(connect));
3955
3956 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3957 return -EINVAL;
3958
3959 if (!info->attrs[NL80211_ATTR_SSID] ||
3960 !nla_len(info->attrs[NL80211_ATTR_SSID]))
3961 return -EINVAL;
3962
3963 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
3964 connect.auth_type =
3965 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
3966 if (!nl80211_valid_auth_type(connect.auth_type))
3967 return -EINVAL;
3968 } else
3969 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
3970
3971 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
3972
Johannes Bergc0692b82010-08-27 14:26:53 +03003973 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02003974 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02003975 if (err)
3976 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003977
Johannes Berg074ac8d2010-09-16 14:58:22 +02003978 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02003979 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
3980 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003981
Johannes Berg79c97e92009-07-07 03:56:12 +02003982 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02003983
Samuel Ortizb23aa672009-07-01 21:26:54 +02003984 if (info->attrs[NL80211_ATTR_MAC])
3985 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
3986 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
3987 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
3988
3989 if (info->attrs[NL80211_ATTR_IE]) {
3990 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
3991 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3992 }
3993
3994 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
3995 connect.channel =
3996 ieee80211_get_channel(wiphy,
3997 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
3998 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02003999 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4000 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004001 }
4002
Johannes Bergfffd0932009-07-08 14:22:54 +02004003 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4004 connkeys = nl80211_parse_connkeys(rdev,
4005 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004006 if (IS_ERR(connkeys))
4007 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004008 }
4009
4010 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004011 if (err)
4012 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004013 return err;
4014}
4015
4016static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4017{
Johannes Berg4c476992010-10-04 21:36:35 +02004018 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4019 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004020 u16 reason;
4021
4022 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4023 reason = WLAN_REASON_DEAUTH_LEAVING;
4024 else
4025 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4026
4027 if (reason == 0)
4028 return -EINVAL;
4029
Johannes Berg074ac8d2010-09-16 14:58:22 +02004030 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004031 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4032 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004033
Johannes Berg4c476992010-10-04 21:36:35 +02004034 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004035}
4036
Johannes Berg463d0182009-07-14 00:33:35 +02004037static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4038{
Johannes Berg4c476992010-10-04 21:36:35 +02004039 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004040 struct net *net;
4041 int err;
4042 u32 pid;
4043
4044 if (!info->attrs[NL80211_ATTR_PID])
4045 return -EINVAL;
4046
4047 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4048
Johannes Berg463d0182009-07-14 00:33:35 +02004049 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004050 if (IS_ERR(net))
4051 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004052
4053 err = 0;
4054
4055 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004056 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4057 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004058
Johannes Berg463d0182009-07-14 00:33:35 +02004059 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004060 return err;
4061}
4062
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004063static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4064{
Johannes Berg4c476992010-10-04 21:36:35 +02004065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004066 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4067 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004068 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004069 struct cfg80211_pmksa pmksa;
4070
4071 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4072
4073 if (!info->attrs[NL80211_ATTR_MAC])
4074 return -EINVAL;
4075
4076 if (!info->attrs[NL80211_ATTR_PMKID])
4077 return -EINVAL;
4078
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004079 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4080 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4081
Johannes Berg074ac8d2010-09-16 14:58:22 +02004082 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004083 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4084 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004085
4086 switch (info->genlhdr->cmd) {
4087 case NL80211_CMD_SET_PMKSA:
4088 rdev_ops = rdev->ops->set_pmksa;
4089 break;
4090 case NL80211_CMD_DEL_PMKSA:
4091 rdev_ops = rdev->ops->del_pmksa;
4092 break;
4093 default:
4094 WARN_ON(1);
4095 break;
4096 }
4097
Johannes Berg4c476992010-10-04 21:36:35 +02004098 if (!rdev_ops)
4099 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004100
Johannes Berg4c476992010-10-04 21:36:35 +02004101 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004102}
4103
4104static int nl80211_flush_pmksa(struct sk_buff *skb, 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];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004108
Johannes Berg074ac8d2010-09-16 14:58:22 +02004109 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004110 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4111 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004112
Johannes Berg4c476992010-10-04 21:36:35 +02004113 if (!rdev->ops->flush_pmksa)
4114 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004115
Johannes Berg4c476992010-10-04 21:36:35 +02004116 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004117}
4118
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004119static int nl80211_remain_on_channel(struct sk_buff *skb,
4120 struct genl_info *info)
4121{
Johannes Berg4c476992010-10-04 21:36:35 +02004122 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4123 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004124 struct ieee80211_channel *chan;
4125 struct sk_buff *msg;
4126 void *hdr;
4127 u64 cookie;
4128 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4129 u32 freq, duration;
4130 int err;
4131
4132 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4133 !info->attrs[NL80211_ATTR_DURATION])
4134 return -EINVAL;
4135
4136 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4137
4138 /*
4139 * We should be on that channel for at least one jiffie,
4140 * and more than 5 seconds seems excessive.
4141 */
4142 if (!duration || !msecs_to_jiffies(duration) || duration > 5000)
4143 return -EINVAL;
4144
Johannes Berg4c476992010-10-04 21:36:35 +02004145 if (!rdev->ops->remain_on_channel)
4146 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004147
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004148 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4149 channel_type = nla_get_u32(
4150 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4151 if (channel_type != NL80211_CHAN_NO_HT &&
4152 channel_type != NL80211_CHAN_HT20 &&
4153 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004154 channel_type != NL80211_CHAN_HT40MINUS)
4155 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004156 }
4157
4158 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4159 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004160 if (chan == NULL)
4161 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004162
4163 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004164 if (!msg)
4165 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004166
4167 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4168 NL80211_CMD_REMAIN_ON_CHANNEL);
4169
4170 if (IS_ERR(hdr)) {
4171 err = PTR_ERR(hdr);
4172 goto free_msg;
4173 }
4174
4175 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4176 channel_type, duration, &cookie);
4177
4178 if (err)
4179 goto free_msg;
4180
4181 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4182
4183 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004184
4185 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004186
4187 nla_put_failure:
4188 err = -ENOBUFS;
4189 free_msg:
4190 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004191 return err;
4192}
4193
4194static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4195 struct genl_info *info)
4196{
Johannes Berg4c476992010-10-04 21:36:35 +02004197 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4198 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004199 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004200
4201 if (!info->attrs[NL80211_ATTR_COOKIE])
4202 return -EINVAL;
4203
Johannes Berg4c476992010-10-04 21:36:35 +02004204 if (!rdev->ops->cancel_remain_on_channel)
4205 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004206
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004207 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4208
Johannes Berg4c476992010-10-04 21:36:35 +02004209 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004210}
4211
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004212static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4213 u8 *rates, u8 rates_len)
4214{
4215 u8 i;
4216 u32 mask = 0;
4217
4218 for (i = 0; i < rates_len; i++) {
4219 int rate = (rates[i] & 0x7f) * 5;
4220 int ridx;
4221 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4222 struct ieee80211_rate *srate =
4223 &sband->bitrates[ridx];
4224 if (rate == srate->bitrate) {
4225 mask |= 1 << ridx;
4226 break;
4227 }
4228 }
4229 if (ridx == sband->n_bitrates)
4230 return 0; /* rate not found */
4231 }
4232
4233 return mask;
4234}
4235
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004236static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004237 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4238 .len = NL80211_MAX_SUPP_RATES },
4239};
4240
4241static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4242 struct genl_info *info)
4243{
4244 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004245 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004246 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004247 int rem, i;
4248 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004249 struct nlattr *tx_rates;
4250 struct ieee80211_supported_band *sband;
4251
4252 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4253 return -EINVAL;
4254
Johannes Berg4c476992010-10-04 21:36:35 +02004255 if (!rdev->ops->set_bitrate_mask)
4256 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004257
4258 memset(&mask, 0, sizeof(mask));
4259 /* Default to all rates enabled */
4260 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
4261 sband = rdev->wiphy.bands[i];
4262 mask.control[i].legacy =
4263 sband ? (1 << sband->n_bitrates) - 1 : 0;
4264 }
4265
4266 /*
4267 * The nested attribute uses enum nl80211_band as the index. This maps
4268 * directly to the enum ieee80211_band values used in cfg80211.
4269 */
4270 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
4271 {
4272 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02004273 if (band < 0 || band >= IEEE80211_NUM_BANDS)
4274 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004275 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02004276 if (sband == NULL)
4277 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004278 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
4279 nla_len(tx_rates), nl80211_txattr_policy);
4280 if (tb[NL80211_TXRATE_LEGACY]) {
4281 mask.control[band].legacy = rateset_to_mask(
4282 sband,
4283 nla_data(tb[NL80211_TXRATE_LEGACY]),
4284 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02004285 if (mask.control[band].legacy == 0)
4286 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004287 }
4288 }
4289
Johannes Berg4c476992010-10-04 21:36:35 +02004290 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004291}
4292
Johannes Berg2e161f72010-08-12 15:38:38 +02004293static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004294{
Johannes Berg4c476992010-10-04 21:36:35 +02004295 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4296 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02004297 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02004298
4299 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
4300 return -EINVAL;
4301
Johannes Berg2e161f72010-08-12 15:38:38 +02004302 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
4303 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02004304
Johannes Berg9d38d852010-06-09 17:20:33 +02004305 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004306 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004307 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4308 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4309 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004310 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4311 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004312
4313 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02004314 if (!rdev->ops->mgmt_tx)
4315 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004316
Johannes Berg4c476992010-10-04 21:36:35 +02004317 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02004318 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02004319 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
4320 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02004321}
4322
Johannes Berg2e161f72010-08-12 15:38:38 +02004323static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02004324{
Johannes Berg4c476992010-10-04 21:36:35 +02004325 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4326 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02004327 struct ieee80211_channel *chan;
4328 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02004329 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02004330 u32 freq;
4331 int err;
4332 void *hdr;
4333 u64 cookie;
4334 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004335 unsigned int wait = 0;
4336 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02004337
4338 if (!info->attrs[NL80211_ATTR_FRAME] ||
4339 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
4340 return -EINVAL;
4341
Johannes Berg4c476992010-10-04 21:36:35 +02004342 if (!rdev->ops->mgmt_tx)
4343 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004344
Johannes Berg9d38d852010-06-09 17:20:33 +02004345 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02004346 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02004347 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4348 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4349 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg4c476992010-10-04 21:36:35 +02004350 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4351 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02004352
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004353 if (info->attrs[NL80211_ATTR_DURATION]) {
4354 if (!rdev->ops->mgmt_tx_cancel_wait)
4355 return -EINVAL;
4356 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4357 }
4358
Jouni Malinen026331c2010-02-15 12:53:10 +02004359 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4360 channel_type = nla_get_u32(
4361 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4362 if (channel_type != NL80211_CHAN_NO_HT &&
4363 channel_type != NL80211_CHAN_HT20 &&
4364 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004365 channel_type != NL80211_CHAN_HT40MINUS)
4366 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02004367 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02004368 }
4369
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004370 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
4371
Jouni Malinen026331c2010-02-15 12:53:10 +02004372 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4373 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004374 if (chan == NULL)
4375 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02004376
4377 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004378 if (!msg)
4379 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02004380
4381 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02004382 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02004383
4384 if (IS_ERR(hdr)) {
4385 err = PTR_ERR(hdr);
4386 goto free_msg;
4387 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004388 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
4389 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02004390 nla_data(info->attrs[NL80211_ATTR_FRAME]),
4391 nla_len(info->attrs[NL80211_ATTR_FRAME]),
4392 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02004393 if (err)
4394 goto free_msg;
4395
4396 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4397
4398 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004399 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02004400
4401 nla_put_failure:
4402 err = -ENOBUFS;
4403 free_msg:
4404 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02004405 return err;
4406}
4407
Johannes Bergf7ca38d2010-11-25 10:02:29 +01004408static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
4409{
4410 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4411 struct net_device *dev = info->user_ptr[1];
4412 u64 cookie;
4413
4414 if (!info->attrs[NL80211_ATTR_COOKIE])
4415 return -EINVAL;
4416
4417 if (!rdev->ops->mgmt_tx_cancel_wait)
4418 return -EOPNOTSUPP;
4419
4420 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
4421 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
4422 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
4423 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
4424 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
4425 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
4426 return -EOPNOTSUPP;
4427
4428 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4429
4430 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
4431}
4432
Kalle Valoffb9eb32010-02-17 17:58:10 +02004433static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
4434{
Johannes Berg4c476992010-10-04 21:36:35 +02004435 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004436 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004437 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004438 u8 ps_state;
4439 bool state;
4440 int err;
4441
Johannes Berg4c476992010-10-04 21:36:35 +02004442 if (!info->attrs[NL80211_ATTR_PS_STATE])
4443 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004444
4445 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
4446
Johannes Berg4c476992010-10-04 21:36:35 +02004447 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
4448 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004449
4450 wdev = dev->ieee80211_ptr;
4451
Johannes Berg4c476992010-10-04 21:36:35 +02004452 if (!rdev->ops->set_power_mgmt)
4453 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004454
4455 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
4456
4457 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02004458 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004459
Johannes Berg4c476992010-10-04 21:36:35 +02004460 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
4461 wdev->ps_timeout);
4462 if (!err)
4463 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004464 return err;
4465}
4466
4467static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
4468{
Johannes Berg4c476992010-10-04 21:36:35 +02004469 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004470 enum nl80211_ps_state ps_state;
4471 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004472 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02004473 struct sk_buff *msg;
4474 void *hdr;
4475 int err;
4476
Kalle Valoffb9eb32010-02-17 17:58:10 +02004477 wdev = dev->ieee80211_ptr;
4478
Johannes Berg4c476992010-10-04 21:36:35 +02004479 if (!rdev->ops->set_power_mgmt)
4480 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004481
4482 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004483 if (!msg)
4484 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004485
4486 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4487 NL80211_CMD_GET_POWER_SAVE);
4488 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02004489 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02004490 goto free_msg;
4491 }
4492
4493 if (wdev->ps)
4494 ps_state = NL80211_PS_ENABLED;
4495 else
4496 ps_state = NL80211_PS_DISABLED;
4497
4498 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
4499
4500 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004501 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004502
Johannes Berg4c476992010-10-04 21:36:35 +02004503 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004504 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02004505 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02004506 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02004507 return err;
4508}
4509
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004510static struct nla_policy
4511nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
4512 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
4513 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
4514 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
4515};
4516
4517static int nl80211_set_cqm_rssi(struct genl_info *info,
4518 s32 threshold, u32 hysteresis)
4519{
Johannes Berg4c476992010-10-04 21:36:35 +02004520 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004521 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02004522 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004523
4524 if (threshold > 0)
4525 return -EINVAL;
4526
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004527 wdev = dev->ieee80211_ptr;
4528
Johannes Berg4c476992010-10-04 21:36:35 +02004529 if (!rdev->ops->set_cqm_rssi_config)
4530 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004531
Johannes Berg074ac8d2010-09-16 14:58:22 +02004532 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004533 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
4534 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004535
Johannes Berg4c476992010-10-04 21:36:35 +02004536 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
4537 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02004538}
4539
4540static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
4541{
4542 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
4543 struct nlattr *cqm;
4544 int err;
4545
4546 cqm = info->attrs[NL80211_ATTR_CQM];
4547 if (!cqm) {
4548 err = -EINVAL;
4549 goto out;
4550 }
4551
4552 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
4553 nl80211_attr_cqm_policy);
4554 if (err)
4555 goto out;
4556
4557 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
4558 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
4559 s32 threshold;
4560 u32 hysteresis;
4561 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
4562 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
4563 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
4564 } else
4565 err = -EINVAL;
4566
4567out:
4568 return err;
4569}
4570
Johannes Berg29cbe682010-12-03 09:20:44 +01004571static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
4572{
4573 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4574 struct net_device *dev = info->user_ptr[1];
4575 struct mesh_config cfg;
4576 int err;
4577
4578 /* start with default */
4579 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
4580
4581 if (info->attrs[NL80211_ATTR_MESH_PARAMS]) {
4582 /* and parse parameters if given */
4583 err = nl80211_parse_mesh_params(info, &cfg, NULL);
4584 if (err)
4585 return err;
4586 }
4587
4588 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
4589 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
4590 return -EINVAL;
4591
4592 return cfg80211_join_mesh(rdev, dev,
4593 nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
4594 nla_len(info->attrs[NL80211_ATTR_MESH_ID]),
4595 &cfg);
4596}
4597
4598static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
4599{
4600 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4601 struct net_device *dev = info->user_ptr[1];
4602
4603 return cfg80211_leave_mesh(rdev, dev);
4604}
4605
Johannes Berg4c476992010-10-04 21:36:35 +02004606#define NL80211_FLAG_NEED_WIPHY 0x01
4607#define NL80211_FLAG_NEED_NETDEV 0x02
4608#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02004609#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
4610#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
4611 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02004612
4613static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
4614 struct genl_info *info)
4615{
4616 struct cfg80211_registered_device *rdev;
4617 struct net_device *dev;
4618 int err;
4619 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
4620
4621 if (rtnl)
4622 rtnl_lock();
4623
4624 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
4625 rdev = cfg80211_get_dev_from_info(info);
4626 if (IS_ERR(rdev)) {
4627 if (rtnl)
4628 rtnl_unlock();
4629 return PTR_ERR(rdev);
4630 }
4631 info->user_ptr[0] = rdev;
4632 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
4633 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
4634 if (err) {
4635 if (rtnl)
4636 rtnl_unlock();
4637 return err;
4638 }
Johannes Berg41265712010-10-04 21:14:05 +02004639 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
4640 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02004641 cfg80211_unlock_rdev(rdev);
4642 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02004643 if (rtnl)
4644 rtnl_unlock();
4645 return -ENETDOWN;
4646 }
Johannes Berg4c476992010-10-04 21:36:35 +02004647 info->user_ptr[0] = rdev;
4648 info->user_ptr[1] = dev;
4649 }
4650
4651 return 0;
4652}
4653
4654static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
4655 struct genl_info *info)
4656{
4657 if (info->user_ptr[0])
4658 cfg80211_unlock_rdev(info->user_ptr[0]);
4659 if (info->user_ptr[1])
4660 dev_put(info->user_ptr[1]);
4661 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
4662 rtnl_unlock();
4663}
4664
Johannes Berg55682962007-09-20 13:09:35 -04004665static struct genl_ops nl80211_ops[] = {
4666 {
4667 .cmd = NL80211_CMD_GET_WIPHY,
4668 .doit = nl80211_get_wiphy,
4669 .dumpit = nl80211_dump_wiphy,
4670 .policy = nl80211_policy,
4671 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004672 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04004673 },
4674 {
4675 .cmd = NL80211_CMD_SET_WIPHY,
4676 .doit = nl80211_set_wiphy,
4677 .policy = nl80211_policy,
4678 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004679 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004680 },
4681 {
4682 .cmd = NL80211_CMD_GET_INTERFACE,
4683 .doit = nl80211_get_interface,
4684 .dumpit = nl80211_dump_interface,
4685 .policy = nl80211_policy,
4686 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004687 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04004688 },
4689 {
4690 .cmd = NL80211_CMD_SET_INTERFACE,
4691 .doit = nl80211_set_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 },
4697 {
4698 .cmd = NL80211_CMD_NEW_INTERFACE,
4699 .doit = nl80211_new_interface,
4700 .policy = nl80211_policy,
4701 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004702 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4703 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004704 },
4705 {
4706 .cmd = NL80211_CMD_DEL_INTERFACE,
4707 .doit = nl80211_del_interface,
4708 .policy = nl80211_policy,
4709 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004710 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4711 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04004712 },
Johannes Berg41ade002007-12-19 02:03:29 +01004713 {
4714 .cmd = NL80211_CMD_GET_KEY,
4715 .doit = nl80211_get_key,
4716 .policy = nl80211_policy,
4717 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004718 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4719 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004720 },
4721 {
4722 .cmd = NL80211_CMD_SET_KEY,
4723 .doit = nl80211_set_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 },
4729 {
4730 .cmd = NL80211_CMD_NEW_KEY,
4731 .doit = nl80211_new_key,
4732 .policy = nl80211_policy,
4733 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004734 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004735 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004736 },
4737 {
4738 .cmd = NL80211_CMD_DEL_KEY,
4739 .doit = nl80211_del_key,
4740 .policy = nl80211_policy,
4741 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004742 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004743 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01004744 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01004745 {
4746 .cmd = NL80211_CMD_SET_BEACON,
4747 .policy = nl80211_policy,
4748 .flags = GENL_ADMIN_PERM,
4749 .doit = nl80211_addset_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 },
4753 {
4754 .cmd = NL80211_CMD_NEW_BEACON,
4755 .policy = nl80211_policy,
4756 .flags = GENL_ADMIN_PERM,
4757 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004758 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4759 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004760 },
4761 {
4762 .cmd = NL80211_CMD_DEL_BEACON,
4763 .policy = nl80211_policy,
4764 .flags = GENL_ADMIN_PERM,
4765 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02004766 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4767 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01004768 },
Johannes Berg5727ef12007-12-19 02:03:34 +01004769 {
4770 .cmd = NL80211_CMD_GET_STATION,
4771 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004772 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01004773 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02004774 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4775 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004776 },
4777 {
4778 .cmd = NL80211_CMD_SET_STATION,
4779 .doit = nl80211_set_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 },
4785 {
4786 .cmd = NL80211_CMD_NEW_STATION,
4787 .doit = nl80211_new_station,
4788 .policy = nl80211_policy,
4789 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004790 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004791 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004792 },
4793 {
4794 .cmd = NL80211_CMD_DEL_STATION,
4795 .doit = nl80211_del_station,
4796 .policy = nl80211_policy,
4797 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004798 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4799 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01004800 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004801 {
4802 .cmd = NL80211_CMD_GET_MPATH,
4803 .doit = nl80211_get_mpath,
4804 .dumpit = nl80211_dump_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_SET_MPATH,
4812 .doit = nl80211_set_mpath,
4813 .policy = nl80211_policy,
4814 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004815 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004816 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004817 },
4818 {
4819 .cmd = NL80211_CMD_NEW_MPATH,
4820 .doit = nl80211_new_mpath,
4821 .policy = nl80211_policy,
4822 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004823 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004824 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004825 },
4826 {
4827 .cmd = NL80211_CMD_DEL_MPATH,
4828 .doit = nl80211_del_mpath,
4829 .policy = nl80211_policy,
4830 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004831 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4832 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01004833 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004834 {
4835 .cmd = NL80211_CMD_SET_BSS,
4836 .doit = nl80211_set_bss,
4837 .policy = nl80211_policy,
4838 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004839 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4840 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03004841 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004842 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08004843 .cmd = NL80211_CMD_GET_REG,
4844 .doit = nl80211_get_reg,
4845 .policy = nl80211_policy,
4846 /* can be retrieved by unprivileged users */
4847 },
4848 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07004849 .cmd = NL80211_CMD_SET_REG,
4850 .doit = nl80211_set_reg,
4851 .policy = nl80211_policy,
4852 .flags = GENL_ADMIN_PERM,
4853 },
4854 {
4855 .cmd = NL80211_CMD_REQ_SET_REG,
4856 .doit = nl80211_req_set_reg,
4857 .policy = nl80211_policy,
4858 .flags = GENL_ADMIN_PERM,
4859 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004860 {
4861 .cmd = NL80211_CMD_GET_MESH_PARAMS,
4862 .doit = nl80211_get_mesh_params,
4863 .policy = nl80211_policy,
4864 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02004865 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4866 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004867 },
4868 {
4869 .cmd = NL80211_CMD_SET_MESH_PARAMS,
Johannes Berg29cbe682010-12-03 09:20:44 +01004870 .doit = nl80211_update_mesh_params,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004871 .policy = nl80211_policy,
4872 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01004873 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004874 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07004875 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02004876 {
Johannes Berg2a519312009-02-10 21:25:55 +01004877 .cmd = NL80211_CMD_TRIGGER_SCAN,
4878 .doit = nl80211_trigger_scan,
4879 .policy = nl80211_policy,
4880 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004881 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004882 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01004883 },
4884 {
4885 .cmd = NL80211_CMD_GET_SCAN,
4886 .policy = nl80211_policy,
4887 .dumpit = nl80211_dump_scan,
4888 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02004889 {
4890 .cmd = NL80211_CMD_AUTHENTICATE,
4891 .doit = nl80211_authenticate,
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_ASSOCIATE,
4899 .doit = nl80211_associate,
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 },
4905 {
4906 .cmd = NL80211_CMD_DEAUTHENTICATE,
4907 .doit = nl80211_deauthenticate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004912 },
4913 {
4914 .cmd = NL80211_CMD_DISASSOCIATE,
4915 .doit = nl80211_disassociate,
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,
Jouni Malinen636a5d32009-03-19 13:39:22 +02004920 },
Johannes Berg04a773a2009-04-19 21:24:32 +02004921 {
4922 .cmd = NL80211_CMD_JOIN_IBSS,
4923 .doit = nl80211_join_ibss,
4924 .policy = nl80211_policy,
4925 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004926 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004927 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004928 },
4929 {
4930 .cmd = NL80211_CMD_LEAVE_IBSS,
4931 .doit = nl80211_leave_ibss,
4932 .policy = nl80211_policy,
4933 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004934 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004935 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02004936 },
Johannes Bergaff89a92009-07-01 21:26:51 +02004937#ifdef CONFIG_NL80211_TESTMODE
4938 {
4939 .cmd = NL80211_CMD_TESTMODE,
4940 .doit = nl80211_testmode_do,
4941 .policy = nl80211_policy,
4942 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004943 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4944 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02004945 },
4946#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02004947 {
4948 .cmd = NL80211_CMD_CONNECT,
4949 .doit = nl80211_connect,
4950 .policy = nl80211_policy,
4951 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004952 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004953 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004954 },
4955 {
4956 .cmd = NL80211_CMD_DISCONNECT,
4957 .doit = nl80211_disconnect,
4958 .policy = nl80211_policy,
4959 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02004960 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02004961 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02004962 },
Johannes Berg463d0182009-07-14 00:33:35 +02004963 {
4964 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
4965 .doit = nl80211_wiphy_netns,
4966 .policy = nl80211_policy,
4967 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004968 .internal_flags = NL80211_FLAG_NEED_WIPHY |
4969 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02004970 },
Holger Schurig61fa7132009-11-11 12:25:40 +01004971 {
4972 .cmd = NL80211_CMD_GET_SURVEY,
4973 .policy = nl80211_policy,
4974 .dumpit = nl80211_dump_survey,
4975 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004976 {
4977 .cmd = NL80211_CMD_SET_PMKSA,
4978 .doit = nl80211_setdel_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 },
4984 {
4985 .cmd = NL80211_CMD_DEL_PMKSA,
4986 .doit = nl80211_setdel_pmksa,
4987 .policy = nl80211_policy,
4988 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004989 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4990 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004991 },
4992 {
4993 .cmd = NL80211_CMD_FLUSH_PMKSA,
4994 .doit = nl80211_flush_pmksa,
4995 .policy = nl80211_policy,
4996 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02004997 .internal_flags = NL80211_FLAG_NEED_NETDEV |
4998 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004999 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005000 {
5001 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
5002 .doit = nl80211_remain_on_channel,
5003 .policy = nl80211_policy,
5004 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005005 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005006 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005007 },
5008 {
5009 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5010 .doit = nl80211_cancel_remain_on_channel,
5011 .policy = nl80211_policy,
5012 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005013 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005014 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005015 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005016 {
5017 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
5018 .doit = nl80211_set_tx_bitrate_mask,
5019 .policy = nl80211_policy,
5020 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005021 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5022 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005023 },
Jouni Malinen026331c2010-02-15 12:53:10 +02005024 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005025 .cmd = NL80211_CMD_REGISTER_FRAME,
5026 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005027 .policy = nl80211_policy,
5028 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005029 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5030 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005031 },
5032 {
Johannes Berg2e161f72010-08-12 15:38:38 +02005033 .cmd = NL80211_CMD_FRAME,
5034 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02005035 .policy = nl80211_policy,
5036 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005037 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005038 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02005039 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02005040 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005041 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
5042 .doit = nl80211_tx_mgmt_cancel_wait,
5043 .policy = nl80211_policy,
5044 .flags = GENL_ADMIN_PERM,
5045 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5046 NL80211_FLAG_NEED_RTNL,
5047 },
5048 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02005049 .cmd = NL80211_CMD_SET_POWER_SAVE,
5050 .doit = nl80211_set_power_save,
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,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005055 },
5056 {
5057 .cmd = NL80211_CMD_GET_POWER_SAVE,
5058 .doit = nl80211_get_power_save,
5059 .policy = nl80211_policy,
5060 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005061 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5062 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02005063 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005064 {
5065 .cmd = NL80211_CMD_SET_CQM,
5066 .doit = nl80211_set_cqm,
5067 .policy = nl80211_policy,
5068 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005069 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5070 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005071 },
Johannes Bergf444de02010-05-05 15:25:02 +02005072 {
5073 .cmd = NL80211_CMD_SET_CHANNEL,
5074 .doit = nl80211_set_channel,
5075 .policy = nl80211_policy,
5076 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005077 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5078 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02005079 },
Bill Jordane8347eb2010-10-01 13:54:28 -04005080 {
5081 .cmd = NL80211_CMD_SET_WDS_PEER,
5082 .doit = nl80211_set_wds_peer,
5083 .policy = nl80211_policy,
5084 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02005085 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5086 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04005087 },
Johannes Berg29cbe682010-12-03 09:20:44 +01005088 {
5089 .cmd = NL80211_CMD_JOIN_MESH,
5090 .doit = nl80211_join_mesh,
5091 .policy = nl80211_policy,
5092 .flags = GENL_ADMIN_PERM,
5093 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5094 NL80211_FLAG_NEED_RTNL,
5095 },
5096 {
5097 .cmd = NL80211_CMD_LEAVE_MESH,
5098 .doit = nl80211_leave_mesh,
5099 .policy = nl80211_policy,
5100 .flags = GENL_ADMIN_PERM,
5101 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5102 NL80211_FLAG_NEED_RTNL,
5103 },
Johannes Berg55682962007-09-20 13:09:35 -04005104};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005105
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005106static struct genl_multicast_group nl80211_mlme_mcgrp = {
5107 .name = "mlme",
5108};
Johannes Berg55682962007-09-20 13:09:35 -04005109
5110/* multicast groups */
5111static struct genl_multicast_group nl80211_config_mcgrp = {
5112 .name = "config",
5113};
Johannes Berg2a519312009-02-10 21:25:55 +01005114static struct genl_multicast_group nl80211_scan_mcgrp = {
5115 .name = "scan",
5116};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005117static struct genl_multicast_group nl80211_regulatory_mcgrp = {
5118 .name = "regulatory",
5119};
Johannes Berg55682962007-09-20 13:09:35 -04005120
5121/* notification functions */
5122
5123void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
5124{
5125 struct sk_buff *msg;
5126
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005127 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005128 if (!msg)
5129 return;
5130
5131 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
5132 nlmsg_free(msg);
5133 return;
5134 }
5135
Johannes Berg463d0182009-07-14 00:33:35 +02005136 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5137 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04005138}
5139
Johannes Berg362a4152009-05-24 16:43:15 +02005140static int nl80211_add_scan_req(struct sk_buff *msg,
5141 struct cfg80211_registered_device *rdev)
5142{
5143 struct cfg80211_scan_request *req = rdev->scan_req;
5144 struct nlattr *nest;
5145 int i;
5146
Johannes Berg667503d2009-07-07 03:56:11 +02005147 ASSERT_RDEV_LOCK(rdev);
5148
Johannes Berg362a4152009-05-24 16:43:15 +02005149 if (WARN_ON(!req))
5150 return 0;
5151
5152 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
5153 if (!nest)
5154 goto nla_put_failure;
5155 for (i = 0; i < req->n_ssids; i++)
5156 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
5157 nla_nest_end(msg, nest);
5158
5159 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
5160 if (!nest)
5161 goto nla_put_failure;
5162 for (i = 0; i < req->n_channels; i++)
5163 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
5164 nla_nest_end(msg, nest);
5165
5166 if (req->ie)
5167 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
5168
5169 return 0;
5170 nla_put_failure:
5171 return -ENOBUFS;
5172}
5173
Johannes Berga538e2d2009-06-16 19:56:42 +02005174static int nl80211_send_scan_msg(struct sk_buff *msg,
5175 struct cfg80211_registered_device *rdev,
5176 struct net_device *netdev,
5177 u32 pid, u32 seq, int flags,
5178 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01005179{
5180 void *hdr;
5181
5182 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
5183 if (!hdr)
5184 return -1;
5185
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05005186 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01005187 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5188
Johannes Berg362a4152009-05-24 16:43:15 +02005189 /* ignore errors and send incomplete event anyway */
5190 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01005191
5192 return genlmsg_end(msg, hdr);
5193
5194 nla_put_failure:
5195 genlmsg_cancel(msg, hdr);
5196 return -EMSGSIZE;
5197}
5198
Johannes Berga538e2d2009-06-16 19:56:42 +02005199void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
5200 struct net_device *netdev)
5201{
5202 struct sk_buff *msg;
5203
5204 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
5205 if (!msg)
5206 return;
5207
5208 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5209 NL80211_CMD_TRIGGER_SCAN) < 0) {
5210 nlmsg_free(msg);
5211 return;
5212 }
5213
Johannes Berg463d0182009-07-14 00:33:35 +02005214 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5215 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02005216}
5217
Johannes Berg2a519312009-02-10 21:25:55 +01005218void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
5219 struct net_device *netdev)
5220{
5221 struct sk_buff *msg;
5222
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005223 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005224 if (!msg)
5225 return;
5226
Johannes Berga538e2d2009-06-16 19:56:42 +02005227 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5228 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005229 nlmsg_free(msg);
5230 return;
5231 }
5232
Johannes Berg463d0182009-07-14 00:33:35 +02005233 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5234 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005235}
5236
5237void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
5238 struct net_device *netdev)
5239{
5240 struct sk_buff *msg;
5241
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005242 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005243 if (!msg)
5244 return;
5245
Johannes Berga538e2d2009-06-16 19:56:42 +02005246 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
5247 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01005248 nlmsg_free(msg);
5249 return;
5250 }
5251
Johannes Berg463d0182009-07-14 00:33:35 +02005252 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5253 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01005254}
5255
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005256/*
5257 * This can happen on global regulatory changes or device specific settings
5258 * based on custom world regulatory domains.
5259 */
5260void nl80211_send_reg_change_event(struct regulatory_request *request)
5261{
5262 struct sk_buff *msg;
5263 void *hdr;
5264
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005265 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005266 if (!msg)
5267 return;
5268
5269 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
5270 if (!hdr) {
5271 nlmsg_free(msg);
5272 return;
5273 }
5274
5275 /* Userspace can always count this one always being set */
5276 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
5277
5278 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
5279 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5280 NL80211_REGDOM_TYPE_WORLD);
5281 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
5282 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5283 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
5284 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
5285 request->intersect)
5286 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5287 NL80211_REGDOM_TYPE_INTERSECTION);
5288 else {
5289 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
5290 NL80211_REGDOM_TYPE_COUNTRY);
5291 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
5292 }
5293
5294 if (wiphy_idx_valid(request->wiphy_idx))
5295 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
5296
5297 if (genlmsg_end(msg, hdr) < 0) {
5298 nlmsg_free(msg);
5299 return;
5300 }
5301
Johannes Bergbc43b282009-07-25 10:54:13 +02005302 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02005303 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02005304 GFP_ATOMIC);
5305 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005306
5307 return;
5308
5309nla_put_failure:
5310 genlmsg_cancel(msg, hdr);
5311 nlmsg_free(msg);
5312}
5313
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005314static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
5315 struct net_device *netdev,
5316 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005317 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005318{
5319 struct sk_buff *msg;
5320 void *hdr;
5321
Johannes Berge6d6e342009-07-01 21:26:47 +02005322 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005323 if (!msg)
5324 return;
5325
5326 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5327 if (!hdr) {
5328 nlmsg_free(msg);
5329 return;
5330 }
5331
5332 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5333 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5334 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5335
5336 if (genlmsg_end(msg, hdr) < 0) {
5337 nlmsg_free(msg);
5338 return;
5339 }
5340
Johannes Berg463d0182009-07-14 00:33:35 +02005341 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5342 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005343 return;
5344
5345 nla_put_failure:
5346 genlmsg_cancel(msg, hdr);
5347 nlmsg_free(msg);
5348}
5349
5350void nl80211_send_rx_auth(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_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005356}
5357
5358void nl80211_send_rx_assoc(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{
Johannes Berge6d6e342009-07-01 21:26:47 +02005362 nl80211_send_mlme_event(rdev, netdev, buf, len,
5363 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005364}
5365
Jouni Malinen53b46b82009-03-27 20:53:56 +02005366void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005367 struct net_device *netdev, const u8 *buf,
5368 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005369{
5370 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005371 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005372}
5373
Jouni Malinen53b46b82009-03-27 20:53:56 +02005374void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
5375 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02005376 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005377{
5378 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02005379 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005380}
5381
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04005382static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
5383 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02005384 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005385{
5386 struct sk_buff *msg;
5387 void *hdr;
5388
Johannes Berge6d6e342009-07-01 21:26:47 +02005389 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005390 if (!msg)
5391 return;
5392
5393 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5394 if (!hdr) {
5395 nlmsg_free(msg);
5396 return;
5397 }
5398
5399 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5400 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5401 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
5402 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5403
5404 if (genlmsg_end(msg, hdr) < 0) {
5405 nlmsg_free(msg);
5406 return;
5407 }
5408
Johannes Berg463d0182009-07-14 00:33:35 +02005409 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5410 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005411 return;
5412
5413 nla_put_failure:
5414 genlmsg_cancel(msg, hdr);
5415 nlmsg_free(msg);
5416}
5417
5418void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005419 struct net_device *netdev, const u8 *addr,
5420 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005421{
5422 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02005423 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005424}
5425
5426void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02005427 struct net_device *netdev, const u8 *addr,
5428 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03005429{
Johannes Berge6d6e342009-07-01 21:26:47 +02005430 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
5431 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03005432}
5433
Samuel Ortizb23aa672009-07-01 21:26:54 +02005434void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
5435 struct net_device *netdev, const u8 *bssid,
5436 const u8 *req_ie, size_t req_ie_len,
5437 const u8 *resp_ie, size_t resp_ie_len,
5438 u16 status, gfp_t gfp)
5439{
5440 struct sk_buff *msg;
5441 void *hdr;
5442
5443 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5444 if (!msg)
5445 return;
5446
5447 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
5448 if (!hdr) {
5449 nlmsg_free(msg);
5450 return;
5451 }
5452
5453 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5454 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5455 if (bssid)
5456 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5457 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
5458 if (req_ie)
5459 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5460 if (resp_ie)
5461 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5462
5463 if (genlmsg_end(msg, hdr) < 0) {
5464 nlmsg_free(msg);
5465 return;
5466 }
5467
Johannes Berg463d0182009-07-14 00:33:35 +02005468 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5469 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005470 return;
5471
5472 nla_put_failure:
5473 genlmsg_cancel(msg, hdr);
5474 nlmsg_free(msg);
5475
5476}
5477
5478void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
5479 struct net_device *netdev, const u8 *bssid,
5480 const u8 *req_ie, size_t req_ie_len,
5481 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
5482{
5483 struct sk_buff *msg;
5484 void *hdr;
5485
5486 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5487 if (!msg)
5488 return;
5489
5490 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
5491 if (!hdr) {
5492 nlmsg_free(msg);
5493 return;
5494 }
5495
5496 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5497 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5498 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5499 if (req_ie)
5500 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
5501 if (resp_ie)
5502 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
5503
5504 if (genlmsg_end(msg, hdr) < 0) {
5505 nlmsg_free(msg);
5506 return;
5507 }
5508
Johannes Berg463d0182009-07-14 00:33:35 +02005509 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5510 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005511 return;
5512
5513 nla_put_failure:
5514 genlmsg_cancel(msg, hdr);
5515 nlmsg_free(msg);
5516
5517}
5518
5519void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
5520 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02005521 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02005522{
5523 struct sk_buff *msg;
5524 void *hdr;
5525
Johannes Berg667503d2009-07-07 03:56:11 +02005526 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005527 if (!msg)
5528 return;
5529
5530 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
5531 if (!hdr) {
5532 nlmsg_free(msg);
5533 return;
5534 }
5535
5536 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5537 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5538 if (from_ap && reason)
5539 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
5540 if (from_ap)
5541 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
5542 if (ie)
5543 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
5544
5545 if (genlmsg_end(msg, hdr) < 0) {
5546 nlmsg_free(msg);
5547 return;
5548 }
5549
Johannes Berg463d0182009-07-14 00:33:35 +02005550 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5551 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02005552 return;
5553
5554 nla_put_failure:
5555 genlmsg_cancel(msg, hdr);
5556 nlmsg_free(msg);
5557
5558}
5559
Johannes Berg04a773a2009-04-19 21:24:32 +02005560void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
5561 struct net_device *netdev, const u8 *bssid,
5562 gfp_t gfp)
5563{
5564 struct sk_buff *msg;
5565 void *hdr;
5566
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005567 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005568 if (!msg)
5569 return;
5570
5571 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
5572 if (!hdr) {
5573 nlmsg_free(msg);
5574 return;
5575 }
5576
5577 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5578 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5579 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
5580
5581 if (genlmsg_end(msg, hdr) < 0) {
5582 nlmsg_free(msg);
5583 return;
5584 }
5585
Johannes Berg463d0182009-07-14 00:33:35 +02005586 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5587 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02005588 return;
5589
5590 nla_put_failure:
5591 genlmsg_cancel(msg, hdr);
5592 nlmsg_free(msg);
5593}
5594
Jouni Malinena3b8b052009-03-27 21:59:49 +02005595void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
5596 struct net_device *netdev, const u8 *addr,
5597 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02005598 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02005599{
5600 struct sk_buff *msg;
5601 void *hdr;
5602
Johannes Berge6d6e342009-07-01 21:26:47 +02005603 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005604 if (!msg)
5605 return;
5606
5607 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
5608 if (!hdr) {
5609 nlmsg_free(msg);
5610 return;
5611 }
5612
5613 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5614 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5615 if (addr)
5616 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
5617 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
5618 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
5619 if (tsc)
5620 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
5621
5622 if (genlmsg_end(msg, hdr) < 0) {
5623 nlmsg_free(msg);
5624 return;
5625 }
5626
Johannes Berg463d0182009-07-14 00:33:35 +02005627 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5628 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02005629 return;
5630
5631 nla_put_failure:
5632 genlmsg_cancel(msg, hdr);
5633 nlmsg_free(msg);
5634}
5635
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005636void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
5637 struct ieee80211_channel *channel_before,
5638 struct ieee80211_channel *channel_after)
5639{
5640 struct sk_buff *msg;
5641 void *hdr;
5642 struct nlattr *nl_freq;
5643
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07005644 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005645 if (!msg)
5646 return;
5647
5648 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
5649 if (!hdr) {
5650 nlmsg_free(msg);
5651 return;
5652 }
5653
5654 /*
5655 * Since we are applying the beacon hint to a wiphy we know its
5656 * wiphy_idx is valid
5657 */
5658 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
5659
5660 /* Before */
5661 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
5662 if (!nl_freq)
5663 goto nla_put_failure;
5664 if (nl80211_msg_put_channel(msg, channel_before))
5665 goto nla_put_failure;
5666 nla_nest_end(msg, nl_freq);
5667
5668 /* After */
5669 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
5670 if (!nl_freq)
5671 goto nla_put_failure;
5672 if (nl80211_msg_put_channel(msg, channel_after))
5673 goto nla_put_failure;
5674 nla_nest_end(msg, nl_freq);
5675
5676 if (genlmsg_end(msg, hdr) < 0) {
5677 nlmsg_free(msg);
5678 return;
5679 }
5680
Johannes Berg463d0182009-07-14 00:33:35 +02005681 rcu_read_lock();
5682 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
5683 GFP_ATOMIC);
5684 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04005685
5686 return;
5687
5688nla_put_failure:
5689 genlmsg_cancel(msg, hdr);
5690 nlmsg_free(msg);
5691}
5692
Jouni Malinen9588bbd2009-12-23 13:15:41 +01005693static void nl80211_send_remain_on_chan_event(
5694 int cmd, struct cfg80211_registered_device *rdev,
5695 struct net_device *netdev, u64 cookie,
5696 struct ieee80211_channel *chan,
5697 enum nl80211_channel_type channel_type,
5698 unsigned int duration, gfp_t gfp)
5699{
5700 struct sk_buff *msg;
5701 void *hdr;
5702
5703 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5704 if (!msg)
5705 return;
5706
5707 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
5708 if (!hdr) {
5709 nlmsg_free(msg);
5710 return;
5711 }
5712
5713 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5714 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5715 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
5716 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
5717 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5718
5719 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
5720 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
5721
5722 if (genlmsg_end(msg, hdr) < 0) {
5723 nlmsg_free(msg);
5724 return;
5725 }
5726
5727 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5728 nl80211_mlme_mcgrp.id, gfp);
5729 return;
5730
5731 nla_put_failure:
5732 genlmsg_cancel(msg, hdr);
5733 nlmsg_free(msg);
5734}
5735
5736void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
5737 struct net_device *netdev, u64 cookie,
5738 struct ieee80211_channel *chan,
5739 enum nl80211_channel_type channel_type,
5740 unsigned int duration, gfp_t gfp)
5741{
5742 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
5743 rdev, netdev, cookie, chan,
5744 channel_type, duration, gfp);
5745}
5746
5747void nl80211_send_remain_on_channel_cancel(
5748 struct cfg80211_registered_device *rdev, struct net_device *netdev,
5749 u64 cookie, struct ieee80211_channel *chan,
5750 enum nl80211_channel_type channel_type, gfp_t gfp)
5751{
5752 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
5753 rdev, netdev, cookie, chan,
5754 channel_type, 0, gfp);
5755}
5756
Johannes Berg98b62182009-12-23 13:15:44 +01005757void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
5758 struct net_device *dev, const u8 *mac_addr,
5759 struct station_info *sinfo, gfp_t gfp)
5760{
5761 struct sk_buff *msg;
5762
5763 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5764 if (!msg)
5765 return;
5766
5767 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
5768 nlmsg_free(msg);
5769 return;
5770 }
5771
5772 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5773 nl80211_mlme_mcgrp.id, gfp);
5774}
5775
Johannes Berg2e161f72010-08-12 15:38:38 +02005776int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
5777 struct net_device *netdev, u32 nlpid,
5778 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005779{
5780 struct sk_buff *msg;
5781 void *hdr;
5782 int err;
5783
5784 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5785 if (!msg)
5786 return -ENOMEM;
5787
Johannes Berg2e161f72010-08-12 15:38:38 +02005788 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005789 if (!hdr) {
5790 nlmsg_free(msg);
5791 return -ENOMEM;
5792 }
5793
5794 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5795 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5796 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
5797 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5798
5799 err = genlmsg_end(msg, hdr);
5800 if (err < 0) {
5801 nlmsg_free(msg);
5802 return err;
5803 }
5804
5805 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
5806 if (err < 0)
5807 return err;
5808 return 0;
5809
5810 nla_put_failure:
5811 genlmsg_cancel(msg, hdr);
5812 nlmsg_free(msg);
5813 return -ENOBUFS;
5814}
5815
Johannes Berg2e161f72010-08-12 15:38:38 +02005816void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
5817 struct net_device *netdev, u64 cookie,
5818 const u8 *buf, size_t len, bool ack,
5819 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02005820{
5821 struct sk_buff *msg;
5822 void *hdr;
5823
5824 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
5825 if (!msg)
5826 return;
5827
Johannes Berg2e161f72010-08-12 15:38:38 +02005828 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02005829 if (!hdr) {
5830 nlmsg_free(msg);
5831 return;
5832 }
5833
5834 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5835 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5836 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
5837 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5838 if (ack)
5839 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
5840
5841 if (genlmsg_end(msg, hdr) < 0) {
5842 nlmsg_free(msg);
5843 return;
5844 }
5845
5846 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
5847 return;
5848
5849 nla_put_failure:
5850 genlmsg_cancel(msg, hdr);
5851 nlmsg_free(msg);
5852}
5853
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005854void
5855nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
5856 struct net_device *netdev,
5857 enum nl80211_cqm_rssi_threshold_event rssi_event,
5858 gfp_t gfp)
5859{
5860 struct sk_buff *msg;
5861 struct nlattr *pinfoattr;
5862 void *hdr;
5863
5864 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5865 if (!msg)
5866 return;
5867
5868 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5869 if (!hdr) {
5870 nlmsg_free(msg);
5871 return;
5872 }
5873
5874 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5875 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5876
5877 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5878 if (!pinfoattr)
5879 goto nla_put_failure;
5880
5881 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
5882 rssi_event);
5883
5884 nla_nest_end(msg, pinfoattr);
5885
5886 if (genlmsg_end(msg, hdr) < 0) {
5887 nlmsg_free(msg);
5888 return;
5889 }
5890
5891 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5892 nl80211_mlme_mcgrp.id, gfp);
5893 return;
5894
5895 nla_put_failure:
5896 genlmsg_cancel(msg, hdr);
5897 nlmsg_free(msg);
5898}
5899
Johannes Bergc063dbf2010-11-24 08:10:05 +01005900void
5901nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
5902 struct net_device *netdev, const u8 *peer,
5903 u32 num_packets, gfp_t gfp)
5904{
5905 struct sk_buff *msg;
5906 struct nlattr *pinfoattr;
5907 void *hdr;
5908
5909 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
5910 if (!msg)
5911 return;
5912
5913 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
5914 if (!hdr) {
5915 nlmsg_free(msg);
5916 return;
5917 }
5918
5919 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
5920 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
5921 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
5922
5923 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
5924 if (!pinfoattr)
5925 goto nla_put_failure;
5926
5927 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
5928
5929 nla_nest_end(msg, pinfoattr);
5930
5931 if (genlmsg_end(msg, hdr) < 0) {
5932 nlmsg_free(msg);
5933 return;
5934 }
5935
5936 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
5937 nl80211_mlme_mcgrp.id, gfp);
5938 return;
5939
5940 nla_put_failure:
5941 genlmsg_cancel(msg, hdr);
5942 nlmsg_free(msg);
5943}
5944
Jouni Malinen026331c2010-02-15 12:53:10 +02005945static int nl80211_netlink_notify(struct notifier_block * nb,
5946 unsigned long state,
5947 void *_notify)
5948{
5949 struct netlink_notify *notify = _notify;
5950 struct cfg80211_registered_device *rdev;
5951 struct wireless_dev *wdev;
5952
5953 if (state != NETLINK_URELEASE)
5954 return NOTIFY_DONE;
5955
5956 rcu_read_lock();
5957
5958 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
5959 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02005960 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02005961
5962 rcu_read_unlock();
5963
5964 return NOTIFY_DONE;
5965}
5966
5967static struct notifier_block nl80211_netlink_notifier = {
5968 .notifier_call = nl80211_netlink_notify,
5969};
5970
Johannes Berg55682962007-09-20 13:09:35 -04005971/* initialisation/exit functions */
5972
5973int nl80211_init(void)
5974{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005975 int err;
Johannes Berg55682962007-09-20 13:09:35 -04005976
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00005977 err = genl_register_family_with_ops(&nl80211_fam,
5978 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04005979 if (err)
5980 return err;
5981
Johannes Berg55682962007-09-20 13:09:35 -04005982 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
5983 if (err)
5984 goto err_out;
5985
Johannes Berg2a519312009-02-10 21:25:55 +01005986 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
5987 if (err)
5988 goto err_out;
5989
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04005990 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
5991 if (err)
5992 goto err_out;
5993
Jouni Malinen6039f6d2009-03-19 13:39:21 +02005994 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
5995 if (err)
5996 goto err_out;
5997
Johannes Bergaff89a92009-07-01 21:26:51 +02005998#ifdef CONFIG_NL80211_TESTMODE
5999 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
6000 if (err)
6001 goto err_out;
6002#endif
6003
Jouni Malinen026331c2010-02-15 12:53:10 +02006004 err = netlink_register_notifier(&nl80211_netlink_notifier);
6005 if (err)
6006 goto err_out;
6007
Johannes Berg55682962007-09-20 13:09:35 -04006008 return 0;
6009 err_out:
6010 genl_unregister_family(&nl80211_fam);
6011 return err;
6012}
6013
6014void nl80211_exit(void)
6015{
Jouni Malinen026331c2010-02-15 12:53:10 +02006016 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04006017 genl_unregister_family(&nl80211_fam);
6018}