blob: 2aa6a2189842982b199272438951583f97ee23bd [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
Jouni Malinen5fb628e2011-08-10 23:54:35 +030026static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type);
27static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
28 struct genl_info *info,
29 struct cfg80211_crypto_settings *settings,
30 int cipher_limit);
31
Johannes Berg4c476992010-10-04 21:36:35 +020032static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
33 struct genl_info *info);
34static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
35 struct genl_info *info);
36
Johannes Berg55682962007-09-20 13:09:35 -040037/* the netlink family */
38static struct genl_family nl80211_fam = {
39 .id = GENL_ID_GENERATE, /* don't bother with a hardcoded ID */
40 .name = "nl80211", /* have users key off the name instead */
41 .hdrsize = 0, /* no private header */
42 .version = 1, /* no particular meaning now */
43 .maxattr = NL80211_ATTR_MAX,
Johannes Berg463d0182009-07-14 00:33:35 +020044 .netnsok = true,
Johannes Berg4c476992010-10-04 21:36:35 +020045 .pre_doit = nl80211_pre_doit,
46 .post_doit = nl80211_post_doit,
Johannes Berg55682962007-09-20 13:09:35 -040047};
48
Johannes Berg79c97e92009-07-07 03:56:12 +020049/* internal helper: get rdev and dev */
Johannes Berg463d0182009-07-14 00:33:35 +020050static int get_rdev_dev_by_info_ifindex(struct genl_info *info,
Johannes Berg79c97e92009-07-07 03:56:12 +020051 struct cfg80211_registered_device **rdev,
Johannes Berg55682962007-09-20 13:09:35 -040052 struct net_device **dev)
53{
Johannes Berg463d0182009-07-14 00:33:35 +020054 struct nlattr **attrs = info->attrs;
Johannes Berg55682962007-09-20 13:09:35 -040055 int ifindex;
56
Johannes Bergbba95fe2008-07-29 13:22:51 +020057 if (!attrs[NL80211_ATTR_IFINDEX])
Johannes Berg55682962007-09-20 13:09:35 -040058 return -EINVAL;
59
Johannes Bergbba95fe2008-07-29 13:22:51 +020060 ifindex = nla_get_u32(attrs[NL80211_ATTR_IFINDEX]);
Johannes Berg463d0182009-07-14 00:33:35 +020061 *dev = dev_get_by_index(genl_info_net(info), ifindex);
Johannes Berg55682962007-09-20 13:09:35 -040062 if (!*dev)
63 return -ENODEV;
64
Johannes Berg463d0182009-07-14 00:33:35 +020065 *rdev = cfg80211_get_dev_from_ifindex(genl_info_net(info), ifindex);
Johannes Berg79c97e92009-07-07 03:56:12 +020066 if (IS_ERR(*rdev)) {
Johannes Berg55682962007-09-20 13:09:35 -040067 dev_put(*dev);
Johannes Berg79c97e92009-07-07 03:56:12 +020068 return PTR_ERR(*rdev);
Johannes Berg55682962007-09-20 13:09:35 -040069 }
70
71 return 0;
72}
73
74/* policy for the attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +000075static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
Johannes Berg55682962007-09-20 13:09:35 -040076 [NL80211_ATTR_WIPHY] = { .type = NLA_U32 },
77 [NL80211_ATTR_WIPHY_NAME] = { .type = NLA_NUL_STRING,
David S. Miller079e24e2009-05-26 21:15:00 -070078 .len = 20-1 },
Jouni Malinen31888482008-10-30 16:59:24 +020079 [NL80211_ATTR_WIPHY_TXQ_PARAMS] = { .type = NLA_NESTED },
Jouni Malinen72bdcf32008-11-26 16:15:24 +020080 [NL80211_ATTR_WIPHY_FREQ] = { .type = NLA_U32 },
Sujith094d05d2008-12-12 11:57:43 +053081 [NL80211_ATTR_WIPHY_CHANNEL_TYPE] = { .type = NLA_U32 },
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +020082 [NL80211_ATTR_WIPHY_RETRY_SHORT] = { .type = NLA_U8 },
83 [NL80211_ATTR_WIPHY_RETRY_LONG] = { .type = NLA_U8 },
84 [NL80211_ATTR_WIPHY_FRAG_THRESHOLD] = { .type = NLA_U32 },
85 [NL80211_ATTR_WIPHY_RTS_THRESHOLD] = { .type = NLA_U32 },
Lukáš Turek81077e82009-12-21 22:50:47 +010086 [NL80211_ATTR_WIPHY_COVERAGE_CLASS] = { .type = NLA_U8 },
Johannes Berg55682962007-09-20 13:09:35 -040087
88 [NL80211_ATTR_IFTYPE] = { .type = NLA_U32 },
89 [NL80211_ATTR_IFINDEX] = { .type = NLA_U32 },
90 [NL80211_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
Johannes Berg41ade002007-12-19 02:03:29 +010091
92 [NL80211_ATTR_MAC] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg3e5d7642009-07-07 14:37:26 +020093 [NL80211_ATTR_PREV_BSSID] = { .type = NLA_BINARY, .len = ETH_ALEN },
Johannes Berg41ade002007-12-19 02:03:29 +010094
Johannes Bergb9454e82009-07-08 13:29:08 +020095 [NL80211_ATTR_KEY] = { .type = NLA_NESTED, },
Johannes Berg41ade002007-12-19 02:03:29 +010096 [NL80211_ATTR_KEY_DATA] = { .type = NLA_BINARY,
97 .len = WLAN_MAX_KEY_LEN },
98 [NL80211_ATTR_KEY_IDX] = { .type = NLA_U8 },
99 [NL80211_ATTR_KEY_CIPHER] = { .type = NLA_U32 },
100 [NL80211_ATTR_KEY_DEFAULT] = { .type = NLA_FLAG },
Jouni Malinen9f26a952009-05-15 12:38:32 +0300101 [NL80211_ATTR_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
Johannes Berge31b8212010-10-05 19:39:30 +0200102 [NL80211_ATTR_KEY_TYPE] = { .type = NLA_U32 },
Johannes Berged1b6cc2007-12-19 02:03:32 +0100103
104 [NL80211_ATTR_BEACON_INTERVAL] = { .type = NLA_U32 },
105 [NL80211_ATTR_DTIM_PERIOD] = { .type = NLA_U32 },
106 [NL80211_ATTR_BEACON_HEAD] = { .type = NLA_BINARY,
107 .len = IEEE80211_MAX_DATA_LEN },
108 [NL80211_ATTR_BEACON_TAIL] = { .type = NLA_BINARY,
109 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg5727ef12007-12-19 02:03:34 +0100110 [NL80211_ATTR_STA_AID] = { .type = NLA_U16 },
111 [NL80211_ATTR_STA_FLAGS] = { .type = NLA_NESTED },
112 [NL80211_ATTR_STA_LISTEN_INTERVAL] = { .type = NLA_U16 },
113 [NL80211_ATTR_STA_SUPPORTED_RATES] = { .type = NLA_BINARY,
114 .len = NL80211_MAX_SUPP_RATES },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100115 [NL80211_ATTR_STA_PLINK_ACTION] = { .type = NLA_U8 },
Johannes Berg5727ef12007-12-19 02:03:34 +0100116 [NL80211_ATTR_STA_VLAN] = { .type = NLA_U32 },
Johannes Berg0a9542e2008-10-15 11:54:04 +0200117 [NL80211_ATTR_MNTR_FLAGS] = { /* NLA_NESTED can't be empty */ },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +0100118 [NL80211_ATTR_MESH_ID] = { .type = NLA_BINARY,
119 .len = IEEE80211_MAX_MESH_ID_LEN },
120 [NL80211_ATTR_MPATH_NEXT_HOP] = { .type = NLA_U32 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300121
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -0700122 [NL80211_ATTR_REG_ALPHA2] = { .type = NLA_STRING, .len = 2 },
123 [NL80211_ATTR_REG_RULES] = { .type = NLA_NESTED },
124
Jouni Malinen9f1ba902008-08-07 20:07:01 +0300125 [NL80211_ATTR_BSS_CTS_PROT] = { .type = NLA_U8 },
126 [NL80211_ATTR_BSS_SHORT_PREAMBLE] = { .type = NLA_U8 },
127 [NL80211_ATTR_BSS_SHORT_SLOT_TIME] = { .type = NLA_U8 },
Jouni Malinen90c97a02008-10-30 16:59:22 +0200128 [NL80211_ATTR_BSS_BASIC_RATES] = { .type = NLA_BINARY,
129 .len = NL80211_MAX_SUPP_RATES },
Helmut Schaa50b12f52010-11-19 12:40:25 +0100130 [NL80211_ATTR_BSS_HT_OPMODE] = { .type = NLA_U16 },
Jouni Malinen36aedc92008-08-25 11:58:58 +0300131
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800132 [NL80211_ATTR_MESH_CONFIG] = { .type = NLA_NESTED },
Javier Cardona15d5dda2011-04-07 15:08:28 -0700133 [NL80211_ATTR_SUPPORT_MESH_AUTH] = { .type = NLA_FLAG },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -0700134
Jouni Malinen36aedc92008-08-25 11:58:58 +0300135 [NL80211_ATTR_HT_CAPABILITY] = { .type = NLA_BINARY,
136 .len = NL80211_HT_CAPABILITY_LEN },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +0200137
138 [NL80211_ATTR_MGMT_SUBTYPE] = { .type = NLA_U8 },
139 [NL80211_ATTR_IE] = { .type = NLA_BINARY,
140 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg2a519312009-02-10 21:25:55 +0100141 [NL80211_ATTR_SCAN_FREQUENCIES] = { .type = NLA_NESTED },
142 [NL80211_ATTR_SCAN_SSIDS] = { .type = NLA_NESTED },
Jouni Malinen636a5d32009-03-19 13:39:22 +0200143
144 [NL80211_ATTR_SSID] = { .type = NLA_BINARY,
145 .len = IEEE80211_MAX_SSID_LEN },
146 [NL80211_ATTR_AUTH_TYPE] = { .type = NLA_U32 },
147 [NL80211_ATTR_REASON_CODE] = { .type = NLA_U16 },
Johannes Berg04a773a2009-04-19 21:24:32 +0200148 [NL80211_ATTR_FREQ_FIXED] = { .type = NLA_FLAG },
Jouni Malinen1965c852009-04-22 21:38:25 +0300149 [NL80211_ATTR_TIMED_OUT] = { .type = NLA_FLAG },
Jouni Malinendc6382ce2009-05-06 22:09:37 +0300150 [NL80211_ATTR_USE_MFP] = { .type = NLA_U32 },
Johannes Bergeccb8e82009-05-11 21:57:56 +0300151 [NL80211_ATTR_STA_FLAGS2] = {
152 .len = sizeof(struct nl80211_sta_flag_update),
153 },
Jouni Malinen3f77316c2009-05-11 21:57:57 +0300154 [NL80211_ATTR_CONTROL_PORT] = { .type = NLA_FLAG },
Johannes Bergc0692b82010-08-27 14:26:53 +0300155 [NL80211_ATTR_CONTROL_PORT_ETHERTYPE] = { .type = NLA_U16 },
156 [NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT] = { .type = NLA_FLAG },
Samuel Ortizb23aa672009-07-01 21:26:54 +0200157 [NL80211_ATTR_PRIVACY] = { .type = NLA_FLAG },
158 [NL80211_ATTR_CIPHER_SUITE_GROUP] = { .type = NLA_U32 },
159 [NL80211_ATTR_WPA_VERSIONS] = { .type = NLA_U32 },
Johannes Berg463d0182009-07-14 00:33:35 +0200160 [NL80211_ATTR_PID] = { .type = NLA_U32 },
Felix Fietkau8b787642009-11-10 18:53:10 +0100161 [NL80211_ATTR_4ADDR] = { .type = NLA_U8 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100162 [NL80211_ATTR_PMKID] = { .type = NLA_BINARY,
163 .len = WLAN_PMKID_LEN },
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100164 [NL80211_ATTR_DURATION] = { .type = NLA_U32 },
165 [NL80211_ATTR_COOKIE] = { .type = NLA_U64 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200166 [NL80211_ATTR_TX_RATES] = { .type = NLA_NESTED },
Jouni Malinen026331c2010-02-15 12:53:10 +0200167 [NL80211_ATTR_FRAME] = { .type = NLA_BINARY,
168 .len = IEEE80211_MAX_DATA_LEN },
169 [NL80211_ATTR_FRAME_MATCH] = { .type = NLA_BINARY, },
Kalle Valoffb9eb32010-02-17 17:58:10 +0200170 [NL80211_ATTR_PS_STATE] = { .type = NLA_U32 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +0200171 [NL80211_ATTR_CQM] = { .type = NLA_NESTED, },
Jouni Malinend5cdfac2010-04-04 09:37:19 +0300172 [NL80211_ATTR_LOCAL_STATE_CHANGE] = { .type = NLA_FLAG },
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +0200173 [NL80211_ATTR_AP_ISOLATE] = { .type = NLA_U8 },
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +0300174 [NL80211_ATTR_WIPHY_TX_POWER_SETTING] = { .type = NLA_U32 },
175 [NL80211_ATTR_WIPHY_TX_POWER_LEVEL] = { .type = NLA_U32 },
Johannes Berg2e161f72010-08-12 15:38:38 +0200176 [NL80211_ATTR_FRAME_TYPE] = { .type = NLA_U16 },
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900177 [NL80211_ATTR_WIPHY_ANTENNA_TX] = { .type = NLA_U32 },
178 [NL80211_ATTR_WIPHY_ANTENNA_RX] = { .type = NLA_U32 },
Felix Fietkau885a46d2010-11-11 15:07:22 +0100179 [NL80211_ATTR_MCAST_RATE] = { .type = NLA_U32 },
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100180 [NL80211_ATTR_OFFCHANNEL_TX_OK] = { .type = NLA_FLAG },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100181 [NL80211_ATTR_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200182 [NL80211_ATTR_WOWLAN_TRIGGERS] = { .type = NLA_NESTED },
Javier Cardona9c3990a2011-05-03 16:57:11 -0700183 [NL80211_ATTR_STA_PLINK_STATE] = { .type = NLA_U8 },
Luciano Coelhobbe6ad62011-05-11 17:09:37 +0300184 [NL80211_ATTR_SCHED_SCAN_INTERVAL] = { .type = NLA_U32 },
Johannes Berge5497d72011-07-05 16:35:40 +0200185 [NL80211_ATTR_REKEY_DATA] = { .type = NLA_NESTED },
Johannes Berg34850ab2011-07-18 18:08:35 +0200186 [NL80211_ATTR_SCAN_SUPP_RATES] = { .type = NLA_NESTED },
Jouni Malinen32e9de82011-08-10 23:53:31 +0300187 [NL80211_ATTR_HIDDEN_SSID] = { .type = NLA_U32 },
Jouni Malinen9946ecf2011-08-10 23:55:56 +0300188 [NL80211_ATTR_IE_PROBE_RESP] = { .type = NLA_BINARY,
189 .len = IEEE80211_MAX_DATA_LEN },
190 [NL80211_ATTR_IE_ASSOC_RESP] = { .type = NLA_BINARY,
191 .len = IEEE80211_MAX_DATA_LEN },
Johannes Berg55682962007-09-20 13:09:35 -0400192};
193
Johannes Berge31b8212010-10-05 19:39:30 +0200194/* policy for the key attributes */
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000195static const struct nla_policy nl80211_key_policy[NL80211_KEY_MAX + 1] = {
Johannes Bergfffd0932009-07-08 14:22:54 +0200196 [NL80211_KEY_DATA] = { .type = NLA_BINARY, .len = WLAN_MAX_KEY_LEN },
Johannes Bergb9454e82009-07-08 13:29:08 +0200197 [NL80211_KEY_IDX] = { .type = NLA_U8 },
198 [NL80211_KEY_CIPHER] = { .type = NLA_U32 },
199 [NL80211_KEY_SEQ] = { .type = NLA_BINARY, .len = 8 },
200 [NL80211_KEY_DEFAULT] = { .type = NLA_FLAG },
201 [NL80211_KEY_DEFAULT_MGMT] = { .type = NLA_FLAG },
Johannes Berge31b8212010-10-05 19:39:30 +0200202 [NL80211_KEY_TYPE] = { .type = NLA_U32 },
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100203 [NL80211_KEY_DEFAULT_TYPES] = { .type = NLA_NESTED },
204};
205
206/* policy for the key default flags */
207static const struct nla_policy
208nl80211_key_default_policy[NUM_NL80211_KEY_DEFAULT_TYPES] = {
209 [NL80211_KEY_DEFAULT_TYPE_UNICAST] = { .type = NLA_FLAG },
210 [NL80211_KEY_DEFAULT_TYPE_MULTICAST] = { .type = NLA_FLAG },
Johannes Bergb9454e82009-07-08 13:29:08 +0200211};
212
Johannes Bergff1b6e62011-05-04 15:37:28 +0200213/* policy for WoWLAN attributes */
214static const struct nla_policy
215nl80211_wowlan_policy[NUM_NL80211_WOWLAN_TRIG] = {
216 [NL80211_WOWLAN_TRIG_ANY] = { .type = NLA_FLAG },
217 [NL80211_WOWLAN_TRIG_DISCONNECT] = { .type = NLA_FLAG },
218 [NL80211_WOWLAN_TRIG_MAGIC_PKT] = { .type = NLA_FLAG },
219 [NL80211_WOWLAN_TRIG_PKT_PATTERN] = { .type = NLA_NESTED },
Johannes Berg77dbbb12011-07-13 10:48:55 +0200220 [NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE] = { .type = NLA_FLAG },
221 [NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST] = { .type = NLA_FLAG },
222 [NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE] = { .type = NLA_FLAG },
223 [NL80211_WOWLAN_TRIG_RFKILL_RELEASE] = { .type = NLA_FLAG },
Johannes Bergff1b6e62011-05-04 15:37:28 +0200224};
225
Johannes Berge5497d72011-07-05 16:35:40 +0200226/* policy for GTK rekey offload attributes */
227static const struct nla_policy
228nl80211_rekey_policy[NUM_NL80211_REKEY_DATA] = {
229 [NL80211_REKEY_DATA_KEK] = { .len = NL80211_KEK_LEN },
230 [NL80211_REKEY_DATA_KCK] = { .len = NL80211_KCK_LEN },
231 [NL80211_REKEY_DATA_REPLAY_CTR] = { .len = NL80211_REPLAY_CTR_LEN },
232};
233
Holger Schuriga0438972009-11-11 11:30:02 +0100234/* ifidx get helper */
235static int nl80211_get_ifidx(struct netlink_callback *cb)
236{
237 int res;
238
239 res = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
240 nl80211_fam.attrbuf, nl80211_fam.maxattr,
241 nl80211_policy);
242 if (res)
243 return res;
244
245 if (!nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX])
246 return -EINVAL;
247
248 res = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_IFINDEX]);
249 if (!res)
250 return -EINVAL;
251 return res;
252}
253
Johannes Berg67748892010-10-04 21:14:06 +0200254static int nl80211_prepare_netdev_dump(struct sk_buff *skb,
255 struct netlink_callback *cb,
256 struct cfg80211_registered_device **rdev,
257 struct net_device **dev)
258{
259 int ifidx = cb->args[0];
260 int err;
261
262 if (!ifidx)
263 ifidx = nl80211_get_ifidx(cb);
264 if (ifidx < 0)
265 return ifidx;
266
267 cb->args[0] = ifidx;
268
269 rtnl_lock();
270
271 *dev = __dev_get_by_index(sock_net(skb->sk), ifidx);
272 if (!*dev) {
273 err = -ENODEV;
274 goto out_rtnl;
275 }
276
277 *rdev = cfg80211_get_dev_from_ifindex(sock_net(skb->sk), ifidx);
Felix Fietkau3cc25e52010-10-31 15:31:54 +0100278 if (IS_ERR(*rdev)) {
279 err = PTR_ERR(*rdev);
Johannes Berg67748892010-10-04 21:14:06 +0200280 goto out_rtnl;
281 }
282
283 return 0;
284 out_rtnl:
285 rtnl_unlock();
286 return err;
287}
288
289static void nl80211_finish_netdev_dump(struct cfg80211_registered_device *rdev)
290{
291 cfg80211_unlock_rdev(rdev);
292 rtnl_unlock();
293}
294
Johannes Bergf4a11bb2009-03-27 12:40:28 +0100295/* IE validation */
296static bool is_valid_ie_attr(const struct nlattr *attr)
297{
298 const u8 *pos;
299 int len;
300
301 if (!attr)
302 return true;
303
304 pos = nla_data(attr);
305 len = nla_len(attr);
306
307 while (len) {
308 u8 elemlen;
309
310 if (len < 2)
311 return false;
312 len -= 2;
313
314 elemlen = pos[1];
315 if (elemlen > len)
316 return false;
317
318 len -= elemlen;
319 pos += 2 + elemlen;
320 }
321
322 return true;
323}
324
Johannes Berg55682962007-09-20 13:09:35 -0400325/* message building helper */
326static inline void *nl80211hdr_put(struct sk_buff *skb, u32 pid, u32 seq,
327 int flags, u8 cmd)
328{
329 /* since there is no private header just add the generic one */
330 return genlmsg_put(skb, pid, seq, &nl80211_fam, flags, cmd);
331}
332
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400333static int nl80211_msg_put_channel(struct sk_buff *msg,
334 struct ieee80211_channel *chan)
335{
336 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_FREQ,
337 chan->center_freq);
338
339 if (chan->flags & IEEE80211_CHAN_DISABLED)
340 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_DISABLED);
341 if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
342 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_PASSIVE_SCAN);
343 if (chan->flags & IEEE80211_CHAN_NO_IBSS)
344 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_NO_IBSS);
345 if (chan->flags & IEEE80211_CHAN_RADAR)
346 NLA_PUT_FLAG(msg, NL80211_FREQUENCY_ATTR_RADAR);
347
348 NLA_PUT_U32(msg, NL80211_FREQUENCY_ATTR_MAX_TX_POWER,
349 DBM_TO_MBM(chan->max_power));
350
351 return 0;
352
353 nla_put_failure:
354 return -ENOBUFS;
355}
356
Johannes Berg55682962007-09-20 13:09:35 -0400357/* netlink command implementations */
358
Johannes Bergb9454e82009-07-08 13:29:08 +0200359struct key_parse {
360 struct key_params p;
361 int idx;
Johannes Berge31b8212010-10-05 19:39:30 +0200362 int type;
Johannes Bergb9454e82009-07-08 13:29:08 +0200363 bool def, defmgmt;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100364 bool def_uni, def_multi;
Johannes Bergb9454e82009-07-08 13:29:08 +0200365};
366
367static int nl80211_parse_key_new(struct nlattr *key, struct key_parse *k)
368{
369 struct nlattr *tb[NL80211_KEY_MAX + 1];
370 int err = nla_parse_nested(tb, NL80211_KEY_MAX, key,
371 nl80211_key_policy);
372 if (err)
373 return err;
374
375 k->def = !!tb[NL80211_KEY_DEFAULT];
376 k->defmgmt = !!tb[NL80211_KEY_DEFAULT_MGMT];
377
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100378 if (k->def) {
379 k->def_uni = true;
380 k->def_multi = true;
381 }
382 if (k->defmgmt)
383 k->def_multi = true;
384
Johannes Bergb9454e82009-07-08 13:29:08 +0200385 if (tb[NL80211_KEY_IDX])
386 k->idx = nla_get_u8(tb[NL80211_KEY_IDX]);
387
388 if (tb[NL80211_KEY_DATA]) {
389 k->p.key = nla_data(tb[NL80211_KEY_DATA]);
390 k->p.key_len = nla_len(tb[NL80211_KEY_DATA]);
391 }
392
393 if (tb[NL80211_KEY_SEQ]) {
394 k->p.seq = nla_data(tb[NL80211_KEY_SEQ]);
395 k->p.seq_len = nla_len(tb[NL80211_KEY_SEQ]);
396 }
397
398 if (tb[NL80211_KEY_CIPHER])
399 k->p.cipher = nla_get_u32(tb[NL80211_KEY_CIPHER]);
400
Johannes Berge31b8212010-10-05 19:39:30 +0200401 if (tb[NL80211_KEY_TYPE]) {
402 k->type = nla_get_u32(tb[NL80211_KEY_TYPE]);
403 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
404 return -EINVAL;
405 }
406
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100407 if (tb[NL80211_KEY_DEFAULT_TYPES]) {
408 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
409 int err = nla_parse_nested(kdt,
410 NUM_NL80211_KEY_DEFAULT_TYPES - 1,
411 tb[NL80211_KEY_DEFAULT_TYPES],
412 nl80211_key_default_policy);
413 if (err)
414 return err;
415
416 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
417 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
418 }
419
Johannes Bergb9454e82009-07-08 13:29:08 +0200420 return 0;
421}
422
423static int nl80211_parse_key_old(struct genl_info *info, struct key_parse *k)
424{
425 if (info->attrs[NL80211_ATTR_KEY_DATA]) {
426 k->p.key = nla_data(info->attrs[NL80211_ATTR_KEY_DATA]);
427 k->p.key_len = nla_len(info->attrs[NL80211_ATTR_KEY_DATA]);
428 }
429
430 if (info->attrs[NL80211_ATTR_KEY_SEQ]) {
431 k->p.seq = nla_data(info->attrs[NL80211_ATTR_KEY_SEQ]);
432 k->p.seq_len = nla_len(info->attrs[NL80211_ATTR_KEY_SEQ]);
433 }
434
435 if (info->attrs[NL80211_ATTR_KEY_IDX])
436 k->idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
437
438 if (info->attrs[NL80211_ATTR_KEY_CIPHER])
439 k->p.cipher = nla_get_u32(info->attrs[NL80211_ATTR_KEY_CIPHER]);
440
441 k->def = !!info->attrs[NL80211_ATTR_KEY_DEFAULT];
442 k->defmgmt = !!info->attrs[NL80211_ATTR_KEY_DEFAULT_MGMT];
443
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100444 if (k->def) {
445 k->def_uni = true;
446 k->def_multi = true;
447 }
448 if (k->defmgmt)
449 k->def_multi = true;
450
Johannes Berge31b8212010-10-05 19:39:30 +0200451 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
452 k->type = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
453 if (k->type < 0 || k->type >= NUM_NL80211_KEYTYPES)
454 return -EINVAL;
455 }
456
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100457 if (info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES]) {
458 struct nlattr *kdt[NUM_NL80211_KEY_DEFAULT_TYPES];
459 int err = nla_parse_nested(
460 kdt, NUM_NL80211_KEY_DEFAULT_TYPES - 1,
461 info->attrs[NL80211_ATTR_KEY_DEFAULT_TYPES],
462 nl80211_key_default_policy);
463 if (err)
464 return err;
465
466 k->def_uni = kdt[NL80211_KEY_DEFAULT_TYPE_UNICAST];
467 k->def_multi = kdt[NL80211_KEY_DEFAULT_TYPE_MULTICAST];
468 }
469
Johannes Bergb9454e82009-07-08 13:29:08 +0200470 return 0;
471}
472
473static int nl80211_parse_key(struct genl_info *info, struct key_parse *k)
474{
475 int err;
476
477 memset(k, 0, sizeof(*k));
478 k->idx = -1;
Johannes Berge31b8212010-10-05 19:39:30 +0200479 k->type = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +0200480
481 if (info->attrs[NL80211_ATTR_KEY])
482 err = nl80211_parse_key_new(info->attrs[NL80211_ATTR_KEY], k);
483 else
484 err = nl80211_parse_key_old(info, k);
485
486 if (err)
487 return err;
488
489 if (k->def && k->defmgmt)
490 return -EINVAL;
491
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100492 if (k->defmgmt) {
493 if (k->def_uni || !k->def_multi)
494 return -EINVAL;
495 }
496
Johannes Bergb9454e82009-07-08 13:29:08 +0200497 if (k->idx != -1) {
498 if (k->defmgmt) {
499 if (k->idx < 4 || k->idx > 5)
500 return -EINVAL;
501 } else if (k->def) {
502 if (k->idx < 0 || k->idx > 3)
503 return -EINVAL;
504 } else {
505 if (k->idx < 0 || k->idx > 5)
506 return -EINVAL;
507 }
508 }
509
510 return 0;
511}
512
Johannes Bergfffd0932009-07-08 14:22:54 +0200513static struct cfg80211_cached_keys *
514nl80211_parse_connkeys(struct cfg80211_registered_device *rdev,
515 struct nlattr *keys)
516{
517 struct key_parse parse;
518 struct nlattr *key;
519 struct cfg80211_cached_keys *result;
520 int rem, err, def = 0;
521
522 result = kzalloc(sizeof(*result), GFP_KERNEL);
523 if (!result)
524 return ERR_PTR(-ENOMEM);
525
526 result->def = -1;
527 result->defmgmt = -1;
528
529 nla_for_each_nested(key, keys, rem) {
530 memset(&parse, 0, sizeof(parse));
531 parse.idx = -1;
532
533 err = nl80211_parse_key_new(key, &parse);
534 if (err)
535 goto error;
536 err = -EINVAL;
537 if (!parse.p.key)
538 goto error;
539 if (parse.idx < 0 || parse.idx > 4)
540 goto error;
541 if (parse.def) {
542 if (def)
543 goto error;
544 def = 1;
545 result->def = parse.idx;
Johannes Bergdbd2fd62010-12-09 19:58:59 +0100546 if (!parse.def_uni || !parse.def_multi)
547 goto error;
Johannes Bergfffd0932009-07-08 14:22:54 +0200548 } else if (parse.defmgmt)
549 goto error;
550 err = cfg80211_validate_key_settings(rdev, &parse.p,
Johannes Berge31b8212010-10-05 19:39:30 +0200551 parse.idx, false, NULL);
Johannes Bergfffd0932009-07-08 14:22:54 +0200552 if (err)
553 goto error;
554 result->params[parse.idx].cipher = parse.p.cipher;
555 result->params[parse.idx].key_len = parse.p.key_len;
556 result->params[parse.idx].key = result->data[parse.idx];
557 memcpy(result->data[parse.idx], parse.p.key, parse.p.key_len);
558 }
559
560 return result;
561 error:
562 kfree(result);
563 return ERR_PTR(err);
564}
565
566static int nl80211_key_allowed(struct wireless_dev *wdev)
567{
568 ASSERT_WDEV_LOCK(wdev);
569
Johannes Bergfffd0932009-07-08 14:22:54 +0200570 switch (wdev->iftype) {
571 case NL80211_IFTYPE_AP:
572 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200573 case NL80211_IFTYPE_P2P_GO:
Thomas Pedersenff973af2011-05-03 16:57:12 -0700574 case NL80211_IFTYPE_MESH_POINT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200575 break;
576 case NL80211_IFTYPE_ADHOC:
577 if (!wdev->current_bss)
578 return -ENOLINK;
579 break;
580 case NL80211_IFTYPE_STATION:
Johannes Berg074ac8d2010-09-16 14:58:22 +0200581 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Bergfffd0932009-07-08 14:22:54 +0200582 if (wdev->sme_state != CFG80211_SME_CONNECTED)
583 return -ENOLINK;
584 break;
585 default:
586 return -EINVAL;
587 }
588
589 return 0;
590}
591
Johannes Berg7527a782011-05-13 10:58:57 +0200592static int nl80211_put_iftypes(struct sk_buff *msg, u32 attr, u16 ifmodes)
593{
594 struct nlattr *nl_modes = nla_nest_start(msg, attr);
595 int i;
596
597 if (!nl_modes)
598 goto nla_put_failure;
599
600 i = 0;
601 while (ifmodes) {
602 if (ifmodes & 1)
603 NLA_PUT_FLAG(msg, i);
604 ifmodes >>= 1;
605 i++;
606 }
607
608 nla_nest_end(msg, nl_modes);
609 return 0;
610
611nla_put_failure:
612 return -ENOBUFS;
613}
614
615static int nl80211_put_iface_combinations(struct wiphy *wiphy,
616 struct sk_buff *msg)
617{
618 struct nlattr *nl_combis;
619 int i, j;
620
621 nl_combis = nla_nest_start(msg,
622 NL80211_ATTR_INTERFACE_COMBINATIONS);
623 if (!nl_combis)
624 goto nla_put_failure;
625
626 for (i = 0; i < wiphy->n_iface_combinations; i++) {
627 const struct ieee80211_iface_combination *c;
628 struct nlattr *nl_combi, *nl_limits;
629
630 c = &wiphy->iface_combinations[i];
631
632 nl_combi = nla_nest_start(msg, i + 1);
633 if (!nl_combi)
634 goto nla_put_failure;
635
636 nl_limits = nla_nest_start(msg, NL80211_IFACE_COMB_LIMITS);
637 if (!nl_limits)
638 goto nla_put_failure;
639
640 for (j = 0; j < c->n_limits; j++) {
641 struct nlattr *nl_limit;
642
643 nl_limit = nla_nest_start(msg, j + 1);
644 if (!nl_limit)
645 goto nla_put_failure;
646 NLA_PUT_U32(msg, NL80211_IFACE_LIMIT_MAX,
647 c->limits[j].max);
648 if (nl80211_put_iftypes(msg, NL80211_IFACE_LIMIT_TYPES,
649 c->limits[j].types))
650 goto nla_put_failure;
651 nla_nest_end(msg, nl_limit);
652 }
653
654 nla_nest_end(msg, nl_limits);
655
656 if (c->beacon_int_infra_match)
657 NLA_PUT_FLAG(msg,
658 NL80211_IFACE_COMB_STA_AP_BI_MATCH);
659 NLA_PUT_U32(msg, NL80211_IFACE_COMB_NUM_CHANNELS,
660 c->num_different_channels);
661 NLA_PUT_U32(msg, NL80211_IFACE_COMB_MAXNUM,
662 c->max_interfaces);
663
664 nla_nest_end(msg, nl_combi);
665 }
666
667 nla_nest_end(msg, nl_combis);
668
669 return 0;
670nla_put_failure:
671 return -ENOBUFS;
672}
673
Johannes Berg55682962007-09-20 13:09:35 -0400674static int nl80211_send_wiphy(struct sk_buff *msg, u32 pid, u32 seq, int flags,
675 struct cfg80211_registered_device *dev)
676{
677 void *hdr;
Johannes Bergee688b002008-01-24 19:38:39 +0100678 struct nlattr *nl_bands, *nl_band;
679 struct nlattr *nl_freqs, *nl_freq;
680 struct nlattr *nl_rates, *nl_rate;
Johannes Berg8fdc6212009-03-14 09:34:01 +0100681 struct nlattr *nl_cmds;
Johannes Bergee688b002008-01-24 19:38:39 +0100682 enum ieee80211_band band;
683 struct ieee80211_channel *chan;
684 struct ieee80211_rate *rate;
685 int i;
Johannes Berg2e161f72010-08-12 15:38:38 +0200686 const struct ieee80211_txrx_stypes *mgmt_stypes =
687 dev->wiphy.mgmt_stypes;
Johannes Berg55682962007-09-20 13:09:35 -0400688
689 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_WIPHY);
690 if (!hdr)
691 return -1;
692
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -0500693 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, dev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -0400694 NLA_PUT_STRING(msg, NL80211_ATTR_WIPHY_NAME, wiphy_name(&dev->wiphy));
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200695
Johannes Bergf5ea9122009-08-07 16:17:38 +0200696 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
697 cfg80211_rdev_list_generation);
698
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +0200699 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_SHORT,
700 dev->wiphy.retry_short);
701 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_RETRY_LONG,
702 dev->wiphy.retry_long);
703 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FRAG_THRESHOLD,
704 dev->wiphy.frag_threshold);
705 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_RTS_THRESHOLD,
706 dev->wiphy.rts_threshold);
Lukáš Turek81077e82009-12-21 22:50:47 +0100707 NLA_PUT_U8(msg, NL80211_ATTR_WIPHY_COVERAGE_CLASS,
708 dev->wiphy.coverage_class);
Johannes Berg2a519312009-02-10 21:25:55 +0100709 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCAN_SSIDS,
710 dev->wiphy.max_scan_ssids);
Luciano Coelho93b6aa62011-07-13 14:57:28 +0300711 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_SCHED_SCAN_SSIDS,
712 dev->wiphy.max_sched_scan_ssids);
Johannes Berg18a83652009-03-31 12:12:05 +0200713 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCAN_IE_LEN,
714 dev->wiphy.max_scan_ie_len);
Luciano Coelho5a865ba2011-07-13 14:57:29 +0300715 NLA_PUT_U16(msg, NL80211_ATTR_MAX_SCHED_SCAN_IE_LEN,
716 dev->wiphy.max_sched_scan_ie_len);
Johannes Bergee688b002008-01-24 19:38:39 +0100717
Johannes Berge31b8212010-10-05 19:39:30 +0200718 if (dev->wiphy.flags & WIPHY_FLAG_IBSS_RSN)
719 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_IBSS_RSN);
Javier Cardona15d5dda2011-04-07 15:08:28 -0700720 if (dev->wiphy.flags & WIPHY_FLAG_MESH_AUTH)
721 NLA_PUT_FLAG(msg, NL80211_ATTR_SUPPORT_MESH_AUTH);
Johannes Berge31b8212010-10-05 19:39:30 +0200722
Johannes Berg25e47c182009-04-02 20:14:06 +0200723 NLA_PUT(msg, NL80211_ATTR_CIPHER_SUITES,
724 sizeof(u32) * dev->wiphy.n_cipher_suites,
725 dev->wiphy.cipher_suites);
726
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100727 NLA_PUT_U8(msg, NL80211_ATTR_MAX_NUM_PMKIDS,
728 dev->wiphy.max_num_pmkids);
729
Johannes Bergc0692b82010-08-27 14:26:53 +0300730 if (dev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL)
731 NLA_PUT_FLAG(msg, NL80211_ATTR_CONTROL_PORT_ETHERTYPE);
732
Bruno Randolf39fd5de2010-12-16 11:30:28 +0900733 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_TX,
734 dev->wiphy.available_antennas_tx);
735 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_AVAIL_RX,
736 dev->wiphy.available_antennas_rx);
737
Bruno Randolf7f531e02010-12-16 11:30:22 +0900738 if ((dev->wiphy.available_antennas_tx ||
739 dev->wiphy.available_antennas_rx) && dev->ops->get_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +0900740 u32 tx_ant = 0, rx_ant = 0;
741 int res;
742 res = dev->ops->get_antenna(&dev->wiphy, &tx_ant, &rx_ant);
743 if (!res) {
744 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_TX, tx_ant);
745 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_ANTENNA_RX, rx_ant);
746 }
747 }
748
Johannes Berg7527a782011-05-13 10:58:57 +0200749 if (nl80211_put_iftypes(msg, NL80211_ATTR_SUPPORTED_IFTYPES,
750 dev->wiphy.interface_modes))
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700751 goto nla_put_failure;
752
Johannes Bergee688b002008-01-24 19:38:39 +0100753 nl_bands = nla_nest_start(msg, NL80211_ATTR_WIPHY_BANDS);
754 if (!nl_bands)
755 goto nla_put_failure;
756
757 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
758 if (!dev->wiphy.bands[band])
759 continue;
760
761 nl_band = nla_nest_start(msg, band);
762 if (!nl_band)
763 goto nla_put_failure;
764
Johannes Bergd51626d2008-10-09 12:20:13 +0200765 /* add HT info */
766 if (dev->wiphy.bands[band]->ht_cap.ht_supported) {
767 NLA_PUT(msg, NL80211_BAND_ATTR_HT_MCS_SET,
768 sizeof(dev->wiphy.bands[band]->ht_cap.mcs),
769 &dev->wiphy.bands[band]->ht_cap.mcs);
770 NLA_PUT_U16(msg, NL80211_BAND_ATTR_HT_CAPA,
771 dev->wiphy.bands[band]->ht_cap.cap);
772 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_FACTOR,
773 dev->wiphy.bands[band]->ht_cap.ampdu_factor);
774 NLA_PUT_U8(msg, NL80211_BAND_ATTR_HT_AMPDU_DENSITY,
775 dev->wiphy.bands[band]->ht_cap.ampdu_density);
776 }
777
Johannes Bergee688b002008-01-24 19:38:39 +0100778 /* add frequencies */
779 nl_freqs = nla_nest_start(msg, NL80211_BAND_ATTR_FREQS);
780 if (!nl_freqs)
781 goto nla_put_failure;
782
783 for (i = 0; i < dev->wiphy.bands[band]->n_channels; i++) {
784 nl_freq = nla_nest_start(msg, i);
785 if (!nl_freq)
786 goto nla_put_failure;
787
788 chan = &dev->wiphy.bands[band]->channels[i];
Johannes Bergee688b002008-01-24 19:38:39 +0100789
Luis R. Rodriguez5dab3b82009-04-02 14:08:08 -0400790 if (nl80211_msg_put_channel(msg, chan))
791 goto nla_put_failure;
Jouni Malinene2f367f262008-11-21 19:01:30 +0200792
Johannes Bergee688b002008-01-24 19:38:39 +0100793 nla_nest_end(msg, nl_freq);
794 }
795
796 nla_nest_end(msg, nl_freqs);
797
798 /* add bitrates */
799 nl_rates = nla_nest_start(msg, NL80211_BAND_ATTR_RATES);
800 if (!nl_rates)
801 goto nla_put_failure;
802
803 for (i = 0; i < dev->wiphy.bands[band]->n_bitrates; i++) {
804 nl_rate = nla_nest_start(msg, i);
805 if (!nl_rate)
806 goto nla_put_failure;
807
808 rate = &dev->wiphy.bands[band]->bitrates[i];
809 NLA_PUT_U32(msg, NL80211_BITRATE_ATTR_RATE,
810 rate->bitrate);
811 if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE)
812 NLA_PUT_FLAG(msg,
813 NL80211_BITRATE_ATTR_2GHZ_SHORTPREAMBLE);
814
815 nla_nest_end(msg, nl_rate);
816 }
817
818 nla_nest_end(msg, nl_rates);
819
820 nla_nest_end(msg, nl_band);
821 }
822 nla_nest_end(msg, nl_bands);
823
Johannes Berg8fdc6212009-03-14 09:34:01 +0100824 nl_cmds = nla_nest_start(msg, NL80211_ATTR_SUPPORTED_COMMANDS);
825 if (!nl_cmds)
826 goto nla_put_failure;
827
828 i = 0;
829#define CMD(op, n) \
830 do { \
831 if (dev->ops->op) { \
832 i++; \
833 NLA_PUT_U32(msg, i, NL80211_CMD_ ## n); \
834 } \
835 } while (0)
836
837 CMD(add_virtual_intf, NEW_INTERFACE);
838 CMD(change_virtual_intf, SET_INTERFACE);
839 CMD(add_key, NEW_KEY);
840 CMD(add_beacon, NEW_BEACON);
841 CMD(add_station, NEW_STATION);
842 CMD(add_mpath, NEW_MPATH);
Javier Cardona24bdd9f2010-12-16 17:37:48 -0800843 CMD(update_mesh_config, SET_MESH_CONFIG);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100844 CMD(change_bss, SET_BSS);
Jouni Malinen636a5d32009-03-19 13:39:22 +0200845 CMD(auth, AUTHENTICATE);
846 CMD(assoc, ASSOCIATE);
847 CMD(deauth, DEAUTHENTICATE);
848 CMD(disassoc, DISASSOCIATE);
Johannes Berg04a773a2009-04-19 21:24:32 +0200849 CMD(join_ibss, JOIN_IBSS);
Johannes Berg29cbe682010-12-03 09:20:44 +0100850 CMD(join_mesh, JOIN_MESH);
Samuel Ortiz67fbb162009-11-24 23:59:15 +0100851 CMD(set_pmksa, SET_PMKSA);
852 CMD(del_pmksa, DEL_PMKSA);
853 CMD(flush_pmksa, FLUSH_PMKSA);
Jouni Malinen9588bbd2009-12-23 13:15:41 +0100854 CMD(remain_on_channel, REMAIN_ON_CHANNEL);
Jouni Malinen13ae75b2009-12-29 12:59:45 +0200855 CMD(set_bitrate_mask, SET_TX_BITRATE_MASK);
Johannes Berg2e161f72010-08-12 15:38:38 +0200856 CMD(mgmt_tx, FRAME);
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100857 CMD(mgmt_tx_cancel_wait, FRAME_WAIT_CANCEL);
Johannes Berg5be83de2009-11-19 00:56:28 +0100858 if (dev->wiphy.flags & WIPHY_FLAG_NETNS_OK) {
Johannes Berg463d0182009-07-14 00:33:35 +0200859 i++;
860 NLA_PUT_U32(msg, i, NL80211_CMD_SET_WIPHY_NETNS);
861 }
Johannes Bergf444de02010-05-05 15:25:02 +0200862 CMD(set_channel, SET_CHANNEL);
Bill Jordane8347eb2010-10-01 13:54:28 -0400863 CMD(set_wds_peer, SET_WDS_PEER);
Luciano Coelho807f8a82011-05-11 17:09:35 +0300864 if (dev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN)
865 CMD(sched_scan_start, START_SCHED_SCAN);
Johannes Berg8fdc6212009-03-14 09:34:01 +0100866
867#undef CMD
Samuel Ortizb23aa672009-07-01 21:26:54 +0200868
Johannes Berg6829c8782009-07-02 09:13:27 +0200869 if (dev->ops->connect || dev->ops->auth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200870 i++;
871 NLA_PUT_U32(msg, i, NL80211_CMD_CONNECT);
872 }
873
Johannes Berg6829c8782009-07-02 09:13:27 +0200874 if (dev->ops->disconnect || dev->ops->deauth) {
Samuel Ortizb23aa672009-07-01 21:26:54 +0200875 i++;
876 NLA_PUT_U32(msg, i, NL80211_CMD_DISCONNECT);
877 }
878
Johannes Berg8fdc6212009-03-14 09:34:01 +0100879 nla_nest_end(msg, nl_cmds);
880
Johannes Berga2939112010-12-14 17:54:28 +0100881 if (dev->ops->remain_on_channel)
882 NLA_PUT_U32(msg, NL80211_ATTR_MAX_REMAIN_ON_CHANNEL_DURATION,
883 dev->wiphy.max_remain_on_channel_duration);
884
Jouni Malinend2da5872011-08-08 12:10:30 +0300885 if (dev->ops->mgmt_tx_cancel_wait)
Johannes Bergf7ca38d2010-11-25 10:02:29 +0100886 NLA_PUT_FLAG(msg, NL80211_ATTR_OFFCHANNEL_TX_OK);
887
Johannes Berg2e161f72010-08-12 15:38:38 +0200888 if (mgmt_stypes) {
889 u16 stypes;
890 struct nlattr *nl_ftypes, *nl_ifs;
891 enum nl80211_iftype ift;
892
893 nl_ifs = nla_nest_start(msg, NL80211_ATTR_TX_FRAME_TYPES);
894 if (!nl_ifs)
895 goto nla_put_failure;
896
897 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
898 nl_ftypes = nla_nest_start(msg, ift);
899 if (!nl_ftypes)
900 goto nla_put_failure;
901 i = 0;
902 stypes = mgmt_stypes[ift].tx;
903 while (stypes) {
904 if (stypes & 1)
905 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
906 (i << 4) | IEEE80211_FTYPE_MGMT);
907 stypes >>= 1;
908 i++;
909 }
910 nla_nest_end(msg, nl_ftypes);
911 }
912
Johannes Berg74b70a42010-08-24 12:15:53 +0200913 nla_nest_end(msg, nl_ifs);
914
Johannes Berg2e161f72010-08-12 15:38:38 +0200915 nl_ifs = nla_nest_start(msg, NL80211_ATTR_RX_FRAME_TYPES);
916 if (!nl_ifs)
917 goto nla_put_failure;
918
919 for (ift = 0; ift < NUM_NL80211_IFTYPES; ift++) {
920 nl_ftypes = nla_nest_start(msg, ift);
921 if (!nl_ftypes)
922 goto nla_put_failure;
923 i = 0;
924 stypes = mgmt_stypes[ift].rx;
925 while (stypes) {
926 if (stypes & 1)
927 NLA_PUT_U16(msg, NL80211_ATTR_FRAME_TYPE,
928 (i << 4) | IEEE80211_FTYPE_MGMT);
929 stypes >>= 1;
930 i++;
931 }
932 nla_nest_end(msg, nl_ftypes);
933 }
934 nla_nest_end(msg, nl_ifs);
935 }
936
Johannes Bergff1b6e62011-05-04 15:37:28 +0200937 if (dev->wiphy.wowlan.flags || dev->wiphy.wowlan.n_patterns) {
938 struct nlattr *nl_wowlan;
939
940 nl_wowlan = nla_nest_start(msg,
941 NL80211_ATTR_WOWLAN_TRIGGERS_SUPPORTED);
942 if (!nl_wowlan)
943 goto nla_put_failure;
944
945 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_ANY)
946 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
947 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_DISCONNECT)
948 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
949 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_MAGIC_PKT)
950 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +0200951 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)
952 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED);
953 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)
954 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
955 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)
956 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
957 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)
958 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
959 if (dev->wiphy.wowlan.flags & WIPHY_WOWLAN_RFKILL_RELEASE)
960 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +0200961 if (dev->wiphy.wowlan.n_patterns) {
962 struct nl80211_wowlan_pattern_support pat = {
963 .max_patterns = dev->wiphy.wowlan.n_patterns,
964 .min_pattern_len =
965 dev->wiphy.wowlan.pattern_min_len,
966 .max_pattern_len =
967 dev->wiphy.wowlan.pattern_max_len,
968 };
969 NLA_PUT(msg, NL80211_WOWLAN_TRIG_PKT_PATTERN,
970 sizeof(pat), &pat);
971 }
972
973 nla_nest_end(msg, nl_wowlan);
974 }
975
Johannes Berg7527a782011-05-13 10:58:57 +0200976 if (nl80211_put_iftypes(msg, NL80211_ATTR_SOFTWARE_IFTYPES,
977 dev->wiphy.software_iftypes))
978 goto nla_put_failure;
979
980 if (nl80211_put_iface_combinations(&dev->wiphy, msg))
981 goto nla_put_failure;
982
Johannes Berg55682962007-09-20 13:09:35 -0400983 return genlmsg_end(msg, hdr);
984
985 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -0700986 genlmsg_cancel(msg, hdr);
987 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -0400988}
989
990static int nl80211_dump_wiphy(struct sk_buff *skb, struct netlink_callback *cb)
991{
992 int idx = 0;
993 int start = cb->args[0];
994 struct cfg80211_registered_device *dev;
995
Luis R. Rodrigueza1794392009-02-21 00:04:21 -0500996 mutex_lock(&cfg80211_mutex);
Johannes Berg79c97e92009-07-07 03:56:12 +0200997 list_for_each_entry(dev, &cfg80211_rdev_list, list) {
Johannes Berg463d0182009-07-14 00:33:35 +0200998 if (!net_eq(wiphy_net(&dev->wiphy), sock_net(skb->sk)))
999 continue;
Julius Volzb4637272008-07-08 14:02:19 +02001000 if (++idx <= start)
Johannes Berg55682962007-09-20 13:09:35 -04001001 continue;
1002 if (nl80211_send_wiphy(skb, NETLINK_CB(cb->skb).pid,
1003 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Julius Volzb4637272008-07-08 14:02:19 +02001004 dev) < 0) {
1005 idx--;
Johannes Berg55682962007-09-20 13:09:35 -04001006 break;
Julius Volzb4637272008-07-08 14:02:19 +02001007 }
Johannes Berg55682962007-09-20 13:09:35 -04001008 }
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001009 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001010
1011 cb->args[0] = idx;
1012
1013 return skb->len;
1014}
1015
1016static int nl80211_get_wiphy(struct sk_buff *skb, struct genl_info *info)
1017{
1018 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001019 struct cfg80211_registered_device *dev = info->user_ptr[0];
Johannes Berg55682962007-09-20 13:09:35 -04001020
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001021 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001022 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001023 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001024
Johannes Berg4c476992010-10-04 21:36:35 +02001025 if (nl80211_send_wiphy(msg, info->snd_pid, info->snd_seq, 0, dev) < 0) {
1026 nlmsg_free(msg);
1027 return -ENOBUFS;
1028 }
Johannes Berg55682962007-09-20 13:09:35 -04001029
Johannes Berg134e6372009-07-10 09:51:34 +00001030 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001031}
1032
Jouni Malinen31888482008-10-30 16:59:24 +02001033static const struct nla_policy txq_params_policy[NL80211_TXQ_ATTR_MAX + 1] = {
1034 [NL80211_TXQ_ATTR_QUEUE] = { .type = NLA_U8 },
1035 [NL80211_TXQ_ATTR_TXOP] = { .type = NLA_U16 },
1036 [NL80211_TXQ_ATTR_CWMIN] = { .type = NLA_U16 },
1037 [NL80211_TXQ_ATTR_CWMAX] = { .type = NLA_U16 },
1038 [NL80211_TXQ_ATTR_AIFS] = { .type = NLA_U8 },
1039};
1040
1041static int parse_txq_params(struct nlattr *tb[],
1042 struct ieee80211_txq_params *txq_params)
1043{
1044 if (!tb[NL80211_TXQ_ATTR_QUEUE] || !tb[NL80211_TXQ_ATTR_TXOP] ||
1045 !tb[NL80211_TXQ_ATTR_CWMIN] || !tb[NL80211_TXQ_ATTR_CWMAX] ||
1046 !tb[NL80211_TXQ_ATTR_AIFS])
1047 return -EINVAL;
1048
1049 txq_params->queue = nla_get_u8(tb[NL80211_TXQ_ATTR_QUEUE]);
1050 txq_params->txop = nla_get_u16(tb[NL80211_TXQ_ATTR_TXOP]);
1051 txq_params->cwmin = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMIN]);
1052 txq_params->cwmax = nla_get_u16(tb[NL80211_TXQ_ATTR_CWMAX]);
1053 txq_params->aifs = nla_get_u8(tb[NL80211_TXQ_ATTR_AIFS]);
1054
1055 return 0;
1056}
1057
Johannes Bergf444de02010-05-05 15:25:02 +02001058static bool nl80211_can_set_dev_channel(struct wireless_dev *wdev)
1059{
1060 /*
1061 * You can only set the channel explicitly for AP, mesh
1062 * and WDS type interfaces; all others have their channel
1063 * managed via their respective "establish a connection"
1064 * command (connect, join, ...)
1065 *
1066 * Monitors are special as they are normally slaved to
1067 * whatever else is going on, so they behave as though
1068 * you tried setting the wiphy channel itself.
1069 */
1070 return !wdev ||
1071 wdev->iftype == NL80211_IFTYPE_AP ||
1072 wdev->iftype == NL80211_IFTYPE_WDS ||
1073 wdev->iftype == NL80211_IFTYPE_MESH_POINT ||
Johannes Berg074ac8d2010-09-16 14:58:22 +02001074 wdev->iftype == NL80211_IFTYPE_MONITOR ||
1075 wdev->iftype == NL80211_IFTYPE_P2P_GO;
Johannes Bergf444de02010-05-05 15:25:02 +02001076}
1077
1078static int __nl80211_set_channel(struct cfg80211_registered_device *rdev,
1079 struct wireless_dev *wdev,
1080 struct genl_info *info)
1081{
1082 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
1083 u32 freq;
1084 int result;
1085
1086 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
1087 return -EINVAL;
1088
1089 if (!nl80211_can_set_dev_channel(wdev))
1090 return -EOPNOTSUPP;
1091
1092 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
1093 channel_type = nla_get_u32(info->attrs[
1094 NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
1095 if (channel_type != NL80211_CHAN_NO_HT &&
1096 channel_type != NL80211_CHAN_HT20 &&
1097 channel_type != NL80211_CHAN_HT40PLUS &&
1098 channel_type != NL80211_CHAN_HT40MINUS)
1099 return -EINVAL;
1100 }
1101
1102 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
1103
1104 mutex_lock(&rdev->devlist_mtx);
1105 if (wdev) {
1106 wdev_lock(wdev);
1107 result = cfg80211_set_freq(rdev, wdev, freq, channel_type);
1108 wdev_unlock(wdev);
1109 } else {
1110 result = cfg80211_set_freq(rdev, NULL, freq, channel_type);
1111 }
1112 mutex_unlock(&rdev->devlist_mtx);
1113
1114 return result;
1115}
1116
1117static int nl80211_set_channel(struct sk_buff *skb, struct genl_info *info)
1118{
Johannes Berg4c476992010-10-04 21:36:35 +02001119 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1120 struct net_device *netdev = info->user_ptr[1];
Johannes Bergf444de02010-05-05 15:25:02 +02001121
Johannes Berg4c476992010-10-04 21:36:35 +02001122 return __nl80211_set_channel(rdev, netdev->ieee80211_ptr, info);
Johannes Bergf444de02010-05-05 15:25:02 +02001123}
1124
Bill Jordane8347eb2010-10-01 13:54:28 -04001125static int nl80211_set_wds_peer(struct sk_buff *skb, struct genl_info *info)
1126{
Johannes Berg43b19952010-10-07 13:10:30 +02001127 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1128 struct net_device *dev = info->user_ptr[1];
1129 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berg388ac772010-10-07 13:11:09 +02001130 const u8 *bssid;
Bill Jordane8347eb2010-10-01 13:54:28 -04001131
1132 if (!info->attrs[NL80211_ATTR_MAC])
1133 return -EINVAL;
1134
Johannes Berg43b19952010-10-07 13:10:30 +02001135 if (netif_running(dev))
1136 return -EBUSY;
Bill Jordane8347eb2010-10-01 13:54:28 -04001137
Johannes Berg43b19952010-10-07 13:10:30 +02001138 if (!rdev->ops->set_wds_peer)
1139 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001140
Johannes Berg43b19952010-10-07 13:10:30 +02001141 if (wdev->iftype != NL80211_IFTYPE_WDS)
1142 return -EOPNOTSUPP;
Bill Jordane8347eb2010-10-01 13:54:28 -04001143
1144 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg43b19952010-10-07 13:10:30 +02001145 return rdev->ops->set_wds_peer(wdev->wiphy, dev, bssid);
Bill Jordane8347eb2010-10-01 13:54:28 -04001146}
1147
1148
Johannes Berg55682962007-09-20 13:09:35 -04001149static int nl80211_set_wiphy(struct sk_buff *skb, struct genl_info *info)
1150{
1151 struct cfg80211_registered_device *rdev;
Johannes Bergf444de02010-05-05 15:25:02 +02001152 struct net_device *netdev = NULL;
1153 struct wireless_dev *wdev;
Bill Jordana1e567c2010-09-10 11:22:32 -04001154 int result = 0, rem_txq_params = 0;
Jouni Malinen31888482008-10-30 16:59:24 +02001155 struct nlattr *nl_txq_params;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001156 u32 changed;
1157 u8 retry_short = 0, retry_long = 0;
1158 u32 frag_threshold = 0, rts_threshold = 0;
Lukáš Turek81077e82009-12-21 22:50:47 +01001159 u8 coverage_class = 0;
Johannes Berg55682962007-09-20 13:09:35 -04001160
Johannes Bergf444de02010-05-05 15:25:02 +02001161 /*
1162 * Try to find the wiphy and netdev. Normally this
1163 * function shouldn't need the netdev, but this is
1164 * done for backward compatibility -- previously
1165 * setting the channel was done per wiphy, but now
1166 * it is per netdev. Previous userland like hostapd
1167 * also passed a netdev to set_wiphy, so that it is
1168 * possible to let that go to the right netdev!
1169 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001170 mutex_lock(&cfg80211_mutex);
1171
Johannes Bergf444de02010-05-05 15:25:02 +02001172 if (info->attrs[NL80211_ATTR_IFINDEX]) {
1173 int ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
1174
1175 netdev = dev_get_by_index(genl_info_net(info), ifindex);
1176 if (netdev && netdev->ieee80211_ptr) {
1177 rdev = wiphy_to_dev(netdev->ieee80211_ptr->wiphy);
1178 mutex_lock(&rdev->mtx);
1179 } else
1180 netdev = NULL;
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001181 }
1182
Johannes Bergf444de02010-05-05 15:25:02 +02001183 if (!netdev) {
1184 rdev = __cfg80211_rdev_from_info(info);
1185 if (IS_ERR(rdev)) {
1186 mutex_unlock(&cfg80211_mutex);
Johannes Berg4c476992010-10-04 21:36:35 +02001187 return PTR_ERR(rdev);
Johannes Bergf444de02010-05-05 15:25:02 +02001188 }
1189 wdev = NULL;
1190 netdev = NULL;
1191 result = 0;
1192
1193 mutex_lock(&rdev->mtx);
1194 } else if (netif_running(netdev) &&
1195 nl80211_can_set_dev_channel(netdev->ieee80211_ptr))
1196 wdev = netdev->ieee80211_ptr;
1197 else
1198 wdev = NULL;
1199
1200 /*
1201 * end workaround code, by now the rdev is available
1202 * and locked, and wdev may or may not be NULL.
1203 */
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001204
1205 if (info->attrs[NL80211_ATTR_WIPHY_NAME])
Jouni Malinen31888482008-10-30 16:59:24 +02001206 result = cfg80211_dev_rename(
1207 rdev, nla_data(info->attrs[NL80211_ATTR_WIPHY_NAME]));
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001208
1209 mutex_unlock(&cfg80211_mutex);
1210
1211 if (result)
1212 goto bad_res;
Johannes Berg55682962007-09-20 13:09:35 -04001213
Jouni Malinen31888482008-10-30 16:59:24 +02001214 if (info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS]) {
1215 struct ieee80211_txq_params txq_params;
1216 struct nlattr *tb[NL80211_TXQ_ATTR_MAX + 1];
1217
1218 if (!rdev->ops->set_txq_params) {
1219 result = -EOPNOTSUPP;
1220 goto bad_res;
1221 }
1222
1223 nla_for_each_nested(nl_txq_params,
1224 info->attrs[NL80211_ATTR_WIPHY_TXQ_PARAMS],
1225 rem_txq_params) {
1226 nla_parse(tb, NL80211_TXQ_ATTR_MAX,
1227 nla_data(nl_txq_params),
1228 nla_len(nl_txq_params),
1229 txq_params_policy);
1230 result = parse_txq_params(tb, &txq_params);
1231 if (result)
1232 goto bad_res;
1233
1234 result = rdev->ops->set_txq_params(&rdev->wiphy,
1235 &txq_params);
1236 if (result)
1237 goto bad_res;
1238 }
1239 }
1240
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001241 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
Johannes Bergf444de02010-05-05 15:25:02 +02001242 result = __nl80211_set_channel(rdev, wdev, info);
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001243 if (result)
1244 goto bad_res;
1245 }
1246
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001247 if (info->attrs[NL80211_ATTR_WIPHY_TX_POWER_SETTING]) {
1248 enum nl80211_tx_power_setting type;
1249 int idx, mbm = 0;
1250
1251 if (!rdev->ops->set_tx_power) {
Jiri Slaby60ea3852010-07-07 15:02:46 +02001252 result = -EOPNOTSUPP;
Juuso Oikarinen98d2ff82010-06-23 12:12:38 +03001253 goto bad_res;
1254 }
1255
1256 idx = NL80211_ATTR_WIPHY_TX_POWER_SETTING;
1257 type = nla_get_u32(info->attrs[idx]);
1258
1259 if (!info->attrs[NL80211_ATTR_WIPHY_TX_POWER_LEVEL] &&
1260 (type != NL80211_TX_POWER_AUTOMATIC)) {
1261 result = -EINVAL;
1262 goto bad_res;
1263 }
1264
1265 if (type != NL80211_TX_POWER_AUTOMATIC) {
1266 idx = NL80211_ATTR_WIPHY_TX_POWER_LEVEL;
1267 mbm = nla_get_u32(info->attrs[idx]);
1268 }
1269
1270 result = rdev->ops->set_tx_power(&rdev->wiphy, type, mbm);
1271 if (result)
1272 goto bad_res;
1273 }
1274
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001275 if (info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX] &&
1276 info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]) {
1277 u32 tx_ant, rx_ant;
Bruno Randolf7f531e02010-12-16 11:30:22 +09001278 if ((!rdev->wiphy.available_antennas_tx &&
1279 !rdev->wiphy.available_antennas_rx) ||
1280 !rdev->ops->set_antenna) {
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001281 result = -EOPNOTSUPP;
1282 goto bad_res;
1283 }
1284
1285 tx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_TX]);
1286 rx_ant = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_ANTENNA_RX]);
1287
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001288 /* reject antenna configurations which don't match the
Bruno Randolf7f531e02010-12-16 11:30:22 +09001289 * available antenna masks, except for the "all" mask */
1290 if ((~tx_ant && (tx_ant & ~rdev->wiphy.available_antennas_tx)) ||
1291 (~rx_ant && (rx_ant & ~rdev->wiphy.available_antennas_rx))) {
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001292 result = -EINVAL;
1293 goto bad_res;
1294 }
1295
Bruno Randolf7f531e02010-12-16 11:30:22 +09001296 tx_ant = tx_ant & rdev->wiphy.available_antennas_tx;
1297 rx_ant = rx_ant & rdev->wiphy.available_antennas_rx;
Bruno Randolfa7ffac92010-12-08 13:59:24 +09001298
Bruno Randolfafe0cbf2010-11-10 12:50:50 +09001299 result = rdev->ops->set_antenna(&rdev->wiphy, tx_ant, rx_ant);
1300 if (result)
1301 goto bad_res;
1302 }
1303
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001304 changed = 0;
1305
1306 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]) {
1307 retry_short = nla_get_u8(
1308 info->attrs[NL80211_ATTR_WIPHY_RETRY_SHORT]);
1309 if (retry_short == 0) {
1310 result = -EINVAL;
1311 goto bad_res;
1312 }
1313 changed |= WIPHY_PARAM_RETRY_SHORT;
1314 }
1315
1316 if (info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]) {
1317 retry_long = nla_get_u8(
1318 info->attrs[NL80211_ATTR_WIPHY_RETRY_LONG]);
1319 if (retry_long == 0) {
1320 result = -EINVAL;
1321 goto bad_res;
1322 }
1323 changed |= WIPHY_PARAM_RETRY_LONG;
1324 }
1325
1326 if (info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]) {
1327 frag_threshold = nla_get_u32(
1328 info->attrs[NL80211_ATTR_WIPHY_FRAG_THRESHOLD]);
1329 if (frag_threshold < 256) {
1330 result = -EINVAL;
1331 goto bad_res;
1332 }
1333 if (frag_threshold != (u32) -1) {
1334 /*
1335 * Fragments (apart from the last one) are required to
1336 * have even length. Make the fragmentation code
1337 * simpler by stripping LSB should someone try to use
1338 * odd threshold value.
1339 */
1340 frag_threshold &= ~0x1;
1341 }
1342 changed |= WIPHY_PARAM_FRAG_THRESHOLD;
1343 }
1344
1345 if (info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]) {
1346 rts_threshold = nla_get_u32(
1347 info->attrs[NL80211_ATTR_WIPHY_RTS_THRESHOLD]);
1348 changed |= WIPHY_PARAM_RTS_THRESHOLD;
1349 }
1350
Lukáš Turek81077e82009-12-21 22:50:47 +01001351 if (info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]) {
1352 coverage_class = nla_get_u8(
1353 info->attrs[NL80211_ATTR_WIPHY_COVERAGE_CLASS]);
1354 changed |= WIPHY_PARAM_COVERAGE_CLASS;
1355 }
1356
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001357 if (changed) {
1358 u8 old_retry_short, old_retry_long;
1359 u32 old_frag_threshold, old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001360 u8 old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001361
1362 if (!rdev->ops->set_wiphy_params) {
1363 result = -EOPNOTSUPP;
1364 goto bad_res;
1365 }
1366
1367 old_retry_short = rdev->wiphy.retry_short;
1368 old_retry_long = rdev->wiphy.retry_long;
1369 old_frag_threshold = rdev->wiphy.frag_threshold;
1370 old_rts_threshold = rdev->wiphy.rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001371 old_coverage_class = rdev->wiphy.coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001372
1373 if (changed & WIPHY_PARAM_RETRY_SHORT)
1374 rdev->wiphy.retry_short = retry_short;
1375 if (changed & WIPHY_PARAM_RETRY_LONG)
1376 rdev->wiphy.retry_long = retry_long;
1377 if (changed & WIPHY_PARAM_FRAG_THRESHOLD)
1378 rdev->wiphy.frag_threshold = frag_threshold;
1379 if (changed & WIPHY_PARAM_RTS_THRESHOLD)
1380 rdev->wiphy.rts_threshold = rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001381 if (changed & WIPHY_PARAM_COVERAGE_CLASS)
1382 rdev->wiphy.coverage_class = coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001383
1384 result = rdev->ops->set_wiphy_params(&rdev->wiphy, changed);
1385 if (result) {
1386 rdev->wiphy.retry_short = old_retry_short;
1387 rdev->wiphy.retry_long = old_retry_long;
1388 rdev->wiphy.frag_threshold = old_frag_threshold;
1389 rdev->wiphy.rts_threshold = old_rts_threshold;
Lukáš Turek81077e82009-12-21 22:50:47 +01001390 rdev->wiphy.coverage_class = old_coverage_class;
Jouni Malinenb9a5f8ca2009-04-20 18:39:05 +02001391 }
1392 }
Jouni Malinen72bdcf32008-11-26 16:15:24 +02001393
Johannes Berg306d6112008-12-08 12:39:04 +01001394 bad_res:
Johannes Berg4bbf4d52009-03-24 09:35:46 +01001395 mutex_unlock(&rdev->mtx);
Johannes Bergf444de02010-05-05 15:25:02 +02001396 if (netdev)
1397 dev_put(netdev);
Johannes Berg55682962007-09-20 13:09:35 -04001398 return result;
1399}
1400
1401
1402static int nl80211_send_iface(struct sk_buff *msg, u32 pid, u32 seq, int flags,
Johannes Bergd7264052009-04-19 16:23:20 +02001403 struct cfg80211_registered_device *rdev,
Johannes Berg55682962007-09-20 13:09:35 -04001404 struct net_device *dev)
1405{
1406 void *hdr;
1407
1408 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_INTERFACE);
1409 if (!hdr)
1410 return -1;
1411
1412 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
Johannes Bergd7264052009-04-19 16:23:20 +02001413 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg55682962007-09-20 13:09:35 -04001414 NLA_PUT_STRING(msg, NL80211_ATTR_IFNAME, dev->name);
Johannes Berg60719ff2008-09-16 14:55:09 +02001415 NLA_PUT_U32(msg, NL80211_ATTR_IFTYPE, dev->ieee80211_ptr->iftype);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001416
1417 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION,
1418 rdev->devlist_generation ^
1419 (cfg80211_rdev_list_generation << 2));
1420
Johannes Berg55682962007-09-20 13:09:35 -04001421 return genlmsg_end(msg, hdr);
1422
1423 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07001424 genlmsg_cancel(msg, hdr);
1425 return -EMSGSIZE;
Johannes Berg55682962007-09-20 13:09:35 -04001426}
1427
1428static int nl80211_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
1429{
1430 int wp_idx = 0;
1431 int if_idx = 0;
1432 int wp_start = cb->args[0];
1433 int if_start = cb->args[1];
Johannes Bergf5ea9122009-08-07 16:17:38 +02001434 struct cfg80211_registered_device *rdev;
Johannes Berg55682962007-09-20 13:09:35 -04001435 struct wireless_dev *wdev;
1436
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001437 mutex_lock(&cfg80211_mutex);
Johannes Bergf5ea9122009-08-07 16:17:38 +02001438 list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
1439 if (!net_eq(wiphy_net(&rdev->wiphy), sock_net(skb->sk)))
Johannes Berg463d0182009-07-14 00:33:35 +02001440 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001441 if (wp_idx < wp_start) {
1442 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001443 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001444 }
Johannes Berg55682962007-09-20 13:09:35 -04001445 if_idx = 0;
1446
Johannes Bergf5ea9122009-08-07 16:17:38 +02001447 mutex_lock(&rdev->devlist_mtx);
1448 list_for_each_entry(wdev, &rdev->netdev_list, list) {
Johannes Bergbba95fe2008-07-29 13:22:51 +02001449 if (if_idx < if_start) {
1450 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001451 continue;
Johannes Bergbba95fe2008-07-29 13:22:51 +02001452 }
Johannes Berg55682962007-09-20 13:09:35 -04001453 if (nl80211_send_iface(skb, NETLINK_CB(cb->skb).pid,
1454 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Bergf5ea9122009-08-07 16:17:38 +02001455 rdev, wdev->netdev) < 0) {
1456 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001457 goto out;
1458 }
1459 if_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001460 }
Johannes Bergf5ea9122009-08-07 16:17:38 +02001461 mutex_unlock(&rdev->devlist_mtx);
Johannes Bergbba95fe2008-07-29 13:22:51 +02001462
1463 wp_idx++;
Johannes Berg55682962007-09-20 13:09:35 -04001464 }
Johannes Bergbba95fe2008-07-29 13:22:51 +02001465 out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05001466 mutex_unlock(&cfg80211_mutex);
Johannes Berg55682962007-09-20 13:09:35 -04001467
1468 cb->args[0] = wp_idx;
1469 cb->args[1] = if_idx;
1470
1471 return skb->len;
1472}
1473
1474static int nl80211_get_interface(struct sk_buff *skb, struct genl_info *info)
1475{
1476 struct sk_buff *msg;
Johannes Berg4c476992010-10-04 21:36:35 +02001477 struct cfg80211_registered_device *dev = info->user_ptr[0];
1478 struct net_device *netdev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001479
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001480 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04001481 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02001482 return -ENOMEM;
Johannes Berg55682962007-09-20 13:09:35 -04001483
Johannes Bergd7264052009-04-19 16:23:20 +02001484 if (nl80211_send_iface(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02001485 dev, netdev) < 0) {
1486 nlmsg_free(msg);
1487 return -ENOBUFS;
1488 }
Johannes Berg55682962007-09-20 13:09:35 -04001489
Johannes Berg134e6372009-07-10 09:51:34 +00001490 return genlmsg_reply(msg, info);
Johannes Berg55682962007-09-20 13:09:35 -04001491}
1492
Michael Wu66f7ac52008-01-31 19:48:22 +01001493static const struct nla_policy mntr_flags_policy[NL80211_MNTR_FLAG_MAX + 1] = {
1494 [NL80211_MNTR_FLAG_FCSFAIL] = { .type = NLA_FLAG },
1495 [NL80211_MNTR_FLAG_PLCPFAIL] = { .type = NLA_FLAG },
1496 [NL80211_MNTR_FLAG_CONTROL] = { .type = NLA_FLAG },
1497 [NL80211_MNTR_FLAG_OTHER_BSS] = { .type = NLA_FLAG },
1498 [NL80211_MNTR_FLAG_COOK_FRAMES] = { .type = NLA_FLAG },
1499};
1500
1501static int parse_monitor_flags(struct nlattr *nla, u32 *mntrflags)
1502{
1503 struct nlattr *flags[NL80211_MNTR_FLAG_MAX + 1];
1504 int flag;
1505
1506 *mntrflags = 0;
1507
1508 if (!nla)
1509 return -EINVAL;
1510
1511 if (nla_parse_nested(flags, NL80211_MNTR_FLAG_MAX,
1512 nla, mntr_flags_policy))
1513 return -EINVAL;
1514
1515 for (flag = 1; flag <= NL80211_MNTR_FLAG_MAX; flag++)
1516 if (flags[flag])
1517 *mntrflags |= (1<<flag);
1518
1519 return 0;
1520}
1521
Johannes Berg9bc383d2009-11-19 11:55:19 +01001522static int nl80211_valid_4addr(struct cfg80211_registered_device *rdev,
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001523 struct net_device *netdev, u8 use_4addr,
1524 enum nl80211_iftype iftype)
Johannes Berg9bc383d2009-11-19 11:55:19 +01001525{
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001526 if (!use_4addr) {
Jiri Pirkof350a0a82010-06-15 06:50:45 +00001527 if (netdev && (netdev->priv_flags & IFF_BRIDGE_PORT))
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001528 return -EBUSY;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001529 return 0;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001530 }
Johannes Berg9bc383d2009-11-19 11:55:19 +01001531
1532 switch (iftype) {
1533 case NL80211_IFTYPE_AP_VLAN:
1534 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_AP)
1535 return 0;
1536 break;
1537 case NL80211_IFTYPE_STATION:
1538 if (rdev->wiphy.flags & WIPHY_FLAG_4ADDR_STATION)
1539 return 0;
1540 break;
1541 default:
1542 break;
1543 }
1544
1545 return -EOPNOTSUPP;
1546}
1547
Johannes Berg55682962007-09-20 13:09:35 -04001548static int nl80211_set_interface(struct sk_buff *skb, struct genl_info *info)
1549{
Johannes Berg4c476992010-10-04 21:36:35 +02001550 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001551 struct vif_params params;
Johannes Berge36d56b2009-06-09 21:04:43 +02001552 int err;
Johannes Berg04a773a2009-04-19 21:24:32 +02001553 enum nl80211_iftype otype, ntype;
Johannes Berg4c476992010-10-04 21:36:35 +02001554 struct net_device *dev = info->user_ptr[1];
Johannes Berg92ffe052008-09-16 20:39:36 +02001555 u32 _flags, *flags = NULL;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001556 bool change = false;
Johannes Berg55682962007-09-20 13:09:35 -04001557
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001558 memset(&params, 0, sizeof(params));
1559
Johannes Berg04a773a2009-04-19 21:24:32 +02001560 otype = ntype = dev->ieee80211_ptr->iftype;
Johannes Berg55682962007-09-20 13:09:35 -04001561
Johannes Berg723b0382008-09-16 20:22:09 +02001562 if (info->attrs[NL80211_ATTR_IFTYPE]) {
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001563 ntype = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
Johannes Berg04a773a2009-04-19 21:24:32 +02001564 if (otype != ntype)
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001565 change = true;
Johannes Berg4c476992010-10-04 21:36:35 +02001566 if (ntype > NL80211_IFTYPE_MAX)
1567 return -EINVAL;
Johannes Berg723b0382008-09-16 20:22:09 +02001568 }
1569
Johannes Berg92ffe052008-09-16 20:39:36 +02001570 if (info->attrs[NL80211_ATTR_MESH_ID]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01001571 struct wireless_dev *wdev = dev->ieee80211_ptr;
1572
Johannes Berg4c476992010-10-04 21:36:35 +02001573 if (ntype != NL80211_IFTYPE_MESH_POINT)
1574 return -EINVAL;
Johannes Berg29cbe682010-12-03 09:20:44 +01001575 if (netif_running(dev))
1576 return -EBUSY;
1577
1578 wdev_lock(wdev);
1579 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1580 IEEE80211_MAX_MESH_ID_LEN);
1581 wdev->mesh_id_up_len =
1582 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1583 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1584 wdev->mesh_id_up_len);
1585 wdev_unlock(wdev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001586 }
1587
Felix Fietkau8b787642009-11-10 18:53:10 +01001588 if (info->attrs[NL80211_ATTR_4ADDR]) {
1589 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
1590 change = true;
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001591 err = nl80211_valid_4addr(rdev, dev, params.use_4addr, ntype);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001592 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001593 return err;
Felix Fietkau8b787642009-11-10 18:53:10 +01001594 } else {
1595 params.use_4addr = -1;
1596 }
1597
Johannes Berg92ffe052008-09-16 20:39:36 +02001598 if (info->attrs[NL80211_ATTR_MNTR_FLAGS]) {
Johannes Berg4c476992010-10-04 21:36:35 +02001599 if (ntype != NL80211_IFTYPE_MONITOR)
1600 return -EINVAL;
Johannes Berg92ffe052008-09-16 20:39:36 +02001601 err = parse_monitor_flags(info->attrs[NL80211_ATTR_MNTR_FLAGS],
1602 &_flags);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001603 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001604 return err;
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001605
1606 flags = &_flags;
1607 change = true;
Johannes Berg92ffe052008-09-16 20:39:36 +02001608 }
Johannes Berg3b858752009-03-12 09:55:09 +01001609
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001610 if (change)
Johannes Berg3d54d252009-08-21 14:51:05 +02001611 err = cfg80211_change_iface(rdev, dev, ntype, flags, &params);
Johannes Bergac7f9cf2009-03-21 17:07:59 +01001612 else
1613 err = 0;
Johannes Berg60719ff2008-09-16 14:55:09 +02001614
Johannes Berg9bc383d2009-11-19 11:55:19 +01001615 if (!err && params.use_4addr != -1)
1616 dev->ieee80211_ptr->use_4addr = params.use_4addr;
1617
Johannes Berg55682962007-09-20 13:09:35 -04001618 return err;
1619}
1620
1621static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
1622{
Johannes Berg4c476992010-10-04 21:36:35 +02001623 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001624 struct vif_params params;
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001625 struct net_device *dev;
Johannes Berg55682962007-09-20 13:09:35 -04001626 int err;
1627 enum nl80211_iftype type = NL80211_IFTYPE_UNSPECIFIED;
Michael Wu66f7ac52008-01-31 19:48:22 +01001628 u32 flags;
Johannes Berg55682962007-09-20 13:09:35 -04001629
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001630 memset(&params, 0, sizeof(params));
1631
Johannes Berg55682962007-09-20 13:09:35 -04001632 if (!info->attrs[NL80211_ATTR_IFNAME])
1633 return -EINVAL;
1634
1635 if (info->attrs[NL80211_ATTR_IFTYPE]) {
1636 type = nla_get_u32(info->attrs[NL80211_ATTR_IFTYPE]);
1637 if (type > NL80211_IFTYPE_MAX)
1638 return -EINVAL;
1639 }
1640
Johannes Berg79c97e92009-07-07 03:56:12 +02001641 if (!rdev->ops->add_virtual_intf ||
Johannes Berg4c476992010-10-04 21:36:35 +02001642 !(rdev->wiphy.interface_modes & (1 << type)))
1643 return -EOPNOTSUPP;
Johannes Berg55682962007-09-20 13:09:35 -04001644
Johannes Berg9bc383d2009-11-19 11:55:19 +01001645 if (info->attrs[NL80211_ATTR_4ADDR]) {
Felix Fietkau8b787642009-11-10 18:53:10 +01001646 params.use_4addr = !!nla_get_u8(info->attrs[NL80211_ATTR_4ADDR]);
Johannes Bergad4bb6f2009-11-19 00:56:30 +01001647 err = nl80211_valid_4addr(rdev, NULL, params.use_4addr, type);
Johannes Berg9bc383d2009-11-19 11:55:19 +01001648 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02001649 return err;
Johannes Berg9bc383d2009-11-19 11:55:19 +01001650 }
Felix Fietkau8b787642009-11-10 18:53:10 +01001651
Michael Wu66f7ac52008-01-31 19:48:22 +01001652 err = parse_monitor_flags(type == NL80211_IFTYPE_MONITOR ?
1653 info->attrs[NL80211_ATTR_MNTR_FLAGS] : NULL,
1654 &flags);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001655 dev = rdev->ops->add_virtual_intf(&rdev->wiphy,
Michael Wu66f7ac52008-01-31 19:48:22 +01001656 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001657 type, err ? NULL : &flags, &params);
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001658 if (IS_ERR(dev))
1659 return PTR_ERR(dev);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01001660
Johannes Berg29cbe682010-12-03 09:20:44 +01001661 if (type == NL80211_IFTYPE_MESH_POINT &&
1662 info->attrs[NL80211_ATTR_MESH_ID]) {
1663 struct wireless_dev *wdev = dev->ieee80211_ptr;
1664
1665 wdev_lock(wdev);
1666 BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN !=
1667 IEEE80211_MAX_MESH_ID_LEN);
1668 wdev->mesh_id_up_len =
1669 nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
1670 memcpy(wdev->ssid, nla_data(info->attrs[NL80211_ATTR_MESH_ID]),
1671 wdev->mesh_id_up_len);
1672 wdev_unlock(wdev);
1673 }
1674
Johannes Bergf9e10ce2010-12-03 09:20:42 +01001675 return 0;
Johannes Berg55682962007-09-20 13:09:35 -04001676}
1677
1678static int nl80211_del_interface(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];
1681 struct net_device *dev = info->user_ptr[1];
Johannes Berg55682962007-09-20 13:09:35 -04001682
Johannes Berg4c476992010-10-04 21:36:35 +02001683 if (!rdev->ops->del_virtual_intf)
1684 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001685
Johannes Berg4c476992010-10-04 21:36:35 +02001686 return rdev->ops->del_virtual_intf(&rdev->wiphy, dev);
Johannes Berg55682962007-09-20 13:09:35 -04001687}
1688
Johannes Berg41ade002007-12-19 02:03:29 +01001689struct get_key_cookie {
1690 struct sk_buff *msg;
1691 int error;
Johannes Bergb9454e82009-07-08 13:29:08 +02001692 int idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001693};
1694
1695static void get_key_callback(void *c, struct key_params *params)
1696{
Johannes Bergb9454e82009-07-08 13:29:08 +02001697 struct nlattr *key;
Johannes Berg41ade002007-12-19 02:03:29 +01001698 struct get_key_cookie *cookie = c;
1699
1700 if (params->key)
1701 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_DATA,
1702 params->key_len, params->key);
1703
1704 if (params->seq)
1705 NLA_PUT(cookie->msg, NL80211_ATTR_KEY_SEQ,
1706 params->seq_len, params->seq);
1707
1708 if (params->cipher)
1709 NLA_PUT_U32(cookie->msg, NL80211_ATTR_KEY_CIPHER,
1710 params->cipher);
1711
Johannes Bergb9454e82009-07-08 13:29:08 +02001712 key = nla_nest_start(cookie->msg, NL80211_ATTR_KEY);
1713 if (!key)
1714 goto nla_put_failure;
1715
1716 if (params->key)
1717 NLA_PUT(cookie->msg, NL80211_KEY_DATA,
1718 params->key_len, params->key);
1719
1720 if (params->seq)
1721 NLA_PUT(cookie->msg, NL80211_KEY_SEQ,
1722 params->seq_len, params->seq);
1723
1724 if (params->cipher)
1725 NLA_PUT_U32(cookie->msg, NL80211_KEY_CIPHER,
1726 params->cipher);
1727
1728 NLA_PUT_U8(cookie->msg, NL80211_ATTR_KEY_IDX, cookie->idx);
1729
1730 nla_nest_end(cookie->msg, key);
1731
Johannes Berg41ade002007-12-19 02:03:29 +01001732 return;
1733 nla_put_failure:
1734 cookie->error = 1;
1735}
1736
1737static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
1738{
Johannes Berg4c476992010-10-04 21:36:35 +02001739 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001740 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001741 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001742 u8 key_idx = 0;
Johannes Berge31b8212010-10-05 19:39:30 +02001743 const u8 *mac_addr = NULL;
1744 bool pairwise;
Johannes Berg41ade002007-12-19 02:03:29 +01001745 struct get_key_cookie cookie = {
1746 .error = 0,
1747 };
1748 void *hdr;
1749 struct sk_buff *msg;
1750
1751 if (info->attrs[NL80211_ATTR_KEY_IDX])
1752 key_idx = nla_get_u8(info->attrs[NL80211_ATTR_KEY_IDX]);
1753
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001754 if (key_idx > 5)
Johannes Berg41ade002007-12-19 02:03:29 +01001755 return -EINVAL;
1756
1757 if (info->attrs[NL80211_ATTR_MAC])
1758 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1759
Johannes Berge31b8212010-10-05 19:39:30 +02001760 pairwise = !!mac_addr;
1761 if (info->attrs[NL80211_ATTR_KEY_TYPE]) {
1762 u32 kt = nla_get_u32(info->attrs[NL80211_ATTR_KEY_TYPE]);
1763 if (kt >= NUM_NL80211_KEYTYPES)
1764 return -EINVAL;
1765 if (kt != NL80211_KEYTYPE_GROUP &&
1766 kt != NL80211_KEYTYPE_PAIRWISE)
1767 return -EINVAL;
1768 pairwise = kt == NL80211_KEYTYPE_PAIRWISE;
1769 }
1770
Johannes Berg4c476992010-10-04 21:36:35 +02001771 if (!rdev->ops->get_key)
1772 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001773
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07001774 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02001775 if (!msg)
1776 return -ENOMEM;
Johannes Berg41ade002007-12-19 02:03:29 +01001777
1778 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
1779 NL80211_CMD_NEW_KEY);
Johannes Berg4c476992010-10-04 21:36:35 +02001780 if (IS_ERR(hdr))
1781 return PTR_ERR(hdr);
Johannes Berg41ade002007-12-19 02:03:29 +01001782
1783 cookie.msg = msg;
Johannes Bergb9454e82009-07-08 13:29:08 +02001784 cookie.idx = key_idx;
Johannes Berg41ade002007-12-19 02:03:29 +01001785
1786 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
1787 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_idx);
1788 if (mac_addr)
1789 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
1790
Johannes Berge31b8212010-10-05 19:39:30 +02001791 if (pairwise && mac_addr &&
1792 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1793 return -ENOENT;
1794
1795 err = rdev->ops->get_key(&rdev->wiphy, dev, key_idx, pairwise,
1796 mac_addr, &cookie, get_key_callback);
Johannes Berg41ade002007-12-19 02:03:29 +01001797
1798 if (err)
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001799 goto free_msg;
Johannes Berg41ade002007-12-19 02:03:29 +01001800
1801 if (cookie.error)
1802 goto nla_put_failure;
1803
1804 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02001805 return genlmsg_reply(msg, info);
Johannes Berg41ade002007-12-19 02:03:29 +01001806
1807 nla_put_failure:
1808 err = -ENOBUFS;
Niko Jokinen6c95e2a2009-07-15 11:00:53 +03001809 free_msg:
Johannes Berg41ade002007-12-19 02:03:29 +01001810 nlmsg_free(msg);
Johannes Berg41ade002007-12-19 02:03:29 +01001811 return err;
1812}
1813
1814static int nl80211_set_key(struct sk_buff *skb, struct genl_info *info)
1815{
Johannes Berg4c476992010-10-04 21:36:35 +02001816 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergb9454e82009-07-08 13:29:08 +02001817 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001818 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001819 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001820
Johannes Bergb9454e82009-07-08 13:29:08 +02001821 err = nl80211_parse_key(info, &key);
1822 if (err)
1823 return err;
1824
1825 if (key.idx < 0)
Johannes Berg41ade002007-12-19 02:03:29 +01001826 return -EINVAL;
1827
Johannes Bergb9454e82009-07-08 13:29:08 +02001828 /* only support setting default key */
1829 if (!key.def && !key.defmgmt)
Johannes Berg41ade002007-12-19 02:03:29 +01001830 return -EINVAL;
1831
Johannes Bergfffd0932009-07-08 14:22:54 +02001832 wdev_lock(dev->ieee80211_ptr);
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001833
1834 if (key.def) {
1835 if (!rdev->ops->set_default_key) {
1836 err = -EOPNOTSUPP;
1837 goto out;
1838 }
1839
1840 err = nl80211_key_allowed(dev->ieee80211_ptr);
1841 if (err)
1842 goto out;
1843
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001844 err = rdev->ops->set_default_key(&rdev->wiphy, dev, key.idx,
1845 key.def_uni, key.def_multi);
1846
1847 if (err)
1848 goto out;
Johannes Bergfffd0932009-07-08 14:22:54 +02001849
Johannes Berg3d23e342009-09-29 23:27:28 +02001850#ifdef CONFIG_CFG80211_WEXT
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001851 dev->ieee80211_ptr->wext.default_key = key.idx;
Johannes Berg08645122009-05-11 13:54:58 +02001852#endif
Johannes Bergdbd2fd62010-12-09 19:58:59 +01001853 } else {
1854 if (key.def_uni || !key.def_multi) {
1855 err = -EINVAL;
1856 goto out;
1857 }
1858
1859 if (!rdev->ops->set_default_mgmt_key) {
1860 err = -EOPNOTSUPP;
1861 goto out;
1862 }
1863
1864 err = nl80211_key_allowed(dev->ieee80211_ptr);
1865 if (err)
1866 goto out;
1867
1868 err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
1869 dev, key.idx);
1870 if (err)
1871 goto out;
1872
1873#ifdef CONFIG_CFG80211_WEXT
1874 dev->ieee80211_ptr->wext.default_mgmt_key = key.idx;
1875#endif
1876 }
1877
1878 out:
Johannes Bergfffd0932009-07-08 14:22:54 +02001879 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001880
Johannes Berg41ade002007-12-19 02:03:29 +01001881 return err;
1882}
1883
1884static int nl80211_new_key(struct sk_buff *skb, struct genl_info *info)
1885{
Johannes Berg4c476992010-10-04 21:36:35 +02001886 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergfffd0932009-07-08 14:22:54 +02001887 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001888 struct net_device *dev = info->user_ptr[1];
Johannes Bergb9454e82009-07-08 13:29:08 +02001889 struct key_parse key;
Johannes Berge31b8212010-10-05 19:39:30 +02001890 const u8 *mac_addr = NULL;
Johannes Berg41ade002007-12-19 02:03:29 +01001891
Johannes Bergb9454e82009-07-08 13:29:08 +02001892 err = nl80211_parse_key(info, &key);
1893 if (err)
1894 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001895
Johannes Bergb9454e82009-07-08 13:29:08 +02001896 if (!key.p.key)
Johannes Berg41ade002007-12-19 02:03:29 +01001897 return -EINVAL;
1898
Johannes Berg41ade002007-12-19 02:03:29 +01001899 if (info->attrs[NL80211_ATTR_MAC])
1900 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1901
Johannes Berge31b8212010-10-05 19:39:30 +02001902 if (key.type == -1) {
1903 if (mac_addr)
1904 key.type = NL80211_KEYTYPE_PAIRWISE;
1905 else
1906 key.type = NL80211_KEYTYPE_GROUP;
1907 }
1908
1909 /* for now */
1910 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1911 key.type != NL80211_KEYTYPE_GROUP)
1912 return -EINVAL;
1913
Johannes Berg4c476992010-10-04 21:36:35 +02001914 if (!rdev->ops->add_key)
1915 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01001916
Johannes Berge31b8212010-10-05 19:39:30 +02001917 if (cfg80211_validate_key_settings(rdev, &key.p, key.idx,
1918 key.type == NL80211_KEYTYPE_PAIRWISE,
1919 mac_addr))
Johannes Berg4c476992010-10-04 21:36:35 +02001920 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02001921
1922 wdev_lock(dev->ieee80211_ptr);
1923 err = nl80211_key_allowed(dev->ieee80211_ptr);
1924 if (!err)
1925 err = rdev->ops->add_key(&rdev->wiphy, dev, key.idx,
Johannes Berge31b8212010-10-05 19:39:30 +02001926 key.type == NL80211_KEYTYPE_PAIRWISE,
Johannes Bergfffd0932009-07-08 14:22:54 +02001927 mac_addr, &key.p);
1928 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg41ade002007-12-19 02:03:29 +01001929
Johannes Berg41ade002007-12-19 02:03:29 +01001930 return err;
1931}
1932
1933static int nl80211_del_key(struct sk_buff *skb, struct genl_info *info)
1934{
Johannes Berg4c476992010-10-04 21:36:35 +02001935 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg41ade002007-12-19 02:03:29 +01001936 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02001937 struct net_device *dev = info->user_ptr[1];
Johannes Berg41ade002007-12-19 02:03:29 +01001938 u8 *mac_addr = NULL;
Johannes Bergb9454e82009-07-08 13:29:08 +02001939 struct key_parse key;
Johannes Berg41ade002007-12-19 02:03:29 +01001940
Johannes Bergb9454e82009-07-08 13:29:08 +02001941 err = nl80211_parse_key(info, &key);
1942 if (err)
1943 return err;
Johannes Berg41ade002007-12-19 02:03:29 +01001944
1945 if (info->attrs[NL80211_ATTR_MAC])
1946 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
1947
Johannes Berge31b8212010-10-05 19:39:30 +02001948 if (key.type == -1) {
1949 if (mac_addr)
1950 key.type = NL80211_KEYTYPE_PAIRWISE;
1951 else
1952 key.type = NL80211_KEYTYPE_GROUP;
1953 }
1954
1955 /* for now */
1956 if (key.type != NL80211_KEYTYPE_PAIRWISE &&
1957 key.type != NL80211_KEYTYPE_GROUP)
1958 return -EINVAL;
1959
Johannes Berg4c476992010-10-04 21:36:35 +02001960 if (!rdev->ops->del_key)
1961 return -EOPNOTSUPP;
Johannes Berg41ade002007-12-19 02:03:29 +01001962
Johannes Bergfffd0932009-07-08 14:22:54 +02001963 wdev_lock(dev->ieee80211_ptr);
1964 err = nl80211_key_allowed(dev->ieee80211_ptr);
Johannes Berge31b8212010-10-05 19:39:30 +02001965
1966 if (key.type == NL80211_KEYTYPE_PAIRWISE && mac_addr &&
1967 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
1968 err = -ENOENT;
1969
Johannes Bergfffd0932009-07-08 14:22:54 +02001970 if (!err)
Johannes Berge31b8212010-10-05 19:39:30 +02001971 err = rdev->ops->del_key(&rdev->wiphy, dev, key.idx,
1972 key.type == NL80211_KEYTYPE_PAIRWISE,
1973 mac_addr);
Johannes Berg41ade002007-12-19 02:03:29 +01001974
Johannes Berg3d23e342009-09-29 23:27:28 +02001975#ifdef CONFIG_CFG80211_WEXT
Johannes Berg08645122009-05-11 13:54:58 +02001976 if (!err) {
Johannes Bergb9454e82009-07-08 13:29:08 +02001977 if (key.idx == dev->ieee80211_ptr->wext.default_key)
Johannes Berg08645122009-05-11 13:54:58 +02001978 dev->ieee80211_ptr->wext.default_key = -1;
Johannes Bergb9454e82009-07-08 13:29:08 +02001979 else if (key.idx == dev->ieee80211_ptr->wext.default_mgmt_key)
Johannes Berg08645122009-05-11 13:54:58 +02001980 dev->ieee80211_ptr->wext.default_mgmt_key = -1;
1981 }
1982#endif
Johannes Bergfffd0932009-07-08 14:22:54 +02001983 wdev_unlock(dev->ieee80211_ptr);
Johannes Berg08645122009-05-11 13:54:58 +02001984
Johannes Berg41ade002007-12-19 02:03:29 +01001985 return err;
1986}
1987
Johannes Berged1b6cc2007-12-19 02:03:32 +01001988static int nl80211_addset_beacon(struct sk_buff *skb, struct genl_info *info)
1989{
1990 int (*call)(struct wiphy *wiphy, struct net_device *dev,
1991 struct beacon_parameters *info);
Johannes Berg4c476992010-10-04 21:36:35 +02001992 struct cfg80211_registered_device *rdev = info->user_ptr[0];
1993 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02001994 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001995 struct beacon_parameters params;
Johannes Berg56d18932011-05-09 18:41:15 +02001996 int haveinfo = 0, err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01001997
Jouni Malinen9946ecf2011-08-10 23:55:56 +03001998 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_BEACON_TAIL]) ||
1999 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]) ||
2000 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_PROBE_RESP]) ||
2001 !is_valid_ie_attr(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]))
Johannes Bergf4a11bb2009-03-27 12:40:28 +01002002 return -EINVAL;
2003
Johannes Berg074ac8d2010-09-16 14:58:22 +02002004 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002005 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2006 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002007
Johannes Berg56d18932011-05-09 18:41:15 +02002008 memset(&params, 0, sizeof(params));
2009
Johannes Berged1b6cc2007-12-19 02:03:32 +01002010 switch (info->genlhdr->cmd) {
2011 case NL80211_CMD_NEW_BEACON:
2012 /* these are required for NEW_BEACON */
2013 if (!info->attrs[NL80211_ATTR_BEACON_INTERVAL] ||
2014 !info->attrs[NL80211_ATTR_DTIM_PERIOD] ||
Johannes Berg4c476992010-10-04 21:36:35 +02002015 !info->attrs[NL80211_ATTR_BEACON_HEAD])
2016 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002017
Johannes Berg56d18932011-05-09 18:41:15 +02002018 params.interval =
2019 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
2020 params.dtim_period =
2021 nla_get_u32(info->attrs[NL80211_ATTR_DTIM_PERIOD]);
2022
2023 err = cfg80211_validate_beacon_int(rdev, params.interval);
2024 if (err)
2025 return err;
2026
Jouni Malinen32e9de82011-08-10 23:53:31 +03002027 /*
2028 * In theory, some of these attributes could be required for
2029 * NEW_BEACON, but since they were not used when the command was
2030 * originally added, keep them optional for old user space
2031 * programs to work with drivers that do not need the additional
2032 * information.
2033 */
2034 if (info->attrs[NL80211_ATTR_SSID]) {
2035 params.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
2036 params.ssid_len =
2037 nla_len(info->attrs[NL80211_ATTR_SSID]);
2038 if (params.ssid_len == 0 ||
2039 params.ssid_len > IEEE80211_MAX_SSID_LEN)
2040 return -EINVAL;
2041 }
2042
2043 if (info->attrs[NL80211_ATTR_HIDDEN_SSID]) {
2044 params.hidden_ssid = nla_get_u32(
2045 info->attrs[NL80211_ATTR_HIDDEN_SSID]);
2046 if (params.hidden_ssid !=
2047 NL80211_HIDDEN_SSID_NOT_IN_USE &&
2048 params.hidden_ssid !=
2049 NL80211_HIDDEN_SSID_ZERO_LEN &&
2050 params.hidden_ssid !=
2051 NL80211_HIDDEN_SSID_ZERO_CONTENTS)
2052 return -EINVAL;
2053 }
2054
Jouni Malinen5fb628e2011-08-10 23:54:35 +03002055 params.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
2056
2057 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
2058 params.auth_type = nla_get_u32(
2059 info->attrs[NL80211_ATTR_AUTH_TYPE]);
2060 if (!nl80211_valid_auth_type(params.auth_type))
2061 return -EINVAL;
2062 } else
2063 params.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
2064
2065 err = nl80211_crypto_settings(rdev, info, &params.crypto,
2066 NL80211_MAX_NR_CIPHER_SUITES);
2067 if (err)
2068 return err;
2069
Johannes Berg79c97e92009-07-07 03:56:12 +02002070 call = rdev->ops->add_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002071 break;
2072 case NL80211_CMD_SET_BEACON:
Johannes Berg79c97e92009-07-07 03:56:12 +02002073 call = rdev->ops->set_beacon;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002074 break;
2075 default:
2076 WARN_ON(1);
Johannes Berg4c476992010-10-04 21:36:35 +02002077 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002078 }
2079
Johannes Berg4c476992010-10-04 21:36:35 +02002080 if (!call)
2081 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002082
Johannes Berged1b6cc2007-12-19 02:03:32 +01002083 if (info->attrs[NL80211_ATTR_BEACON_HEAD]) {
2084 params.head = nla_data(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2085 params.head_len =
2086 nla_len(info->attrs[NL80211_ATTR_BEACON_HEAD]);
2087 haveinfo = 1;
2088 }
2089
2090 if (info->attrs[NL80211_ATTR_BEACON_TAIL]) {
2091 params.tail = nla_data(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2092 params.tail_len =
2093 nla_len(info->attrs[NL80211_ATTR_BEACON_TAIL]);
2094 haveinfo = 1;
2095 }
2096
Johannes Berg4c476992010-10-04 21:36:35 +02002097 if (!haveinfo)
2098 return -EINVAL;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002099
Jouni Malinen9946ecf2011-08-10 23:55:56 +03002100 if (info->attrs[NL80211_ATTR_IE]) {
2101 params.beacon_ies = nla_data(info->attrs[NL80211_ATTR_IE]);
2102 params.beacon_ies_len = nla_len(info->attrs[NL80211_ATTR_IE]);
2103 }
2104
2105 if (info->attrs[NL80211_ATTR_IE_PROBE_RESP]) {
2106 params.proberesp_ies =
2107 nla_data(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2108 params.proberesp_ies_len =
2109 nla_len(info->attrs[NL80211_ATTR_IE_PROBE_RESP]);
2110 }
2111
2112 if (info->attrs[NL80211_ATTR_IE_ASSOC_RESP]) {
2113 params.assocresp_ies =
2114 nla_data(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2115 params.assocresp_ies_len =
2116 nla_len(info->attrs[NL80211_ATTR_IE_ASSOC_RESP]);
2117 }
2118
Johannes Berg56d18932011-05-09 18:41:15 +02002119 err = call(&rdev->wiphy, dev, &params);
2120 if (!err && params.interval)
2121 wdev->beacon_interval = params.interval;
2122 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002123}
2124
2125static int nl80211_del_beacon(struct sk_buff *skb, struct genl_info *info)
2126{
Johannes Berg4c476992010-10-04 21:36:35 +02002127 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2128 struct net_device *dev = info->user_ptr[1];
Johannes Berg56d18932011-05-09 18:41:15 +02002129 struct wireless_dev *wdev = dev->ieee80211_ptr;
2130 int err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002131
Johannes Berg4c476992010-10-04 21:36:35 +02002132 if (!rdev->ops->del_beacon)
2133 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002134
Johannes Berg074ac8d2010-09-16 14:58:22 +02002135 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002136 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2137 return -EOPNOTSUPP;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002138
Johannes Berg56d18932011-05-09 18:41:15 +02002139 err = rdev->ops->del_beacon(&rdev->wiphy, dev);
2140 if (!err)
2141 wdev->beacon_interval = 0;
2142 return err;
Johannes Berged1b6cc2007-12-19 02:03:32 +01002143}
2144
Johannes Berg5727ef12007-12-19 02:03:34 +01002145static const struct nla_policy sta_flags_policy[NL80211_STA_FLAG_MAX + 1] = {
2146 [NL80211_STA_FLAG_AUTHORIZED] = { .type = NLA_FLAG },
2147 [NL80211_STA_FLAG_SHORT_PREAMBLE] = { .type = NLA_FLAG },
2148 [NL80211_STA_FLAG_WME] = { .type = NLA_FLAG },
Jouni Malinen0e467242009-05-11 21:57:55 +03002149 [NL80211_STA_FLAG_MFP] = { .type = NLA_FLAG },
Javier Cardonab39c48f2011-04-07 15:08:30 -07002150 [NL80211_STA_FLAG_AUTHENTICATED] = { .type = NLA_FLAG },
Johannes Berg5727ef12007-12-19 02:03:34 +01002151};
2152
Johannes Bergeccb8e82009-05-11 21:57:56 +03002153static int parse_station_flags(struct genl_info *info,
2154 struct station_parameters *params)
Johannes Berg5727ef12007-12-19 02:03:34 +01002155{
2156 struct nlattr *flags[NL80211_STA_FLAG_MAX + 1];
Johannes Bergeccb8e82009-05-11 21:57:56 +03002157 struct nlattr *nla;
Johannes Berg5727ef12007-12-19 02:03:34 +01002158 int flag;
2159
Johannes Bergeccb8e82009-05-11 21:57:56 +03002160 /*
2161 * Try parsing the new attribute first so userspace
2162 * can specify both for older kernels.
2163 */
2164 nla = info->attrs[NL80211_ATTR_STA_FLAGS2];
2165 if (nla) {
2166 struct nl80211_sta_flag_update *sta_flags;
Johannes Berg5727ef12007-12-19 02:03:34 +01002167
Johannes Bergeccb8e82009-05-11 21:57:56 +03002168 sta_flags = nla_data(nla);
2169 params->sta_flags_mask = sta_flags->mask;
2170 params->sta_flags_set = sta_flags->set;
2171 if ((params->sta_flags_mask |
2172 params->sta_flags_set) & BIT(__NL80211_STA_FLAG_INVALID))
2173 return -EINVAL;
2174 return 0;
2175 }
2176
2177 /* if present, parse the old attribute */
2178
2179 nla = info->attrs[NL80211_ATTR_STA_FLAGS];
Johannes Berg5727ef12007-12-19 02:03:34 +01002180 if (!nla)
2181 return 0;
2182
2183 if (nla_parse_nested(flags, NL80211_STA_FLAG_MAX,
2184 nla, sta_flags_policy))
2185 return -EINVAL;
2186
Johannes Bergeccb8e82009-05-11 21:57:56 +03002187 params->sta_flags_mask = (1 << __NL80211_STA_FLAG_AFTER_LAST) - 1;
2188 params->sta_flags_mask &= ~1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002189
2190 for (flag = 1; flag <= NL80211_STA_FLAG_MAX; flag++)
2191 if (flags[flag])
Johannes Bergeccb8e82009-05-11 21:57:56 +03002192 params->sta_flags_set |= (1<<flag);
Johannes Berg5727ef12007-12-19 02:03:34 +01002193
2194 return 0;
2195}
2196
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002197static bool nl80211_put_sta_rate(struct sk_buff *msg, struct rate_info *info,
2198 int attr)
2199{
2200 struct nlattr *rate;
2201 u16 bitrate;
2202
2203 rate = nla_nest_start(msg, attr);
2204 if (!rate)
2205 goto nla_put_failure;
2206
2207 /* cfg80211_calculate_bitrate will return 0 for mcs >= 32 */
2208 bitrate = cfg80211_calculate_bitrate(info);
2209 if (bitrate > 0)
2210 NLA_PUT_U16(msg, NL80211_RATE_INFO_BITRATE, bitrate);
2211
2212 if (info->flags & RATE_INFO_FLAGS_MCS)
2213 NLA_PUT_U8(msg, NL80211_RATE_INFO_MCS, info->mcs);
2214 if (info->flags & RATE_INFO_FLAGS_40_MHZ_WIDTH)
2215 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_40_MHZ_WIDTH);
2216 if (info->flags & RATE_INFO_FLAGS_SHORT_GI)
2217 NLA_PUT_FLAG(msg, NL80211_RATE_INFO_SHORT_GI);
2218
2219 nla_nest_end(msg, rate);
2220 return true;
2221
2222nla_put_failure:
2223 return false;
2224}
2225
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002226static int nl80211_send_station(struct sk_buff *msg, u32 pid, u32 seq,
2227 int flags, struct net_device *dev,
Johannes Berg98b62182009-12-23 13:15:44 +01002228 const u8 *mac_addr, struct station_info *sinfo)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002229{
2230 void *hdr;
Paul Stewartf4263c92011-03-31 09:25:41 -07002231 struct nlattr *sinfoattr, *bss_param;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002232
2233 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2234 if (!hdr)
2235 return -1;
2236
2237 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2238 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
2239
Johannes Bergf5ea9122009-08-07 16:17:38 +02002240 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, sinfo->generation);
2241
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002242 sinfoattr = nla_nest_start(msg, NL80211_ATTR_STA_INFO);
2243 if (!sinfoattr)
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002244 goto nla_put_failure;
Mohammed Shafi Shajakhanebe27c92011-04-08 21:24:24 +05302245 if (sinfo->filled & STATION_INFO_CONNECTED_TIME)
2246 NLA_PUT_U32(msg, NL80211_STA_INFO_CONNECTED_TIME,
2247 sinfo->connected_time);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002248 if (sinfo->filled & STATION_INFO_INACTIVE_TIME)
2249 NLA_PUT_U32(msg, NL80211_STA_INFO_INACTIVE_TIME,
2250 sinfo->inactive_time);
2251 if (sinfo->filled & STATION_INFO_RX_BYTES)
2252 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_BYTES,
2253 sinfo->rx_bytes);
2254 if (sinfo->filled & STATION_INFO_TX_BYTES)
2255 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_BYTES,
2256 sinfo->tx_bytes);
2257 if (sinfo->filled & STATION_INFO_LLID)
2258 NLA_PUT_U16(msg, NL80211_STA_INFO_LLID,
2259 sinfo->llid);
2260 if (sinfo->filled & STATION_INFO_PLID)
2261 NLA_PUT_U16(msg, NL80211_STA_INFO_PLID,
2262 sinfo->plid);
2263 if (sinfo->filled & STATION_INFO_PLINK_STATE)
2264 NLA_PUT_U8(msg, NL80211_STA_INFO_PLINK_STATE,
2265 sinfo->plink_state);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002266 if (sinfo->filled & STATION_INFO_SIGNAL)
2267 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL,
2268 sinfo->signal);
Bruno Randolf541a45a2010-12-02 19:12:43 +09002269 if (sinfo->filled & STATION_INFO_SIGNAL_AVG)
2270 NLA_PUT_U8(msg, NL80211_STA_INFO_SIGNAL_AVG,
2271 sinfo->signal_avg);
Henning Rogge420e7fa2008-12-11 22:04:19 +01002272 if (sinfo->filled & STATION_INFO_TX_BITRATE) {
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002273 if (!nl80211_put_sta_rate(msg, &sinfo->txrate,
2274 NL80211_STA_INFO_TX_BITRATE))
Henning Rogge420e7fa2008-12-11 22:04:19 +01002275 goto nla_put_failure;
Felix Fietkauc8dcfd82011-02-27 22:08:00 +01002276 }
2277 if (sinfo->filled & STATION_INFO_RX_BITRATE) {
2278 if (!nl80211_put_sta_rate(msg, &sinfo->rxrate,
2279 NL80211_STA_INFO_RX_BITRATE))
2280 goto nla_put_failure;
Henning Rogge420e7fa2008-12-11 22:04:19 +01002281 }
Jouni Malinen98c8a60a2009-02-17 13:24:57 +02002282 if (sinfo->filled & STATION_INFO_RX_PACKETS)
2283 NLA_PUT_U32(msg, NL80211_STA_INFO_RX_PACKETS,
2284 sinfo->rx_packets);
2285 if (sinfo->filled & STATION_INFO_TX_PACKETS)
2286 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_PACKETS,
2287 sinfo->tx_packets);
Bruno Randolfb206b4e2010-10-06 18:34:12 +09002288 if (sinfo->filled & STATION_INFO_TX_RETRIES)
2289 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_RETRIES,
2290 sinfo->tx_retries);
2291 if (sinfo->filled & STATION_INFO_TX_FAILED)
2292 NLA_PUT_U32(msg, NL80211_STA_INFO_TX_FAILED,
2293 sinfo->tx_failed);
Paul Stewartf4263c92011-03-31 09:25:41 -07002294 if (sinfo->filled & STATION_INFO_BSS_PARAM) {
2295 bss_param = nla_nest_start(msg, NL80211_STA_INFO_BSS_PARAM);
2296 if (!bss_param)
2297 goto nla_put_failure;
2298
2299 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_CTS_PROT)
2300 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_CTS_PROT);
2301 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_PREAMBLE)
2302 NLA_PUT_FLAG(msg, NL80211_STA_BSS_PARAM_SHORT_PREAMBLE);
2303 if (sinfo->bss_param.flags & BSS_PARAM_FLAGS_SHORT_SLOT_TIME)
2304 NLA_PUT_FLAG(msg,
2305 NL80211_STA_BSS_PARAM_SHORT_SLOT_TIME);
2306 NLA_PUT_U8(msg, NL80211_STA_BSS_PARAM_DTIM_PERIOD,
2307 sinfo->bss_param.dtim_period);
2308 NLA_PUT_U16(msg, NL80211_STA_BSS_PARAM_BEACON_INTERVAL,
2309 sinfo->bss_param.beacon_interval);
2310
2311 nla_nest_end(msg, bss_param);
2312 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002313 nla_nest_end(msg, sinfoattr);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002314
Felix Fietkau040bdf72011-08-10 19:00:33 -06002315 if (sinfo->filled & STATION_INFO_ASSOC_REQ_IES)
Jouni Malinen50d3dfb2011-08-08 12:11:52 +03002316 NLA_PUT(msg, NL80211_ATTR_IE, sinfo->assoc_req_ies_len,
2317 sinfo->assoc_req_ies);
2318
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002319 return genlmsg_end(msg, hdr);
2320
2321 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002322 genlmsg_cancel(msg, hdr);
2323 return -EMSGSIZE;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002324}
2325
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002326static int nl80211_dump_station(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002327 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002328{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002329 struct station_info sinfo;
2330 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002331 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002332 u8 mac_addr[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002333 int sta_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002334 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002335
Johannes Berg67748892010-10-04 21:14:06 +02002336 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2337 if (err)
2338 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002339
2340 if (!dev->ops->dump_station) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002341 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002342 goto out_err;
2343 }
2344
Johannes Bergbba95fe2008-07-29 13:22:51 +02002345 while (1) {
Jouni Malinenf612ced2011-08-11 11:46:22 +03002346 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergbba95fe2008-07-29 13:22:51 +02002347 err = dev->ops->dump_station(&dev->wiphy, netdev, sta_idx,
2348 mac_addr, &sinfo);
2349 if (err == -ENOENT)
2350 break;
2351 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002352 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002353
2354 if (nl80211_send_station(skb,
2355 NETLINK_CB(cb->skb).pid,
2356 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2357 netdev, mac_addr,
2358 &sinfo) < 0)
2359 goto out;
2360
2361 sta_idx++;
2362 }
2363
2364
2365 out:
2366 cb->args[1] = sta_idx;
2367 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002368 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002369 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002370
2371 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002372}
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002373
Johannes Berg5727ef12007-12-19 02:03:34 +01002374static int nl80211_get_station(struct sk_buff *skb, struct genl_info *info)
2375{
Johannes Berg4c476992010-10-04 21:36:35 +02002376 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2377 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002378 struct station_info sinfo;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002379 struct sk_buff *msg;
2380 u8 *mac_addr = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02002381 int err;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002382
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002383 memset(&sinfo, 0, sizeof(sinfo));
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002384
2385 if (!info->attrs[NL80211_ATTR_MAC])
2386 return -EINVAL;
2387
2388 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2389
Johannes Berg4c476992010-10-04 21:36:35 +02002390 if (!rdev->ops->get_station)
2391 return -EOPNOTSUPP;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002392
Johannes Berg79c97e92009-07-07 03:56:12 +02002393 err = rdev->ops->get_station(&rdev->wiphy, dev, mac_addr, &sinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002394 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002395 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002396
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002397 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002398 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002399 return -ENOMEM;
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002400
2401 if (nl80211_send_station(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002402 dev, mac_addr, &sinfo) < 0) {
2403 nlmsg_free(msg);
2404 return -ENOBUFS;
2405 }
Johannes Bergfd5b74d2007-12-19 02:03:36 +01002406
Johannes Berg4c476992010-10-04 21:36:35 +02002407 return genlmsg_reply(msg, info);
Johannes Berg5727ef12007-12-19 02:03:34 +01002408}
2409
2410/*
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002411 * Get vlan interface making sure it is running and on the right wiphy.
Johannes Berg5727ef12007-12-19 02:03:34 +01002412 */
Johannes Berg463d0182009-07-14 00:33:35 +02002413static int get_vlan(struct genl_info *info,
Johannes Berg5727ef12007-12-19 02:03:34 +01002414 struct cfg80211_registered_device *rdev,
2415 struct net_device **vlan)
2416{
Johannes Berg463d0182009-07-14 00:33:35 +02002417 struct nlattr *vlanattr = info->attrs[NL80211_ATTR_STA_VLAN];
Johannes Berg5727ef12007-12-19 02:03:34 +01002418 *vlan = NULL;
2419
2420 if (vlanattr) {
Johannes Berg463d0182009-07-14 00:33:35 +02002421 *vlan = dev_get_by_index(genl_info_net(info),
2422 nla_get_u32(vlanattr));
Johannes Berg5727ef12007-12-19 02:03:34 +01002423 if (!*vlan)
2424 return -ENODEV;
2425 if (!(*vlan)->ieee80211_ptr)
2426 return -EINVAL;
2427 if ((*vlan)->ieee80211_ptr->wiphy != &rdev->wiphy)
2428 return -EINVAL;
Felix Fietkauc258d2d2009-11-11 17:23:31 +01002429 if (!netif_running(*vlan))
2430 return -ENETDOWN;
Johannes Berg5727ef12007-12-19 02:03:34 +01002431 }
2432 return 0;
2433}
2434
2435static int nl80211_set_station(struct sk_buff *skb, struct genl_info *info)
2436{
Johannes Berg4c476992010-10-04 21:36:35 +02002437 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002438 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002439 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002440 struct station_parameters params;
2441 u8 *mac_addr = NULL;
2442
2443 memset(&params, 0, sizeof(params));
2444
2445 params.listen_interval = -1;
Javier Cardona57cf8042011-05-13 10:45:43 -07002446 params.plink_state = -1;
Johannes Berg5727ef12007-12-19 02:03:34 +01002447
2448 if (info->attrs[NL80211_ATTR_STA_AID])
2449 return -EINVAL;
2450
2451 if (!info->attrs[NL80211_ATTR_MAC])
2452 return -EINVAL;
2453
2454 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2455
2456 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]) {
2457 params.supported_rates =
2458 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2459 params.supported_rates_len =
2460 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2461 }
2462
2463 if (info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2464 params.listen_interval =
2465 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
2466
Jouni Malinen36aedc92008-08-25 11:58:58 +03002467 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2468 params.ht_capa =
2469 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
2470
Johannes Bergeccb8e82009-05-11 21:57:56 +03002471 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002472 return -EINVAL;
2473
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002474 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2475 params.plink_action =
2476 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2477
Javier Cardona9c3990a2011-05-03 16:57:11 -07002478 if (info->attrs[NL80211_ATTR_STA_PLINK_STATE])
2479 params.plink_state =
2480 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_STATE]);
2481
Johannes Berg463d0182009-07-14 00:33:35 +02002482 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002483 if (err)
Johannes Berg034d6552009-05-27 10:35:29 +02002484 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002485
2486 /* validate settings */
2487 err = 0;
2488
2489 switch (dev->ieee80211_ptr->iftype) {
2490 case NL80211_IFTYPE_AP:
2491 case NL80211_IFTYPE_AP_VLAN:
Johannes Berg074ac8d2010-09-16 14:58:22 +02002492 case NL80211_IFTYPE_P2P_GO:
Johannes Berga97f4422009-06-18 17:23:43 +02002493 /* disallow mesh-specific things */
2494 if (params.plink_action)
2495 err = -EINVAL;
2496 break;
Johannes Berg074ac8d2010-09-16 14:58:22 +02002497 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berga97f4422009-06-18 17:23:43 +02002498 case NL80211_IFTYPE_STATION:
2499 /* disallow everything but AUTHORIZED flag */
2500 if (params.plink_action)
2501 err = -EINVAL;
2502 if (params.vlan)
2503 err = -EINVAL;
2504 if (params.supported_rates)
2505 err = -EINVAL;
2506 if (params.ht_capa)
2507 err = -EINVAL;
2508 if (params.listen_interval >= 0)
2509 err = -EINVAL;
2510 if (params.sta_flags_mask & ~BIT(NL80211_STA_FLAG_AUTHORIZED))
2511 err = -EINVAL;
2512 break;
2513 case NL80211_IFTYPE_MESH_POINT:
2514 /* disallow things mesh doesn't support */
2515 if (params.vlan)
2516 err = -EINVAL;
2517 if (params.ht_capa)
2518 err = -EINVAL;
2519 if (params.listen_interval >= 0)
2520 err = -EINVAL;
Javier Cardonab39c48f2011-04-07 15:08:30 -07002521 if (params.sta_flags_mask &
2522 ~(BIT(NL80211_STA_FLAG_AUTHENTICATED) |
Thomas Pedersen84298282011-05-03 16:57:13 -07002523 BIT(NL80211_STA_FLAG_MFP) |
Javier Cardonab39c48f2011-04-07 15:08:30 -07002524 BIT(NL80211_STA_FLAG_AUTHORIZED)))
Johannes Berga97f4422009-06-18 17:23:43 +02002525 err = -EINVAL;
2526 break;
2527 default:
2528 err = -EINVAL;
Johannes Berg034d6552009-05-27 10:35:29 +02002529 }
2530
Johannes Berg5727ef12007-12-19 02:03:34 +01002531 if (err)
2532 goto out;
2533
Johannes Berg79c97e92009-07-07 03:56:12 +02002534 if (!rdev->ops->change_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002535 err = -EOPNOTSUPP;
2536 goto out;
2537 }
2538
Johannes Berg79c97e92009-07-07 03:56:12 +02002539 err = rdev->ops->change_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002540
2541 out:
2542 if (params.vlan)
2543 dev_put(params.vlan);
Johannes Berg3b858752009-03-12 09:55:09 +01002544
Johannes Berg5727ef12007-12-19 02:03:34 +01002545 return err;
2546}
2547
2548static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
2549{
Johannes Berg4c476992010-10-04 21:36:35 +02002550 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg5727ef12007-12-19 02:03:34 +01002551 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002552 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002553 struct station_parameters params;
2554 u8 *mac_addr = NULL;
2555
2556 memset(&params, 0, sizeof(params));
2557
2558 if (!info->attrs[NL80211_ATTR_MAC])
2559 return -EINVAL;
2560
Johannes Berg5727ef12007-12-19 02:03:34 +01002561 if (!info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL])
2562 return -EINVAL;
2563
2564 if (!info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES])
2565 return -EINVAL;
2566
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002567 if (!info->attrs[NL80211_ATTR_STA_AID])
2568 return -EINVAL;
2569
Johannes Berg5727ef12007-12-19 02:03:34 +01002570 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2571 params.supported_rates =
2572 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2573 params.supported_rates_len =
2574 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_RATES]);
2575 params.listen_interval =
2576 nla_get_u16(info->attrs[NL80211_ATTR_STA_LISTEN_INTERVAL]);
Johannes Berg51b50fb2009-05-24 16:42:30 +02002577
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002578 params.aid = nla_get_u16(info->attrs[NL80211_ATTR_STA_AID]);
2579 if (!params.aid || params.aid > IEEE80211_MAX_AID)
2580 return -EINVAL;
Johannes Berg51b50fb2009-05-24 16:42:30 +02002581
Jouni Malinen36aedc92008-08-25 11:58:58 +03002582 if (info->attrs[NL80211_ATTR_HT_CAPABILITY])
2583 params.ht_capa =
2584 nla_data(info->attrs[NL80211_ATTR_HT_CAPABILITY]);
Johannes Berg5727ef12007-12-19 02:03:34 +01002585
Javier Cardona96b78df2011-04-07 15:08:33 -07002586 if (info->attrs[NL80211_ATTR_STA_PLINK_ACTION])
2587 params.plink_action =
2588 nla_get_u8(info->attrs[NL80211_ATTR_STA_PLINK_ACTION]);
2589
Johannes Bergeccb8e82009-05-11 21:57:56 +03002590 if (parse_station_flags(info, &params))
Johannes Berg5727ef12007-12-19 02:03:34 +01002591 return -EINVAL;
2592
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002593 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002594 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardona96b78df2011-04-07 15:08:33 -07002595 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002596 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2597 return -EINVAL;
Thadeu Lima de Souza Cascardo0e956c12010-02-12 12:34:50 -02002598
Johannes Berg463d0182009-07-14 00:33:35 +02002599 err = get_vlan(info, rdev, &params.vlan);
Johannes Berga97f4422009-06-18 17:23:43 +02002600 if (err)
Johannes Berge80cf852009-05-11 14:43:13 +02002601 goto out;
Johannes Berga97f4422009-06-18 17:23:43 +02002602
2603 /* validate settings */
2604 err = 0;
2605
Johannes Berg79c97e92009-07-07 03:56:12 +02002606 if (!rdev->ops->add_station) {
Johannes Berg5727ef12007-12-19 02:03:34 +01002607 err = -EOPNOTSUPP;
2608 goto out;
2609 }
2610
Johannes Berg79c97e92009-07-07 03:56:12 +02002611 err = rdev->ops->add_station(&rdev->wiphy, dev, mac_addr, &params);
Johannes Berg5727ef12007-12-19 02:03:34 +01002612
2613 out:
2614 if (params.vlan)
2615 dev_put(params.vlan);
Johannes Berg5727ef12007-12-19 02:03:34 +01002616 return err;
2617}
2618
2619static int nl80211_del_station(struct sk_buff *skb, struct genl_info *info)
2620{
Johannes Berg4c476992010-10-04 21:36:35 +02002621 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2622 struct net_device *dev = info->user_ptr[1];
Johannes Berg5727ef12007-12-19 02:03:34 +01002623 u8 *mac_addr = NULL;
2624
2625 if (info->attrs[NL80211_ATTR_MAC])
2626 mac_addr = nla_data(info->attrs[NL80211_ATTR_MAC]);
2627
Johannes Berge80cf852009-05-11 14:43:13 +02002628 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Marco Porschd5d9de02010-03-30 10:00:16 +02002629 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02002630 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02002631 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2632 return -EINVAL;
Johannes Berge80cf852009-05-11 14:43:13 +02002633
Johannes Berg4c476992010-10-04 21:36:35 +02002634 if (!rdev->ops->del_station)
2635 return -EOPNOTSUPP;
Johannes Berg5727ef12007-12-19 02:03:34 +01002636
Johannes Berg4c476992010-10-04 21:36:35 +02002637 return rdev->ops->del_station(&rdev->wiphy, dev, mac_addr);
Johannes Berg5727ef12007-12-19 02:03:34 +01002638}
2639
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002640static int nl80211_send_mpath(struct sk_buff *msg, u32 pid, u32 seq,
2641 int flags, struct net_device *dev,
2642 u8 *dst, u8 *next_hop,
2643 struct mpath_info *pinfo)
2644{
2645 void *hdr;
2646 struct nlattr *pinfoattr;
2647
2648 hdr = nl80211hdr_put(msg, pid, seq, flags, NL80211_CMD_NEW_STATION);
2649 if (!hdr)
2650 return -1;
2651
2652 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
2653 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, dst);
2654 NLA_PUT(msg, NL80211_ATTR_MPATH_NEXT_HOP, ETH_ALEN, next_hop);
2655
Johannes Bergf5ea9122009-08-07 16:17:38 +02002656 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, pinfo->generation);
2657
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002658 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MPATH_INFO);
2659 if (!pinfoattr)
2660 goto nla_put_failure;
2661 if (pinfo->filled & MPATH_INFO_FRAME_QLEN)
2662 NLA_PUT_U32(msg, NL80211_MPATH_INFO_FRAME_QLEN,
2663 pinfo->frame_qlen);
Rui Paulod19b3bf2009-11-09 23:46:55 +00002664 if (pinfo->filled & MPATH_INFO_SN)
2665 NLA_PUT_U32(msg, NL80211_MPATH_INFO_SN,
2666 pinfo->sn);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002667 if (pinfo->filled & MPATH_INFO_METRIC)
2668 NLA_PUT_U32(msg, NL80211_MPATH_INFO_METRIC,
2669 pinfo->metric);
2670 if (pinfo->filled & MPATH_INFO_EXPTIME)
2671 NLA_PUT_U32(msg, NL80211_MPATH_INFO_EXPTIME,
2672 pinfo->exptime);
2673 if (pinfo->filled & MPATH_INFO_FLAGS)
2674 NLA_PUT_U8(msg, NL80211_MPATH_INFO_FLAGS,
2675 pinfo->flags);
2676 if (pinfo->filled & MPATH_INFO_DISCOVERY_TIMEOUT)
2677 NLA_PUT_U32(msg, NL80211_MPATH_INFO_DISCOVERY_TIMEOUT,
2678 pinfo->discovery_timeout);
2679 if (pinfo->filled & MPATH_INFO_DISCOVERY_RETRIES)
2680 NLA_PUT_U8(msg, NL80211_MPATH_INFO_DISCOVERY_RETRIES,
2681 pinfo->discovery_retries);
2682
2683 nla_nest_end(msg, pinfoattr);
2684
2685 return genlmsg_end(msg, hdr);
2686
2687 nla_put_failure:
Thomas Grafbc3ed282008-06-03 16:36:54 -07002688 genlmsg_cancel(msg, hdr);
2689 return -EMSGSIZE;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002690}
2691
2692static int nl80211_dump_mpath(struct sk_buff *skb,
Johannes Bergbba95fe2008-07-29 13:22:51 +02002693 struct netlink_callback *cb)
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002694{
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002695 struct mpath_info pinfo;
2696 struct cfg80211_registered_device *dev;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002697 struct net_device *netdev;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002698 u8 dst[ETH_ALEN];
2699 u8 next_hop[ETH_ALEN];
Johannes Bergbba95fe2008-07-29 13:22:51 +02002700 int path_idx = cb->args[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002701 int err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002702
Johannes Berg67748892010-10-04 21:14:06 +02002703 err = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
2704 if (err)
2705 return err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002706
2707 if (!dev->ops->dump_mpath) {
Jouni Malineneec60b02009-03-20 21:21:19 +02002708 err = -EOPNOTSUPP;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002709 goto out_err;
2710 }
2711
Jouni Malineneec60b02009-03-20 21:21:19 +02002712 if (netdev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT) {
2713 err = -EOPNOTSUPP;
Roel Kluin0448b5f2009-08-22 21:15:49 +02002714 goto out_err;
Jouni Malineneec60b02009-03-20 21:21:19 +02002715 }
2716
Johannes Bergbba95fe2008-07-29 13:22:51 +02002717 while (1) {
2718 err = dev->ops->dump_mpath(&dev->wiphy, netdev, path_idx,
2719 dst, next_hop, &pinfo);
2720 if (err == -ENOENT)
2721 break;
2722 if (err)
Johannes Berg3b858752009-03-12 09:55:09 +01002723 goto out_err;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002724
2725 if (nl80211_send_mpath(skb, NETLINK_CB(cb->skb).pid,
2726 cb->nlh->nlmsg_seq, NLM_F_MULTI,
2727 netdev, dst, next_hop,
2728 &pinfo) < 0)
2729 goto out;
2730
2731 path_idx++;
2732 }
2733
2734
2735 out:
2736 cb->args[1] = path_idx;
2737 err = skb->len;
Johannes Bergbba95fe2008-07-29 13:22:51 +02002738 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02002739 nl80211_finish_netdev_dump(dev);
Johannes Bergbba95fe2008-07-29 13:22:51 +02002740 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002741}
2742
2743static int nl80211_get_mpath(struct sk_buff *skb, struct genl_info *info)
2744{
Johannes Berg4c476992010-10-04 21:36:35 +02002745 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002746 int err;
Johannes Berg4c476992010-10-04 21:36:35 +02002747 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002748 struct mpath_info pinfo;
2749 struct sk_buff *msg;
2750 u8 *dst = NULL;
2751 u8 next_hop[ETH_ALEN];
2752
2753 memset(&pinfo, 0, sizeof(pinfo));
2754
2755 if (!info->attrs[NL80211_ATTR_MAC])
2756 return -EINVAL;
2757
2758 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2759
Johannes Berg4c476992010-10-04 21:36:35 +02002760 if (!rdev->ops->get_mpath)
2761 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002762
Johannes Berg4c476992010-10-04 21:36:35 +02002763 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2764 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002765
Johannes Berg79c97e92009-07-07 03:56:12 +02002766 err = rdev->ops->get_mpath(&rdev->wiphy, dev, dst, next_hop, &pinfo);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002767 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002768 return err;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002769
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002770 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002771 if (!msg)
Johannes Berg4c476992010-10-04 21:36:35 +02002772 return -ENOMEM;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002773
2774 if (nl80211_send_mpath(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg4c476992010-10-04 21:36:35 +02002775 dev, dst, next_hop, &pinfo) < 0) {
2776 nlmsg_free(msg);
2777 return -ENOBUFS;
2778 }
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002779
Johannes Berg4c476992010-10-04 21:36:35 +02002780 return genlmsg_reply(msg, info);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002781}
2782
2783static int nl80211_set_mpath(struct sk_buff *skb, struct genl_info *info)
2784{
Johannes Berg4c476992010-10-04 21:36:35 +02002785 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2786 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002787 u8 *dst = NULL;
2788 u8 *next_hop = NULL;
2789
2790 if (!info->attrs[NL80211_ATTR_MAC])
2791 return -EINVAL;
2792
2793 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2794 return -EINVAL;
2795
2796 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2797 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2798
Johannes Berg4c476992010-10-04 21:36:35 +02002799 if (!rdev->ops->change_mpath)
2800 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002801
Johannes Berg4c476992010-10-04 21:36:35 +02002802 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2803 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002804
Johannes Berg4c476992010-10-04 21:36:35 +02002805 return rdev->ops->change_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002806}
Johannes Berg4c476992010-10-04 21:36:35 +02002807
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002808static int nl80211_new_mpath(struct sk_buff *skb, struct genl_info *info)
2809{
Johannes Berg4c476992010-10-04 21:36:35 +02002810 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2811 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002812 u8 *dst = NULL;
2813 u8 *next_hop = NULL;
2814
2815 if (!info->attrs[NL80211_ATTR_MAC])
2816 return -EINVAL;
2817
2818 if (!info->attrs[NL80211_ATTR_MPATH_NEXT_HOP])
2819 return -EINVAL;
2820
2821 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2822 next_hop = nla_data(info->attrs[NL80211_ATTR_MPATH_NEXT_HOP]);
2823
Johannes Berg4c476992010-10-04 21:36:35 +02002824 if (!rdev->ops->add_mpath)
2825 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002826
Johannes Berg4c476992010-10-04 21:36:35 +02002827 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
2828 return -EOPNOTSUPP;
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002829
Johannes Berg4c476992010-10-04 21:36:35 +02002830 return rdev->ops->add_mpath(&rdev->wiphy, dev, dst, next_hop);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002831}
2832
2833static int nl80211_del_mpath(struct sk_buff *skb, struct genl_info *info)
2834{
Johannes Berg4c476992010-10-04 21:36:35 +02002835 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2836 struct net_device *dev = info->user_ptr[1];
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002837 u8 *dst = NULL;
2838
2839 if (info->attrs[NL80211_ATTR_MAC])
2840 dst = nla_data(info->attrs[NL80211_ATTR_MAC]);
2841
Johannes Berg4c476992010-10-04 21:36:35 +02002842 if (!rdev->ops->del_mpath)
2843 return -EOPNOTSUPP;
Johannes Berg3b858752009-03-12 09:55:09 +01002844
Johannes Berg4c476992010-10-04 21:36:35 +02002845 return rdev->ops->del_mpath(&rdev->wiphy, dev, dst);
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01002846}
2847
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002848static int nl80211_set_bss(struct sk_buff *skb, struct genl_info *info)
2849{
Johannes Berg4c476992010-10-04 21:36:35 +02002850 struct cfg80211_registered_device *rdev = info->user_ptr[0];
2851 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002852 struct bss_parameters params;
2853
2854 memset(&params, 0, sizeof(params));
2855 /* default to not changing parameters */
2856 params.use_cts_prot = -1;
2857 params.use_short_preamble = -1;
2858 params.use_short_slot_time = -1;
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002859 params.ap_isolate = -1;
Helmut Schaa50b12f52010-11-19 12:40:25 +01002860 params.ht_opmode = -1;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002861
2862 if (info->attrs[NL80211_ATTR_BSS_CTS_PROT])
2863 params.use_cts_prot =
2864 nla_get_u8(info->attrs[NL80211_ATTR_BSS_CTS_PROT]);
2865 if (info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE])
2866 params.use_short_preamble =
2867 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_PREAMBLE]);
2868 if (info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME])
2869 params.use_short_slot_time =
2870 nla_get_u8(info->attrs[NL80211_ATTR_BSS_SHORT_SLOT_TIME]);
Jouni Malinen90c97a02008-10-30 16:59:22 +02002871 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
2872 params.basic_rates =
2873 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2874 params.basic_rates_len =
2875 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
2876 }
Felix Fietkaufd8aaaf2010-04-27 01:23:35 +02002877 if (info->attrs[NL80211_ATTR_AP_ISOLATE])
2878 params.ap_isolate = !!nla_get_u8(info->attrs[NL80211_ATTR_AP_ISOLATE]);
Helmut Schaa50b12f52010-11-19 12:40:25 +01002879 if (info->attrs[NL80211_ATTR_BSS_HT_OPMODE])
2880 params.ht_opmode =
2881 nla_get_u16(info->attrs[NL80211_ATTR_BSS_HT_OPMODE]);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002882
Johannes Berg4c476992010-10-04 21:36:35 +02002883 if (!rdev->ops->change_bss)
2884 return -EOPNOTSUPP;
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002885
Johannes Berg074ac8d2010-09-16 14:58:22 +02002886 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
Johannes Berg4c476992010-10-04 21:36:35 +02002887 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
2888 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02002889
Johannes Berg4c476992010-10-04 21:36:35 +02002890 return rdev->ops->change_bss(&rdev->wiphy, dev, &params);
Jouni Malinen9f1ba902008-08-07 20:07:01 +03002891}
2892
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00002893static const struct nla_policy reg_rule_policy[NL80211_REG_RULE_ATTR_MAX + 1] = {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002894 [NL80211_ATTR_REG_RULE_FLAGS] = { .type = NLA_U32 },
2895 [NL80211_ATTR_FREQ_RANGE_START] = { .type = NLA_U32 },
2896 [NL80211_ATTR_FREQ_RANGE_END] = { .type = NLA_U32 },
2897 [NL80211_ATTR_FREQ_RANGE_MAX_BW] = { .type = NLA_U32 },
2898 [NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN] = { .type = NLA_U32 },
2899 [NL80211_ATTR_POWER_RULE_MAX_EIRP] = { .type = NLA_U32 },
2900};
2901
2902static int parse_reg_rule(struct nlattr *tb[],
2903 struct ieee80211_reg_rule *reg_rule)
2904{
2905 struct ieee80211_freq_range *freq_range = &reg_rule->freq_range;
2906 struct ieee80211_power_rule *power_rule = &reg_rule->power_rule;
2907
2908 if (!tb[NL80211_ATTR_REG_RULE_FLAGS])
2909 return -EINVAL;
2910 if (!tb[NL80211_ATTR_FREQ_RANGE_START])
2911 return -EINVAL;
2912 if (!tb[NL80211_ATTR_FREQ_RANGE_END])
2913 return -EINVAL;
2914 if (!tb[NL80211_ATTR_FREQ_RANGE_MAX_BW])
2915 return -EINVAL;
2916 if (!tb[NL80211_ATTR_POWER_RULE_MAX_EIRP])
2917 return -EINVAL;
2918
2919 reg_rule->flags = nla_get_u32(tb[NL80211_ATTR_REG_RULE_FLAGS]);
2920
2921 freq_range->start_freq_khz =
2922 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_START]);
2923 freq_range->end_freq_khz =
2924 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_END]);
2925 freq_range->max_bandwidth_khz =
2926 nla_get_u32(tb[NL80211_ATTR_FREQ_RANGE_MAX_BW]);
2927
2928 power_rule->max_eirp =
2929 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_EIRP]);
2930
2931 if (tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN])
2932 power_rule->max_antenna_gain =
2933 nla_get_u32(tb[NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN]);
2934
2935 return 0;
2936}
2937
2938static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
2939{
2940 int r;
2941 char *data = NULL;
2942
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002943 /*
2944 * You should only get this when cfg80211 hasn't yet initialized
2945 * completely when built-in to the kernel right between the time
2946 * window between nl80211_init() and regulatory_init(), if that is
2947 * even possible.
2948 */
2949 mutex_lock(&cfg80211_mutex);
2950 if (unlikely(!cfg80211_regdomain)) {
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002951 mutex_unlock(&cfg80211_mutex);
2952 return -EINPROGRESS;
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002953 }
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002954 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguez80778f12009-02-21 00:04:22 -05002955
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002956 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
2957 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002958
2959 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
2960
Luis R. Rodriguezfe33eb32009-02-21 00:04:30 -05002961 r = regulatory_hint_user(data);
2962
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07002963 return r;
2964}
2965
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002966static int nl80211_get_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01002967 struct genl_info *info)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002968{
Johannes Berg4c476992010-10-04 21:36:35 +02002969 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg4c476992010-10-04 21:36:35 +02002970 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01002971 struct wireless_dev *wdev = dev->ieee80211_ptr;
2972 struct mesh_config cur_params;
2973 int err = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002974 void *hdr;
2975 struct nlattr *pinfoattr;
2976 struct sk_buff *msg;
2977
Johannes Berg29cbe682010-12-03 09:20:44 +01002978 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
2979 return -EOPNOTSUPP;
2980
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002981 if (!rdev->ops->get_mesh_config)
Johannes Berg4c476992010-10-04 21:36:35 +02002982 return -EOPNOTSUPP;
Jouni Malinenf3f92582009-03-20 17:57:36 +02002983
Johannes Berg29cbe682010-12-03 09:20:44 +01002984 wdev_lock(wdev);
2985 /* If not connected, get default parameters */
2986 if (!wdev->mesh_id_len)
2987 memcpy(&cur_params, &default_mesh_config, sizeof(cur_params));
2988 else
Javier Cardona24bdd9f2010-12-16 17:37:48 -08002989 err = rdev->ops->get_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01002990 &cur_params);
2991 wdev_unlock(wdev);
2992
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002993 if (err)
Johannes Berg4c476992010-10-04 21:36:35 +02002994 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07002995
2996 /* Draw up a netlink message to send back */
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07002997 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02002998 if (!msg)
2999 return -ENOMEM;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003000 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003001 NL80211_CMD_GET_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003002 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003003 goto out;
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003004 pinfoattr = nla_nest_start(msg, NL80211_ATTR_MESH_CONFIG);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003005 if (!pinfoattr)
3006 goto nla_put_failure;
3007 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3008 NLA_PUT_U16(msg, NL80211_MESHCONF_RETRY_TIMEOUT,
3009 cur_params.dot11MeshRetryTimeout);
3010 NLA_PUT_U16(msg, NL80211_MESHCONF_CONFIRM_TIMEOUT,
3011 cur_params.dot11MeshConfirmTimeout);
3012 NLA_PUT_U16(msg, NL80211_MESHCONF_HOLDING_TIMEOUT,
3013 cur_params.dot11MeshHoldingTimeout);
3014 NLA_PUT_U16(msg, NL80211_MESHCONF_MAX_PEER_LINKS,
3015 cur_params.dot11MeshMaxPeerLinks);
3016 NLA_PUT_U8(msg, NL80211_MESHCONF_MAX_RETRIES,
3017 cur_params.dot11MeshMaxRetries);
3018 NLA_PUT_U8(msg, NL80211_MESHCONF_TTL,
3019 cur_params.dot11MeshTTL);
Javier Cardona45904f22010-12-03 09:20:40 +01003020 NLA_PUT_U8(msg, NL80211_MESHCONF_ELEMENT_TTL,
3021 cur_params.element_ttl);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003022 NLA_PUT_U8(msg, NL80211_MESHCONF_AUTO_OPEN_PLINKS,
3023 cur_params.auto_open_plinks);
3024 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3025 cur_params.dot11MeshHWMPmaxPREQretries);
3026 NLA_PUT_U32(msg, NL80211_MESHCONF_PATH_REFRESH_TIME,
3027 cur_params.path_refresh_time);
3028 NLA_PUT_U16(msg, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3029 cur_params.min_discovery_timeout);
3030 NLA_PUT_U32(msg, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3031 cur_params.dot11MeshHWMPactivePathTimeout);
3032 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3033 cur_params.dot11MeshHWMPpreqMinInterval);
3034 NLA_PUT_U16(msg, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3035 cur_params.dot11MeshHWMPnetDiameterTraversalTime);
Rui Paulo63c57232009-11-09 23:46:57 +00003036 NLA_PUT_U8(msg, NL80211_MESHCONF_HWMP_ROOTMODE,
3037 cur_params.dot11MeshHWMPRootMode);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003038 nla_nest_end(msg, pinfoattr);
3039 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02003040 return genlmsg_reply(msg, info);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003041
Johannes Berg3b858752009-03-12 09:55:09 +01003042 nla_put_failure:
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003043 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003044 out:
Yuri Ershovd080e272010-06-29 15:08:07 +04003045 nlmsg_free(msg);
Johannes Berg4c476992010-10-04 21:36:35 +02003046 return -ENOBUFS;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003047}
3048
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00003049static const struct nla_policy nl80211_meshconf_params_policy[NL80211_MESHCONF_ATTR_MAX+1] = {
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003050 [NL80211_MESHCONF_RETRY_TIMEOUT] = { .type = NLA_U16 },
3051 [NL80211_MESHCONF_CONFIRM_TIMEOUT] = { .type = NLA_U16 },
3052 [NL80211_MESHCONF_HOLDING_TIMEOUT] = { .type = NLA_U16 },
3053 [NL80211_MESHCONF_MAX_PEER_LINKS] = { .type = NLA_U16 },
3054 [NL80211_MESHCONF_MAX_RETRIES] = { .type = NLA_U8 },
3055 [NL80211_MESHCONF_TTL] = { .type = NLA_U8 },
Javier Cardona45904f22010-12-03 09:20:40 +01003056 [NL80211_MESHCONF_ELEMENT_TTL] = { .type = NLA_U8 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003057 [NL80211_MESHCONF_AUTO_OPEN_PLINKS] = { .type = NLA_U8 },
3058
3059 [NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES] = { .type = NLA_U8 },
3060 [NL80211_MESHCONF_PATH_REFRESH_TIME] = { .type = NLA_U32 },
3061 [NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT] = { .type = NLA_U16 },
3062 [NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT] = { .type = NLA_U32 },
3063 [NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL] = { .type = NLA_U16 },
3064 [NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME] = { .type = NLA_U16 },
3065};
3066
Javier Cardonac80d5452010-12-16 17:37:49 -08003067static const struct nla_policy
3068 nl80211_mesh_setup_params_policy[NL80211_MESH_SETUP_ATTR_MAX+1] = {
3069 [NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL] = { .type = NLA_U8 },
3070 [NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC] = { .type = NLA_U8 },
Javier Cardona15d5dda2011-04-07 15:08:28 -07003071 [NL80211_MESH_SETUP_USERSPACE_AUTH] = { .type = NLA_FLAG },
Javier Cardona581a8b02011-04-07 15:08:27 -07003072 [NL80211_MESH_SETUP_IE] = { .type = NLA_BINARY,
Javier Cardonac80d5452010-12-16 17:37:49 -08003073 .len = IEEE80211_MAX_DATA_LEN },
Javier Cardonab130e5c2011-05-03 16:57:07 -07003074 [NL80211_MESH_SETUP_USERSPACE_AMPE] = { .type = NLA_FLAG },
Javier Cardonac80d5452010-12-16 17:37:49 -08003075};
3076
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003077static int nl80211_parse_mesh_config(struct genl_info *info,
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003078 struct mesh_config *cfg,
3079 u32 *mask_out)
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003080{
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003081 struct nlattr *tb[NL80211_MESHCONF_ATTR_MAX + 1];
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003082 u32 mask = 0;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003083
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003084#define FILL_IN_MESH_PARAM_IF_SET(table, cfg, param, mask, attr_num, nla_fn) \
3085do {\
3086 if (table[attr_num]) {\
3087 cfg->param = nla_fn(table[attr_num]); \
3088 mask |= (1 << (attr_num - 1)); \
3089 } \
3090} while (0);\
3091
3092
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003093 if (!info->attrs[NL80211_ATTR_MESH_CONFIG])
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003094 return -EINVAL;
3095 if (nla_parse_nested(tb, NL80211_MESHCONF_ATTR_MAX,
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003096 info->attrs[NL80211_ATTR_MESH_CONFIG],
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003097 nl80211_meshconf_params_policy))
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003098 return -EINVAL;
3099
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003100 /* This makes sure that there aren't more than 32 mesh config
3101 * parameters (otherwise our bitfield scheme would not work.) */
3102 BUILD_BUG_ON(NL80211_MESHCONF_ATTR_MAX > 32);
3103
3104 /* Fill in the params struct */
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003105 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshRetryTimeout,
3106 mask, NL80211_MESHCONF_RETRY_TIMEOUT, nla_get_u16);
3107 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshConfirmTimeout,
3108 mask, NL80211_MESHCONF_CONFIRM_TIMEOUT, nla_get_u16);
3109 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHoldingTimeout,
3110 mask, NL80211_MESHCONF_HOLDING_TIMEOUT, nla_get_u16);
3111 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxPeerLinks,
3112 mask, NL80211_MESHCONF_MAX_PEER_LINKS, nla_get_u16);
3113 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshMaxRetries,
3114 mask, NL80211_MESHCONF_MAX_RETRIES, nla_get_u8);
3115 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshTTL,
3116 mask, NL80211_MESHCONF_TTL, nla_get_u8);
Javier Cardona45904f22010-12-03 09:20:40 +01003117 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, element_ttl,
3118 mask, NL80211_MESHCONF_ELEMENT_TTL, nla_get_u8);
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003119 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, auto_open_plinks,
3120 mask, NL80211_MESHCONF_AUTO_OPEN_PLINKS, nla_get_u8);
3121 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPmaxPREQretries,
3122 mask, NL80211_MESHCONF_HWMP_MAX_PREQ_RETRIES,
3123 nla_get_u8);
3124 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, path_refresh_time,
3125 mask, NL80211_MESHCONF_PATH_REFRESH_TIME, nla_get_u32);
3126 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, min_discovery_timeout,
3127 mask, NL80211_MESHCONF_MIN_DISCOVERY_TIMEOUT,
3128 nla_get_u16);
3129 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPactivePathTimeout,
3130 mask, NL80211_MESHCONF_HWMP_ACTIVE_PATH_TIMEOUT,
3131 nla_get_u32);
3132 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshHWMPpreqMinInterval,
3133 mask, NL80211_MESHCONF_HWMP_PREQ_MIN_INTERVAL,
3134 nla_get_u16);
3135 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3136 dot11MeshHWMPnetDiameterTraversalTime,
3137 mask, NL80211_MESHCONF_HWMP_NET_DIAM_TRVS_TIME,
3138 nla_get_u16);
Rui Paulo63c57232009-11-09 23:46:57 +00003139 FILL_IN_MESH_PARAM_IF_SET(tb, cfg,
3140 dot11MeshHWMPRootMode, mask,
3141 NL80211_MESHCONF_HWMP_ROOTMODE,
3142 nla_get_u8);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003143 if (mask_out)
3144 *mask_out = mask;
Javier Cardonac80d5452010-12-16 17:37:49 -08003145
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003146 return 0;
3147
3148#undef FILL_IN_MESH_PARAM_IF_SET
3149}
3150
Javier Cardonac80d5452010-12-16 17:37:49 -08003151static int nl80211_parse_mesh_setup(struct genl_info *info,
3152 struct mesh_setup *setup)
3153{
3154 struct nlattr *tb[NL80211_MESH_SETUP_ATTR_MAX + 1];
3155
3156 if (!info->attrs[NL80211_ATTR_MESH_SETUP])
3157 return -EINVAL;
3158 if (nla_parse_nested(tb, NL80211_MESH_SETUP_ATTR_MAX,
3159 info->attrs[NL80211_ATTR_MESH_SETUP],
3160 nl80211_mesh_setup_params_policy))
3161 return -EINVAL;
3162
3163 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])
3164 setup->path_sel_proto =
3165 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_PATH_SEL])) ?
3166 IEEE80211_PATH_PROTOCOL_VENDOR :
3167 IEEE80211_PATH_PROTOCOL_HWMP;
3168
3169 if (tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])
3170 setup->path_metric =
3171 (nla_get_u8(tb[NL80211_MESH_SETUP_ENABLE_VENDOR_METRIC])) ?
3172 IEEE80211_PATH_METRIC_VENDOR :
3173 IEEE80211_PATH_METRIC_AIRTIME;
3174
Javier Cardona581a8b02011-04-07 15:08:27 -07003175
3176 if (tb[NL80211_MESH_SETUP_IE]) {
Javier Cardonac80d5452010-12-16 17:37:49 -08003177 struct nlattr *ieattr =
Javier Cardona581a8b02011-04-07 15:08:27 -07003178 tb[NL80211_MESH_SETUP_IE];
Javier Cardonac80d5452010-12-16 17:37:49 -08003179 if (!is_valid_ie_attr(ieattr))
3180 return -EINVAL;
Javier Cardona581a8b02011-04-07 15:08:27 -07003181 setup->ie = nla_data(ieattr);
3182 setup->ie_len = nla_len(ieattr);
Javier Cardonac80d5452010-12-16 17:37:49 -08003183 }
Javier Cardonab130e5c2011-05-03 16:57:07 -07003184 setup->is_authenticated = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AUTH]);
3185 setup->is_secure = nla_get_flag(tb[NL80211_MESH_SETUP_USERSPACE_AMPE]);
Javier Cardonac80d5452010-12-16 17:37:49 -08003186
3187 return 0;
3188}
3189
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003190static int nl80211_update_mesh_config(struct sk_buff *skb,
Johannes Berg29cbe682010-12-03 09:20:44 +01003191 struct genl_info *info)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003192{
3193 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3194 struct net_device *dev = info->user_ptr[1];
Johannes Berg29cbe682010-12-03 09:20:44 +01003195 struct wireless_dev *wdev = dev->ieee80211_ptr;
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003196 struct mesh_config cfg;
3197 u32 mask;
3198 int err;
3199
Johannes Berg29cbe682010-12-03 09:20:44 +01003200 if (wdev->iftype != NL80211_IFTYPE_MESH_POINT)
3201 return -EOPNOTSUPP;
3202
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003203 if (!rdev->ops->update_mesh_config)
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003204 return -EOPNOTSUPP;
3205
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003206 err = nl80211_parse_mesh_config(info, &cfg, &mask);
Johannes Bergbd90fdc2010-12-03 09:20:43 +01003207 if (err)
3208 return err;
3209
Johannes Berg29cbe682010-12-03 09:20:44 +01003210 wdev_lock(wdev);
3211 if (!wdev->mesh_id_len)
3212 err = -ENOLINK;
3213
3214 if (!err)
Javier Cardona24bdd9f2010-12-16 17:37:48 -08003215 err = rdev->ops->update_mesh_config(&rdev->wiphy, dev,
Johannes Berg29cbe682010-12-03 09:20:44 +01003216 mask, &cfg);
3217
3218 wdev_unlock(wdev);
3219
3220 return err;
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07003221}
3222
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003223static int nl80211_get_reg(struct sk_buff *skb, struct genl_info *info)
3224{
3225 struct sk_buff *msg;
3226 void *hdr = NULL;
3227 struct nlattr *nl_reg_rules;
3228 unsigned int i;
3229 int err = -EINVAL;
3230
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003231 mutex_lock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003232
3233 if (!cfg80211_regdomain)
3234 goto out;
3235
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07003236 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003237 if (!msg) {
3238 err = -ENOBUFS;
3239 goto out;
3240 }
3241
3242 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
3243 NL80211_CMD_GET_REG);
3244 if (!hdr)
Julia Lawallefe1cf02011-01-28 15:17:11 +01003245 goto put_failure;
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003246
3247 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2,
3248 cfg80211_regdomain->alpha2);
3249
3250 nl_reg_rules = nla_nest_start(msg, NL80211_ATTR_REG_RULES);
3251 if (!nl_reg_rules)
3252 goto nla_put_failure;
3253
3254 for (i = 0; i < cfg80211_regdomain->n_reg_rules; i++) {
3255 struct nlattr *nl_reg_rule;
3256 const struct ieee80211_reg_rule *reg_rule;
3257 const struct ieee80211_freq_range *freq_range;
3258 const struct ieee80211_power_rule *power_rule;
3259
3260 reg_rule = &cfg80211_regdomain->reg_rules[i];
3261 freq_range = &reg_rule->freq_range;
3262 power_rule = &reg_rule->power_rule;
3263
3264 nl_reg_rule = nla_nest_start(msg, i);
3265 if (!nl_reg_rule)
3266 goto nla_put_failure;
3267
3268 NLA_PUT_U32(msg, NL80211_ATTR_REG_RULE_FLAGS,
3269 reg_rule->flags);
3270 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_START,
3271 freq_range->start_freq_khz);
3272 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_END,
3273 freq_range->end_freq_khz);
3274 NLA_PUT_U32(msg, NL80211_ATTR_FREQ_RANGE_MAX_BW,
3275 freq_range->max_bandwidth_khz);
3276 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_ANT_GAIN,
3277 power_rule->max_antenna_gain);
3278 NLA_PUT_U32(msg, NL80211_ATTR_POWER_RULE_MAX_EIRP,
3279 power_rule->max_eirp);
3280
3281 nla_nest_end(msg, nl_reg_rule);
3282 }
3283
3284 nla_nest_end(msg, nl_reg_rules);
3285
3286 genlmsg_end(msg, hdr);
Johannes Berg134e6372009-07-10 09:51:34 +00003287 err = genlmsg_reply(msg, info);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003288 goto out;
3289
3290nla_put_failure:
3291 genlmsg_cancel(msg, hdr);
Julia Lawallefe1cf02011-01-28 15:17:11 +01003292put_failure:
Yuri Ershovd080e272010-06-29 15:08:07 +04003293 nlmsg_free(msg);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003294 err = -EMSGSIZE;
3295out:
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003296 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08003297 return err;
3298}
3299
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003300static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
3301{
3302 struct nlattr *tb[NL80211_REG_RULE_ATTR_MAX + 1];
3303 struct nlattr *nl_reg_rule;
3304 char *alpha2 = NULL;
3305 int rem_reg_rules = 0, r = 0;
3306 u32 num_rules = 0, rule_idx = 0, size_of_regd;
3307 struct ieee80211_regdomain *rd = NULL;
3308
3309 if (!info->attrs[NL80211_ATTR_REG_ALPHA2])
3310 return -EINVAL;
3311
3312 if (!info->attrs[NL80211_ATTR_REG_RULES])
3313 return -EINVAL;
3314
3315 alpha2 = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
3316
3317 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3318 rem_reg_rules) {
3319 num_rules++;
3320 if (num_rules > NL80211_MAX_SUPP_REG_RULES)
Luis R. Rodriguez4776c6e2009-05-13 17:04:39 -04003321 return -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003322 }
3323
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003324 mutex_lock(&cfg80211_mutex);
3325
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003326 if (!reg_is_valid_request(alpha2)) {
3327 r = -EINVAL;
3328 goto bad_reg;
3329 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003330
3331 size_of_regd = sizeof(struct ieee80211_regdomain) +
3332 (num_rules * sizeof(struct ieee80211_reg_rule));
3333
3334 rd = kzalloc(size_of_regd, GFP_KERNEL);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003335 if (!rd) {
3336 r = -ENOMEM;
3337 goto bad_reg;
3338 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003339
3340 rd->n_reg_rules = num_rules;
3341 rd->alpha2[0] = alpha2[0];
3342 rd->alpha2[1] = alpha2[1];
3343
3344 nla_for_each_nested(nl_reg_rule, info->attrs[NL80211_ATTR_REG_RULES],
3345 rem_reg_rules) {
3346 nla_parse(tb, NL80211_REG_RULE_ATTR_MAX,
3347 nla_data(nl_reg_rule), nla_len(nl_reg_rule),
3348 reg_rule_policy);
3349 r = parse_reg_rule(tb, &rd->reg_rules[rule_idx]);
3350 if (r)
3351 goto bad_reg;
3352
3353 rule_idx++;
3354
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003355 if (rule_idx > NL80211_MAX_SUPP_REG_RULES) {
3356 r = -EINVAL;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003357 goto bad_reg;
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003358 }
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003359 }
3360
3361 BUG_ON(rule_idx != num_rules);
3362
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003363 r = set_regdom(rd);
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003364
Luis R. Rodrigueza1794392009-02-21 00:04:21 -05003365 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003366
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003367 return r;
3368
Johannes Bergd2372b32008-10-24 20:32:20 +02003369 bad_reg:
Luis R. Rodriguez61405e92009-05-13 17:04:41 -04003370 mutex_unlock(&cfg80211_mutex);
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003371 kfree(rd);
Luis R. Rodriguezd0e18f82009-05-13 17:04:40 -04003372 return r;
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07003373}
3374
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003375static int validate_scan_freqs(struct nlattr *freqs)
3376{
3377 struct nlattr *attr1, *attr2;
3378 int n_channels = 0, tmp1, tmp2;
3379
3380 nla_for_each_nested(attr1, freqs, tmp1) {
3381 n_channels++;
3382 /*
3383 * Some hardware has a limited channel list for
3384 * scanning, and it is pretty much nonsensical
3385 * to scan for a channel twice, so disallow that
3386 * and don't require drivers to check that the
3387 * channel list they get isn't longer than what
3388 * they can scan, as long as they can scan all
3389 * the channels they registered at once.
3390 */
3391 nla_for_each_nested(attr2, freqs, tmp2)
3392 if (attr1 != attr2 &&
3393 nla_get_u32(attr1) == nla_get_u32(attr2))
3394 return 0;
3395 }
3396
3397 return n_channels;
3398}
3399
Johannes Berg2a519312009-02-10 21:25:55 +01003400static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
3401{
Johannes Berg4c476992010-10-04 21:36:35 +02003402 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3403 struct net_device *dev = info->user_ptr[1];
Johannes Berg2a519312009-02-10 21:25:55 +01003404 struct cfg80211_scan_request *request;
Johannes Berg2a519312009-02-10 21:25:55 +01003405 struct nlattr *attr;
3406 struct wiphy *wiphy;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003407 int err, tmp, n_ssids = 0, n_channels, i;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003408 size_t ie_len;
Johannes Berg2a519312009-02-10 21:25:55 +01003409
Johannes Bergf4a11bb2009-03-27 12:40:28 +01003410 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3411 return -EINVAL;
3412
Johannes Berg79c97e92009-07-07 03:56:12 +02003413 wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003414
Johannes Berg4c476992010-10-04 21:36:35 +02003415 if (!rdev->ops->scan)
3416 return -EOPNOTSUPP;
Johannes Berg2a519312009-02-10 21:25:55 +01003417
Johannes Berg4c476992010-10-04 21:36:35 +02003418 if (rdev->scan_req)
3419 return -EBUSY;
Johannes Berg2a519312009-02-10 21:25:55 +01003420
3421 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003422 n_channels = validate_scan_freqs(
3423 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
Johannes Berg4c476992010-10-04 21:36:35 +02003424 if (!n_channels)
3425 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003426 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003427 enum ieee80211_band band;
Johannes Berg83f5e2c2009-06-17 17:41:49 +02003428 n_channels = 0;
3429
Johannes Berg2a519312009-02-10 21:25:55 +01003430 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3431 if (wiphy->bands[band])
3432 n_channels += wiphy->bands[band]->n_channels;
3433 }
3434
3435 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3436 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp)
3437 n_ssids++;
3438
Johannes Berg4c476992010-10-04 21:36:35 +02003439 if (n_ssids > wiphy->max_scan_ssids)
3440 return -EINVAL;
Johannes Berg2a519312009-02-10 21:25:55 +01003441
Jouni Malinen70692ad2009-02-16 19:39:13 +02003442 if (info->attrs[NL80211_ATTR_IE])
3443 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3444 else
3445 ie_len = 0;
3446
Johannes Berg4c476992010-10-04 21:36:35 +02003447 if (ie_len > wiphy->max_scan_ie_len)
3448 return -EINVAL;
Johannes Berg18a83652009-03-31 12:12:05 +02003449
Johannes Berg2a519312009-02-10 21:25:55 +01003450 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003451 + sizeof(*request->ssids) * n_ssids
3452 + sizeof(*request->channels) * n_channels
Jouni Malinen70692ad2009-02-16 19:39:13 +02003453 + ie_len, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02003454 if (!request)
3455 return -ENOMEM;
Johannes Berg2a519312009-02-10 21:25:55 +01003456
Johannes Berg2a519312009-02-10 21:25:55 +01003457 if (n_ssids)
Johannes Berg5ba63532009-08-07 17:54:07 +02003458 request->ssids = (void *)&request->channels[n_channels];
Johannes Berg2a519312009-02-10 21:25:55 +01003459 request->n_ssids = n_ssids;
Jouni Malinen70692ad2009-02-16 19:39:13 +02003460 if (ie_len) {
3461 if (request->ssids)
3462 request->ie = (void *)(request->ssids + n_ssids);
3463 else
3464 request->ie = (void *)(request->channels + n_channels);
3465 }
Johannes Berg2a519312009-02-10 21:25:55 +01003466
Johannes Berg584991d2009-11-02 13:32:03 +01003467 i = 0;
Johannes Berg2a519312009-02-10 21:25:55 +01003468 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3469 /* user specified, bail out if channel not found */
Johannes Berg2a519312009-02-10 21:25:55 +01003470 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_FREQUENCIES], tmp) {
Johannes Berg584991d2009-11-02 13:32:03 +01003471 struct ieee80211_channel *chan;
3472
3473 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3474
3475 if (!chan) {
Johannes Berg2a519312009-02-10 21:25:55 +01003476 err = -EINVAL;
3477 goto out_free;
3478 }
Johannes Berg584991d2009-11-02 13:32:03 +01003479
3480 /* ignore disabled channels */
3481 if (chan->flags & IEEE80211_CHAN_DISABLED)
3482 continue;
3483
3484 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003485 i++;
3486 }
3487 } else {
Johannes Berg34850ab2011-07-18 18:08:35 +02003488 enum ieee80211_band band;
3489
Johannes Berg2a519312009-02-10 21:25:55 +01003490 /* all channels */
Johannes Berg2a519312009-02-10 21:25:55 +01003491 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3492 int j;
3493 if (!wiphy->bands[band])
3494 continue;
3495 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
Johannes Berg584991d2009-11-02 13:32:03 +01003496 struct ieee80211_channel *chan;
3497
3498 chan = &wiphy->bands[band]->channels[j];
3499
3500 if (chan->flags & IEEE80211_CHAN_DISABLED)
3501 continue;
3502
3503 request->channels[i] = chan;
Johannes Berg2a519312009-02-10 21:25:55 +01003504 i++;
3505 }
3506 }
3507 }
3508
Johannes Berg584991d2009-11-02 13:32:03 +01003509 if (!i) {
3510 err = -EINVAL;
3511 goto out_free;
3512 }
3513
3514 request->n_channels = i;
3515
Johannes Berg2a519312009-02-10 21:25:55 +01003516 i = 0;
3517 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3518 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS], tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003519 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Johannes Berg2a519312009-02-10 21:25:55 +01003520 err = -EINVAL;
3521 goto out_free;
3522 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003523 request->ssids[i].ssid_len = nla_len(attr);
Johannes Berg2a519312009-02-10 21:25:55 +01003524 memcpy(request->ssids[i].ssid, nla_data(attr), nla_len(attr));
Johannes Berg2a519312009-02-10 21:25:55 +01003525 i++;
3526 }
3527 }
3528
Jouni Malinen70692ad2009-02-16 19:39:13 +02003529 if (info->attrs[NL80211_ATTR_IE]) {
3530 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Johannes Bergde95a542009-04-01 11:58:36 +02003531 memcpy((void *)request->ie,
3532 nla_data(info->attrs[NL80211_ATTR_IE]),
Jouni Malinen70692ad2009-02-16 19:39:13 +02003533 request->ie_len);
3534 }
3535
Johannes Berg34850ab2011-07-18 18:08:35 +02003536 for (i = 0; i < IEEE80211_NUM_BANDS; i++)
Johannes Berga401d2b2011-07-20 00:52:16 +02003537 if (wiphy->bands[i])
3538 request->rates[i] =
3539 (1 << wiphy->bands[i]->n_bitrates) - 1;
Johannes Berg34850ab2011-07-18 18:08:35 +02003540
3541 if (info->attrs[NL80211_ATTR_SCAN_SUPP_RATES]) {
3542 nla_for_each_nested(attr,
3543 info->attrs[NL80211_ATTR_SCAN_SUPP_RATES],
3544 tmp) {
3545 enum ieee80211_band band = nla_type(attr);
3546
Dan Carpenter84404622011-07-29 11:52:18 +03003547 if (band < 0 || band >= IEEE80211_NUM_BANDS) {
Johannes Berg34850ab2011-07-18 18:08:35 +02003548 err = -EINVAL;
3549 goto out_free;
3550 }
3551 err = ieee80211_get_ratemask(wiphy->bands[band],
3552 nla_data(attr),
3553 nla_len(attr),
3554 &request->rates[band]);
3555 if (err)
3556 goto out_free;
3557 }
3558 }
3559
Johannes Berg463d0182009-07-14 00:33:35 +02003560 request->dev = dev;
Johannes Berg79c97e92009-07-07 03:56:12 +02003561 request->wiphy = &rdev->wiphy;
Johannes Berg2a519312009-02-10 21:25:55 +01003562
Johannes Berg79c97e92009-07-07 03:56:12 +02003563 rdev->scan_req = request;
3564 err = rdev->ops->scan(&rdev->wiphy, dev, request);
Johannes Berg2a519312009-02-10 21:25:55 +01003565
Johannes Berg463d0182009-07-14 00:33:35 +02003566 if (!err) {
Johannes Berg79c97e92009-07-07 03:56:12 +02003567 nl80211_send_scan_start(rdev, dev);
Johannes Berg463d0182009-07-14 00:33:35 +02003568 dev_hold(dev);
Johannes Berg4c476992010-10-04 21:36:35 +02003569 } else {
Johannes Berg2a519312009-02-10 21:25:55 +01003570 out_free:
Johannes Berg79c97e92009-07-07 03:56:12 +02003571 rdev->scan_req = NULL;
Johannes Berg2a519312009-02-10 21:25:55 +01003572 kfree(request);
3573 }
Johannes Berg3b858752009-03-12 09:55:09 +01003574
Johannes Berg2a519312009-02-10 21:25:55 +01003575 return err;
3576}
3577
Luciano Coelho807f8a82011-05-11 17:09:35 +03003578static int nl80211_start_sched_scan(struct sk_buff *skb,
3579 struct genl_info *info)
3580{
3581 struct cfg80211_sched_scan_request *request;
3582 struct cfg80211_registered_device *rdev = info->user_ptr[0];
3583 struct net_device *dev = info->user_ptr[1];
Luciano Coelho807f8a82011-05-11 17:09:35 +03003584 struct nlattr *attr;
3585 struct wiphy *wiphy;
3586 int err, tmp, n_ssids = 0, n_channels, i;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003587 u32 interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003588 enum ieee80211_band band;
3589 size_t ie_len;
3590
3591 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3592 !rdev->ops->sched_scan_start)
3593 return -EOPNOTSUPP;
3594
3595 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
3596 return -EINVAL;
3597
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003598 if (!info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL])
3599 return -EINVAL;
3600
3601 interval = nla_get_u32(info->attrs[NL80211_ATTR_SCHED_SCAN_INTERVAL]);
3602 if (interval == 0)
3603 return -EINVAL;
3604
Luciano Coelho807f8a82011-05-11 17:09:35 +03003605 wiphy = &rdev->wiphy;
3606
3607 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3608 n_channels = validate_scan_freqs(
3609 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]);
3610 if (!n_channels)
3611 return -EINVAL;
3612 } else {
3613 n_channels = 0;
3614
3615 for (band = 0; band < IEEE80211_NUM_BANDS; band++)
3616 if (wiphy->bands[band])
3617 n_channels += wiphy->bands[band]->n_channels;
3618 }
3619
3620 if (info->attrs[NL80211_ATTR_SCAN_SSIDS])
3621 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3622 tmp)
3623 n_ssids++;
3624
Luciano Coelho93b6aa62011-07-13 14:57:28 +03003625 if (n_ssids > wiphy->max_sched_scan_ssids)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003626 return -EINVAL;
3627
3628 if (info->attrs[NL80211_ATTR_IE])
3629 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3630 else
3631 ie_len = 0;
3632
Luciano Coelho5a865ba2011-07-13 14:57:29 +03003633 if (ie_len > wiphy->max_sched_scan_ie_len)
Luciano Coelho807f8a82011-05-11 17:09:35 +03003634 return -EINVAL;
3635
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003636 mutex_lock(&rdev->sched_scan_mtx);
3637
3638 if (rdev->sched_scan_req) {
3639 err = -EINPROGRESS;
3640 goto out;
3641 }
3642
Luciano Coelho807f8a82011-05-11 17:09:35 +03003643 request = kzalloc(sizeof(*request)
Luciano Coelhoa2cd43c2011-05-18 11:42:03 +03003644 + sizeof(*request->ssids) * n_ssids
3645 + sizeof(*request->channels) * n_channels
Luciano Coelho807f8a82011-05-11 17:09:35 +03003646 + ie_len, GFP_KERNEL);
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003647 if (!request) {
3648 err = -ENOMEM;
3649 goto out;
3650 }
Luciano Coelho807f8a82011-05-11 17:09:35 +03003651
3652 if (n_ssids)
3653 request->ssids = (void *)&request->channels[n_channels];
3654 request->n_ssids = n_ssids;
3655 if (ie_len) {
3656 if (request->ssids)
3657 request->ie = (void *)(request->ssids + n_ssids);
3658 else
3659 request->ie = (void *)(request->channels + n_channels);
3660 }
3661
3662 i = 0;
3663 if (info->attrs[NL80211_ATTR_SCAN_FREQUENCIES]) {
3664 /* user specified, bail out if channel not found */
3665 nla_for_each_nested(attr,
3666 info->attrs[NL80211_ATTR_SCAN_FREQUENCIES],
3667 tmp) {
3668 struct ieee80211_channel *chan;
3669
3670 chan = ieee80211_get_channel(wiphy, nla_get_u32(attr));
3671
3672 if (!chan) {
3673 err = -EINVAL;
3674 goto out_free;
3675 }
3676
3677 /* ignore disabled channels */
3678 if (chan->flags & IEEE80211_CHAN_DISABLED)
3679 continue;
3680
3681 request->channels[i] = chan;
3682 i++;
3683 }
3684 } else {
3685 /* all channels */
3686 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
3687 int j;
3688 if (!wiphy->bands[band])
3689 continue;
3690 for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
3691 struct ieee80211_channel *chan;
3692
3693 chan = &wiphy->bands[band]->channels[j];
3694
3695 if (chan->flags & IEEE80211_CHAN_DISABLED)
3696 continue;
3697
3698 request->channels[i] = chan;
3699 i++;
3700 }
3701 }
3702 }
3703
3704 if (!i) {
3705 err = -EINVAL;
3706 goto out_free;
3707 }
3708
3709 request->n_channels = i;
3710
3711 i = 0;
3712 if (info->attrs[NL80211_ATTR_SCAN_SSIDS]) {
3713 nla_for_each_nested(attr, info->attrs[NL80211_ATTR_SCAN_SSIDS],
3714 tmp) {
Luciano Coelho57a27e12011-06-07 20:42:26 +03003715 if (nla_len(attr) > IEEE80211_MAX_SSID_LEN) {
Luciano Coelho807f8a82011-05-11 17:09:35 +03003716 err = -EINVAL;
3717 goto out_free;
3718 }
Luciano Coelho57a27e12011-06-07 20:42:26 +03003719 request->ssids[i].ssid_len = nla_len(attr);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003720 memcpy(request->ssids[i].ssid, nla_data(attr),
3721 nla_len(attr));
Luciano Coelho807f8a82011-05-11 17:09:35 +03003722 i++;
3723 }
3724 }
3725
3726 if (info->attrs[NL80211_ATTR_IE]) {
3727 request->ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
3728 memcpy((void *)request->ie,
3729 nla_data(info->attrs[NL80211_ATTR_IE]),
3730 request->ie_len);
3731 }
3732
3733 request->dev = dev;
3734 request->wiphy = &rdev->wiphy;
Luciano Coelhobbe6ad62011-05-11 17:09:37 +03003735 request->interval = interval;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003736
3737 err = rdev->ops->sched_scan_start(&rdev->wiphy, dev, request);
3738 if (!err) {
3739 rdev->sched_scan_req = request;
3740 nl80211_send_sched_scan(rdev, dev,
3741 NL80211_CMD_START_SCHED_SCAN);
3742 goto out;
3743 }
3744
3745out_free:
3746 kfree(request);
3747out:
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003748 mutex_unlock(&rdev->sched_scan_mtx);
Luciano Coelho807f8a82011-05-11 17:09:35 +03003749 return err;
3750}
3751
3752static int nl80211_stop_sched_scan(struct sk_buff *skb,
3753 struct genl_info *info)
3754{
3755 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003756 int err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003757
3758 if (!(rdev->wiphy.flags & WIPHY_FLAG_SUPPORTS_SCHED_SCAN) ||
3759 !rdev->ops->sched_scan_stop)
3760 return -EOPNOTSUPP;
3761
Luciano Coelhoc10841c2011-06-30 08:32:41 +03003762 mutex_lock(&rdev->sched_scan_mtx);
3763 err = __cfg80211_stop_sched_scan(rdev, false);
3764 mutex_unlock(&rdev->sched_scan_mtx);
3765
3766 return err;
Luciano Coelho807f8a82011-05-11 17:09:35 +03003767}
3768
Johannes Berg9720bb32011-06-21 09:45:33 +02003769static int nl80211_send_bss(struct sk_buff *msg, struct netlink_callback *cb,
3770 u32 seq, int flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003771 struct cfg80211_registered_device *rdev,
Johannes Berg48ab9052009-07-10 18:42:31 +02003772 struct wireless_dev *wdev,
3773 struct cfg80211_internal_bss *intbss)
Johannes Berg2a519312009-02-10 21:25:55 +01003774{
Johannes Berg48ab9052009-07-10 18:42:31 +02003775 struct cfg80211_bss *res = &intbss->pub;
Johannes Berg2a519312009-02-10 21:25:55 +01003776 void *hdr;
3777 struct nlattr *bss;
Johannes Berg48ab9052009-07-10 18:42:31 +02003778 int i;
3779
3780 ASSERT_WDEV_LOCK(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003781
Johannes Berg9720bb32011-06-21 09:45:33 +02003782 hdr = nl80211hdr_put(msg, NETLINK_CB(cb->skb).pid, seq, flags,
Johannes Berg2a519312009-02-10 21:25:55 +01003783 NL80211_CMD_NEW_SCAN_RESULTS);
3784 if (!hdr)
3785 return -1;
3786
Johannes Berg9720bb32011-06-21 09:45:33 +02003787 genl_dump_check_consistent(cb, hdr, &nl80211_fam);
3788
Johannes Bergf5ea9122009-08-07 16:17:38 +02003789 NLA_PUT_U32(msg, NL80211_ATTR_GENERATION, rdev->bss_generation);
Johannes Berg48ab9052009-07-10 18:42:31 +02003790 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, wdev->netdev->ifindex);
Johannes Berg2a519312009-02-10 21:25:55 +01003791
3792 bss = nla_nest_start(msg, NL80211_ATTR_BSS);
3793 if (!bss)
3794 goto nla_put_failure;
3795 if (!is_zero_ether_addr(res->bssid))
3796 NLA_PUT(msg, NL80211_BSS_BSSID, ETH_ALEN, res->bssid);
3797 if (res->information_elements && res->len_information_elements)
3798 NLA_PUT(msg, NL80211_BSS_INFORMATION_ELEMENTS,
3799 res->len_information_elements,
3800 res->information_elements);
Jouni Malinen34a6edd2010-01-06 16:19:24 +02003801 if (res->beacon_ies && res->len_beacon_ies &&
3802 res->beacon_ies != res->information_elements)
3803 NLA_PUT(msg, NL80211_BSS_BEACON_IES,
3804 res->len_beacon_ies, res->beacon_ies);
Johannes Berg2a519312009-02-10 21:25:55 +01003805 if (res->tsf)
3806 NLA_PUT_U64(msg, NL80211_BSS_TSF, res->tsf);
3807 if (res->beacon_interval)
3808 NLA_PUT_U16(msg, NL80211_BSS_BEACON_INTERVAL, res->beacon_interval);
3809 NLA_PUT_U16(msg, NL80211_BSS_CAPABILITY, res->capability);
3810 NLA_PUT_U32(msg, NL80211_BSS_FREQUENCY, res->channel->center_freq);
Holger Schurig7c896062009-09-24 12:21:01 +02003811 NLA_PUT_U32(msg, NL80211_BSS_SEEN_MS_AGO,
3812 jiffies_to_msecs(jiffies - intbss->ts));
Johannes Berg2a519312009-02-10 21:25:55 +01003813
Johannes Berg77965c92009-02-18 18:45:06 +01003814 switch (rdev->wiphy.signal_type) {
Johannes Berg2a519312009-02-10 21:25:55 +01003815 case CFG80211_SIGNAL_TYPE_MBM:
3816 NLA_PUT_U32(msg, NL80211_BSS_SIGNAL_MBM, res->signal);
3817 break;
3818 case CFG80211_SIGNAL_TYPE_UNSPEC:
3819 NLA_PUT_U8(msg, NL80211_BSS_SIGNAL_UNSPEC, res->signal);
3820 break;
3821 default:
3822 break;
3823 }
3824
Johannes Berg48ab9052009-07-10 18:42:31 +02003825 switch (wdev->iftype) {
Johannes Berg074ac8d2010-09-16 14:58:22 +02003826 case NL80211_IFTYPE_P2P_CLIENT:
Johannes Berg48ab9052009-07-10 18:42:31 +02003827 case NL80211_IFTYPE_STATION:
3828 if (intbss == wdev->current_bss)
3829 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3830 NL80211_BSS_STATUS_ASSOCIATED);
3831 else for (i = 0; i < MAX_AUTH_BSSES; i++) {
3832 if (intbss != wdev->auth_bsses[i])
3833 continue;
3834 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3835 NL80211_BSS_STATUS_AUTHENTICATED);
3836 break;
3837 }
3838 break;
3839 case NL80211_IFTYPE_ADHOC:
3840 if (intbss == wdev->current_bss)
3841 NLA_PUT_U32(msg, NL80211_BSS_STATUS,
3842 NL80211_BSS_STATUS_IBSS_JOINED);
3843 break;
3844 default:
3845 break;
3846 }
3847
Johannes Berg2a519312009-02-10 21:25:55 +01003848 nla_nest_end(msg, bss);
3849
3850 return genlmsg_end(msg, hdr);
3851
3852 nla_put_failure:
3853 genlmsg_cancel(msg, hdr);
3854 return -EMSGSIZE;
3855}
3856
3857static int nl80211_dump_scan(struct sk_buff *skb,
3858 struct netlink_callback *cb)
3859{
Johannes Berg48ab9052009-07-10 18:42:31 +02003860 struct cfg80211_registered_device *rdev;
3861 struct net_device *dev;
Johannes Berg2a519312009-02-10 21:25:55 +01003862 struct cfg80211_internal_bss *scan;
Johannes Berg48ab9052009-07-10 18:42:31 +02003863 struct wireless_dev *wdev;
Johannes Berg2a519312009-02-10 21:25:55 +01003864 int start = cb->args[1], idx = 0;
3865 int err;
3866
Johannes Berg67748892010-10-04 21:14:06 +02003867 err = nl80211_prepare_netdev_dump(skb, cb, &rdev, &dev);
3868 if (err)
3869 return err;
Johannes Berg2a519312009-02-10 21:25:55 +01003870
Johannes Berg48ab9052009-07-10 18:42:31 +02003871 wdev = dev->ieee80211_ptr;
Johannes Berg2a519312009-02-10 21:25:55 +01003872
Johannes Berg48ab9052009-07-10 18:42:31 +02003873 wdev_lock(wdev);
3874 spin_lock_bh(&rdev->bss_lock);
3875 cfg80211_bss_expire(rdev);
3876
Johannes Berg9720bb32011-06-21 09:45:33 +02003877 cb->seq = rdev->bss_generation;
3878
Johannes Berg48ab9052009-07-10 18:42:31 +02003879 list_for_each_entry(scan, &rdev->bss_list, list) {
Johannes Berg2a519312009-02-10 21:25:55 +01003880 if (++idx <= start)
3881 continue;
Johannes Berg9720bb32011-06-21 09:45:33 +02003882 if (nl80211_send_bss(skb, cb,
Johannes Berg2a519312009-02-10 21:25:55 +01003883 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Johannes Berg48ab9052009-07-10 18:42:31 +02003884 rdev, wdev, scan) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01003885 idx--;
Johannes Berg67748892010-10-04 21:14:06 +02003886 break;
Johannes Berg2a519312009-02-10 21:25:55 +01003887 }
3888 }
3889
Johannes Berg48ab9052009-07-10 18:42:31 +02003890 spin_unlock_bh(&rdev->bss_lock);
3891 wdev_unlock(wdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003892
3893 cb->args[1] = idx;
Johannes Berg67748892010-10-04 21:14:06 +02003894 nl80211_finish_netdev_dump(rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01003895
Johannes Berg67748892010-10-04 21:14:06 +02003896 return skb->len;
Johannes Berg2a519312009-02-10 21:25:55 +01003897}
3898
Holger Schurig61fa7132009-11-11 12:25:40 +01003899static int nl80211_send_survey(struct sk_buff *msg, u32 pid, u32 seq,
3900 int flags, struct net_device *dev,
3901 struct survey_info *survey)
3902{
3903 void *hdr;
3904 struct nlattr *infoattr;
3905
Holger Schurig61fa7132009-11-11 12:25:40 +01003906 hdr = nl80211hdr_put(msg, pid, seq, flags,
3907 NL80211_CMD_NEW_SURVEY_RESULTS);
3908 if (!hdr)
3909 return -ENOMEM;
3910
3911 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
3912
3913 infoattr = nla_nest_start(msg, NL80211_ATTR_SURVEY_INFO);
3914 if (!infoattr)
3915 goto nla_put_failure;
3916
3917 NLA_PUT_U32(msg, NL80211_SURVEY_INFO_FREQUENCY,
3918 survey->channel->center_freq);
3919 if (survey->filled & SURVEY_INFO_NOISE_DBM)
3920 NLA_PUT_U8(msg, NL80211_SURVEY_INFO_NOISE,
3921 survey->noise);
Felix Fietkau17e5a802010-09-29 17:15:30 +02003922 if (survey->filled & SURVEY_INFO_IN_USE)
3923 NLA_PUT_FLAG(msg, NL80211_SURVEY_INFO_IN_USE);
Felix Fietkau8610c292010-10-09 02:39:29 +02003924 if (survey->filled & SURVEY_INFO_CHANNEL_TIME)
3925 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME,
3926 survey->channel_time);
3927 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_BUSY)
3928 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_BUSY,
3929 survey->channel_time_busy);
3930 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_EXT_BUSY)
3931 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_EXT_BUSY,
3932 survey->channel_time_ext_busy);
3933 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_RX)
3934 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_RX,
3935 survey->channel_time_rx);
3936 if (survey->filled & SURVEY_INFO_CHANNEL_TIME_TX)
3937 NLA_PUT_U64(msg, NL80211_SURVEY_INFO_CHANNEL_TIME_TX,
3938 survey->channel_time_tx);
Holger Schurig61fa7132009-11-11 12:25:40 +01003939
3940 nla_nest_end(msg, infoattr);
3941
3942 return genlmsg_end(msg, hdr);
3943
3944 nla_put_failure:
3945 genlmsg_cancel(msg, hdr);
3946 return -EMSGSIZE;
3947}
3948
3949static int nl80211_dump_survey(struct sk_buff *skb,
3950 struct netlink_callback *cb)
3951{
3952 struct survey_info survey;
3953 struct cfg80211_registered_device *dev;
3954 struct net_device *netdev;
Holger Schurig61fa7132009-11-11 12:25:40 +01003955 int survey_idx = cb->args[1];
3956 int res;
3957
Johannes Berg67748892010-10-04 21:14:06 +02003958 res = nl80211_prepare_netdev_dump(skb, cb, &dev, &netdev);
3959 if (res)
3960 return res;
Holger Schurig61fa7132009-11-11 12:25:40 +01003961
3962 if (!dev->ops->dump_survey) {
3963 res = -EOPNOTSUPP;
3964 goto out_err;
3965 }
3966
3967 while (1) {
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07003968 struct ieee80211_channel *chan;
3969
Holger Schurig61fa7132009-11-11 12:25:40 +01003970 res = dev->ops->dump_survey(&dev->wiphy, netdev, survey_idx,
3971 &survey);
3972 if (res == -ENOENT)
3973 break;
3974 if (res)
3975 goto out_err;
3976
Luis R. Rodriguez180cdc72011-05-27 07:24:02 -07003977 /* Survey without a channel doesn't make sense */
3978 if (!survey.channel) {
3979 res = -EINVAL;
3980 goto out;
3981 }
3982
3983 chan = ieee80211_get_channel(&dev->wiphy,
3984 survey.channel->center_freq);
3985 if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
3986 survey_idx++;
3987 continue;
3988 }
3989
Holger Schurig61fa7132009-11-11 12:25:40 +01003990 if (nl80211_send_survey(skb,
3991 NETLINK_CB(cb->skb).pid,
3992 cb->nlh->nlmsg_seq, NLM_F_MULTI,
3993 netdev,
3994 &survey) < 0)
3995 goto out;
3996 survey_idx++;
3997 }
3998
3999 out:
4000 cb->args[1] = survey_idx;
4001 res = skb->len;
4002 out_err:
Johannes Berg67748892010-10-04 21:14:06 +02004003 nl80211_finish_netdev_dump(dev);
Holger Schurig61fa7132009-11-11 12:25:40 +01004004 return res;
4005}
4006
Jouni Malinen255e7372009-03-20 21:21:17 +02004007static bool nl80211_valid_auth_type(enum nl80211_auth_type auth_type)
4008{
Samuel Ortizb23aa672009-07-01 21:26:54 +02004009 return auth_type <= NL80211_AUTHTYPE_MAX;
Jouni Malinen255e7372009-03-20 21:21:17 +02004010}
4011
Samuel Ortizb23aa672009-07-01 21:26:54 +02004012static bool nl80211_valid_wpa_versions(u32 wpa_versions)
4013{
4014 return !(wpa_versions & ~(NL80211_WPA_VERSION_1 |
4015 NL80211_WPA_VERSION_2));
4016}
4017
4018static bool nl80211_valid_akm_suite(u32 akm)
4019{
4020 return akm == WLAN_AKM_SUITE_8021X ||
4021 akm == WLAN_AKM_SUITE_PSK;
4022}
4023
4024static bool nl80211_valid_cipher_suite(u32 cipher)
4025{
4026 return cipher == WLAN_CIPHER_SUITE_WEP40 ||
4027 cipher == WLAN_CIPHER_SUITE_WEP104 ||
4028 cipher == WLAN_CIPHER_SUITE_TKIP ||
4029 cipher == WLAN_CIPHER_SUITE_CCMP ||
4030 cipher == WLAN_CIPHER_SUITE_AES_CMAC;
4031}
4032
4033
Jouni Malinen636a5d32009-03-19 13:39:22 +02004034static int nl80211_authenticate(struct sk_buff *skb, struct genl_info *info)
4035{
Johannes Berg4c476992010-10-04 21:36:35 +02004036 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4037 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004038 struct ieee80211_channel *chan;
4039 const u8 *bssid, *ssid, *ie = NULL;
4040 int err, ssid_len, ie_len = 0;
4041 enum nl80211_auth_type auth_type;
Johannes Bergfffd0932009-07-08 14:22:54 +02004042 struct key_parse key;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004043 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004044
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004045 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4046 return -EINVAL;
4047
4048 if (!info->attrs[NL80211_ATTR_MAC])
4049 return -EINVAL;
4050
Jouni Malinen17780922009-03-27 20:52:47 +02004051 if (!info->attrs[NL80211_ATTR_AUTH_TYPE])
4052 return -EINVAL;
4053
Johannes Berg19957bb2009-07-02 17:20:43 +02004054 if (!info->attrs[NL80211_ATTR_SSID])
4055 return -EINVAL;
4056
4057 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ])
4058 return -EINVAL;
4059
Johannes Bergfffd0932009-07-08 14:22:54 +02004060 err = nl80211_parse_key(info, &key);
4061 if (err)
4062 return err;
4063
4064 if (key.idx >= 0) {
Johannes Berge31b8212010-10-05 19:39:30 +02004065 if (key.type != -1 && key.type != NL80211_KEYTYPE_GROUP)
4066 return -EINVAL;
Johannes Bergfffd0932009-07-08 14:22:54 +02004067 if (!key.p.key || !key.p.key_len)
4068 return -EINVAL;
4069 if ((key.p.cipher != WLAN_CIPHER_SUITE_WEP40 ||
4070 key.p.key_len != WLAN_KEY_LEN_WEP40) &&
4071 (key.p.cipher != WLAN_CIPHER_SUITE_WEP104 ||
4072 key.p.key_len != WLAN_KEY_LEN_WEP104))
4073 return -EINVAL;
4074 if (key.idx > 4)
4075 return -EINVAL;
4076 } else {
4077 key.p.key_len = 0;
4078 key.p.key = NULL;
4079 }
4080
Johannes Bergafea0b72010-08-10 09:46:42 +02004081 if (key.idx >= 0) {
4082 int i;
4083 bool ok = false;
4084 for (i = 0; i < rdev->wiphy.n_cipher_suites; i++) {
4085 if (key.p.cipher == rdev->wiphy.cipher_suites[i]) {
4086 ok = true;
4087 break;
4088 }
4089 }
Johannes Berg4c476992010-10-04 21:36:35 +02004090 if (!ok)
4091 return -EINVAL;
Johannes Bergafea0b72010-08-10 09:46:42 +02004092 }
4093
Johannes Berg4c476992010-10-04 21:36:35 +02004094 if (!rdev->ops->auth)
4095 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004096
Johannes Berg074ac8d2010-09-16 14:58:22 +02004097 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004098 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4099 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004100
Johannes Berg19957bb2009-07-02 17:20:43 +02004101 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Johannes Berg79c97e92009-07-07 03:56:12 +02004102 chan = ieee80211_get_channel(&rdev->wiphy,
Johannes Berg19957bb2009-07-02 17:20:43 +02004103 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004104 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4105 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004106
Johannes Berg19957bb2009-07-02 17:20:43 +02004107 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4108 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4109
4110 if (info->attrs[NL80211_ATTR_IE]) {
4111 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4112 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4113 }
4114
4115 auth_type = nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
Johannes Berg4c476992010-10-04 21:36:35 +02004116 if (!nl80211_valid_auth_type(auth_type))
4117 return -EINVAL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004118
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004119 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4120
Johannes Berg4c476992010-10-04 21:36:35 +02004121 return cfg80211_mlme_auth(rdev, dev, chan, auth_type, bssid,
4122 ssid, ssid_len, ie, ie_len,
4123 key.p.key, key.p.key_len, key.idx,
4124 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004125}
4126
Johannes Bergc0692b82010-08-27 14:26:53 +03004127static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
4128 struct genl_info *info,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004129 struct cfg80211_crypto_settings *settings,
4130 int cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004131{
Johannes Bergc0b2bbd2009-07-25 16:54:36 +02004132 memset(settings, 0, sizeof(*settings));
4133
Samuel Ortizb23aa672009-07-01 21:26:54 +02004134 settings->control_port = info->attrs[NL80211_ATTR_CONTROL_PORT];
4135
Johannes Bergc0692b82010-08-27 14:26:53 +03004136 if (info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]) {
4137 u16 proto;
4138 proto = nla_get_u16(
4139 info->attrs[NL80211_ATTR_CONTROL_PORT_ETHERTYPE]);
4140 settings->control_port_ethertype = cpu_to_be16(proto);
4141 if (!(rdev->wiphy.flags & WIPHY_FLAG_CONTROL_PORT_PROTOCOL) &&
4142 proto != ETH_P_PAE)
4143 return -EINVAL;
4144 if (info->attrs[NL80211_ATTR_CONTROL_PORT_NO_ENCRYPT])
4145 settings->control_port_no_encrypt = true;
4146 } else
4147 settings->control_port_ethertype = cpu_to_be16(ETH_P_PAE);
4148
Samuel Ortizb23aa672009-07-01 21:26:54 +02004149 if (info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]) {
4150 void *data;
4151 int len, i;
4152
4153 data = nla_data(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4154 len = nla_len(info->attrs[NL80211_ATTR_CIPHER_SUITES_PAIRWISE]);
4155 settings->n_ciphers_pairwise = len / sizeof(u32);
4156
4157 if (len % sizeof(u32))
4158 return -EINVAL;
4159
Johannes Berg3dc27d22009-07-02 21:36:37 +02004160 if (settings->n_ciphers_pairwise > cipher_limit)
Samuel Ortizb23aa672009-07-01 21:26:54 +02004161 return -EINVAL;
4162
4163 memcpy(settings->ciphers_pairwise, data, len);
4164
4165 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4166 if (!nl80211_valid_cipher_suite(
4167 settings->ciphers_pairwise[i]))
4168 return -EINVAL;
4169 }
4170
4171 if (info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]) {
4172 settings->cipher_group =
4173 nla_get_u32(info->attrs[NL80211_ATTR_CIPHER_SUITE_GROUP]);
4174 if (!nl80211_valid_cipher_suite(settings->cipher_group))
4175 return -EINVAL;
4176 }
4177
4178 if (info->attrs[NL80211_ATTR_WPA_VERSIONS]) {
4179 settings->wpa_versions =
4180 nla_get_u32(info->attrs[NL80211_ATTR_WPA_VERSIONS]);
4181 if (!nl80211_valid_wpa_versions(settings->wpa_versions))
4182 return -EINVAL;
4183 }
4184
4185 if (info->attrs[NL80211_ATTR_AKM_SUITES]) {
4186 void *data;
4187 int len, i;
4188
4189 data = nla_data(info->attrs[NL80211_ATTR_AKM_SUITES]);
4190 len = nla_len(info->attrs[NL80211_ATTR_AKM_SUITES]);
4191 settings->n_akm_suites = len / sizeof(u32);
4192
4193 if (len % sizeof(u32))
4194 return -EINVAL;
4195
4196 memcpy(settings->akm_suites, data, len);
4197
4198 for (i = 0; i < settings->n_ciphers_pairwise; i++)
4199 if (!nl80211_valid_akm_suite(settings->akm_suites[i]))
4200 return -EINVAL;
4201 }
4202
4203 return 0;
4204}
4205
Jouni Malinen636a5d32009-03-19 13:39:22 +02004206static int nl80211_associate(struct sk_buff *skb, struct genl_info *info)
4207{
Johannes Berg4c476992010-10-04 21:36:35 +02004208 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4209 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004210 struct cfg80211_crypto_settings crypto;
Johannes Bergf444de02010-05-05 15:25:02 +02004211 struct ieee80211_channel *chan;
Johannes Berg3e5d7642009-07-07 14:37:26 +02004212 const u8 *bssid, *ssid, *ie = NULL, *prev_bssid = NULL;
Johannes Berg19957bb2009-07-02 17:20:43 +02004213 int err, ssid_len, ie_len = 0;
4214 bool use_mfp = false;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004215
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004216 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4217 return -EINVAL;
4218
4219 if (!info->attrs[NL80211_ATTR_MAC] ||
Johannes Berg19957bb2009-07-02 17:20:43 +02004220 !info->attrs[NL80211_ATTR_SSID] ||
4221 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004222 return -EINVAL;
4223
Johannes Berg4c476992010-10-04 21:36:35 +02004224 if (!rdev->ops->assoc)
4225 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004226
Johannes Berg074ac8d2010-09-16 14:58:22 +02004227 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004228 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4229 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004230
Johannes Berg19957bb2009-07-02 17:20:43 +02004231 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004232
Johannes Berg19957bb2009-07-02 17:20:43 +02004233 chan = ieee80211_get_channel(&rdev->wiphy,
4234 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
Johannes Berg4c476992010-10-04 21:36:35 +02004235 if (!chan || (chan->flags & IEEE80211_CHAN_DISABLED))
4236 return -EINVAL;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004237
Johannes Berg19957bb2009-07-02 17:20:43 +02004238 ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4239 ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004240
4241 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004242 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4243 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004244 }
4245
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004246 if (info->attrs[NL80211_ATTR_USE_MFP]) {
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004247 enum nl80211_mfp mfp =
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004248 nla_get_u32(info->attrs[NL80211_ATTR_USE_MFP]);
Johannes Berg4f5dadc2009-07-07 03:56:10 +02004249 if (mfp == NL80211_MFP_REQUIRED)
Johannes Berg19957bb2009-07-02 17:20:43 +02004250 use_mfp = true;
Johannes Berg4c476992010-10-04 21:36:35 +02004251 else if (mfp != NL80211_MFP_NO)
4252 return -EINVAL;
Jouni Malinendc6382ce2009-05-06 22:09:37 +03004253 }
4254
Johannes Berg3e5d7642009-07-07 14:37:26 +02004255 if (info->attrs[NL80211_ATTR_PREV_BSSID])
4256 prev_bssid = nla_data(info->attrs[NL80211_ATTR_PREV_BSSID]);
4257
Johannes Bergc0692b82010-08-27 14:26:53 +03004258 err = nl80211_crypto_settings(rdev, info, &crypto, 1);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004259 if (!err)
Johannes Berg3e5d7642009-07-07 14:37:26 +02004260 err = cfg80211_mlme_assoc(rdev, dev, chan, bssid, prev_bssid,
4261 ssid, ssid_len, ie, ie_len, use_mfp,
Johannes Berg19957bb2009-07-02 17:20:43 +02004262 &crypto);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004263
Jouni Malinen636a5d32009-03-19 13:39:22 +02004264 return err;
4265}
4266
4267static int nl80211_deauthenticate(struct sk_buff *skb, struct genl_info *info)
4268{
Johannes Berg4c476992010-10-04 21:36:35 +02004269 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4270 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004271 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004272 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004273 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004274 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004275
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004276 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4277 return -EINVAL;
4278
4279 if (!info->attrs[NL80211_ATTR_MAC])
4280 return -EINVAL;
4281
4282 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4283 return -EINVAL;
4284
Johannes Berg4c476992010-10-04 21:36:35 +02004285 if (!rdev->ops->deauth)
4286 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004287
Johannes Berg074ac8d2010-09-16 14:58:22 +02004288 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004289 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4290 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004291
Johannes Berg19957bb2009-07-02 17:20:43 +02004292 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004293
Johannes Berg19957bb2009-07-02 17:20:43 +02004294 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4295 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004296 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004297 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004298 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004299
4300 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004301 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4302 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004303 }
4304
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004305 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4306
Johannes Berg4c476992010-10-04 21:36:35 +02004307 return cfg80211_mlme_deauth(rdev, dev, bssid, ie, ie_len, reason_code,
4308 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004309}
4310
4311static int nl80211_disassociate(struct sk_buff *skb, struct genl_info *info)
4312{
Johannes Berg4c476992010-10-04 21:36:35 +02004313 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4314 struct net_device *dev = info->user_ptr[1];
Johannes Berg19957bb2009-07-02 17:20:43 +02004315 const u8 *ie = NULL, *bssid;
Johannes Berg4c476992010-10-04 21:36:35 +02004316 int ie_len = 0;
Johannes Berg19957bb2009-07-02 17:20:43 +02004317 u16 reason_code;
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004318 bool local_state_change;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004319
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004320 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4321 return -EINVAL;
4322
4323 if (!info->attrs[NL80211_ATTR_MAC])
4324 return -EINVAL;
4325
4326 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4327 return -EINVAL;
4328
Johannes Berg4c476992010-10-04 21:36:35 +02004329 if (!rdev->ops->disassoc)
4330 return -EOPNOTSUPP;
Jouni Malinen636a5d32009-03-19 13:39:22 +02004331
Johannes Berg074ac8d2010-09-16 14:58:22 +02004332 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004333 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4334 return -EOPNOTSUPP;
Jouni Malineneec60b02009-03-20 21:21:19 +02004335
Johannes Berg19957bb2009-07-02 17:20:43 +02004336 bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004337
Johannes Berg19957bb2009-07-02 17:20:43 +02004338 reason_code = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4339 if (reason_code == 0) {
Johannes Bergf4a11bb2009-03-27 12:40:28 +01004340 /* Reason Code 0 is reserved */
Johannes Berg4c476992010-10-04 21:36:35 +02004341 return -EINVAL;
Jouni Malinen255e7372009-03-20 21:21:17 +02004342 }
Jouni Malinen636a5d32009-03-19 13:39:22 +02004343
4344 if (info->attrs[NL80211_ATTR_IE]) {
Johannes Berg19957bb2009-07-02 17:20:43 +02004345 ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4346 ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004347 }
4348
Jouni Malinend5cdfac2010-04-04 09:37:19 +03004349 local_state_change = !!info->attrs[NL80211_ATTR_LOCAL_STATE_CHANGE];
4350
Johannes Berg4c476992010-10-04 21:36:35 +02004351 return cfg80211_mlme_disassoc(rdev, dev, bssid, ie, ie_len, reason_code,
4352 local_state_change);
Jouni Malinen636a5d32009-03-19 13:39:22 +02004353}
4354
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004355static bool
4356nl80211_parse_mcast_rate(struct cfg80211_registered_device *rdev,
4357 int mcast_rate[IEEE80211_NUM_BANDS],
4358 int rateval)
4359{
4360 struct wiphy *wiphy = &rdev->wiphy;
4361 bool found = false;
4362 int band, i;
4363
4364 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
4365 struct ieee80211_supported_band *sband;
4366
4367 sband = wiphy->bands[band];
4368 if (!sband)
4369 continue;
4370
4371 for (i = 0; i < sband->n_bitrates; i++) {
4372 if (sband->bitrates[i].bitrate == rateval) {
4373 mcast_rate[band] = i + 1;
4374 found = true;
4375 break;
4376 }
4377 }
4378 }
4379
4380 return found;
4381}
4382
Johannes Berg04a773a2009-04-19 21:24:32 +02004383static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
4384{
Johannes Berg4c476992010-10-04 21:36:35 +02004385 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4386 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004387 struct cfg80211_ibss_params ibss;
4388 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004389 struct cfg80211_cached_keys *connkeys = NULL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004390 int err;
4391
Johannes Berg8e30bc52009-04-22 17:45:38 +02004392 memset(&ibss, 0, sizeof(ibss));
4393
Johannes Berg04a773a2009-04-19 21:24:32 +02004394 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4395 return -EINVAL;
4396
4397 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4398 !info->attrs[NL80211_ATTR_SSID] ||
4399 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4400 return -EINVAL;
4401
Johannes Berg8e30bc52009-04-22 17:45:38 +02004402 ibss.beacon_interval = 100;
4403
4404 if (info->attrs[NL80211_ATTR_BEACON_INTERVAL]) {
4405 ibss.beacon_interval =
4406 nla_get_u32(info->attrs[NL80211_ATTR_BEACON_INTERVAL]);
4407 if (ibss.beacon_interval < 1 || ibss.beacon_interval > 10000)
4408 return -EINVAL;
4409 }
4410
Johannes Berg4c476992010-10-04 21:36:35 +02004411 if (!rdev->ops->join_ibss)
4412 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004413
Johannes Berg4c476992010-10-04 21:36:35 +02004414 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4415 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004416
Johannes Berg79c97e92009-07-07 03:56:12 +02004417 wiphy = &rdev->wiphy;
Johannes Berg04a773a2009-04-19 21:24:32 +02004418
4419 if (info->attrs[NL80211_ATTR_MAC])
4420 ibss.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4421 ibss.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4422 ibss.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4423
4424 if (info->attrs[NL80211_ATTR_IE]) {
4425 ibss.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4426 ibss.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4427 }
4428
4429 ibss.channel = ieee80211_get_channel(wiphy,
4430 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4431 if (!ibss.channel ||
4432 ibss.channel->flags & IEEE80211_CHAN_NO_IBSS ||
Johannes Berg4c476992010-10-04 21:36:35 +02004433 ibss.channel->flags & IEEE80211_CHAN_DISABLED)
4434 return -EINVAL;
Johannes Berg04a773a2009-04-19 21:24:32 +02004435
4436 ibss.channel_fixed = !!info->attrs[NL80211_ATTR_FREQ_FIXED];
Johannes Bergfffd0932009-07-08 14:22:54 +02004437 ibss.privacy = !!info->attrs[NL80211_ATTR_PRIVACY];
Johannes Berg04a773a2009-04-19 21:24:32 +02004438
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004439 if (info->attrs[NL80211_ATTR_BSS_BASIC_RATES]) {
4440 u8 *rates =
4441 nla_data(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4442 int n_rates =
4443 nla_len(info->attrs[NL80211_ATTR_BSS_BASIC_RATES]);
4444 struct ieee80211_supported_band *sband =
4445 wiphy->bands[ibss.channel->band];
Johannes Berg34850ab2011-07-18 18:08:35 +02004446 int err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004447
Johannes Berg34850ab2011-07-18 18:08:35 +02004448 err = ieee80211_get_ratemask(sband, rates, n_rates,
4449 &ibss.basic_rates);
4450 if (err)
4451 return err;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004452 }
Felix Fietkaudd5b4cc2010-11-22 20:58:24 +01004453
4454 if (info->attrs[NL80211_ATTR_MCAST_RATE] &&
4455 !nl80211_parse_mcast_rate(rdev, ibss.mcast_rate,
4456 nla_get_u32(info->attrs[NL80211_ATTR_MCAST_RATE])))
4457 return -EINVAL;
Teemu Paasikivifbd2c8d2010-06-14 12:55:31 +03004458
Johannes Berg4c476992010-10-04 21:36:35 +02004459 if (ibss.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4460 connkeys = nl80211_parse_connkeys(rdev,
4461 info->attrs[NL80211_ATTR_KEYS]);
4462 if (IS_ERR(connkeys))
4463 return PTR_ERR(connkeys);
4464 }
Johannes Berg04a773a2009-04-19 21:24:32 +02004465
Johannes Berg4c476992010-10-04 21:36:35 +02004466 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004467 if (err)
4468 kfree(connkeys);
Johannes Berg04a773a2009-04-19 21:24:32 +02004469 return err;
4470}
4471
4472static int nl80211_leave_ibss(struct sk_buff *skb, struct genl_info *info)
4473{
Johannes Berg4c476992010-10-04 21:36:35 +02004474 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4475 struct net_device *dev = info->user_ptr[1];
Johannes Berg04a773a2009-04-19 21:24:32 +02004476
Johannes Berg4c476992010-10-04 21:36:35 +02004477 if (!rdev->ops->leave_ibss)
4478 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004479
Johannes Berg4c476992010-10-04 21:36:35 +02004480 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
4481 return -EOPNOTSUPP;
Johannes Berg04a773a2009-04-19 21:24:32 +02004482
Johannes Berg4c476992010-10-04 21:36:35 +02004483 return cfg80211_leave_ibss(rdev, dev, false);
Johannes Berg04a773a2009-04-19 21:24:32 +02004484}
4485
Johannes Bergaff89a92009-07-01 21:26:51 +02004486#ifdef CONFIG_NL80211_TESTMODE
4487static struct genl_multicast_group nl80211_testmode_mcgrp = {
4488 .name = "testmode",
4489};
4490
4491static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
4492{
Johannes Berg4c476992010-10-04 21:36:35 +02004493 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Bergaff89a92009-07-01 21:26:51 +02004494 int err;
4495
4496 if (!info->attrs[NL80211_ATTR_TESTDATA])
4497 return -EINVAL;
4498
Johannes Bergaff89a92009-07-01 21:26:51 +02004499 err = -EOPNOTSUPP;
4500 if (rdev->ops->testmode_cmd) {
4501 rdev->testmode_info = info;
4502 err = rdev->ops->testmode_cmd(&rdev->wiphy,
4503 nla_data(info->attrs[NL80211_ATTR_TESTDATA]),
4504 nla_len(info->attrs[NL80211_ATTR_TESTDATA]));
4505 rdev->testmode_info = NULL;
4506 }
4507
Johannes Bergaff89a92009-07-01 21:26:51 +02004508 return err;
4509}
4510
Wey-Yi Guy71063f02011-05-20 09:05:54 -07004511static int nl80211_testmode_dump(struct sk_buff *skb,
4512 struct netlink_callback *cb)
4513{
4514 struct cfg80211_registered_device *dev;
4515 int err;
4516 long phy_idx;
4517 void *data = NULL;
4518 int data_len = 0;
4519
4520 if (cb->args[0]) {
4521 /*
4522 * 0 is a valid index, but not valid for args[0],
4523 * so we need to offset by 1.
4524 */
4525 phy_idx = cb->args[0] - 1;
4526 } else {
4527 err = nlmsg_parse(cb->nlh, GENL_HDRLEN + nl80211_fam.hdrsize,
4528 nl80211_fam.attrbuf, nl80211_fam.maxattr,
4529 nl80211_policy);
4530 if (err)
4531 return err;
4532 if (!nl80211_fam.attrbuf[NL80211_ATTR_WIPHY])
4533 return -EINVAL;
4534 phy_idx = nla_get_u32(nl80211_fam.attrbuf[NL80211_ATTR_WIPHY]);
4535 if (nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA])
4536 cb->args[1] =
4537 (long)nl80211_fam.attrbuf[NL80211_ATTR_TESTDATA];
4538 }
4539
4540 if (cb->args[1]) {
4541 data = nla_data((void *)cb->args[1]);
4542 data_len = nla_len((void *)cb->args[1]);
4543 }
4544
4545 mutex_lock(&cfg80211_mutex);
4546 dev = cfg80211_rdev_by_wiphy_idx(phy_idx);
4547 if (!dev) {
4548 mutex_unlock(&cfg80211_mutex);
4549 return -ENOENT;
4550 }
4551 cfg80211_lock_rdev(dev);
4552 mutex_unlock(&cfg80211_mutex);
4553
4554 if (!dev->ops->testmode_dump) {
4555 err = -EOPNOTSUPP;
4556 goto out_err;
4557 }
4558
4559 while (1) {
4560 void *hdr = nl80211hdr_put(skb, NETLINK_CB(cb->skb).pid,
4561 cb->nlh->nlmsg_seq, NLM_F_MULTI,
4562 NL80211_CMD_TESTMODE);
4563 struct nlattr *tmdata;
4564
4565 if (nla_put_u32(skb, NL80211_ATTR_WIPHY, dev->wiphy_idx) < 0) {
4566 genlmsg_cancel(skb, hdr);
4567 break;
4568 }
4569
4570 tmdata = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4571 if (!tmdata) {
4572 genlmsg_cancel(skb, hdr);
4573 break;
4574 }
4575 err = dev->ops->testmode_dump(&dev->wiphy, skb, cb,
4576 data, data_len);
4577 nla_nest_end(skb, tmdata);
4578
4579 if (err == -ENOBUFS || err == -ENOENT) {
4580 genlmsg_cancel(skb, hdr);
4581 break;
4582 } else if (err) {
4583 genlmsg_cancel(skb, hdr);
4584 goto out_err;
4585 }
4586
4587 genlmsg_end(skb, hdr);
4588 }
4589
4590 err = skb->len;
4591 /* see above */
4592 cb->args[0] = phy_idx + 1;
4593 out_err:
4594 cfg80211_unlock_rdev(dev);
4595 return err;
4596}
4597
Johannes Bergaff89a92009-07-01 21:26:51 +02004598static struct sk_buff *
4599__cfg80211_testmode_alloc_skb(struct cfg80211_registered_device *rdev,
4600 int approxlen, u32 pid, u32 seq, gfp_t gfp)
4601{
4602 struct sk_buff *skb;
4603 void *hdr;
4604 struct nlattr *data;
4605
4606 skb = nlmsg_new(approxlen + 100, gfp);
4607 if (!skb)
4608 return NULL;
4609
4610 hdr = nl80211hdr_put(skb, pid, seq, 0, NL80211_CMD_TESTMODE);
4611 if (!hdr) {
4612 kfree_skb(skb);
4613 return NULL;
4614 }
4615
4616 NLA_PUT_U32(skb, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
4617 data = nla_nest_start(skb, NL80211_ATTR_TESTDATA);
4618
4619 ((void **)skb->cb)[0] = rdev;
4620 ((void **)skb->cb)[1] = hdr;
4621 ((void **)skb->cb)[2] = data;
4622
4623 return skb;
4624
4625 nla_put_failure:
4626 kfree_skb(skb);
4627 return NULL;
4628}
4629
4630struct sk_buff *cfg80211_testmode_alloc_reply_skb(struct wiphy *wiphy,
4631 int approxlen)
4632{
4633 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4634
4635 if (WARN_ON(!rdev->testmode_info))
4636 return NULL;
4637
4638 return __cfg80211_testmode_alloc_skb(rdev, approxlen,
4639 rdev->testmode_info->snd_pid,
4640 rdev->testmode_info->snd_seq,
4641 GFP_KERNEL);
4642}
4643EXPORT_SYMBOL(cfg80211_testmode_alloc_reply_skb);
4644
4645int cfg80211_testmode_reply(struct sk_buff *skb)
4646{
4647 struct cfg80211_registered_device *rdev = ((void **)skb->cb)[0];
4648 void *hdr = ((void **)skb->cb)[1];
4649 struct nlattr *data = ((void **)skb->cb)[2];
4650
4651 if (WARN_ON(!rdev->testmode_info)) {
4652 kfree_skb(skb);
4653 return -EINVAL;
4654 }
4655
4656 nla_nest_end(skb, data);
4657 genlmsg_end(skb, hdr);
4658 return genlmsg_reply(skb, rdev->testmode_info);
4659}
4660EXPORT_SYMBOL(cfg80211_testmode_reply);
4661
4662struct sk_buff *cfg80211_testmode_alloc_event_skb(struct wiphy *wiphy,
4663 int approxlen, gfp_t gfp)
4664{
4665 struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
4666
4667 return __cfg80211_testmode_alloc_skb(rdev, approxlen, 0, 0, gfp);
4668}
4669EXPORT_SYMBOL(cfg80211_testmode_alloc_event_skb);
4670
4671void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
4672{
4673 void *hdr = ((void **)skb->cb)[1];
4674 struct nlattr *data = ((void **)skb->cb)[2];
4675
4676 nla_nest_end(skb, data);
4677 genlmsg_end(skb, hdr);
4678 genlmsg_multicast(skb, 0, nl80211_testmode_mcgrp.id, gfp);
4679}
4680EXPORT_SYMBOL(cfg80211_testmode_event);
4681#endif
4682
Samuel Ortizb23aa672009-07-01 21:26:54 +02004683static int nl80211_connect(struct sk_buff *skb, struct genl_info *info)
4684{
Johannes Berg4c476992010-10-04 21:36:35 +02004685 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4686 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004687 struct cfg80211_connect_params connect;
4688 struct wiphy *wiphy;
Johannes Bergfffd0932009-07-08 14:22:54 +02004689 struct cfg80211_cached_keys *connkeys = NULL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004690 int err;
4691
4692 memset(&connect, 0, sizeof(connect));
4693
4694 if (!is_valid_ie_attr(info->attrs[NL80211_ATTR_IE]))
4695 return -EINVAL;
4696
4697 if (!info->attrs[NL80211_ATTR_SSID] ||
4698 !nla_len(info->attrs[NL80211_ATTR_SSID]))
4699 return -EINVAL;
4700
4701 if (info->attrs[NL80211_ATTR_AUTH_TYPE]) {
4702 connect.auth_type =
4703 nla_get_u32(info->attrs[NL80211_ATTR_AUTH_TYPE]);
4704 if (!nl80211_valid_auth_type(connect.auth_type))
4705 return -EINVAL;
4706 } else
4707 connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
4708
4709 connect.privacy = info->attrs[NL80211_ATTR_PRIVACY];
4710
Johannes Bergc0692b82010-08-27 14:26:53 +03004711 err = nl80211_crypto_settings(rdev, info, &connect.crypto,
Johannes Berg3dc27d22009-07-02 21:36:37 +02004712 NL80211_MAX_NR_CIPHER_SUITES);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004713 if (err)
4714 return err;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004715
Johannes Berg074ac8d2010-09-16 14:58:22 +02004716 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004717 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4718 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004719
Johannes Berg79c97e92009-07-07 03:56:12 +02004720 wiphy = &rdev->wiphy;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004721
Samuel Ortizb23aa672009-07-01 21:26:54 +02004722 if (info->attrs[NL80211_ATTR_MAC])
4723 connect.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4724 connect.ssid = nla_data(info->attrs[NL80211_ATTR_SSID]);
4725 connect.ssid_len = nla_len(info->attrs[NL80211_ATTR_SSID]);
4726
4727 if (info->attrs[NL80211_ATTR_IE]) {
4728 connect.ie = nla_data(info->attrs[NL80211_ATTR_IE]);
4729 connect.ie_len = nla_len(info->attrs[NL80211_ATTR_IE]);
4730 }
4731
4732 if (info->attrs[NL80211_ATTR_WIPHY_FREQ]) {
4733 connect.channel =
4734 ieee80211_get_channel(wiphy,
4735 nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]));
4736 if (!connect.channel ||
Johannes Berg4c476992010-10-04 21:36:35 +02004737 connect.channel->flags & IEEE80211_CHAN_DISABLED)
4738 return -EINVAL;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004739 }
4740
Johannes Bergfffd0932009-07-08 14:22:54 +02004741 if (connect.privacy && info->attrs[NL80211_ATTR_KEYS]) {
4742 connkeys = nl80211_parse_connkeys(rdev,
4743 info->attrs[NL80211_ATTR_KEYS]);
Johannes Berg4c476992010-10-04 21:36:35 +02004744 if (IS_ERR(connkeys))
4745 return PTR_ERR(connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004746 }
4747
4748 err = cfg80211_connect(rdev, dev, &connect, connkeys);
Johannes Bergfffd0932009-07-08 14:22:54 +02004749 if (err)
4750 kfree(connkeys);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004751 return err;
4752}
4753
4754static int nl80211_disconnect(struct sk_buff *skb, struct genl_info *info)
4755{
Johannes Berg4c476992010-10-04 21:36:35 +02004756 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4757 struct net_device *dev = info->user_ptr[1];
Samuel Ortizb23aa672009-07-01 21:26:54 +02004758 u16 reason;
4759
4760 if (!info->attrs[NL80211_ATTR_REASON_CODE])
4761 reason = WLAN_REASON_DEAUTH_LEAVING;
4762 else
4763 reason = nla_get_u16(info->attrs[NL80211_ATTR_REASON_CODE]);
4764
4765 if (reason == 0)
4766 return -EINVAL;
4767
Johannes Berg074ac8d2010-09-16 14:58:22 +02004768 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004769 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4770 return -EOPNOTSUPP;
Samuel Ortizb23aa672009-07-01 21:26:54 +02004771
Johannes Berg4c476992010-10-04 21:36:35 +02004772 return cfg80211_disconnect(rdev, dev, reason, true);
Samuel Ortizb23aa672009-07-01 21:26:54 +02004773}
4774
Johannes Berg463d0182009-07-14 00:33:35 +02004775static int nl80211_wiphy_netns(struct sk_buff *skb, struct genl_info *info)
4776{
Johannes Berg4c476992010-10-04 21:36:35 +02004777 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Johannes Berg463d0182009-07-14 00:33:35 +02004778 struct net *net;
4779 int err;
4780 u32 pid;
4781
4782 if (!info->attrs[NL80211_ATTR_PID])
4783 return -EINVAL;
4784
4785 pid = nla_get_u32(info->attrs[NL80211_ATTR_PID]);
4786
Johannes Berg463d0182009-07-14 00:33:35 +02004787 net = get_net_ns_by_pid(pid);
Johannes Berg4c476992010-10-04 21:36:35 +02004788 if (IS_ERR(net))
4789 return PTR_ERR(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004790
4791 err = 0;
4792
4793 /* check if anything to do */
Johannes Berg4c476992010-10-04 21:36:35 +02004794 if (!net_eq(wiphy_net(&rdev->wiphy), net))
4795 err = cfg80211_switch_netns(rdev, net);
Johannes Berg463d0182009-07-14 00:33:35 +02004796
Johannes Berg463d0182009-07-14 00:33:35 +02004797 put_net(net);
Johannes Berg463d0182009-07-14 00:33:35 +02004798 return err;
4799}
4800
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004801static int nl80211_setdel_pmksa(struct sk_buff *skb, struct genl_info *info)
4802{
Johannes Berg4c476992010-10-04 21:36:35 +02004803 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004804 int (*rdev_ops)(struct wiphy *wiphy, struct net_device *dev,
4805 struct cfg80211_pmksa *pmksa) = NULL;
Johannes Berg4c476992010-10-04 21:36:35 +02004806 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004807 struct cfg80211_pmksa pmksa;
4808
4809 memset(&pmksa, 0, sizeof(struct cfg80211_pmksa));
4810
4811 if (!info->attrs[NL80211_ATTR_MAC])
4812 return -EINVAL;
4813
4814 if (!info->attrs[NL80211_ATTR_PMKID])
4815 return -EINVAL;
4816
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004817 pmksa.pmkid = nla_data(info->attrs[NL80211_ATTR_PMKID]);
4818 pmksa.bssid = nla_data(info->attrs[NL80211_ATTR_MAC]);
4819
Johannes Berg074ac8d2010-09-16 14:58:22 +02004820 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004821 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4822 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004823
4824 switch (info->genlhdr->cmd) {
4825 case NL80211_CMD_SET_PMKSA:
4826 rdev_ops = rdev->ops->set_pmksa;
4827 break;
4828 case NL80211_CMD_DEL_PMKSA:
4829 rdev_ops = rdev->ops->del_pmksa;
4830 break;
4831 default:
4832 WARN_ON(1);
4833 break;
4834 }
4835
Johannes Berg4c476992010-10-04 21:36:35 +02004836 if (!rdev_ops)
4837 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004838
Johannes Berg4c476992010-10-04 21:36:35 +02004839 return rdev_ops(&rdev->wiphy, dev, &pmksa);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004840}
4841
4842static int nl80211_flush_pmksa(struct sk_buff *skb, struct genl_info *info)
4843{
Johannes Berg4c476992010-10-04 21:36:35 +02004844 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4845 struct net_device *dev = info->user_ptr[1];
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004846
Johannes Berg074ac8d2010-09-16 14:58:22 +02004847 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02004848 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT)
4849 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004850
Johannes Berg4c476992010-10-04 21:36:35 +02004851 if (!rdev->ops->flush_pmksa)
4852 return -EOPNOTSUPP;
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004853
Johannes Berg4c476992010-10-04 21:36:35 +02004854 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
Samuel Ortiz67fbb162009-11-24 23:59:15 +01004855}
4856
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004857static int nl80211_remain_on_channel(struct sk_buff *skb,
4858 struct genl_info *info)
4859{
Johannes Berg4c476992010-10-04 21:36:35 +02004860 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4861 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004862 struct ieee80211_channel *chan;
4863 struct sk_buff *msg;
4864 void *hdr;
4865 u64 cookie;
4866 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
4867 u32 freq, duration;
4868 int err;
4869
4870 if (!info->attrs[NL80211_ATTR_WIPHY_FREQ] ||
4871 !info->attrs[NL80211_ATTR_DURATION])
4872 return -EINVAL;
4873
4874 duration = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
4875
4876 /*
4877 * We should be on that channel for at least one jiffie,
4878 * and more than 5 seconds seems excessive.
4879 */
Johannes Berga2939112010-12-14 17:54:28 +01004880 if (!duration || !msecs_to_jiffies(duration) ||
4881 duration > rdev->wiphy.max_remain_on_channel_duration)
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004882 return -EINVAL;
4883
Johannes Berg4c476992010-10-04 21:36:35 +02004884 if (!rdev->ops->remain_on_channel)
4885 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004886
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004887 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
4888 channel_type = nla_get_u32(
4889 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
4890 if (channel_type != NL80211_CHAN_NO_HT &&
4891 channel_type != NL80211_CHAN_HT20 &&
4892 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02004893 channel_type != NL80211_CHAN_HT40MINUS)
4894 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004895 }
4896
4897 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
4898 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02004899 if (chan == NULL)
4900 return -EINVAL;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004901
4902 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02004903 if (!msg)
4904 return -ENOMEM;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004905
4906 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
4907 NL80211_CMD_REMAIN_ON_CHANNEL);
4908
4909 if (IS_ERR(hdr)) {
4910 err = PTR_ERR(hdr);
4911 goto free_msg;
4912 }
4913
4914 err = rdev->ops->remain_on_channel(&rdev->wiphy, dev, chan,
4915 channel_type, duration, &cookie);
4916
4917 if (err)
4918 goto free_msg;
4919
4920 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
4921
4922 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02004923
4924 return genlmsg_reply(msg, info);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004925
4926 nla_put_failure:
4927 err = -ENOBUFS;
4928 free_msg:
4929 nlmsg_free(msg);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004930 return err;
4931}
4932
4933static int nl80211_cancel_remain_on_channel(struct sk_buff *skb,
4934 struct genl_info *info)
4935{
Johannes Berg4c476992010-10-04 21:36:35 +02004936 struct cfg80211_registered_device *rdev = info->user_ptr[0];
4937 struct net_device *dev = info->user_ptr[1];
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004938 u64 cookie;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004939
4940 if (!info->attrs[NL80211_ATTR_COOKIE])
4941 return -EINVAL;
4942
Johannes Berg4c476992010-10-04 21:36:35 +02004943 if (!rdev->ops->cancel_remain_on_channel)
4944 return -EOPNOTSUPP;
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004945
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004946 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
4947
Johannes Berg4c476992010-10-04 21:36:35 +02004948 return rdev->ops->cancel_remain_on_channel(&rdev->wiphy, dev, cookie);
Jouni Malinen9588bbd2009-12-23 13:15:41 +01004949}
4950
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004951static u32 rateset_to_mask(struct ieee80211_supported_band *sband,
4952 u8 *rates, u8 rates_len)
4953{
4954 u8 i;
4955 u32 mask = 0;
4956
4957 for (i = 0; i < rates_len; i++) {
4958 int rate = (rates[i] & 0x7f) * 5;
4959 int ridx;
4960 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
4961 struct ieee80211_rate *srate =
4962 &sband->bitrates[ridx];
4963 if (rate == srate->bitrate) {
4964 mask |= 1 << ridx;
4965 break;
4966 }
4967 }
4968 if (ridx == sband->n_bitrates)
4969 return 0; /* rate not found */
4970 }
4971
4972 return mask;
4973}
4974
Alexey Dobriyanb54452b2010-02-18 08:14:31 +00004975static const struct nla_policy nl80211_txattr_policy[NL80211_TXRATE_MAX + 1] = {
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004976 [NL80211_TXRATE_LEGACY] = { .type = NLA_BINARY,
4977 .len = NL80211_MAX_SUPP_RATES },
4978};
4979
4980static int nl80211_set_tx_bitrate_mask(struct sk_buff *skb,
4981 struct genl_info *info)
4982{
4983 struct nlattr *tb[NL80211_TXRATE_MAX + 1];
Johannes Berg4c476992010-10-04 21:36:35 +02004984 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004985 struct cfg80211_bitrate_mask mask;
Johannes Berg4c476992010-10-04 21:36:35 +02004986 int rem, i;
4987 struct net_device *dev = info->user_ptr[1];
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004988 struct nlattr *tx_rates;
4989 struct ieee80211_supported_band *sband;
4990
4991 if (info->attrs[NL80211_ATTR_TX_RATES] == NULL)
4992 return -EINVAL;
4993
Johannes Berg4c476992010-10-04 21:36:35 +02004994 if (!rdev->ops->set_bitrate_mask)
4995 return -EOPNOTSUPP;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02004996
4997 memset(&mask, 0, sizeof(mask));
4998 /* Default to all rates enabled */
4999 for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
5000 sband = rdev->wiphy.bands[i];
5001 mask.control[i].legacy =
5002 sband ? (1 << sband->n_bitrates) - 1 : 0;
5003 }
5004
5005 /*
5006 * The nested attribute uses enum nl80211_band as the index. This maps
5007 * directly to the enum ieee80211_band values used in cfg80211.
5008 */
5009 nla_for_each_nested(tx_rates, info->attrs[NL80211_ATTR_TX_RATES], rem)
5010 {
5011 enum ieee80211_band band = nla_type(tx_rates);
Johannes Berg4c476992010-10-04 21:36:35 +02005012 if (band < 0 || band >= IEEE80211_NUM_BANDS)
5013 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005014 sband = rdev->wiphy.bands[band];
Johannes Berg4c476992010-10-04 21:36:35 +02005015 if (sband == NULL)
5016 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005017 nla_parse(tb, NL80211_TXRATE_MAX, nla_data(tx_rates),
5018 nla_len(tx_rates), nl80211_txattr_policy);
5019 if (tb[NL80211_TXRATE_LEGACY]) {
5020 mask.control[band].legacy = rateset_to_mask(
5021 sband,
5022 nla_data(tb[NL80211_TXRATE_LEGACY]),
5023 nla_len(tb[NL80211_TXRATE_LEGACY]));
Johannes Berg4c476992010-10-04 21:36:35 +02005024 if (mask.control[band].legacy == 0)
5025 return -EINVAL;
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005026 }
5027 }
5028
Johannes Berg4c476992010-10-04 21:36:35 +02005029 return rdev->ops->set_bitrate_mask(&rdev->wiphy, dev, NULL, &mask);
Jouni Malinen13ae75b2009-12-29 12:59:45 +02005030}
5031
Johannes Berg2e161f72010-08-12 15:38:38 +02005032static int nl80211_register_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005033{
Johannes Berg4c476992010-10-04 21:36:35 +02005034 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5035 struct net_device *dev = info->user_ptr[1];
Johannes Berg2e161f72010-08-12 15:38:38 +02005036 u16 frame_type = IEEE80211_FTYPE_MGMT | IEEE80211_STYPE_ACTION;
Jouni Malinen026331c2010-02-15 12:53:10 +02005037
5038 if (!info->attrs[NL80211_ATTR_FRAME_MATCH])
5039 return -EINVAL;
5040
Johannes Berg2e161f72010-08-12 15:38:38 +02005041 if (info->attrs[NL80211_ATTR_FRAME_TYPE])
5042 frame_type = nla_get_u16(info->attrs[NL80211_ATTR_FRAME_TYPE]);
Jouni Malinen026331c2010-02-15 12:53:10 +02005043
Johannes Berg9d38d852010-06-09 17:20:33 +02005044 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005045 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005046 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5047 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5048 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005049 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005050 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5051 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005052
5053 /* not much point in registering if we can't reply */
Johannes Berg4c476992010-10-04 21:36:35 +02005054 if (!rdev->ops->mgmt_tx)
5055 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005056
Johannes Berg4c476992010-10-04 21:36:35 +02005057 return cfg80211_mlme_register_mgmt(dev->ieee80211_ptr, info->snd_pid,
Johannes Berg2e161f72010-08-12 15:38:38 +02005058 frame_type,
Jouni Malinen026331c2010-02-15 12:53:10 +02005059 nla_data(info->attrs[NL80211_ATTR_FRAME_MATCH]),
5060 nla_len(info->attrs[NL80211_ATTR_FRAME_MATCH]));
Jouni Malinen026331c2010-02-15 12:53:10 +02005061}
5062
Johannes Berg2e161f72010-08-12 15:38:38 +02005063static int nl80211_tx_mgmt(struct sk_buff *skb, struct genl_info *info)
Jouni Malinen026331c2010-02-15 12:53:10 +02005064{
Johannes Berg4c476992010-10-04 21:36:35 +02005065 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5066 struct net_device *dev = info->user_ptr[1];
Jouni Malinen026331c2010-02-15 12:53:10 +02005067 struct ieee80211_channel *chan;
5068 enum nl80211_channel_type channel_type = NL80211_CHAN_NO_HT;
Johannes Berg252aa632010-05-19 12:17:12 +02005069 bool channel_type_valid = false;
Jouni Malinen026331c2010-02-15 12:53:10 +02005070 u32 freq;
5071 int err;
5072 void *hdr;
5073 u64 cookie;
5074 struct sk_buff *msg;
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005075 unsigned int wait = 0;
5076 bool offchan;
Jouni Malinen026331c2010-02-15 12:53:10 +02005077
5078 if (!info->attrs[NL80211_ATTR_FRAME] ||
5079 !info->attrs[NL80211_ATTR_WIPHY_FREQ])
5080 return -EINVAL;
5081
Johannes Berg4c476992010-10-04 21:36:35 +02005082 if (!rdev->ops->mgmt_tx)
5083 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005084
Johannes Berg9d38d852010-06-09 17:20:33 +02005085 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg074ac8d2010-09-16 14:58:22 +02005086 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
Johannes Berg663fcaf2010-09-30 21:06:09 +02005087 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5088 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5089 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
Javier Cardonac7108a72010-12-16 17:37:50 -08005090 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT &&
Johannes Berg4c476992010-10-04 21:36:35 +02005091 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5092 return -EOPNOTSUPP;
Jouni Malinen026331c2010-02-15 12:53:10 +02005093
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005094 if (info->attrs[NL80211_ATTR_DURATION]) {
5095 if (!rdev->ops->mgmt_tx_cancel_wait)
5096 return -EINVAL;
5097 wait = nla_get_u32(info->attrs[NL80211_ATTR_DURATION]);
5098 }
5099
Jouni Malinen026331c2010-02-15 12:53:10 +02005100 if (info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]) {
5101 channel_type = nla_get_u32(
5102 info->attrs[NL80211_ATTR_WIPHY_CHANNEL_TYPE]);
5103 if (channel_type != NL80211_CHAN_NO_HT &&
5104 channel_type != NL80211_CHAN_HT20 &&
5105 channel_type != NL80211_CHAN_HT40PLUS &&
Johannes Berg4c476992010-10-04 21:36:35 +02005106 channel_type != NL80211_CHAN_HT40MINUS)
5107 return -EINVAL;
Johannes Berg252aa632010-05-19 12:17:12 +02005108 channel_type_valid = true;
Jouni Malinen026331c2010-02-15 12:53:10 +02005109 }
5110
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005111 offchan = info->attrs[NL80211_ATTR_OFFCHANNEL_TX_OK];
5112
Jouni Malinen026331c2010-02-15 12:53:10 +02005113 freq = nla_get_u32(info->attrs[NL80211_ATTR_WIPHY_FREQ]);
5114 chan = rdev_freq_to_chan(rdev, freq, channel_type);
Johannes Berg4c476992010-10-04 21:36:35 +02005115 if (chan == NULL)
5116 return -EINVAL;
Jouni Malinen026331c2010-02-15 12:53:10 +02005117
5118 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005119 if (!msg)
5120 return -ENOMEM;
Jouni Malinen026331c2010-02-15 12:53:10 +02005121
5122 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
Johannes Berg2e161f72010-08-12 15:38:38 +02005123 NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02005124
5125 if (IS_ERR(hdr)) {
5126 err = PTR_ERR(hdr);
5127 goto free_msg;
5128 }
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005129 err = cfg80211_mlme_mgmt_tx(rdev, dev, chan, offchan, channel_type,
5130 channel_type_valid, wait,
Johannes Berg2e161f72010-08-12 15:38:38 +02005131 nla_data(info->attrs[NL80211_ATTR_FRAME]),
5132 nla_len(info->attrs[NL80211_ATTR_FRAME]),
5133 &cookie);
Jouni Malinen026331c2010-02-15 12:53:10 +02005134 if (err)
5135 goto free_msg;
5136
5137 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
5138
5139 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005140 return genlmsg_reply(msg, info);
Jouni Malinen026331c2010-02-15 12:53:10 +02005141
5142 nla_put_failure:
5143 err = -ENOBUFS;
5144 free_msg:
5145 nlmsg_free(msg);
Jouni Malinen026331c2010-02-15 12:53:10 +02005146 return err;
5147}
5148
Johannes Bergf7ca38d2010-11-25 10:02:29 +01005149static int nl80211_tx_mgmt_cancel_wait(struct sk_buff *skb, struct genl_info *info)
5150{
5151 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5152 struct net_device *dev = info->user_ptr[1];
5153 u64 cookie;
5154
5155 if (!info->attrs[NL80211_ATTR_COOKIE])
5156 return -EINVAL;
5157
5158 if (!rdev->ops->mgmt_tx_cancel_wait)
5159 return -EOPNOTSUPP;
5160
5161 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION &&
5162 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC &&
5163 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_CLIENT &&
5164 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP &&
5165 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP_VLAN &&
5166 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO)
5167 return -EOPNOTSUPP;
5168
5169 cookie = nla_get_u64(info->attrs[NL80211_ATTR_COOKIE]);
5170
5171 return rdev->ops->mgmt_tx_cancel_wait(&rdev->wiphy, dev, cookie);
5172}
5173
Kalle Valoffb9eb32010-02-17 17:58:10 +02005174static int nl80211_set_power_save(struct sk_buff *skb, struct genl_info *info)
5175{
Johannes Berg4c476992010-10-04 21:36:35 +02005176 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005177 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005178 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005179 u8 ps_state;
5180 bool state;
5181 int err;
5182
Johannes Berg4c476992010-10-04 21:36:35 +02005183 if (!info->attrs[NL80211_ATTR_PS_STATE])
5184 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005185
5186 ps_state = nla_get_u32(info->attrs[NL80211_ATTR_PS_STATE]);
5187
Johannes Berg4c476992010-10-04 21:36:35 +02005188 if (ps_state != NL80211_PS_DISABLED && ps_state != NL80211_PS_ENABLED)
5189 return -EINVAL;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005190
5191 wdev = dev->ieee80211_ptr;
5192
Johannes Berg4c476992010-10-04 21:36:35 +02005193 if (!rdev->ops->set_power_mgmt)
5194 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005195
5196 state = (ps_state == NL80211_PS_ENABLED) ? true : false;
5197
5198 if (state == wdev->ps)
Johannes Berg4c476992010-10-04 21:36:35 +02005199 return 0;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005200
Johannes Berg4c476992010-10-04 21:36:35 +02005201 err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, state,
5202 wdev->ps_timeout);
5203 if (!err)
5204 wdev->ps = state;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005205 return err;
5206}
5207
5208static int nl80211_get_power_save(struct sk_buff *skb, struct genl_info *info)
5209{
Johannes Berg4c476992010-10-04 21:36:35 +02005210 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005211 enum nl80211_ps_state ps_state;
5212 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005213 struct net_device *dev = info->user_ptr[1];
Kalle Valoffb9eb32010-02-17 17:58:10 +02005214 struct sk_buff *msg;
5215 void *hdr;
5216 int err;
5217
Kalle Valoffb9eb32010-02-17 17:58:10 +02005218 wdev = dev->ieee80211_ptr;
5219
Johannes Berg4c476992010-10-04 21:36:35 +02005220 if (!rdev->ops->set_power_mgmt)
5221 return -EOPNOTSUPP;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005222
5223 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg4c476992010-10-04 21:36:35 +02005224 if (!msg)
5225 return -ENOMEM;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005226
5227 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5228 NL80211_CMD_GET_POWER_SAVE);
5229 if (!hdr) {
Johannes Berg4c476992010-10-04 21:36:35 +02005230 err = -ENOBUFS;
Kalle Valoffb9eb32010-02-17 17:58:10 +02005231 goto free_msg;
5232 }
5233
5234 if (wdev->ps)
5235 ps_state = NL80211_PS_ENABLED;
5236 else
5237 ps_state = NL80211_PS_DISABLED;
5238
5239 NLA_PUT_U32(msg, NL80211_ATTR_PS_STATE, ps_state);
5240
5241 genlmsg_end(msg, hdr);
Johannes Berg4c476992010-10-04 21:36:35 +02005242 return genlmsg_reply(msg, info);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005243
Johannes Berg4c476992010-10-04 21:36:35 +02005244 nla_put_failure:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005245 err = -ENOBUFS;
Johannes Berg4c476992010-10-04 21:36:35 +02005246 free_msg:
Kalle Valoffb9eb32010-02-17 17:58:10 +02005247 nlmsg_free(msg);
Kalle Valoffb9eb32010-02-17 17:58:10 +02005248 return err;
5249}
5250
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005251static struct nla_policy
5252nl80211_attr_cqm_policy[NL80211_ATTR_CQM_MAX + 1] __read_mostly = {
5253 [NL80211_ATTR_CQM_RSSI_THOLD] = { .type = NLA_U32 },
5254 [NL80211_ATTR_CQM_RSSI_HYST] = { .type = NLA_U32 },
5255 [NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT] = { .type = NLA_U32 },
5256};
5257
5258static int nl80211_set_cqm_rssi(struct genl_info *info,
5259 s32 threshold, u32 hysteresis)
5260{
Johannes Berg4c476992010-10-04 21:36:35 +02005261 struct cfg80211_registered_device *rdev = info->user_ptr[0];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005262 struct wireless_dev *wdev;
Johannes Berg4c476992010-10-04 21:36:35 +02005263 struct net_device *dev = info->user_ptr[1];
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005264
5265 if (threshold > 0)
5266 return -EINVAL;
5267
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005268 wdev = dev->ieee80211_ptr;
5269
Johannes Berg4c476992010-10-04 21:36:35 +02005270 if (!rdev->ops->set_cqm_rssi_config)
5271 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005272
Johannes Berg074ac8d2010-09-16 14:58:22 +02005273 if (wdev->iftype != NL80211_IFTYPE_STATION &&
Johannes Berg4c476992010-10-04 21:36:35 +02005274 wdev->iftype != NL80211_IFTYPE_P2P_CLIENT)
5275 return -EOPNOTSUPP;
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005276
Johannes Berg4c476992010-10-04 21:36:35 +02005277 return rdev->ops->set_cqm_rssi_config(wdev->wiphy, dev,
5278 threshold, hysteresis);
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02005279}
5280
5281static int nl80211_set_cqm(struct sk_buff *skb, struct genl_info *info)
5282{
5283 struct nlattr *attrs[NL80211_ATTR_CQM_MAX + 1];
5284 struct nlattr *cqm;
5285 int err;
5286
5287 cqm = info->attrs[NL80211_ATTR_CQM];
5288 if (!cqm) {
5289 err = -EINVAL;
5290 goto out;
5291 }
5292
5293 err = nla_parse_nested(attrs, NL80211_ATTR_CQM_MAX, cqm,
5294 nl80211_attr_cqm_policy);
5295 if (err)
5296 goto out;
5297
5298 if (attrs[NL80211_ATTR_CQM_RSSI_THOLD] &&
5299 attrs[NL80211_ATTR_CQM_RSSI_HYST]) {
5300 s32 threshold;
5301 u32 hysteresis;
5302 threshold = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_THOLD]);
5303 hysteresis = nla_get_u32(attrs[NL80211_ATTR_CQM_RSSI_HYST]);
5304 err = nl80211_set_cqm_rssi(info, threshold, hysteresis);
5305 } else
5306 err = -EINVAL;
5307
5308out:
5309 return err;
5310}
5311
Johannes Berg29cbe682010-12-03 09:20:44 +01005312static int nl80211_join_mesh(struct sk_buff *skb, struct genl_info *info)
5313{
5314 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5315 struct net_device *dev = info->user_ptr[1];
5316 struct mesh_config cfg;
Javier Cardonac80d5452010-12-16 17:37:49 -08005317 struct mesh_setup setup;
Johannes Berg29cbe682010-12-03 09:20:44 +01005318 int err;
5319
5320 /* start with default */
5321 memcpy(&cfg, &default_mesh_config, sizeof(cfg));
Javier Cardonac80d5452010-12-16 17:37:49 -08005322 memcpy(&setup, &default_mesh_setup, sizeof(setup));
Johannes Berg29cbe682010-12-03 09:20:44 +01005323
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005324 if (info->attrs[NL80211_ATTR_MESH_CONFIG]) {
Johannes Berg29cbe682010-12-03 09:20:44 +01005325 /* and parse parameters if given */
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005326 err = nl80211_parse_mesh_config(info, &cfg, NULL);
Johannes Berg29cbe682010-12-03 09:20:44 +01005327 if (err)
5328 return err;
5329 }
5330
5331 if (!info->attrs[NL80211_ATTR_MESH_ID] ||
5332 !nla_len(info->attrs[NL80211_ATTR_MESH_ID]))
5333 return -EINVAL;
5334
Javier Cardonac80d5452010-12-16 17:37:49 -08005335 setup.mesh_id = nla_data(info->attrs[NL80211_ATTR_MESH_ID]);
5336 setup.mesh_id_len = nla_len(info->attrs[NL80211_ATTR_MESH_ID]);
5337
5338 if (info->attrs[NL80211_ATTR_MESH_SETUP]) {
5339 /* parse additional setup parameters if given */
5340 err = nl80211_parse_mesh_setup(info, &setup);
5341 if (err)
5342 return err;
5343 }
5344
5345 return cfg80211_join_mesh(rdev, dev, &setup, &cfg);
Johannes Berg29cbe682010-12-03 09:20:44 +01005346}
5347
5348static int nl80211_leave_mesh(struct sk_buff *skb, struct genl_info *info)
5349{
5350 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5351 struct net_device *dev = info->user_ptr[1];
5352
5353 return cfg80211_leave_mesh(rdev, dev);
5354}
5355
Johannes Bergff1b6e62011-05-04 15:37:28 +02005356static int nl80211_get_wowlan(struct sk_buff *skb, struct genl_info *info)
5357{
5358 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5359 struct sk_buff *msg;
5360 void *hdr;
5361
5362 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5363 return -EOPNOTSUPP;
5364
5365 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
5366 if (!msg)
5367 return -ENOMEM;
5368
5369 hdr = nl80211hdr_put(msg, info->snd_pid, info->snd_seq, 0,
5370 NL80211_CMD_GET_WOWLAN);
5371 if (!hdr)
5372 goto nla_put_failure;
5373
5374 if (rdev->wowlan) {
5375 struct nlattr *nl_wowlan;
5376
5377 nl_wowlan = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
5378 if (!nl_wowlan)
5379 goto nla_put_failure;
5380
5381 if (rdev->wowlan->any)
5382 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_ANY);
5383 if (rdev->wowlan->disconnect)
5384 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_DISCONNECT);
5385 if (rdev->wowlan->magic_pkt)
5386 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_MAGIC_PKT);
Johannes Berg77dbbb12011-07-13 10:48:55 +02005387 if (rdev->wowlan->gtk_rekey_failure)
5388 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE);
5389 if (rdev->wowlan->eap_identity_req)
5390 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST);
5391 if (rdev->wowlan->four_way_handshake)
5392 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE);
5393 if (rdev->wowlan->rfkill_release)
5394 NLA_PUT_FLAG(msg, NL80211_WOWLAN_TRIG_RFKILL_RELEASE);
Johannes Bergff1b6e62011-05-04 15:37:28 +02005395 if (rdev->wowlan->n_patterns) {
5396 struct nlattr *nl_pats, *nl_pat;
5397 int i, pat_len;
5398
5399 nl_pats = nla_nest_start(msg,
5400 NL80211_WOWLAN_TRIG_PKT_PATTERN);
5401 if (!nl_pats)
5402 goto nla_put_failure;
5403
5404 for (i = 0; i < rdev->wowlan->n_patterns; i++) {
5405 nl_pat = nla_nest_start(msg, i + 1);
5406 if (!nl_pat)
5407 goto nla_put_failure;
5408 pat_len = rdev->wowlan->patterns[i].pattern_len;
5409 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_MASK,
5410 DIV_ROUND_UP(pat_len, 8),
5411 rdev->wowlan->patterns[i].mask);
5412 NLA_PUT(msg, NL80211_WOWLAN_PKTPAT_PATTERN,
5413 pat_len,
5414 rdev->wowlan->patterns[i].pattern);
5415 nla_nest_end(msg, nl_pat);
5416 }
5417 nla_nest_end(msg, nl_pats);
5418 }
5419
5420 nla_nest_end(msg, nl_wowlan);
5421 }
5422
5423 genlmsg_end(msg, hdr);
5424 return genlmsg_reply(msg, info);
5425
5426nla_put_failure:
5427 nlmsg_free(msg);
5428 return -ENOBUFS;
5429}
5430
5431static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
5432{
5433 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5434 struct nlattr *tb[NUM_NL80211_WOWLAN_TRIG];
5435 struct cfg80211_wowlan no_triggers = {};
5436 struct cfg80211_wowlan new_triggers = {};
5437 struct wiphy_wowlan_support *wowlan = &rdev->wiphy.wowlan;
5438 int err, i;
5439
5440 if (!rdev->wiphy.wowlan.flags && !rdev->wiphy.wowlan.n_patterns)
5441 return -EOPNOTSUPP;
5442
5443 if (!info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS])
5444 goto no_triggers;
5445
5446 err = nla_parse(tb, MAX_NL80211_WOWLAN_TRIG,
5447 nla_data(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5448 nla_len(info->attrs[NL80211_ATTR_WOWLAN_TRIGGERS]),
5449 nl80211_wowlan_policy);
5450 if (err)
5451 return err;
5452
5453 if (tb[NL80211_WOWLAN_TRIG_ANY]) {
5454 if (!(wowlan->flags & WIPHY_WOWLAN_ANY))
5455 return -EINVAL;
5456 new_triggers.any = true;
5457 }
5458
5459 if (tb[NL80211_WOWLAN_TRIG_DISCONNECT]) {
5460 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
5461 return -EINVAL;
5462 new_triggers.disconnect = true;
5463 }
5464
5465 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
5466 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
5467 return -EINVAL;
5468 new_triggers.magic_pkt = true;
5469 }
5470
Johannes Berg77dbbb12011-07-13 10:48:55 +02005471 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
5472 return -EINVAL;
5473
5474 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_FAILURE]) {
5475 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
5476 return -EINVAL;
5477 new_triggers.gtk_rekey_failure = true;
5478 }
5479
5480 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
5481 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
5482 return -EINVAL;
5483 new_triggers.eap_identity_req = true;
5484 }
5485
5486 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
5487 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
5488 return -EINVAL;
5489 new_triggers.four_way_handshake = true;
5490 }
5491
5492 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
5493 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
5494 return -EINVAL;
5495 new_triggers.rfkill_release = true;
5496 }
5497
Johannes Bergff1b6e62011-05-04 15:37:28 +02005498 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
5499 struct nlattr *pat;
5500 int n_patterns = 0;
5501 int rem, pat_len, mask_len;
5502 struct nlattr *pat_tb[NUM_NL80211_WOWLAN_PKTPAT];
5503
5504 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5505 rem)
5506 n_patterns++;
5507 if (n_patterns > wowlan->n_patterns)
5508 return -EINVAL;
5509
5510 new_triggers.patterns = kcalloc(n_patterns,
5511 sizeof(new_triggers.patterns[0]),
5512 GFP_KERNEL);
5513 if (!new_triggers.patterns)
5514 return -ENOMEM;
5515
5516 new_triggers.n_patterns = n_patterns;
5517 i = 0;
5518
5519 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
5520 rem) {
5521 nla_parse(pat_tb, MAX_NL80211_WOWLAN_PKTPAT,
5522 nla_data(pat), nla_len(pat), NULL);
5523 err = -EINVAL;
5524 if (!pat_tb[NL80211_WOWLAN_PKTPAT_MASK] ||
5525 !pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN])
5526 goto error;
5527 pat_len = nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]);
5528 mask_len = DIV_ROUND_UP(pat_len, 8);
5529 if (nla_len(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]) !=
5530 mask_len)
5531 goto error;
5532 if (pat_len > wowlan->pattern_max_len ||
5533 pat_len < wowlan->pattern_min_len)
5534 goto error;
5535
5536 new_triggers.patterns[i].mask =
5537 kmalloc(mask_len + pat_len, GFP_KERNEL);
5538 if (!new_triggers.patterns[i].mask) {
5539 err = -ENOMEM;
5540 goto error;
5541 }
5542 new_triggers.patterns[i].pattern =
5543 new_triggers.patterns[i].mask + mask_len;
5544 memcpy(new_triggers.patterns[i].mask,
5545 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_MASK]),
5546 mask_len);
5547 new_triggers.patterns[i].pattern_len = pat_len;
5548 memcpy(new_triggers.patterns[i].pattern,
5549 nla_data(pat_tb[NL80211_WOWLAN_PKTPAT_PATTERN]),
5550 pat_len);
5551 i++;
5552 }
5553 }
5554
5555 if (memcmp(&new_triggers, &no_triggers, sizeof(new_triggers))) {
5556 struct cfg80211_wowlan *ntrig;
5557 ntrig = kmemdup(&new_triggers, sizeof(new_triggers),
5558 GFP_KERNEL);
5559 if (!ntrig) {
5560 err = -ENOMEM;
5561 goto error;
5562 }
5563 cfg80211_rdev_free_wowlan(rdev);
5564 rdev->wowlan = ntrig;
5565 } else {
5566 no_triggers:
5567 cfg80211_rdev_free_wowlan(rdev);
5568 rdev->wowlan = NULL;
5569 }
5570
5571 return 0;
5572 error:
5573 for (i = 0; i < new_triggers.n_patterns; i++)
5574 kfree(new_triggers.patterns[i].mask);
5575 kfree(new_triggers.patterns);
5576 return err;
5577}
5578
Johannes Berge5497d72011-07-05 16:35:40 +02005579static int nl80211_set_rekey_data(struct sk_buff *skb, struct genl_info *info)
5580{
5581 struct cfg80211_registered_device *rdev = info->user_ptr[0];
5582 struct net_device *dev = info->user_ptr[1];
5583 struct wireless_dev *wdev = dev->ieee80211_ptr;
5584 struct nlattr *tb[NUM_NL80211_REKEY_DATA];
5585 struct cfg80211_gtk_rekey_data rekey_data;
5586 int err;
5587
5588 if (!info->attrs[NL80211_ATTR_REKEY_DATA])
5589 return -EINVAL;
5590
5591 err = nla_parse(tb, MAX_NL80211_REKEY_DATA,
5592 nla_data(info->attrs[NL80211_ATTR_REKEY_DATA]),
5593 nla_len(info->attrs[NL80211_ATTR_REKEY_DATA]),
5594 nl80211_rekey_policy);
5595 if (err)
5596 return err;
5597
5598 if (nla_len(tb[NL80211_REKEY_DATA_REPLAY_CTR]) != NL80211_REPLAY_CTR_LEN)
5599 return -ERANGE;
5600 if (nla_len(tb[NL80211_REKEY_DATA_KEK]) != NL80211_KEK_LEN)
5601 return -ERANGE;
5602 if (nla_len(tb[NL80211_REKEY_DATA_KCK]) != NL80211_KCK_LEN)
5603 return -ERANGE;
5604
5605 memcpy(rekey_data.kek, nla_data(tb[NL80211_REKEY_DATA_KEK]),
5606 NL80211_KEK_LEN);
5607 memcpy(rekey_data.kck, nla_data(tb[NL80211_REKEY_DATA_KCK]),
5608 NL80211_KCK_LEN);
5609 memcpy(rekey_data.replay_ctr,
5610 nla_data(tb[NL80211_REKEY_DATA_REPLAY_CTR]),
5611 NL80211_REPLAY_CTR_LEN);
5612
5613 wdev_lock(wdev);
5614 if (!wdev->current_bss) {
5615 err = -ENOTCONN;
5616 goto out;
5617 }
5618
5619 if (!rdev->ops->set_rekey_data) {
5620 err = -EOPNOTSUPP;
5621 goto out;
5622 }
5623
5624 err = rdev->ops->set_rekey_data(&rdev->wiphy, dev, &rekey_data);
5625 out:
5626 wdev_unlock(wdev);
5627 return err;
5628}
5629
Johannes Berg4c476992010-10-04 21:36:35 +02005630#define NL80211_FLAG_NEED_WIPHY 0x01
5631#define NL80211_FLAG_NEED_NETDEV 0x02
5632#define NL80211_FLAG_NEED_RTNL 0x04
Johannes Berg41265712010-10-04 21:14:05 +02005633#define NL80211_FLAG_CHECK_NETDEV_UP 0x08
5634#define NL80211_FLAG_NEED_NETDEV_UP (NL80211_FLAG_NEED_NETDEV |\
5635 NL80211_FLAG_CHECK_NETDEV_UP)
Johannes Berg4c476992010-10-04 21:36:35 +02005636
5637static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
5638 struct genl_info *info)
5639{
5640 struct cfg80211_registered_device *rdev;
5641 struct net_device *dev;
5642 int err;
5643 bool rtnl = ops->internal_flags & NL80211_FLAG_NEED_RTNL;
5644
5645 if (rtnl)
5646 rtnl_lock();
5647
5648 if (ops->internal_flags & NL80211_FLAG_NEED_WIPHY) {
5649 rdev = cfg80211_get_dev_from_info(info);
5650 if (IS_ERR(rdev)) {
5651 if (rtnl)
5652 rtnl_unlock();
5653 return PTR_ERR(rdev);
5654 }
5655 info->user_ptr[0] = rdev;
5656 } else if (ops->internal_flags & NL80211_FLAG_NEED_NETDEV) {
5657 err = get_rdev_dev_by_info_ifindex(info, &rdev, &dev);
5658 if (err) {
5659 if (rtnl)
5660 rtnl_unlock();
5661 return err;
5662 }
Johannes Berg41265712010-10-04 21:14:05 +02005663 if (ops->internal_flags & NL80211_FLAG_CHECK_NETDEV_UP &&
5664 !netif_running(dev)) {
Johannes Bergd537f5f2010-10-05 21:34:11 +02005665 cfg80211_unlock_rdev(rdev);
5666 dev_put(dev);
Johannes Berg41265712010-10-04 21:14:05 +02005667 if (rtnl)
5668 rtnl_unlock();
5669 return -ENETDOWN;
5670 }
Johannes Berg4c476992010-10-04 21:36:35 +02005671 info->user_ptr[0] = rdev;
5672 info->user_ptr[1] = dev;
5673 }
5674
5675 return 0;
5676}
5677
5678static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
5679 struct genl_info *info)
5680{
5681 if (info->user_ptr[0])
5682 cfg80211_unlock_rdev(info->user_ptr[0]);
5683 if (info->user_ptr[1])
5684 dev_put(info->user_ptr[1]);
5685 if (ops->internal_flags & NL80211_FLAG_NEED_RTNL)
5686 rtnl_unlock();
5687}
5688
Johannes Berg55682962007-09-20 13:09:35 -04005689static struct genl_ops nl80211_ops[] = {
5690 {
5691 .cmd = NL80211_CMD_GET_WIPHY,
5692 .doit = nl80211_get_wiphy,
5693 .dumpit = nl80211_dump_wiphy,
5694 .policy = nl80211_policy,
5695 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005696 .internal_flags = NL80211_FLAG_NEED_WIPHY,
Johannes Berg55682962007-09-20 13:09:35 -04005697 },
5698 {
5699 .cmd = NL80211_CMD_SET_WIPHY,
5700 .doit = nl80211_set_wiphy,
5701 .policy = nl80211_policy,
5702 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005703 .internal_flags = NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005704 },
5705 {
5706 .cmd = NL80211_CMD_GET_INTERFACE,
5707 .doit = nl80211_get_interface,
5708 .dumpit = nl80211_dump_interface,
5709 .policy = nl80211_policy,
5710 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005711 .internal_flags = NL80211_FLAG_NEED_NETDEV,
Johannes Berg55682962007-09-20 13:09:35 -04005712 },
5713 {
5714 .cmd = NL80211_CMD_SET_INTERFACE,
5715 .doit = nl80211_set_interface,
5716 .policy = nl80211_policy,
5717 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005718 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5719 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005720 },
5721 {
5722 .cmd = NL80211_CMD_NEW_INTERFACE,
5723 .doit = nl80211_new_interface,
5724 .policy = nl80211_policy,
5725 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005726 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5727 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005728 },
5729 {
5730 .cmd = NL80211_CMD_DEL_INTERFACE,
5731 .doit = nl80211_del_interface,
5732 .policy = nl80211_policy,
5733 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005734 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5735 NL80211_FLAG_NEED_RTNL,
Johannes Berg55682962007-09-20 13:09:35 -04005736 },
Johannes Berg41ade002007-12-19 02:03:29 +01005737 {
5738 .cmd = NL80211_CMD_GET_KEY,
5739 .doit = nl80211_get_key,
5740 .policy = nl80211_policy,
5741 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005742 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5743 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005744 },
5745 {
5746 .cmd = NL80211_CMD_SET_KEY,
5747 .doit = nl80211_set_key,
5748 .policy = nl80211_policy,
5749 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005750 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005751 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005752 },
5753 {
5754 .cmd = NL80211_CMD_NEW_KEY,
5755 .doit = nl80211_new_key,
5756 .policy = nl80211_policy,
5757 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005758 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005759 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005760 },
5761 {
5762 .cmd = NL80211_CMD_DEL_KEY,
5763 .doit = nl80211_del_key,
5764 .policy = nl80211_policy,
5765 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005766 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005767 NL80211_FLAG_NEED_RTNL,
Johannes Berg41ade002007-12-19 02:03:29 +01005768 },
Johannes Berged1b6cc2007-12-19 02:03:32 +01005769 {
5770 .cmd = NL80211_CMD_SET_BEACON,
5771 .policy = nl80211_policy,
5772 .flags = GENL_ADMIN_PERM,
5773 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005774 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5775 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005776 },
5777 {
5778 .cmd = NL80211_CMD_NEW_BEACON,
5779 .policy = nl80211_policy,
5780 .flags = GENL_ADMIN_PERM,
5781 .doit = nl80211_addset_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005782 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5783 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005784 },
5785 {
5786 .cmd = NL80211_CMD_DEL_BEACON,
5787 .policy = nl80211_policy,
5788 .flags = GENL_ADMIN_PERM,
5789 .doit = nl80211_del_beacon,
Johannes Berg4c476992010-10-04 21:36:35 +02005790 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5791 NL80211_FLAG_NEED_RTNL,
Johannes Berged1b6cc2007-12-19 02:03:32 +01005792 },
Johannes Berg5727ef12007-12-19 02:03:34 +01005793 {
5794 .cmd = NL80211_CMD_GET_STATION,
5795 .doit = nl80211_get_station,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005796 .dumpit = nl80211_dump_station,
Johannes Berg5727ef12007-12-19 02:03:34 +01005797 .policy = nl80211_policy,
Johannes Berg4c476992010-10-04 21:36:35 +02005798 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5799 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005800 },
5801 {
5802 .cmd = NL80211_CMD_SET_STATION,
5803 .doit = nl80211_set_station,
5804 .policy = nl80211_policy,
5805 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005806 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5807 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005808 },
5809 {
5810 .cmd = NL80211_CMD_NEW_STATION,
5811 .doit = nl80211_new_station,
5812 .policy = nl80211_policy,
5813 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005814 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005815 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005816 },
5817 {
5818 .cmd = NL80211_CMD_DEL_STATION,
5819 .doit = nl80211_del_station,
5820 .policy = nl80211_policy,
5821 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005822 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5823 NL80211_FLAG_NEED_RTNL,
Johannes Berg5727ef12007-12-19 02:03:34 +01005824 },
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005825 {
5826 .cmd = NL80211_CMD_GET_MPATH,
5827 .doit = nl80211_get_mpath,
5828 .dumpit = nl80211_dump_mpath,
5829 .policy = nl80211_policy,
5830 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005831 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005832 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005833 },
5834 {
5835 .cmd = NL80211_CMD_SET_MPATH,
5836 .doit = nl80211_set_mpath,
5837 .policy = nl80211_policy,
5838 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005839 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005840 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005841 },
5842 {
5843 .cmd = NL80211_CMD_NEW_MPATH,
5844 .doit = nl80211_new_mpath,
5845 .policy = nl80211_policy,
5846 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005847 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005848 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005849 },
5850 {
5851 .cmd = NL80211_CMD_DEL_MPATH,
5852 .doit = nl80211_del_mpath,
5853 .policy = nl80211_policy,
5854 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005855 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5856 NL80211_FLAG_NEED_RTNL,
Luis Carlos Cobo2ec600d2008-02-23 15:17:06 +01005857 },
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005858 {
5859 .cmd = NL80211_CMD_SET_BSS,
5860 .doit = nl80211_set_bss,
5861 .policy = nl80211_policy,
5862 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005863 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5864 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9f1ba902008-08-07 20:07:01 +03005865 },
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005866 {
Luis R. Rodriguezf1303472009-01-30 09:26:42 -08005867 .cmd = NL80211_CMD_GET_REG,
5868 .doit = nl80211_get_reg,
5869 .policy = nl80211_policy,
5870 /* can be retrieved by unprivileged users */
5871 },
5872 {
Luis R. Rodriguezb2e1b302008-09-09 23:19:48 -07005873 .cmd = NL80211_CMD_SET_REG,
5874 .doit = nl80211_set_reg,
5875 .policy = nl80211_policy,
5876 .flags = GENL_ADMIN_PERM,
5877 },
5878 {
5879 .cmd = NL80211_CMD_REQ_SET_REG,
5880 .doit = nl80211_req_set_reg,
5881 .policy = nl80211_policy,
5882 .flags = GENL_ADMIN_PERM,
5883 },
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005884 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005885 .cmd = NL80211_CMD_GET_MESH_CONFIG,
5886 .doit = nl80211_get_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005887 .policy = nl80211_policy,
5888 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02005889 .internal_flags = NL80211_FLAG_NEED_NETDEV |
5890 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005891 },
5892 {
Javier Cardona24bdd9f2010-12-16 17:37:48 -08005893 .cmd = NL80211_CMD_SET_MESH_CONFIG,
5894 .doit = nl80211_update_mesh_config,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005895 .policy = nl80211_policy,
5896 .flags = GENL_ADMIN_PERM,
Johannes Berg29cbe682010-12-03 09:20:44 +01005897 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005898 NL80211_FLAG_NEED_RTNL,
colin@cozybit.com93da9cc2008-10-21 12:03:48 -07005899 },
Jouni Malinen9aed3cc2009-01-13 16:03:29 +02005900 {
Johannes Berg2a519312009-02-10 21:25:55 +01005901 .cmd = NL80211_CMD_TRIGGER_SCAN,
5902 .doit = nl80211_trigger_scan,
5903 .policy = nl80211_policy,
5904 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005905 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005906 NL80211_FLAG_NEED_RTNL,
Johannes Berg2a519312009-02-10 21:25:55 +01005907 },
5908 {
5909 .cmd = NL80211_CMD_GET_SCAN,
5910 .policy = nl80211_policy,
5911 .dumpit = nl80211_dump_scan,
5912 },
Jouni Malinen636a5d32009-03-19 13:39:22 +02005913 {
Luciano Coelho807f8a82011-05-11 17:09:35 +03005914 .cmd = NL80211_CMD_START_SCHED_SCAN,
5915 .doit = nl80211_start_sched_scan,
5916 .policy = nl80211_policy,
5917 .flags = GENL_ADMIN_PERM,
5918 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5919 NL80211_FLAG_NEED_RTNL,
5920 },
5921 {
5922 .cmd = NL80211_CMD_STOP_SCHED_SCAN,
5923 .doit = nl80211_stop_sched_scan,
5924 .policy = nl80211_policy,
5925 .flags = GENL_ADMIN_PERM,
5926 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
5927 NL80211_FLAG_NEED_RTNL,
5928 },
5929 {
Jouni Malinen636a5d32009-03-19 13:39:22 +02005930 .cmd = NL80211_CMD_AUTHENTICATE,
5931 .doit = nl80211_authenticate,
5932 .policy = nl80211_policy,
5933 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005934 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005935 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005936 },
5937 {
5938 .cmd = NL80211_CMD_ASSOCIATE,
5939 .doit = nl80211_associate,
5940 .policy = nl80211_policy,
5941 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005942 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005943 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005944 },
5945 {
5946 .cmd = NL80211_CMD_DEAUTHENTICATE,
5947 .doit = nl80211_deauthenticate,
5948 .policy = nl80211_policy,
5949 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005950 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005951 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005952 },
5953 {
5954 .cmd = NL80211_CMD_DISASSOCIATE,
5955 .doit = nl80211_disassociate,
5956 .policy = nl80211_policy,
5957 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005958 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005959 NL80211_FLAG_NEED_RTNL,
Jouni Malinen636a5d32009-03-19 13:39:22 +02005960 },
Johannes Berg04a773a2009-04-19 21:24:32 +02005961 {
5962 .cmd = NL80211_CMD_JOIN_IBSS,
5963 .doit = nl80211_join_ibss,
5964 .policy = nl80211_policy,
5965 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005966 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005967 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005968 },
5969 {
5970 .cmd = NL80211_CMD_LEAVE_IBSS,
5971 .doit = nl80211_leave_ibss,
5972 .policy = nl80211_policy,
5973 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005974 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005975 NL80211_FLAG_NEED_RTNL,
Johannes Berg04a773a2009-04-19 21:24:32 +02005976 },
Johannes Bergaff89a92009-07-01 21:26:51 +02005977#ifdef CONFIG_NL80211_TESTMODE
5978 {
5979 .cmd = NL80211_CMD_TESTMODE,
5980 .doit = nl80211_testmode_do,
Wey-Yi Guy71063f02011-05-20 09:05:54 -07005981 .dumpit = nl80211_testmode_dump,
Johannes Bergaff89a92009-07-01 21:26:51 +02005982 .policy = nl80211_policy,
5983 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02005984 .internal_flags = NL80211_FLAG_NEED_WIPHY |
5985 NL80211_FLAG_NEED_RTNL,
Johannes Bergaff89a92009-07-01 21:26:51 +02005986 },
5987#endif
Samuel Ortizb23aa672009-07-01 21:26:54 +02005988 {
5989 .cmd = NL80211_CMD_CONNECT,
5990 .doit = nl80211_connect,
5991 .policy = nl80211_policy,
5992 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02005993 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02005994 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02005995 },
5996 {
5997 .cmd = NL80211_CMD_DISCONNECT,
5998 .doit = nl80211_disconnect,
5999 .policy = nl80211_policy,
6000 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006001 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006002 NL80211_FLAG_NEED_RTNL,
Samuel Ortizb23aa672009-07-01 21:26:54 +02006003 },
Johannes Berg463d0182009-07-14 00:33:35 +02006004 {
6005 .cmd = NL80211_CMD_SET_WIPHY_NETNS,
6006 .doit = nl80211_wiphy_netns,
6007 .policy = nl80211_policy,
6008 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006009 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6010 NL80211_FLAG_NEED_RTNL,
Johannes Berg463d0182009-07-14 00:33:35 +02006011 },
Holger Schurig61fa7132009-11-11 12:25:40 +01006012 {
6013 .cmd = NL80211_CMD_GET_SURVEY,
6014 .policy = nl80211_policy,
6015 .dumpit = nl80211_dump_survey,
6016 },
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006017 {
6018 .cmd = NL80211_CMD_SET_PMKSA,
6019 .doit = nl80211_setdel_pmksa,
6020 .policy = nl80211_policy,
6021 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006022 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6023 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006024 },
6025 {
6026 .cmd = NL80211_CMD_DEL_PMKSA,
6027 .doit = nl80211_setdel_pmksa,
6028 .policy = nl80211_policy,
6029 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006030 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6031 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006032 },
6033 {
6034 .cmd = NL80211_CMD_FLUSH_PMKSA,
6035 .doit = nl80211_flush_pmksa,
6036 .policy = nl80211_policy,
6037 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006038 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6039 NL80211_FLAG_NEED_RTNL,
Samuel Ortiz67fbb162009-11-24 23:59:15 +01006040 },
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006041 {
6042 .cmd = NL80211_CMD_REMAIN_ON_CHANNEL,
6043 .doit = nl80211_remain_on_channel,
6044 .policy = nl80211_policy,
6045 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006046 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006047 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006048 },
6049 {
6050 .cmd = NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6051 .doit = nl80211_cancel_remain_on_channel,
6052 .policy = nl80211_policy,
6053 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006054 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006055 NL80211_FLAG_NEED_RTNL,
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006056 },
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006057 {
6058 .cmd = NL80211_CMD_SET_TX_BITRATE_MASK,
6059 .doit = nl80211_set_tx_bitrate_mask,
6060 .policy = nl80211_policy,
6061 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006062 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6063 NL80211_FLAG_NEED_RTNL,
Jouni Malinen13ae75b2009-12-29 12:59:45 +02006064 },
Jouni Malinen026331c2010-02-15 12:53:10 +02006065 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006066 .cmd = NL80211_CMD_REGISTER_FRAME,
6067 .doit = nl80211_register_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006068 .policy = nl80211_policy,
6069 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006070 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6071 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006072 },
6073 {
Johannes Berg2e161f72010-08-12 15:38:38 +02006074 .cmd = NL80211_CMD_FRAME,
6075 .doit = nl80211_tx_mgmt,
Jouni Malinen026331c2010-02-15 12:53:10 +02006076 .policy = nl80211_policy,
6077 .flags = GENL_ADMIN_PERM,
Johannes Berg41265712010-10-04 21:14:05 +02006078 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
Johannes Berg4c476992010-10-04 21:36:35 +02006079 NL80211_FLAG_NEED_RTNL,
Jouni Malinen026331c2010-02-15 12:53:10 +02006080 },
Kalle Valoffb9eb32010-02-17 17:58:10 +02006081 {
Johannes Bergf7ca38d2010-11-25 10:02:29 +01006082 .cmd = NL80211_CMD_FRAME_WAIT_CANCEL,
6083 .doit = nl80211_tx_mgmt_cancel_wait,
6084 .policy = nl80211_policy,
6085 .flags = GENL_ADMIN_PERM,
6086 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6087 NL80211_FLAG_NEED_RTNL,
6088 },
6089 {
Kalle Valoffb9eb32010-02-17 17:58:10 +02006090 .cmd = NL80211_CMD_SET_POWER_SAVE,
6091 .doit = nl80211_set_power_save,
6092 .policy = nl80211_policy,
6093 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006094 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6095 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006096 },
6097 {
6098 .cmd = NL80211_CMD_GET_POWER_SAVE,
6099 .doit = nl80211_get_power_save,
6100 .policy = nl80211_policy,
6101 /* can be retrieved by unprivileged users */
Johannes Berg4c476992010-10-04 21:36:35 +02006102 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6103 NL80211_FLAG_NEED_RTNL,
Kalle Valoffb9eb32010-02-17 17:58:10 +02006104 },
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006105 {
6106 .cmd = NL80211_CMD_SET_CQM,
6107 .doit = nl80211_set_cqm,
6108 .policy = nl80211_policy,
6109 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006110 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6111 NL80211_FLAG_NEED_RTNL,
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02006112 },
Johannes Bergf444de02010-05-05 15:25:02 +02006113 {
6114 .cmd = NL80211_CMD_SET_CHANNEL,
6115 .doit = nl80211_set_channel,
6116 .policy = nl80211_policy,
6117 .flags = GENL_ADMIN_PERM,
Johannes Berg4c476992010-10-04 21:36:35 +02006118 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6119 NL80211_FLAG_NEED_RTNL,
Johannes Bergf444de02010-05-05 15:25:02 +02006120 },
Bill Jordane8347eb2010-10-01 13:54:28 -04006121 {
6122 .cmd = NL80211_CMD_SET_WDS_PEER,
6123 .doit = nl80211_set_wds_peer,
6124 .policy = nl80211_policy,
6125 .flags = GENL_ADMIN_PERM,
Johannes Berg43b19952010-10-07 13:10:30 +02006126 .internal_flags = NL80211_FLAG_NEED_NETDEV |
6127 NL80211_FLAG_NEED_RTNL,
Bill Jordane8347eb2010-10-01 13:54:28 -04006128 },
Johannes Berg29cbe682010-12-03 09:20:44 +01006129 {
6130 .cmd = NL80211_CMD_JOIN_MESH,
6131 .doit = nl80211_join_mesh,
6132 .policy = nl80211_policy,
6133 .flags = GENL_ADMIN_PERM,
6134 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6135 NL80211_FLAG_NEED_RTNL,
6136 },
6137 {
6138 .cmd = NL80211_CMD_LEAVE_MESH,
6139 .doit = nl80211_leave_mesh,
6140 .policy = nl80211_policy,
6141 .flags = GENL_ADMIN_PERM,
6142 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6143 NL80211_FLAG_NEED_RTNL,
6144 },
Johannes Bergff1b6e62011-05-04 15:37:28 +02006145 {
6146 .cmd = NL80211_CMD_GET_WOWLAN,
6147 .doit = nl80211_get_wowlan,
6148 .policy = nl80211_policy,
6149 /* can be retrieved by unprivileged users */
6150 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6151 NL80211_FLAG_NEED_RTNL,
6152 },
6153 {
6154 .cmd = NL80211_CMD_SET_WOWLAN,
6155 .doit = nl80211_set_wowlan,
6156 .policy = nl80211_policy,
6157 .flags = GENL_ADMIN_PERM,
6158 .internal_flags = NL80211_FLAG_NEED_WIPHY |
6159 NL80211_FLAG_NEED_RTNL,
6160 },
Johannes Berge5497d72011-07-05 16:35:40 +02006161 {
6162 .cmd = NL80211_CMD_SET_REKEY_OFFLOAD,
6163 .doit = nl80211_set_rekey_data,
6164 .policy = nl80211_policy,
6165 .flags = GENL_ADMIN_PERM,
6166 .internal_flags = NL80211_FLAG_NEED_NETDEV_UP |
6167 NL80211_FLAG_NEED_RTNL,
6168 },
Johannes Berg55682962007-09-20 13:09:35 -04006169};
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006170
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006171static struct genl_multicast_group nl80211_mlme_mcgrp = {
6172 .name = "mlme",
6173};
Johannes Berg55682962007-09-20 13:09:35 -04006174
6175/* multicast groups */
6176static struct genl_multicast_group nl80211_config_mcgrp = {
6177 .name = "config",
6178};
Johannes Berg2a519312009-02-10 21:25:55 +01006179static struct genl_multicast_group nl80211_scan_mcgrp = {
6180 .name = "scan",
6181};
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006182static struct genl_multicast_group nl80211_regulatory_mcgrp = {
6183 .name = "regulatory",
6184};
Johannes Berg55682962007-09-20 13:09:35 -04006185
6186/* notification functions */
6187
6188void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
6189{
6190 struct sk_buff *msg;
6191
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006192 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006193 if (!msg)
6194 return;
6195
6196 if (nl80211_send_wiphy(msg, 0, 0, 0, rdev) < 0) {
6197 nlmsg_free(msg);
6198 return;
6199 }
6200
Johannes Berg463d0182009-07-14 00:33:35 +02006201 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6202 nl80211_config_mcgrp.id, GFP_KERNEL);
Johannes Berg55682962007-09-20 13:09:35 -04006203}
6204
Johannes Berg362a4152009-05-24 16:43:15 +02006205static int nl80211_add_scan_req(struct sk_buff *msg,
6206 struct cfg80211_registered_device *rdev)
6207{
6208 struct cfg80211_scan_request *req = rdev->scan_req;
6209 struct nlattr *nest;
6210 int i;
6211
Johannes Berg667503d2009-07-07 03:56:11 +02006212 ASSERT_RDEV_LOCK(rdev);
6213
Johannes Berg362a4152009-05-24 16:43:15 +02006214 if (WARN_ON(!req))
6215 return 0;
6216
6217 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_SSIDS);
6218 if (!nest)
6219 goto nla_put_failure;
6220 for (i = 0; i < req->n_ssids; i++)
6221 NLA_PUT(msg, i, req->ssids[i].ssid_len, req->ssids[i].ssid);
6222 nla_nest_end(msg, nest);
6223
6224 nest = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
6225 if (!nest)
6226 goto nla_put_failure;
6227 for (i = 0; i < req->n_channels; i++)
6228 NLA_PUT_U32(msg, i, req->channels[i]->center_freq);
6229 nla_nest_end(msg, nest);
6230
6231 if (req->ie)
6232 NLA_PUT(msg, NL80211_ATTR_IE, req->ie_len, req->ie);
6233
6234 return 0;
6235 nla_put_failure:
6236 return -ENOBUFS;
6237}
6238
Johannes Berga538e2d2009-06-16 19:56:42 +02006239static int nl80211_send_scan_msg(struct sk_buff *msg,
6240 struct cfg80211_registered_device *rdev,
6241 struct net_device *netdev,
6242 u32 pid, u32 seq, int flags,
6243 u32 cmd)
Johannes Berg2a519312009-02-10 21:25:55 +01006244{
6245 void *hdr;
6246
6247 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6248 if (!hdr)
6249 return -1;
6250
Luis R. Rodriguezb5850a72009-02-21 00:04:19 -05006251 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
Johannes Berg2a519312009-02-10 21:25:55 +01006252 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6253
Johannes Berg362a4152009-05-24 16:43:15 +02006254 /* ignore errors and send incomplete event anyway */
6255 nl80211_add_scan_req(msg, rdev);
Johannes Berg2a519312009-02-10 21:25:55 +01006256
6257 return genlmsg_end(msg, hdr);
6258
6259 nla_put_failure:
6260 genlmsg_cancel(msg, hdr);
6261 return -EMSGSIZE;
6262}
6263
Luciano Coelho807f8a82011-05-11 17:09:35 +03006264static int
6265nl80211_send_sched_scan_msg(struct sk_buff *msg,
6266 struct cfg80211_registered_device *rdev,
6267 struct net_device *netdev,
6268 u32 pid, u32 seq, int flags, u32 cmd)
6269{
6270 void *hdr;
6271
6272 hdr = nl80211hdr_put(msg, pid, seq, flags, cmd);
6273 if (!hdr)
6274 return -1;
6275
6276 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6277 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6278
6279 return genlmsg_end(msg, hdr);
6280
6281 nla_put_failure:
6282 genlmsg_cancel(msg, hdr);
6283 return -EMSGSIZE;
6284}
6285
Johannes Berga538e2d2009-06-16 19:56:42 +02006286void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
6287 struct net_device *netdev)
6288{
6289 struct sk_buff *msg;
6290
6291 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6292 if (!msg)
6293 return;
6294
6295 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6296 NL80211_CMD_TRIGGER_SCAN) < 0) {
6297 nlmsg_free(msg);
6298 return;
6299 }
6300
Johannes Berg463d0182009-07-14 00:33:35 +02006301 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6302 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berga538e2d2009-06-16 19:56:42 +02006303}
6304
Johannes Berg2a519312009-02-10 21:25:55 +01006305void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
6306 struct net_device *netdev)
6307{
6308 struct sk_buff *msg;
6309
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006310 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006311 if (!msg)
6312 return;
6313
Johannes Berga538e2d2009-06-16 19:56:42 +02006314 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6315 NL80211_CMD_NEW_SCAN_RESULTS) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006316 nlmsg_free(msg);
6317 return;
6318 }
6319
Johannes Berg463d0182009-07-14 00:33:35 +02006320 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6321 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006322}
6323
6324void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
6325 struct net_device *netdev)
6326{
6327 struct sk_buff *msg;
6328
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006329 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006330 if (!msg)
6331 return;
6332
Johannes Berga538e2d2009-06-16 19:56:42 +02006333 if (nl80211_send_scan_msg(msg, rdev, netdev, 0, 0, 0,
6334 NL80211_CMD_SCAN_ABORTED) < 0) {
Johannes Berg2a519312009-02-10 21:25:55 +01006335 nlmsg_free(msg);
6336 return;
6337 }
6338
Johannes Berg463d0182009-07-14 00:33:35 +02006339 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6340 nl80211_scan_mcgrp.id, GFP_KERNEL);
Johannes Berg2a519312009-02-10 21:25:55 +01006341}
6342
Luciano Coelho807f8a82011-05-11 17:09:35 +03006343void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
6344 struct net_device *netdev)
6345{
6346 struct sk_buff *msg;
6347
6348 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
6349 if (!msg)
6350 return;
6351
6352 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0,
6353 NL80211_CMD_SCHED_SCAN_RESULTS) < 0) {
6354 nlmsg_free(msg);
6355 return;
6356 }
6357
6358 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6359 nl80211_scan_mcgrp.id, GFP_KERNEL);
6360}
6361
6362void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
6363 struct net_device *netdev, u32 cmd)
6364{
6365 struct sk_buff *msg;
6366
6367 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
6368 if (!msg)
6369 return;
6370
6371 if (nl80211_send_sched_scan_msg(msg, rdev, netdev, 0, 0, 0, cmd) < 0) {
6372 nlmsg_free(msg);
6373 return;
6374 }
6375
6376 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6377 nl80211_scan_mcgrp.id, GFP_KERNEL);
6378}
6379
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006380/*
6381 * This can happen on global regulatory changes or device specific settings
6382 * based on custom world regulatory domains.
6383 */
6384void nl80211_send_reg_change_event(struct regulatory_request *request)
6385{
6386 struct sk_buff *msg;
6387 void *hdr;
6388
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006389 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006390 if (!msg)
6391 return;
6392
6393 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_CHANGE);
6394 if (!hdr) {
6395 nlmsg_free(msg);
6396 return;
6397 }
6398
6399 /* Userspace can always count this one always being set */
6400 NLA_PUT_U8(msg, NL80211_ATTR_REG_INITIATOR, request->initiator);
6401
6402 if (request->alpha2[0] == '0' && request->alpha2[1] == '0')
6403 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6404 NL80211_REGDOM_TYPE_WORLD);
6405 else if (request->alpha2[0] == '9' && request->alpha2[1] == '9')
6406 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6407 NL80211_REGDOM_TYPE_CUSTOM_WORLD);
6408 else if ((request->alpha2[0] == '9' && request->alpha2[1] == '8') ||
6409 request->intersect)
6410 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6411 NL80211_REGDOM_TYPE_INTERSECTION);
6412 else {
6413 NLA_PUT_U8(msg, NL80211_ATTR_REG_TYPE,
6414 NL80211_REGDOM_TYPE_COUNTRY);
6415 NLA_PUT_STRING(msg, NL80211_ATTR_REG_ALPHA2, request->alpha2);
6416 }
6417
6418 if (wiphy_idx_valid(request->wiphy_idx))
6419 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, request->wiphy_idx);
6420
6421 if (genlmsg_end(msg, hdr) < 0) {
6422 nlmsg_free(msg);
6423 return;
6424 }
6425
Johannes Bergbc43b282009-07-25 10:54:13 +02006426 rcu_read_lock();
Johannes Berg463d0182009-07-14 00:33:35 +02006427 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
Johannes Bergbc43b282009-07-25 10:54:13 +02006428 GFP_ATOMIC);
6429 rcu_read_unlock();
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04006430
6431 return;
6432
6433nla_put_failure:
6434 genlmsg_cancel(msg, hdr);
6435 nlmsg_free(msg);
6436}
6437
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006438static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
6439 struct net_device *netdev,
6440 const u8 *buf, size_t len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006441 enum nl80211_commands cmd, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006442{
6443 struct sk_buff *msg;
6444 void *hdr;
6445
Johannes Berge6d6e342009-07-01 21:26:47 +02006446 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006447 if (!msg)
6448 return;
6449
6450 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6451 if (!hdr) {
6452 nlmsg_free(msg);
6453 return;
6454 }
6455
6456 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6457 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6458 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
6459
6460 if (genlmsg_end(msg, hdr) < 0) {
6461 nlmsg_free(msg);
6462 return;
6463 }
6464
Johannes Berg463d0182009-07-14 00:33:35 +02006465 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6466 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006467 return;
6468
6469 nla_put_failure:
6470 genlmsg_cancel(msg, hdr);
6471 nlmsg_free(msg);
6472}
6473
6474void nl80211_send_rx_auth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006475 struct net_device *netdev, const u8 *buf,
6476 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006477{
6478 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006479 NL80211_CMD_AUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006480}
6481
6482void nl80211_send_rx_assoc(struct cfg80211_registered_device *rdev,
6483 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006484 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006485{
Johannes Berge6d6e342009-07-01 21:26:47 +02006486 nl80211_send_mlme_event(rdev, netdev, buf, len,
6487 NL80211_CMD_ASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006488}
6489
Jouni Malinen53b46b82009-03-27 20:53:56 +02006490void nl80211_send_deauth(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006491 struct net_device *netdev, const u8 *buf,
6492 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006493{
6494 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006495 NL80211_CMD_DEAUTHENTICATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006496}
6497
Jouni Malinen53b46b82009-03-27 20:53:56 +02006498void nl80211_send_disassoc(struct cfg80211_registered_device *rdev,
6499 struct net_device *netdev, const u8 *buf,
Johannes Berge6d6e342009-07-01 21:26:47 +02006500 size_t len, gfp_t gfp)
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006501{
6502 nl80211_send_mlme_event(rdev, netdev, buf, len,
Johannes Berge6d6e342009-07-01 21:26:47 +02006503 NL80211_CMD_DISASSOCIATE, gfp);
Jouni Malinen6039f6d2009-03-19 13:39:21 +02006504}
6505
Jouni Malinencf4e5942010-12-16 00:52:40 +02006506void nl80211_send_unprot_deauth(struct cfg80211_registered_device *rdev,
6507 struct net_device *netdev, const u8 *buf,
6508 size_t len, gfp_t gfp)
6509{
6510 nl80211_send_mlme_event(rdev, netdev, buf, len,
6511 NL80211_CMD_UNPROT_DEAUTHENTICATE, gfp);
6512}
6513
6514void nl80211_send_unprot_disassoc(struct cfg80211_registered_device *rdev,
6515 struct net_device *netdev, const u8 *buf,
6516 size_t len, gfp_t gfp)
6517{
6518 nl80211_send_mlme_event(rdev, netdev, buf, len,
6519 NL80211_CMD_UNPROT_DISASSOCIATE, gfp);
6520}
6521
Luis R. Rodriguez1b06bb42009-05-02 00:34:48 -04006522static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
6523 struct net_device *netdev, int cmd,
Johannes Berge6d6e342009-07-01 21:26:47 +02006524 const u8 *addr, gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006525{
6526 struct sk_buff *msg;
6527 void *hdr;
6528
Johannes Berge6d6e342009-07-01 21:26:47 +02006529 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006530 if (!msg)
6531 return;
6532
6533 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6534 if (!hdr) {
6535 nlmsg_free(msg);
6536 return;
6537 }
6538
6539 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6540 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6541 NLA_PUT_FLAG(msg, NL80211_ATTR_TIMED_OUT);
6542 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6543
6544 if (genlmsg_end(msg, hdr) < 0) {
6545 nlmsg_free(msg);
6546 return;
6547 }
6548
Johannes Berg463d0182009-07-14 00:33:35 +02006549 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6550 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006551 return;
6552
6553 nla_put_failure:
6554 genlmsg_cancel(msg, hdr);
6555 nlmsg_free(msg);
6556}
6557
6558void nl80211_send_auth_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006559 struct net_device *netdev, const u8 *addr,
6560 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006561{
6562 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_AUTHENTICATE,
Johannes Berge6d6e342009-07-01 21:26:47 +02006563 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006564}
6565
6566void nl80211_send_assoc_timeout(struct cfg80211_registered_device *rdev,
Johannes Berge6d6e342009-07-01 21:26:47 +02006567 struct net_device *netdev, const u8 *addr,
6568 gfp_t gfp)
Jouni Malinen1965c852009-04-22 21:38:25 +03006569{
Johannes Berge6d6e342009-07-01 21:26:47 +02006570 nl80211_send_mlme_timeout(rdev, netdev, NL80211_CMD_ASSOCIATE,
6571 addr, gfp);
Jouni Malinen1965c852009-04-22 21:38:25 +03006572}
6573
Samuel Ortizb23aa672009-07-01 21:26:54 +02006574void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
6575 struct net_device *netdev, const u8 *bssid,
6576 const u8 *req_ie, size_t req_ie_len,
6577 const u8 *resp_ie, size_t resp_ie_len,
6578 u16 status, gfp_t gfp)
6579{
6580 struct sk_buff *msg;
6581 void *hdr;
6582
6583 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6584 if (!msg)
6585 return;
6586
6587 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_CONNECT);
6588 if (!hdr) {
6589 nlmsg_free(msg);
6590 return;
6591 }
6592
6593 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6594 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6595 if (bssid)
6596 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6597 NLA_PUT_U16(msg, NL80211_ATTR_STATUS_CODE, status);
6598 if (req_ie)
6599 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6600 if (resp_ie)
6601 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6602
6603 if (genlmsg_end(msg, hdr) < 0) {
6604 nlmsg_free(msg);
6605 return;
6606 }
6607
Johannes Berg463d0182009-07-14 00:33:35 +02006608 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6609 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006610 return;
6611
6612 nla_put_failure:
6613 genlmsg_cancel(msg, hdr);
6614 nlmsg_free(msg);
6615
6616}
6617
6618void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
6619 struct net_device *netdev, const u8 *bssid,
6620 const u8 *req_ie, size_t req_ie_len,
6621 const u8 *resp_ie, size_t resp_ie_len, gfp_t gfp)
6622{
6623 struct sk_buff *msg;
6624 void *hdr;
6625
6626 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6627 if (!msg)
6628 return;
6629
6630 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_ROAM);
6631 if (!hdr) {
6632 nlmsg_free(msg);
6633 return;
6634 }
6635
6636 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6637 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6638 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6639 if (req_ie)
6640 NLA_PUT(msg, NL80211_ATTR_REQ_IE, req_ie_len, req_ie);
6641 if (resp_ie)
6642 NLA_PUT(msg, NL80211_ATTR_RESP_IE, resp_ie_len, resp_ie);
6643
6644 if (genlmsg_end(msg, hdr) < 0) {
6645 nlmsg_free(msg);
6646 return;
6647 }
6648
Johannes Berg463d0182009-07-14 00:33:35 +02006649 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6650 nl80211_mlme_mcgrp.id, gfp);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006651 return;
6652
6653 nla_put_failure:
6654 genlmsg_cancel(msg, hdr);
6655 nlmsg_free(msg);
6656
6657}
6658
6659void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
6660 struct net_device *netdev, u16 reason,
Johannes Berg667503d2009-07-07 03:56:11 +02006661 const u8 *ie, size_t ie_len, bool from_ap)
Samuel Ortizb23aa672009-07-01 21:26:54 +02006662{
6663 struct sk_buff *msg;
6664 void *hdr;
6665
Johannes Berg667503d2009-07-07 03:56:11 +02006666 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006667 if (!msg)
6668 return;
6669
6670 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DISCONNECT);
6671 if (!hdr) {
6672 nlmsg_free(msg);
6673 return;
6674 }
6675
6676 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6677 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6678 if (from_ap && reason)
6679 NLA_PUT_U16(msg, NL80211_ATTR_REASON_CODE, reason);
6680 if (from_ap)
6681 NLA_PUT_FLAG(msg, NL80211_ATTR_DISCONNECTED_BY_AP);
6682 if (ie)
6683 NLA_PUT(msg, NL80211_ATTR_IE, ie_len, ie);
6684
6685 if (genlmsg_end(msg, hdr) < 0) {
6686 nlmsg_free(msg);
6687 return;
6688 }
6689
Johannes Berg463d0182009-07-14 00:33:35 +02006690 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6691 nl80211_mlme_mcgrp.id, GFP_KERNEL);
Samuel Ortizb23aa672009-07-01 21:26:54 +02006692 return;
6693
6694 nla_put_failure:
6695 genlmsg_cancel(msg, hdr);
6696 nlmsg_free(msg);
6697
6698}
6699
Johannes Berg04a773a2009-04-19 21:24:32 +02006700void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
6701 struct net_device *netdev, const u8 *bssid,
6702 gfp_t gfp)
6703{
6704 struct sk_buff *msg;
6705 void *hdr;
6706
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006707 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006708 if (!msg)
6709 return;
6710
6711 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_JOIN_IBSS);
6712 if (!hdr) {
6713 nlmsg_free(msg);
6714 return;
6715 }
6716
6717 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6718 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6719 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
6720
6721 if (genlmsg_end(msg, hdr) < 0) {
6722 nlmsg_free(msg);
6723 return;
6724 }
6725
Johannes Berg463d0182009-07-14 00:33:35 +02006726 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6727 nl80211_mlme_mcgrp.id, gfp);
Johannes Berg04a773a2009-04-19 21:24:32 +02006728 return;
6729
6730 nla_put_failure:
6731 genlmsg_cancel(msg, hdr);
6732 nlmsg_free(msg);
6733}
6734
Javier Cardonac93b5e72011-04-07 15:08:34 -07006735void nl80211_send_new_peer_candidate(struct cfg80211_registered_device *rdev,
6736 struct net_device *netdev,
6737 const u8 *macaddr, const u8* ie, u8 ie_len,
6738 gfp_t gfp)
6739{
6740 struct sk_buff *msg;
6741 void *hdr;
6742
6743 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6744 if (!msg)
6745 return;
6746
6747 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NEW_PEER_CANDIDATE);
6748 if (!hdr) {
6749 nlmsg_free(msg);
6750 return;
6751 }
6752
6753 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6754 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6755 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, macaddr);
6756 if (ie_len && ie)
6757 NLA_PUT(msg, NL80211_ATTR_IE, ie_len , ie);
6758
6759 if (genlmsg_end(msg, hdr) < 0) {
6760 nlmsg_free(msg);
6761 return;
6762 }
6763
6764 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6765 nl80211_mlme_mcgrp.id, gfp);
6766 return;
6767
6768 nla_put_failure:
6769 genlmsg_cancel(msg, hdr);
6770 nlmsg_free(msg);
6771}
6772
Jouni Malinena3b8b052009-03-27 21:59:49 +02006773void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
6774 struct net_device *netdev, const u8 *addr,
6775 enum nl80211_key_type key_type, int key_id,
Johannes Berge6d6e342009-07-01 21:26:47 +02006776 const u8 *tsc, gfp_t gfp)
Jouni Malinena3b8b052009-03-27 21:59:49 +02006777{
6778 struct sk_buff *msg;
6779 void *hdr;
6780
Johannes Berge6d6e342009-07-01 21:26:47 +02006781 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006782 if (!msg)
6783 return;
6784
6785 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_MICHAEL_MIC_FAILURE);
6786 if (!hdr) {
6787 nlmsg_free(msg);
6788 return;
6789 }
6790
6791 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6792 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6793 if (addr)
6794 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, addr);
6795 NLA_PUT_U32(msg, NL80211_ATTR_KEY_TYPE, key_type);
Arik Nemtsova66b98d2011-06-23 00:00:24 +03006796 if (key_id != -1)
6797 NLA_PUT_U8(msg, NL80211_ATTR_KEY_IDX, key_id);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006798 if (tsc)
6799 NLA_PUT(msg, NL80211_ATTR_KEY_SEQ, 6, tsc);
6800
6801 if (genlmsg_end(msg, hdr) < 0) {
6802 nlmsg_free(msg);
6803 return;
6804 }
6805
Johannes Berg463d0182009-07-14 00:33:35 +02006806 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6807 nl80211_mlme_mcgrp.id, gfp);
Jouni Malinena3b8b052009-03-27 21:59:49 +02006808 return;
6809
6810 nla_put_failure:
6811 genlmsg_cancel(msg, hdr);
6812 nlmsg_free(msg);
6813}
6814
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006815void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
6816 struct ieee80211_channel *channel_before,
6817 struct ieee80211_channel *channel_after)
6818{
6819 struct sk_buff *msg;
6820 void *hdr;
6821 struct nlattr *nl_freq;
6822
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -07006823 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006824 if (!msg)
6825 return;
6826
6827 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_REG_BEACON_HINT);
6828 if (!hdr) {
6829 nlmsg_free(msg);
6830 return;
6831 }
6832
6833 /*
6834 * Since we are applying the beacon hint to a wiphy we know its
6835 * wiphy_idx is valid
6836 */
6837 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, get_wiphy_idx(wiphy));
6838
6839 /* Before */
6840 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_BEFORE);
6841 if (!nl_freq)
6842 goto nla_put_failure;
6843 if (nl80211_msg_put_channel(msg, channel_before))
6844 goto nla_put_failure;
6845 nla_nest_end(msg, nl_freq);
6846
6847 /* After */
6848 nl_freq = nla_nest_start(msg, NL80211_ATTR_FREQ_AFTER);
6849 if (!nl_freq)
6850 goto nla_put_failure;
6851 if (nl80211_msg_put_channel(msg, channel_after))
6852 goto nla_put_failure;
6853 nla_nest_end(msg, nl_freq);
6854
6855 if (genlmsg_end(msg, hdr) < 0) {
6856 nlmsg_free(msg);
6857 return;
6858 }
6859
Johannes Berg463d0182009-07-14 00:33:35 +02006860 rcu_read_lock();
6861 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id,
6862 GFP_ATOMIC);
6863 rcu_read_unlock();
Luis R. Rodriguez6bad8762009-04-02 14:08:09 -04006864
6865 return;
6866
6867nla_put_failure:
6868 genlmsg_cancel(msg, hdr);
6869 nlmsg_free(msg);
6870}
6871
Jouni Malinen9588bbd2009-12-23 13:15:41 +01006872static void nl80211_send_remain_on_chan_event(
6873 int cmd, struct cfg80211_registered_device *rdev,
6874 struct net_device *netdev, u64 cookie,
6875 struct ieee80211_channel *chan,
6876 enum nl80211_channel_type channel_type,
6877 unsigned int duration, gfp_t gfp)
6878{
6879 struct sk_buff *msg;
6880 void *hdr;
6881
6882 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6883 if (!msg)
6884 return;
6885
6886 hdr = nl80211hdr_put(msg, 0, 0, 0, cmd);
6887 if (!hdr) {
6888 nlmsg_free(msg);
6889 return;
6890 }
6891
6892 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
6893 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
6894 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, chan->center_freq);
6895 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_CHANNEL_TYPE, channel_type);
6896 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
6897
6898 if (cmd == NL80211_CMD_REMAIN_ON_CHANNEL)
6899 NLA_PUT_U32(msg, NL80211_ATTR_DURATION, duration);
6900
6901 if (genlmsg_end(msg, hdr) < 0) {
6902 nlmsg_free(msg);
6903 return;
6904 }
6905
6906 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6907 nl80211_mlme_mcgrp.id, gfp);
6908 return;
6909
6910 nla_put_failure:
6911 genlmsg_cancel(msg, hdr);
6912 nlmsg_free(msg);
6913}
6914
6915void nl80211_send_remain_on_channel(struct cfg80211_registered_device *rdev,
6916 struct net_device *netdev, u64 cookie,
6917 struct ieee80211_channel *chan,
6918 enum nl80211_channel_type channel_type,
6919 unsigned int duration, gfp_t gfp)
6920{
6921 nl80211_send_remain_on_chan_event(NL80211_CMD_REMAIN_ON_CHANNEL,
6922 rdev, netdev, cookie, chan,
6923 channel_type, duration, gfp);
6924}
6925
6926void nl80211_send_remain_on_channel_cancel(
6927 struct cfg80211_registered_device *rdev, struct net_device *netdev,
6928 u64 cookie, struct ieee80211_channel *chan,
6929 enum nl80211_channel_type channel_type, gfp_t gfp)
6930{
6931 nl80211_send_remain_on_chan_event(NL80211_CMD_CANCEL_REMAIN_ON_CHANNEL,
6932 rdev, netdev, cookie, chan,
6933 channel_type, 0, gfp);
6934}
6935
Johannes Berg98b62182009-12-23 13:15:44 +01006936void nl80211_send_sta_event(struct cfg80211_registered_device *rdev,
6937 struct net_device *dev, const u8 *mac_addr,
6938 struct station_info *sinfo, gfp_t gfp)
6939{
6940 struct sk_buff *msg;
6941
6942 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6943 if (!msg)
6944 return;
6945
6946 if (nl80211_send_station(msg, 0, 0, 0, dev, mac_addr, sinfo) < 0) {
6947 nlmsg_free(msg);
6948 return;
6949 }
6950
6951 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6952 nl80211_mlme_mcgrp.id, gfp);
6953}
6954
Jouni Malinenec15e682011-03-23 15:29:52 +02006955void nl80211_send_sta_del_event(struct cfg80211_registered_device *rdev,
6956 struct net_device *dev, const u8 *mac_addr,
6957 gfp_t gfp)
6958{
6959 struct sk_buff *msg;
6960 void *hdr;
6961
6962 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
6963 if (!msg)
6964 return;
6965
6966 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_DEL_STATION);
6967 if (!hdr) {
6968 nlmsg_free(msg);
6969 return;
6970 }
6971
6972 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, dev->ifindex);
6973 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, mac_addr);
6974
6975 if (genlmsg_end(msg, hdr) < 0) {
6976 nlmsg_free(msg);
6977 return;
6978 }
6979
6980 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
6981 nl80211_mlme_mcgrp.id, gfp);
6982 return;
6983
6984 nla_put_failure:
6985 genlmsg_cancel(msg, hdr);
6986 nlmsg_free(msg);
6987}
6988
Johannes Berg2e161f72010-08-12 15:38:38 +02006989int nl80211_send_mgmt(struct cfg80211_registered_device *rdev,
6990 struct net_device *netdev, u32 nlpid,
6991 int freq, const u8 *buf, size_t len, gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02006992{
6993 struct sk_buff *msg;
6994 void *hdr;
6995 int err;
6996
6997 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
6998 if (!msg)
6999 return -ENOMEM;
7000
Johannes Berg2e161f72010-08-12 15:38:38 +02007001 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME);
Jouni Malinen026331c2010-02-15 12:53:10 +02007002 if (!hdr) {
7003 nlmsg_free(msg);
7004 return -ENOMEM;
7005 }
7006
7007 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7008 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7009 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY_FREQ, freq);
7010 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7011
7012 err = genlmsg_end(msg, hdr);
7013 if (err < 0) {
7014 nlmsg_free(msg);
7015 return err;
7016 }
7017
7018 err = genlmsg_unicast(wiphy_net(&rdev->wiphy), msg, nlpid);
7019 if (err < 0)
7020 return err;
7021 return 0;
7022
7023 nla_put_failure:
7024 genlmsg_cancel(msg, hdr);
7025 nlmsg_free(msg);
7026 return -ENOBUFS;
7027}
7028
Johannes Berg2e161f72010-08-12 15:38:38 +02007029void nl80211_send_mgmt_tx_status(struct cfg80211_registered_device *rdev,
7030 struct net_device *netdev, u64 cookie,
7031 const u8 *buf, size_t len, bool ack,
7032 gfp_t gfp)
Jouni Malinen026331c2010-02-15 12:53:10 +02007033{
7034 struct sk_buff *msg;
7035 void *hdr;
7036
7037 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, gfp);
7038 if (!msg)
7039 return;
7040
Johannes Berg2e161f72010-08-12 15:38:38 +02007041 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FRAME_TX_STATUS);
Jouni Malinen026331c2010-02-15 12:53:10 +02007042 if (!hdr) {
7043 nlmsg_free(msg);
7044 return;
7045 }
7046
7047 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7048 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7049 NLA_PUT(msg, NL80211_ATTR_FRAME, len, buf);
7050 NLA_PUT_U64(msg, NL80211_ATTR_COOKIE, cookie);
7051 if (ack)
7052 NLA_PUT_FLAG(msg, NL80211_ATTR_ACK);
7053
7054 if (genlmsg_end(msg, hdr) < 0) {
7055 nlmsg_free(msg);
7056 return;
7057 }
7058
7059 genlmsg_multicast(msg, 0, nl80211_mlme_mcgrp.id, gfp);
7060 return;
7061
7062 nla_put_failure:
7063 genlmsg_cancel(msg, hdr);
7064 nlmsg_free(msg);
7065}
7066
Juuso Oikarinend6dc1a32010-03-23 09:02:33 +02007067void
7068nl80211_send_cqm_rssi_notify(struct cfg80211_registered_device *rdev,
7069 struct net_device *netdev,
7070 enum nl80211_cqm_rssi_threshold_event rssi_event,
7071 gfp_t gfp)
7072{
7073 struct sk_buff *msg;
7074 struct nlattr *pinfoattr;
7075 void *hdr;
7076
7077 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7078 if (!msg)
7079 return;
7080
7081 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7082 if (!hdr) {
7083 nlmsg_free(msg);
7084 return;
7085 }
7086
7087 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7088 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7089
7090 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7091 if (!pinfoattr)
7092 goto nla_put_failure;
7093
7094 NLA_PUT_U32(msg, NL80211_ATTR_CQM_RSSI_THRESHOLD_EVENT,
7095 rssi_event);
7096
7097 nla_nest_end(msg, pinfoattr);
7098
7099 if (genlmsg_end(msg, hdr) < 0) {
7100 nlmsg_free(msg);
7101 return;
7102 }
7103
7104 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7105 nl80211_mlme_mcgrp.id, gfp);
7106 return;
7107
7108 nla_put_failure:
7109 genlmsg_cancel(msg, hdr);
7110 nlmsg_free(msg);
7111}
7112
Johannes Berge5497d72011-07-05 16:35:40 +02007113void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
7114 struct net_device *netdev, const u8 *bssid,
7115 const u8 *replay_ctr, gfp_t gfp)
7116{
7117 struct sk_buff *msg;
7118 struct nlattr *rekey_attr;
7119 void *hdr;
7120
7121 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7122 if (!msg)
7123 return;
7124
7125 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_SET_REKEY_OFFLOAD);
7126 if (!hdr) {
7127 nlmsg_free(msg);
7128 return;
7129 }
7130
7131 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7132 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7133 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, bssid);
7134
7135 rekey_attr = nla_nest_start(msg, NL80211_ATTR_REKEY_DATA);
7136 if (!rekey_attr)
7137 goto nla_put_failure;
7138
7139 NLA_PUT(msg, NL80211_REKEY_DATA_REPLAY_CTR,
7140 NL80211_REPLAY_CTR_LEN, replay_ctr);
7141
7142 nla_nest_end(msg, rekey_attr);
7143
7144 if (genlmsg_end(msg, hdr) < 0) {
7145 nlmsg_free(msg);
7146 return;
7147 }
7148
7149 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7150 nl80211_mlme_mcgrp.id, gfp);
7151 return;
7152
7153 nla_put_failure:
7154 genlmsg_cancel(msg, hdr);
7155 nlmsg_free(msg);
7156}
7157
Johannes Bergc063dbf2010-11-24 08:10:05 +01007158void
7159nl80211_send_cqm_pktloss_notify(struct cfg80211_registered_device *rdev,
7160 struct net_device *netdev, const u8 *peer,
7161 u32 num_packets, gfp_t gfp)
7162{
7163 struct sk_buff *msg;
7164 struct nlattr *pinfoattr;
7165 void *hdr;
7166
7167 msg = nlmsg_new(NLMSG_GOODSIZE, gfp);
7168 if (!msg)
7169 return;
7170
7171 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_NOTIFY_CQM);
7172 if (!hdr) {
7173 nlmsg_free(msg);
7174 return;
7175 }
7176
7177 NLA_PUT_U32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx);
7178 NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex);
7179 NLA_PUT(msg, NL80211_ATTR_MAC, ETH_ALEN, peer);
7180
7181 pinfoattr = nla_nest_start(msg, NL80211_ATTR_CQM);
7182 if (!pinfoattr)
7183 goto nla_put_failure;
7184
7185 NLA_PUT_U32(msg, NL80211_ATTR_CQM_PKT_LOSS_EVENT, num_packets);
7186
7187 nla_nest_end(msg, pinfoattr);
7188
7189 if (genlmsg_end(msg, hdr) < 0) {
7190 nlmsg_free(msg);
7191 return;
7192 }
7193
7194 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0,
7195 nl80211_mlme_mcgrp.id, gfp);
7196 return;
7197
7198 nla_put_failure:
7199 genlmsg_cancel(msg, hdr);
7200 nlmsg_free(msg);
7201}
7202
Jouni Malinen026331c2010-02-15 12:53:10 +02007203static int nl80211_netlink_notify(struct notifier_block * nb,
7204 unsigned long state,
7205 void *_notify)
7206{
7207 struct netlink_notify *notify = _notify;
7208 struct cfg80211_registered_device *rdev;
7209 struct wireless_dev *wdev;
7210
7211 if (state != NETLINK_URELEASE)
7212 return NOTIFY_DONE;
7213
7214 rcu_read_lock();
7215
7216 list_for_each_entry_rcu(rdev, &cfg80211_rdev_list, list)
7217 list_for_each_entry_rcu(wdev, &rdev->netdev_list, list)
Johannes Berg2e161f72010-08-12 15:38:38 +02007218 cfg80211_mlme_unregister_socket(wdev, notify->pid);
Jouni Malinen026331c2010-02-15 12:53:10 +02007219
7220 rcu_read_unlock();
7221
7222 return NOTIFY_DONE;
7223}
7224
7225static struct notifier_block nl80211_netlink_notifier = {
7226 .notifier_call = nl80211_netlink_notify,
7227};
7228
Johannes Berg55682962007-09-20 13:09:35 -04007229/* initialisation/exit functions */
7230
7231int nl80211_init(void)
7232{
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007233 int err;
Johannes Berg55682962007-09-20 13:09:35 -04007234
Michał Mirosław0d63cbb2009-05-21 10:34:06 +00007235 err = genl_register_family_with_ops(&nl80211_fam,
7236 nl80211_ops, ARRAY_SIZE(nl80211_ops));
Johannes Berg55682962007-09-20 13:09:35 -04007237 if (err)
7238 return err;
7239
Johannes Berg55682962007-09-20 13:09:35 -04007240 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
7241 if (err)
7242 goto err_out;
7243
Johannes Berg2a519312009-02-10 21:25:55 +01007244 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
7245 if (err)
7246 goto err_out;
7247
Luis R. Rodriguez73d54c92009-03-09 22:07:42 -04007248 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
7249 if (err)
7250 goto err_out;
7251
Jouni Malinen6039f6d2009-03-19 13:39:21 +02007252 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
7253 if (err)
7254 goto err_out;
7255
Johannes Bergaff89a92009-07-01 21:26:51 +02007256#ifdef CONFIG_NL80211_TESTMODE
7257 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
7258 if (err)
7259 goto err_out;
7260#endif
7261
Jouni Malinen026331c2010-02-15 12:53:10 +02007262 err = netlink_register_notifier(&nl80211_netlink_notifier);
7263 if (err)
7264 goto err_out;
7265
Johannes Berg55682962007-09-20 13:09:35 -04007266 return 0;
7267 err_out:
7268 genl_unregister_family(&nl80211_fam);
7269 return err;
7270}
7271
7272void nl80211_exit(void)
7273{
Jouni Malinen026331c2010-02-15 12:53:10 +02007274 netlink_unregister_notifier(&nl80211_netlink_notifier);
Johannes Berg55682962007-09-20 13:09:35 -04007275 genl_unregister_family(&nl80211_fam);
7276}